blob: fec8f8b10b002d186612bfd1dee92f0a61681cbd [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 &&
2184 !Stack->isLoopControlVariable(FD).first)
2185 ImplicitFirstprivate.push_back(E);
2186 return;
2187 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002188 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002189 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002190 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002191 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002192 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002193 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002194 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2195 if (!Stack->checkMappableExprComponentListsForDecl(
2196 VD, /*CurrentRegionOnly=*/true,
2197 [&CurComponents](
2198 OMPClauseMappableExprCommon::MappableExprComponentListRef
2199 StackComponents,
2200 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002201 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002202 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002203 for (const auto &SC : llvm::reverse(StackComponents)) {
2204 // Do both expressions have the same kind?
2205 if (CCI->getAssociatedExpression()->getStmtClass() !=
2206 SC.getAssociatedExpression()->getStmtClass())
2207 if (!(isa<OMPArraySectionExpr>(
2208 SC.getAssociatedExpression()) &&
2209 isa<ArraySubscriptExpr>(
2210 CCI->getAssociatedExpression())))
2211 return false;
2212
Alexey Bataeve3727102018-04-18 15:57:46 +00002213 const Decl *CCD = CCI->getAssociatedDeclaration();
2214 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002215 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2216 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2217 if (SCD != CCD)
2218 return false;
2219 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002220 if (CCI == CCE)
2221 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002222 }
2223 return true;
2224 })) {
2225 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002226 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002227 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002228 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002229 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002230 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002231 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002232 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002233 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002234 // for task|target directives.
2235 // Skip analysis of arguments of implicitly defined map clause for target
2236 // directives.
2237 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2238 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002239 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002240 if (CC)
2241 Visit(CC);
2242 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002243 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002245 }
2246 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002247 for (Stmt *C : S->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002248 if (C && !isa<OMPExecutableDirective>(C))
2249 Visit(C);
2250 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002251 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002252
Alexey Bataeve3727102018-04-18 15:57:46 +00002253 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002254 ArrayRef<Expr *> getImplicitFirstprivate() const {
2255 return ImplicitFirstprivate;
2256 }
2257 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002258 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002259 return VarsWithInheritedDSA;
2260 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002261
Alexey Bataev7ff55242014-06-19 09:13:45 +00002262 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2263 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002264};
Alexey Bataeved09d242014-05-28 05:53:51 +00002265} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002266
Alexey Bataevbae9a792014-06-27 10:37:06 +00002267void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002268 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002269 case OMPD_parallel:
2270 case OMPD_parallel_for:
2271 case OMPD_parallel_for_simd:
2272 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002273 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002274 case OMPD_teams_distribute:
2275 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002276 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002277 QualType KmpInt32PtrTy =
2278 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002279 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002280 std::make_pair(".global_tid.", KmpInt32PtrTy),
2281 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2282 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002283 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002284 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2285 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002286 break;
2287 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002288 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002289 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002290 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002291 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002292 case OMPD_target_teams_distribute:
2293 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002294 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2295 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2296 QualType KmpInt32PtrTy =
2297 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2298 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002299 FunctionProtoType::ExtProtoInfo EPI;
2300 EPI.Variadic = true;
2301 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2302 Sema::CapturedParamNameType Params[] = {
2303 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002304 std::make_pair(".part_id.", KmpInt32PtrTy),
2305 std::make_pair(".privates.", VoidPtrTy),
2306 std::make_pair(
2307 ".copy_fn.",
2308 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002309 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2310 std::make_pair(StringRef(), QualType()) // __context with shared vars
2311 };
2312 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2313 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002314 // Mark this captured region as inlined, because we don't use outlined
2315 // function directly.
2316 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2317 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002318 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002319 Sema::CapturedParamNameType ParamsTarget[] = {
2320 std::make_pair(StringRef(), QualType()) // __context with shared vars
2321 };
2322 // Start a captured region for 'target' with no implicit parameters.
2323 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2324 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002325 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002326 std::make_pair(".global_tid.", KmpInt32PtrTy),
2327 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2328 std::make_pair(StringRef(), QualType()) // __context with shared vars
2329 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002330 // Start a captured region for 'teams' or 'parallel'. Both regions have
2331 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002332 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002333 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002334 break;
2335 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002336 case OMPD_target:
2337 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002338 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2339 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2340 QualType KmpInt32PtrTy =
2341 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2342 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002343 FunctionProtoType::ExtProtoInfo EPI;
2344 EPI.Variadic = true;
2345 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2346 Sema::CapturedParamNameType Params[] = {
2347 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002348 std::make_pair(".part_id.", KmpInt32PtrTy),
2349 std::make_pair(".privates.", VoidPtrTy),
2350 std::make_pair(
2351 ".copy_fn.",
2352 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002353 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2354 std::make_pair(StringRef(), QualType()) // __context with shared vars
2355 };
2356 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2357 Params);
2358 // Mark this captured region as inlined, because we don't use outlined
2359 // function directly.
2360 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2361 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002362 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002363 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2364 std::make_pair(StringRef(), QualType()));
2365 break;
2366 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002367 case OMPD_simd:
2368 case OMPD_for:
2369 case OMPD_for_simd:
2370 case OMPD_sections:
2371 case OMPD_section:
2372 case OMPD_single:
2373 case OMPD_master:
2374 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002375 case OMPD_taskgroup:
2376 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002377 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002378 case OMPD_ordered:
2379 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002380 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002381 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002382 std::make_pair(StringRef(), QualType()) // __context with shared vars
2383 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002384 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2385 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002386 break;
2387 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002388 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002389 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2390 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2391 QualType KmpInt32PtrTy =
2392 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2393 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002394 FunctionProtoType::ExtProtoInfo EPI;
2395 EPI.Variadic = true;
2396 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002397 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002398 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002399 std::make_pair(".part_id.", KmpInt32PtrTy),
2400 std::make_pair(".privates.", VoidPtrTy),
2401 std::make_pair(
2402 ".copy_fn.",
2403 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002404 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002405 std::make_pair(StringRef(), QualType()) // __context with shared vars
2406 };
2407 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2408 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002409 // Mark this captured region as inlined, because we don't use outlined
2410 // function directly.
2411 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2412 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002413 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002414 break;
2415 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002416 case OMPD_taskloop:
2417 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002418 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002419 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2420 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002421 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002422 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2423 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002424 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002425 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2426 .withConst();
2427 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2428 QualType KmpInt32PtrTy =
2429 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2430 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002431 FunctionProtoType::ExtProtoInfo EPI;
2432 EPI.Variadic = true;
2433 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002434 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002435 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002436 std::make_pair(".part_id.", KmpInt32PtrTy),
2437 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002438 std::make_pair(
2439 ".copy_fn.",
2440 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2441 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2442 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002443 std::make_pair(".ub.", KmpUInt64Ty),
2444 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002445 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002446 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002447 std::make_pair(StringRef(), QualType()) // __context with shared vars
2448 };
2449 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2450 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002451 // Mark this captured region as inlined, because we don't use outlined
2452 // function directly.
2453 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2454 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002455 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002456 break;
2457 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002458 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002459 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002460 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002461 QualType KmpInt32PtrTy =
2462 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2463 Sema::CapturedParamNameType Params[] = {
2464 std::make_pair(".global_tid.", KmpInt32PtrTy),
2465 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002466 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2467 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002468 std::make_pair(StringRef(), QualType()) // __context with shared vars
2469 };
2470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2471 Params);
2472 break;
2473 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002474 case OMPD_target_teams_distribute_parallel_for:
2475 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002476 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002477 QualType KmpInt32PtrTy =
2478 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002479 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002480
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002481 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002482 FunctionProtoType::ExtProtoInfo EPI;
2483 EPI.Variadic = true;
2484 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2485 Sema::CapturedParamNameType Params[] = {
2486 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002487 std::make_pair(".part_id.", KmpInt32PtrTy),
2488 std::make_pair(".privates.", VoidPtrTy),
2489 std::make_pair(
2490 ".copy_fn.",
2491 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002492 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2493 std::make_pair(StringRef(), QualType()) // __context with shared vars
2494 };
2495 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2496 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002497 // Mark this captured region as inlined, because we don't use outlined
2498 // function directly.
2499 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2500 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002501 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002502 Sema::CapturedParamNameType ParamsTarget[] = {
2503 std::make_pair(StringRef(), QualType()) // __context with shared vars
2504 };
2505 // Start a captured region for 'target' with no implicit parameters.
2506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2507 ParamsTarget);
2508
2509 Sema::CapturedParamNameType ParamsTeams[] = {
2510 std::make_pair(".global_tid.", KmpInt32PtrTy),
2511 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2512 std::make_pair(StringRef(), QualType()) // __context with shared vars
2513 };
2514 // Start a captured region for 'target' with no implicit parameters.
2515 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2516 ParamsTeams);
2517
2518 Sema::CapturedParamNameType ParamsParallel[] = {
2519 std::make_pair(".global_tid.", KmpInt32PtrTy),
2520 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002521 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2522 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002523 std::make_pair(StringRef(), QualType()) // __context with shared vars
2524 };
2525 // Start a captured region for 'teams' or 'parallel'. Both regions have
2526 // the same implicit parameters.
2527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2528 ParamsParallel);
2529 break;
2530 }
2531
Alexey Bataev46506272017-12-05 17:41:34 +00002532 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002533 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002534 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002535 QualType KmpInt32PtrTy =
2536 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
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 Bertolli62fae152017-11-20 20:46:39 +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 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002560 case OMPD_target_update:
2561 case OMPD_target_enter_data:
2562 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002563 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2564 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2565 QualType KmpInt32PtrTy =
2566 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2567 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002568 FunctionProtoType::ExtProtoInfo EPI;
2569 EPI.Variadic = true;
2570 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2571 Sema::CapturedParamNameType Params[] = {
2572 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002573 std::make_pair(".part_id.", KmpInt32PtrTy),
2574 std::make_pair(".privates.", VoidPtrTy),
2575 std::make_pair(
2576 ".copy_fn.",
2577 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002578 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2579 std::make_pair(StringRef(), QualType()) // __context with shared vars
2580 };
2581 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2582 Params);
2583 // Mark this captured region as inlined, because we don't use outlined
2584 // function directly.
2585 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2586 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002587 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002588 break;
2589 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002590 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002591 case OMPD_taskyield:
2592 case OMPD_barrier:
2593 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002594 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002595 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002596 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002597 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002598 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002599 case OMPD_declare_target:
2600 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002601 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002602 llvm_unreachable("OpenMP Directive is not allowed");
2603 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002604 llvm_unreachable("Unknown OpenMP directive");
2605 }
2606}
2607
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002608int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2609 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2610 getOpenMPCaptureRegions(CaptureRegions, DKind);
2611 return CaptureRegions.size();
2612}
2613
Alexey Bataev3392d762016-02-16 11:18:12 +00002614static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002615 Expr *CaptureExpr, bool WithInit,
2616 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002617 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002618 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002619 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002620 QualType Ty = Init->getType();
2621 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002622 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002623 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002624 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002625 Ty = C.getPointerType(Ty);
2626 ExprResult Res =
2627 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2628 if (!Res.isUsable())
2629 return nullptr;
2630 Init = Res.get();
2631 }
Alexey Bataev61205072016-03-02 04:57:40 +00002632 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002633 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002634 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002635 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002636 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002637 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002638 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002639 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002640 return CED;
2641}
2642
Alexey Bataev61205072016-03-02 04:57:40 +00002643static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2644 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002645 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002646 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002647 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002648 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002649 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2650 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002651 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002652 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002653}
2654
Alexey Bataev5a3af132016-03-29 08:58:54 +00002655static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002656 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002657 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002658 OMPCapturedExprDecl *CD = buildCaptureDecl(
2659 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2660 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002661 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2662 CaptureExpr->getExprLoc());
2663 }
2664 ExprResult Res = Ref;
2665 if (!S.getLangOpts().CPlusPlus &&
2666 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002667 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002668 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002669 if (!Res.isUsable())
2670 return ExprError();
2671 }
2672 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002673}
2674
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002675namespace {
2676// OpenMP directives parsed in this section are represented as a
2677// CapturedStatement with an associated statement. If a syntax error
2678// is detected during the parsing of the associated statement, the
2679// compiler must abort processing and close the CapturedStatement.
2680//
2681// Combined directives such as 'target parallel' have more than one
2682// nested CapturedStatements. This RAII ensures that we unwind out
2683// of all the nested CapturedStatements when an error is found.
2684class CaptureRegionUnwinderRAII {
2685private:
2686 Sema &S;
2687 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002688 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002689
2690public:
2691 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2692 OpenMPDirectiveKind DKind)
2693 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2694 ~CaptureRegionUnwinderRAII() {
2695 if (ErrorFound) {
2696 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2697 while (--ThisCaptureLevel >= 0)
2698 S.ActOnCapturedRegionError();
2699 }
2700 }
2701};
2702} // namespace
2703
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002704StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2705 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002706 bool ErrorFound = false;
2707 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2708 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002709 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002710 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002711 return StmtError();
2712 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002713
Alexey Bataev2ba67042017-11-28 21:11:44 +00002714 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2715 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002716 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002717 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002718 SmallVector<const OMPLinearClause *, 4> LCs;
2719 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002720 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002721 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002722 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2723 Clause->getClauseKind() == OMPC_in_reduction) {
2724 // Capture taskgroup task_reduction descriptors inside the tasking regions
2725 // with the corresponding in_reduction items.
2726 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002727 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002728 if (E)
2729 MarkDeclarationsReferencedInExpr(E);
2730 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002731 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002732 Clause->getClauseKind() == OMPC_copyprivate ||
2733 (getLangOpts().OpenMPUseTLS &&
2734 getASTContext().getTargetInfo().isTLSSupported() &&
2735 Clause->getClauseKind() == OMPC_copyin)) {
2736 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002737 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002738 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002739 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002740 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002741 }
2742 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002743 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002744 } else if (CaptureRegions.size() > 1 ||
2745 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002746 if (auto *C = OMPClauseWithPreInit::get(Clause))
2747 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002748 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002749 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002750 MarkDeclarationsReferencedInExpr(E);
2751 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002752 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002753 if (Clause->getClauseKind() == OMPC_schedule)
2754 SC = cast<OMPScheduleClause>(Clause);
2755 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002756 OC = cast<OMPOrderedClause>(Clause);
2757 else if (Clause->getClauseKind() == OMPC_linear)
2758 LCs.push_back(cast<OMPLinearClause>(Clause));
2759 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002760 // OpenMP, 2.7.1 Loop Construct, Restrictions
2761 // The nonmonotonic modifier cannot be specified if an ordered clause is
2762 // specified.
2763 if (SC &&
2764 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2765 SC->getSecondScheduleModifier() ==
2766 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2767 OC) {
2768 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2769 ? SC->getFirstScheduleModifierLoc()
2770 : SC->getSecondScheduleModifierLoc(),
2771 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002772 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002773 ErrorFound = true;
2774 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002775 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002776 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002777 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002778 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002779 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002780 ErrorFound = true;
2781 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002782 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2783 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2784 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002785 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002786 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2787 ErrorFound = true;
2788 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002789 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002790 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002791 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002792 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002793 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002794 // Mark all variables in private list clauses as used in inner region.
2795 // Required for proper codegen of combined directives.
2796 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002797 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002798 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002799 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2800 // Find the particular capture region for the clause if the
2801 // directive is a combined one with multiple capture regions.
2802 // If the directive is not a combined one, the capture region
2803 // associated with the clause is OMPD_unknown and is generated
2804 // only once.
2805 if (CaptureRegion == ThisCaptureRegion ||
2806 CaptureRegion == OMPD_unknown) {
2807 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002808 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002809 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2810 }
2811 }
2812 }
2813 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002814 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002815 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002816 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002817}
2818
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002819static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2820 OpenMPDirectiveKind CancelRegion,
2821 SourceLocation StartLoc) {
2822 // CancelRegion is only needed for cancel and cancellation_point.
2823 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2824 return false;
2825
2826 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2827 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2828 return false;
2829
2830 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2831 << getOpenMPDirectiveName(CancelRegion);
2832 return true;
2833}
2834
Alexey Bataeve3727102018-04-18 15:57:46 +00002835static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002836 OpenMPDirectiveKind CurrentRegion,
2837 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002838 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002839 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002840 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002841 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2842 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002843 bool NestingProhibited = false;
2844 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002845 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002846 enum {
2847 NoRecommend,
2848 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002849 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002850 ShouldBeInTargetRegion,
2851 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002852 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002853 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002854 // OpenMP [2.16, Nesting of Regions]
2855 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002856 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002857 // An ordered construct with the simd clause is the only OpenMP
2858 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002859 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002860 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2861 // message.
2862 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2863 ? diag::err_omp_prohibited_region_simd
2864 : diag::warn_omp_nesting_simd);
2865 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002866 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002867 if (ParentRegion == OMPD_atomic) {
2868 // OpenMP [2.16, Nesting of Regions]
2869 // OpenMP constructs may not be nested inside an atomic region.
2870 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2871 return true;
2872 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002873 if (CurrentRegion == OMPD_section) {
2874 // OpenMP [2.7.2, sections Construct, Restrictions]
2875 // Orphaned section directives are prohibited. That is, the section
2876 // directives must appear within the sections construct and must not be
2877 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002878 if (ParentRegion != OMPD_sections &&
2879 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002880 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2881 << (ParentRegion != OMPD_unknown)
2882 << getOpenMPDirectiveName(ParentRegion);
2883 return true;
2884 }
2885 return false;
2886 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002887 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002888 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002889 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002890 if (ParentRegion == OMPD_unknown &&
2891 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002892 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002893 if (CurrentRegion == OMPD_cancellation_point ||
2894 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002895 // OpenMP [2.16, Nesting of Regions]
2896 // A cancellation point construct for which construct-type-clause is
2897 // taskgroup must be nested inside a task construct. A cancellation
2898 // point construct for which construct-type-clause is not taskgroup must
2899 // be closely nested inside an OpenMP construct that matches the type
2900 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002901 // A cancel construct for which construct-type-clause is taskgroup must be
2902 // nested inside a task construct. A cancel construct for which
2903 // construct-type-clause is not taskgroup must be closely nested inside an
2904 // OpenMP construct that matches the type specified in
2905 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002906 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002907 !((CancelRegion == OMPD_parallel &&
2908 (ParentRegion == OMPD_parallel ||
2909 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002910 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002911 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002912 ParentRegion == OMPD_target_parallel_for ||
2913 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002914 ParentRegion == OMPD_teams_distribute_parallel_for ||
2915 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002916 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2917 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002918 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2919 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002920 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002921 // OpenMP [2.16, Nesting of Regions]
2922 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002923 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002924 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002925 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002926 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2927 // OpenMP [2.16, Nesting of Regions]
2928 // A critical region may not be nested (closely or otherwise) inside a
2929 // critical region with the same name. Note that this restriction is not
2930 // sufficient to prevent deadlock.
2931 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002932 bool DeadLock = Stack->hasDirective(
2933 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2934 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00002935 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00002936 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2937 PreviousCriticalLoc = Loc;
2938 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002939 }
2940 return false;
David Majnemer9d168222016-08-05 17:44:54 +00002941 },
2942 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002943 if (DeadLock) {
2944 SemaRef.Diag(StartLoc,
2945 diag::err_omp_prohibited_region_critical_same_name)
2946 << CurrentName.getName();
2947 if (PreviousCriticalLoc.isValid())
2948 SemaRef.Diag(PreviousCriticalLoc,
2949 diag::note_omp_previous_critical_region);
2950 return true;
2951 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002952 } else if (CurrentRegion == OMPD_barrier) {
2953 // OpenMP [2.16, Nesting of Regions]
2954 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002955 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002956 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2957 isOpenMPTaskingDirective(ParentRegion) ||
2958 ParentRegion == OMPD_master ||
2959 ParentRegion == OMPD_critical ||
2960 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002961 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002962 !isOpenMPParallelDirective(CurrentRegion) &&
2963 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002964 // OpenMP [2.16, Nesting of Regions]
2965 // A worksharing region may not be closely nested inside a worksharing,
2966 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002967 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2968 isOpenMPTaskingDirective(ParentRegion) ||
2969 ParentRegion == OMPD_master ||
2970 ParentRegion == OMPD_critical ||
2971 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002972 Recommend = ShouldBeInParallelRegion;
2973 } else if (CurrentRegion == OMPD_ordered) {
2974 // OpenMP [2.16, Nesting of Regions]
2975 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002976 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002977 // An ordered region must be closely nested inside a loop region (or
2978 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002979 // OpenMP [2.8.1,simd Construct, Restrictions]
2980 // An ordered construct with the simd clause is the only OpenMP construct
2981 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002982 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002983 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002984 !(isOpenMPSimdDirective(ParentRegion) ||
2985 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002986 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002987 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002988 // OpenMP [2.16, Nesting of Regions]
2989 // If specified, a teams construct must be contained within a target
2990 // construct.
2991 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002992 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002993 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002994 }
Kelvin Libf594a52016-12-17 05:48:59 +00002995 if (!NestingProhibited &&
2996 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2997 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2998 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002999 // OpenMP [2.16, Nesting of Regions]
3000 // distribute, parallel, parallel sections, parallel workshare, and the
3001 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3002 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003003 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3004 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003005 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003006 }
David Majnemer9d168222016-08-05 17:44:54 +00003007 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003008 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003009 // OpenMP 4.5 [2.17 Nesting of Regions]
3010 // The region associated with the distribute construct must be strictly
3011 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003012 NestingProhibited =
3013 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003014 Recommend = ShouldBeInTeamsRegion;
3015 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003016 if (!NestingProhibited &&
3017 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3018 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3019 // OpenMP 4.5 [2.17 Nesting of Regions]
3020 // If a target, target update, target data, target enter data, or
3021 // target exit data construct is encountered during execution of a
3022 // target region, the behavior is unspecified.
3023 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003024 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003025 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003026 if (isOpenMPTargetExecutionDirective(K)) {
3027 OffendingRegion = K;
3028 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003029 }
3030 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003031 },
3032 false /* don't skip top directive */);
3033 CloseNesting = false;
3034 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003035 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003036 if (OrphanSeen) {
3037 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3038 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3039 } else {
3040 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3041 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3042 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3043 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003044 return true;
3045 }
3046 }
3047 return false;
3048}
3049
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003050static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3051 ArrayRef<OMPClause *> Clauses,
3052 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3053 bool ErrorFound = false;
3054 unsigned NamedModifiersNumber = 0;
3055 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3056 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003057 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003058 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003059 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3060 // At most one if clause without a directive-name-modifier can appear on
3061 // the directive.
3062 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3063 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003064 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003065 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3066 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3067 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003068 } else if (CurNM != OMPD_unknown) {
3069 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003070 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003071 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003072 FoundNameModifiers[CurNM] = IC;
3073 if (CurNM == OMPD_unknown)
3074 continue;
3075 // Check if the specified name modifier is allowed for the current
3076 // directive.
3077 // At most one if clause with the particular directive-name-modifier can
3078 // appear on the directive.
3079 bool MatchFound = false;
3080 for (auto NM : AllowedNameModifiers) {
3081 if (CurNM == NM) {
3082 MatchFound = true;
3083 break;
3084 }
3085 }
3086 if (!MatchFound) {
3087 S.Diag(IC->getNameModifierLoc(),
3088 diag::err_omp_wrong_if_directive_name_modifier)
3089 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3090 ErrorFound = true;
3091 }
3092 }
3093 }
3094 // If any if clause on the directive includes a directive-name-modifier then
3095 // all if clauses on the directive must include a directive-name-modifier.
3096 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3097 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003098 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003099 diag::err_omp_no_more_if_clause);
3100 } else {
3101 std::string Values;
3102 std::string Sep(", ");
3103 unsigned AllowedCnt = 0;
3104 unsigned TotalAllowedNum =
3105 AllowedNameModifiers.size() - NamedModifiersNumber;
3106 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3107 ++Cnt) {
3108 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3109 if (!FoundNameModifiers[NM]) {
3110 Values += "'";
3111 Values += getOpenMPDirectiveName(NM);
3112 Values += "'";
3113 if (AllowedCnt + 2 == TotalAllowedNum)
3114 Values += " or ";
3115 else if (AllowedCnt + 1 != TotalAllowedNum)
3116 Values += Sep;
3117 ++AllowedCnt;
3118 }
3119 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003120 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003121 diag::err_omp_unnamed_if_clause)
3122 << (TotalAllowedNum > 1) << Values;
3123 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003124 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003125 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3126 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003127 ErrorFound = true;
3128 }
3129 return ErrorFound;
3130}
3131
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003132StmtResult Sema::ActOnOpenMPExecutableDirective(
3133 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3134 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3135 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003136 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003137 // First check CancelRegion which is then used in checkNestingOfRegions.
3138 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3139 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003140 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003141 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003142
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003143 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003144 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003145 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003146 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003147 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003148 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3149
3150 // Check default data sharing attributes for referenced variables.
3151 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003152 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3153 Stmt *S = AStmt;
3154 while (--ThisCaptureLevel >= 0)
3155 S = cast<CapturedStmt>(S)->getCapturedStmt();
3156 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003157 if (DSAChecker.isErrorFound())
3158 return StmtError();
3159 // Generate list of implicitly defined firstprivate variables.
3160 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003161
Alexey Bataev88202be2017-07-27 13:20:36 +00003162 SmallVector<Expr *, 4> ImplicitFirstprivates(
3163 DSAChecker.getImplicitFirstprivate().begin(),
3164 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003165 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3166 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003167 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003168 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003169 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003170 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003171 if (E)
3172 ImplicitFirstprivates.emplace_back(E);
3173 }
3174 }
3175 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003176 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003177 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3178 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003179 ClausesWithImplicit.push_back(Implicit);
3180 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003181 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003182 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003183 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003184 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003185 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003186 if (!ImplicitMaps.empty()) {
3187 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3188 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3189 SourceLocation(), SourceLocation(), ImplicitMaps,
3190 SourceLocation(), SourceLocation(), SourceLocation())) {
3191 ClausesWithImplicit.emplace_back(Implicit);
3192 ErrorFound |=
3193 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003194 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003195 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003196 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003197 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003198 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003199
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003200 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003201 switch (Kind) {
3202 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003203 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3204 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003205 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003206 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003207 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003208 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3209 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003210 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003211 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003212 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3213 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003214 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003215 case OMPD_for_simd:
3216 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3217 EndLoc, VarsWithInheritedDSA);
3218 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003219 case OMPD_sections:
3220 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3221 EndLoc);
3222 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003223 case OMPD_section:
3224 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003225 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003226 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3227 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003228 case OMPD_single:
3229 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3230 EndLoc);
3231 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003232 case OMPD_master:
3233 assert(ClausesWithImplicit.empty() &&
3234 "No clauses are allowed for 'omp master' directive");
3235 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3236 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003237 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003238 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3239 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003240 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003241 case OMPD_parallel_for:
3242 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3243 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003244 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003245 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003246 case OMPD_parallel_for_simd:
3247 Res = ActOnOpenMPParallelForSimdDirective(
3248 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003249 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003250 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003251 case OMPD_parallel_sections:
3252 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3253 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003254 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003255 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003256 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003257 Res =
3258 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003259 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003260 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003261 case OMPD_taskyield:
3262 assert(ClausesWithImplicit.empty() &&
3263 "No clauses are allowed for 'omp taskyield' directive");
3264 assert(AStmt == nullptr &&
3265 "No associated statement allowed for 'omp taskyield' directive");
3266 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3267 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003268 case OMPD_barrier:
3269 assert(ClausesWithImplicit.empty() &&
3270 "No clauses are allowed for 'omp barrier' directive");
3271 assert(AStmt == nullptr &&
3272 "No associated statement allowed for 'omp barrier' directive");
3273 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3274 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003275 case OMPD_taskwait:
3276 assert(ClausesWithImplicit.empty() &&
3277 "No clauses are allowed for 'omp taskwait' directive");
3278 assert(AStmt == nullptr &&
3279 "No associated statement allowed for 'omp taskwait' directive");
3280 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3281 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003282 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003283 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3284 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003285 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003286 case OMPD_flush:
3287 assert(AStmt == nullptr &&
3288 "No associated statement allowed for 'omp flush' directive");
3289 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3290 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003291 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003292 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3293 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003294 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003295 case OMPD_atomic:
3296 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3297 EndLoc);
3298 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003299 case OMPD_teams:
3300 Res =
3301 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3302 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003303 case OMPD_target:
3304 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3305 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003306 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003307 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003308 case OMPD_target_parallel:
3309 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3310 StartLoc, EndLoc);
3311 AllowedNameModifiers.push_back(OMPD_target);
3312 AllowedNameModifiers.push_back(OMPD_parallel);
3313 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003314 case OMPD_target_parallel_for:
3315 Res = ActOnOpenMPTargetParallelForDirective(
3316 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3317 AllowedNameModifiers.push_back(OMPD_target);
3318 AllowedNameModifiers.push_back(OMPD_parallel);
3319 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003320 case OMPD_cancellation_point:
3321 assert(ClausesWithImplicit.empty() &&
3322 "No clauses are allowed for 'omp cancellation point' directive");
3323 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3324 "cancellation point' directive");
3325 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3326 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003327 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003328 assert(AStmt == nullptr &&
3329 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003330 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3331 CancelRegion);
3332 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003333 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003334 case OMPD_target_data:
3335 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3336 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003337 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003338 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003339 case OMPD_target_enter_data:
3340 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003341 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003342 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3343 break;
Samuel Antao72590762016-01-19 20:04:50 +00003344 case OMPD_target_exit_data:
3345 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003346 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003347 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3348 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003349 case OMPD_taskloop:
3350 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3351 EndLoc, VarsWithInheritedDSA);
3352 AllowedNameModifiers.push_back(OMPD_taskloop);
3353 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003354 case OMPD_taskloop_simd:
3355 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3356 EndLoc, VarsWithInheritedDSA);
3357 AllowedNameModifiers.push_back(OMPD_taskloop);
3358 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003359 case OMPD_distribute:
3360 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3361 EndLoc, VarsWithInheritedDSA);
3362 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003363 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003364 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3365 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003366 AllowedNameModifiers.push_back(OMPD_target_update);
3367 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003368 case OMPD_distribute_parallel_for:
3369 Res = ActOnOpenMPDistributeParallelForDirective(
3370 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3371 AllowedNameModifiers.push_back(OMPD_parallel);
3372 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003373 case OMPD_distribute_parallel_for_simd:
3374 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3375 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3376 AllowedNameModifiers.push_back(OMPD_parallel);
3377 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003378 case OMPD_distribute_simd:
3379 Res = ActOnOpenMPDistributeSimdDirective(
3380 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3381 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003382 case OMPD_target_parallel_for_simd:
3383 Res = ActOnOpenMPTargetParallelForSimdDirective(
3384 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3385 AllowedNameModifiers.push_back(OMPD_target);
3386 AllowedNameModifiers.push_back(OMPD_parallel);
3387 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003388 case OMPD_target_simd:
3389 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3390 EndLoc, VarsWithInheritedDSA);
3391 AllowedNameModifiers.push_back(OMPD_target);
3392 break;
Kelvin Li02532872016-08-05 14:37:37 +00003393 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003394 Res = ActOnOpenMPTeamsDistributeDirective(
3395 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003396 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003397 case OMPD_teams_distribute_simd:
3398 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3399 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3400 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003401 case OMPD_teams_distribute_parallel_for_simd:
3402 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3403 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3404 AllowedNameModifiers.push_back(OMPD_parallel);
3405 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003406 case OMPD_teams_distribute_parallel_for:
3407 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3408 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3409 AllowedNameModifiers.push_back(OMPD_parallel);
3410 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003411 case OMPD_target_teams:
3412 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3413 EndLoc);
3414 AllowedNameModifiers.push_back(OMPD_target);
3415 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003416 case OMPD_target_teams_distribute:
3417 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3418 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3419 AllowedNameModifiers.push_back(OMPD_target);
3420 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003421 case OMPD_target_teams_distribute_parallel_for:
3422 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3423 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3424 AllowedNameModifiers.push_back(OMPD_target);
3425 AllowedNameModifiers.push_back(OMPD_parallel);
3426 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003427 case OMPD_target_teams_distribute_parallel_for_simd:
3428 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3429 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3430 AllowedNameModifiers.push_back(OMPD_target);
3431 AllowedNameModifiers.push_back(OMPD_parallel);
3432 break;
Kelvin Lida681182017-01-10 18:08:18 +00003433 case OMPD_target_teams_distribute_simd:
3434 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3435 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3436 AllowedNameModifiers.push_back(OMPD_target);
3437 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003438 case OMPD_declare_target:
3439 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003440 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003441 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003442 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003443 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003444 llvm_unreachable("OpenMP Directive is not allowed");
3445 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003446 llvm_unreachable("Unknown OpenMP directive");
3447 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003448
Alexey Bataeve3727102018-04-18 15:57:46 +00003449 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003450 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3451 << P.first << P.second->getSourceRange();
3452 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003453 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3454
3455 if (!AllowedNameModifiers.empty())
3456 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3457 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003458
Alexey Bataeved09d242014-05-28 05:53:51 +00003459 if (ErrorFound)
3460 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003461 return Res;
3462}
3463
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003464Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3465 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003466 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003467 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3468 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003469 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003470 assert(Linears.size() == LinModifiers.size());
3471 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003472 if (!DG || DG.get().isNull())
3473 return DeclGroupPtrTy();
3474
3475 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003476 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003477 return DG;
3478 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003479 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003480 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3481 ADecl = FTD->getTemplatedDecl();
3482
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003483 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3484 if (!FD) {
3485 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003486 return DeclGroupPtrTy();
3487 }
3488
Alexey Bataev2af33e32016-04-07 12:45:37 +00003489 // OpenMP [2.8.2, declare simd construct, Description]
3490 // The parameter of the simdlen clause must be a constant positive integer
3491 // expression.
3492 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003493 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003494 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003495 // OpenMP [2.8.2, declare simd construct, Description]
3496 // The special this pointer can be used as if was one of the arguments to the
3497 // function in any of the linear, aligned, or uniform clauses.
3498 // The uniform clause declares one or more arguments to have an invariant
3499 // value for all concurrent invocations of the function in the execution of a
3500 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003501 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3502 const Expr *UniformedLinearThis = nullptr;
3503 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003504 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003505 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3506 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003507 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3508 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003509 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003510 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003511 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003512 }
3513 if (isa<CXXThisExpr>(E)) {
3514 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003515 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003516 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003517 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3518 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003519 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003520 // OpenMP [2.8.2, declare simd construct, Description]
3521 // The aligned clause declares that the object to which each list item points
3522 // is aligned to the number of bytes expressed in the optional parameter of
3523 // the aligned clause.
3524 // The special this pointer can be used as if was one of the arguments to the
3525 // function in any of the linear, aligned, or uniform clauses.
3526 // The type of list items appearing in the aligned clause must be array,
3527 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003528 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3529 const Expr *AlignedThis = nullptr;
3530 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003531 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003532 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3533 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3534 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003535 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3536 FD->getParamDecl(PVD->getFunctionScopeIndex())
3537 ->getCanonicalDecl() == CanonPVD) {
3538 // OpenMP [2.8.1, simd construct, Restrictions]
3539 // A list-item cannot appear in more than one aligned clause.
3540 if (AlignedArgs.count(CanonPVD) > 0) {
3541 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3542 << 1 << E->getSourceRange();
3543 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3544 diag::note_omp_explicit_dsa)
3545 << getOpenMPClauseName(OMPC_aligned);
3546 continue;
3547 }
3548 AlignedArgs[CanonPVD] = E;
3549 QualType QTy = PVD->getType()
3550 .getNonReferenceType()
3551 .getUnqualifiedType()
3552 .getCanonicalType();
3553 const Type *Ty = QTy.getTypePtrOrNull();
3554 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3555 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3556 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3557 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3558 }
3559 continue;
3560 }
3561 }
3562 if (isa<CXXThisExpr>(E)) {
3563 if (AlignedThis) {
3564 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3565 << 2 << E->getSourceRange();
3566 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3567 << getOpenMPClauseName(OMPC_aligned);
3568 }
3569 AlignedThis = E;
3570 continue;
3571 }
3572 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3573 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3574 }
3575 // The optional parameter of the aligned clause, alignment, must be a constant
3576 // positive integer expression. If no optional parameter is specified,
3577 // implementation-defined default alignments for SIMD instructions on the
3578 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003579 SmallVector<const Expr *, 4> NewAligns;
3580 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003581 ExprResult Align;
3582 if (E)
3583 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3584 NewAligns.push_back(Align.get());
3585 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003586 // OpenMP [2.8.2, declare simd construct, Description]
3587 // The linear clause declares one or more list items to be private to a SIMD
3588 // lane and to have a linear relationship with respect to the iteration space
3589 // of a loop.
3590 // The special this pointer can be used as if was one of the arguments to the
3591 // function in any of the linear, aligned, or uniform clauses.
3592 // When a linear-step expression is specified in a linear clause it must be
3593 // either a constant integer expression or an integer-typed parameter that is
3594 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003595 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003596 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3597 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003598 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003599 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3600 ++MI;
3601 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003602 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3603 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3604 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003605 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3606 FD->getParamDecl(PVD->getFunctionScopeIndex())
3607 ->getCanonicalDecl() == CanonPVD) {
3608 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3609 // A list-item cannot appear in more than one linear clause.
3610 if (LinearArgs.count(CanonPVD) > 0) {
3611 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3612 << getOpenMPClauseName(OMPC_linear)
3613 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3614 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3615 diag::note_omp_explicit_dsa)
3616 << getOpenMPClauseName(OMPC_linear);
3617 continue;
3618 }
3619 // Each argument can appear in at most one uniform or linear clause.
3620 if (UniformedArgs.count(CanonPVD) > 0) {
3621 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3622 << getOpenMPClauseName(OMPC_linear)
3623 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3624 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3625 diag::note_omp_explicit_dsa)
3626 << getOpenMPClauseName(OMPC_uniform);
3627 continue;
3628 }
3629 LinearArgs[CanonPVD] = E;
3630 if (E->isValueDependent() || E->isTypeDependent() ||
3631 E->isInstantiationDependent() ||
3632 E->containsUnexpandedParameterPack())
3633 continue;
3634 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3635 PVD->getOriginalType());
3636 continue;
3637 }
3638 }
3639 if (isa<CXXThisExpr>(E)) {
3640 if (UniformedLinearThis) {
3641 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3642 << getOpenMPClauseName(OMPC_linear)
3643 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3644 << E->getSourceRange();
3645 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3646 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3647 : OMPC_linear);
3648 continue;
3649 }
3650 UniformedLinearThis = E;
3651 if (E->isValueDependent() || E->isTypeDependent() ||
3652 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3653 continue;
3654 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3655 E->getType());
3656 continue;
3657 }
3658 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3659 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3660 }
3661 Expr *Step = nullptr;
3662 Expr *NewStep = nullptr;
3663 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003664 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003665 // Skip the same step expression, it was checked already.
3666 if (Step == E || !E) {
3667 NewSteps.push_back(E ? NewStep : nullptr);
3668 continue;
3669 }
3670 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003671 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3672 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3673 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003674 if (UniformedArgs.count(CanonPVD) == 0) {
3675 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3676 << Step->getSourceRange();
3677 } else if (E->isValueDependent() || E->isTypeDependent() ||
3678 E->isInstantiationDependent() ||
3679 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003680 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003681 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003682 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003683 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3684 << Step->getSourceRange();
3685 }
3686 continue;
3687 }
3688 NewStep = Step;
3689 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3690 !Step->isInstantiationDependent() &&
3691 !Step->containsUnexpandedParameterPack()) {
3692 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3693 .get();
3694 if (NewStep)
3695 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3696 }
3697 NewSteps.push_back(NewStep);
3698 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003699 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3700 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003701 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003702 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3703 const_cast<Expr **>(Linears.data()), Linears.size(),
3704 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3705 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003706 ADecl->addAttr(NewAttr);
3707 return ConvertDeclToDeclGroup(ADecl);
3708}
3709
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003710StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3711 Stmt *AStmt,
3712 SourceLocation StartLoc,
3713 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003714 if (!AStmt)
3715 return StmtError();
3716
Alexey Bataeve3727102018-04-18 15:57:46 +00003717 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003718 // 1.2.2 OpenMP Language Terminology
3719 // Structured block - An executable statement with a single entry at the
3720 // top and a single exit at the bottom.
3721 // The point of exit cannot be a branch out of the structured block.
3722 // longjmp() and throw() must not violate the entry/exit criteria.
3723 CS->getCapturedDecl()->setNothrow();
3724
Reid Kleckner87a31802018-03-12 21:43:02 +00003725 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003726
Alexey Bataev25e5b442015-09-15 12:52:43 +00003727 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3728 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003729}
3730
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003731namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003732/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003733/// extracting iteration space of each loop in the loop nest, that will be used
3734/// for IR generation.
3735class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003736 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003737 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003738 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003739 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003740 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003741 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003742 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003743 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003744 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003746 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003747 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003748 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003749 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003750 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003751 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003752 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003753 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003754 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003755 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003756 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003757 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003758 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003759 /// Var < UB
3760 /// Var <= UB
3761 /// UB > Var
3762 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003763 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003764 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003765 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003766 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003767 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768
3769public:
3770 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003771 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003772 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003773 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003774 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003775 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003776 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003777 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003778 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003779 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003780 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003781 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003782 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003783 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003784 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003785 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003786 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003787 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003788 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003789 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003790 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003791 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003792 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003793 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003794 Expr *buildNumIterations(
3795 Scope *S, const bool LimitedType,
3796 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003797 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003798 Expr *
3799 buildPreCond(Scope *S, Expr *Cond,
3800 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003801 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003802 DeclRefExpr *
3803 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3804 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003805 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003806 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003807 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003808 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003809 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003810 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003811 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003812 /// Build loop data with counter value for depend clauses in ordered
3813 /// directives.
3814 Expr *
3815 buildOrderedLoopData(Scope *S, Expr *Counter,
3816 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3817 SourceLocation Loc, Expr *Inc = nullptr,
3818 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003819 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003820 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003821
3822private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003823 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003824 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003825 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003826 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003827 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003828 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003829 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003830 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003831 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003832 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003833};
3834
Alexey Bataeve3727102018-04-18 15:57:46 +00003835bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003836 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003837 assert(!LB && !UB && !Step);
3838 return false;
3839 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003840 return LCDecl->getType()->isDependentType() ||
3841 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3842 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003843}
3844
Alexey Bataeve3727102018-04-18 15:57:46 +00003845bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003846 Expr *NewLCRefExpr,
3847 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003848 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003849 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003850 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003851 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003852 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003853 LCDecl = getCanonicalDecl(NewLCDecl);
3854 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003855 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3856 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003857 if ((Ctor->isCopyOrMoveConstructor() ||
3858 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3859 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003860 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003861 LB = NewLB;
3862 return false;
3863}
3864
Alexey Bataeve3727102018-04-18 15:57:46 +00003865bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003866 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003868 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3869 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 if (!NewUB)
3871 return true;
3872 UB = NewUB;
3873 TestIsLessOp = LessOp;
3874 TestIsStrictOp = StrictOp;
3875 ConditionSrcRange = SR;
3876 ConditionLoc = SL;
3877 return false;
3878}
3879
Alexey Bataeve3727102018-04-18 15:57:46 +00003880bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003881 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003882 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 if (!NewStep)
3884 return true;
3885 if (!NewStep->isValueDependent()) {
3886 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003887 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003888 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3889 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 if (Val.isInvalid())
3891 return true;
3892 NewStep = Val.get();
3893
3894 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3895 // If test-expr is of form var relational-op b and relational-op is < or
3896 // <= then incr-expr must cause var to increase on each iteration of the
3897 // loop. If test-expr is of form var relational-op b and relational-op is
3898 // > or >= then incr-expr must cause var to decrease on each iteration of
3899 // the loop.
3900 // If test-expr is of form b relational-op var and relational-op is < or
3901 // <= then incr-expr must cause var to decrease on each iteration of the
3902 // loop. If test-expr is of form b relational-op var and relational-op is
3903 // > or >= then incr-expr must cause var to increase on each iteration of
3904 // the loop.
3905 llvm::APSInt Result;
3906 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3907 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3908 bool IsConstNeg =
3909 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003910 bool IsConstPos =
3911 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 bool IsConstZero = IsConstant && !Result.getBoolValue();
3913 if (UB && (IsConstZero ||
3914 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003915 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003916 SemaRef.Diag(NewStep->getExprLoc(),
3917 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003918 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919 SemaRef.Diag(ConditionLoc,
3920 diag::note_omp_loop_cond_requres_compatible_incr)
3921 << TestIsLessOp << ConditionSrcRange;
3922 return true;
3923 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003924 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003925 NewStep =
3926 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3927 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003928 Subtract = !Subtract;
3929 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 }
3931
3932 Step = NewStep;
3933 SubtractStep = Subtract;
3934 return false;
3935}
3936
Alexey Bataeve3727102018-04-18 15:57:46 +00003937bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 // Check init-expr for canonical loop form and save loop counter
3939 // variable - #Var and its initialization value - #LB.
3940 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3941 // var = lb
3942 // integer-type var = lb
3943 // random-access-iterator-type var = lb
3944 // pointer-type var = lb
3945 //
3946 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003947 if (EmitDiags) {
3948 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3949 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003950 return true;
3951 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003952 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3953 if (!ExprTemp->cleanupsHaveSideEffects())
3954 S = ExprTemp->getSubExpr();
3955
Alexander Musmana5f070a2014-10-01 06:03:56 +00003956 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003957 if (Expr *E = dyn_cast<Expr>(S))
3958 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003959 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003960 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003961 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003962 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3963 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3964 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003965 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3966 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003967 }
3968 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3969 if (ME->isArrow() &&
3970 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00003971 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003972 }
3973 }
David Majnemer9d168222016-08-05 17:44:54 +00003974 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00003976 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003977 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003978 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003979 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003980 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003981 diag::ext_omp_loop_not_canonical_init)
3982 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00003983 return setLCDeclAndLB(
3984 Var,
3985 buildDeclRefExpr(SemaRef, Var,
3986 Var->getType().getNonReferenceType(),
3987 DS->getBeginLoc()),
3988 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003989 }
3990 }
3991 }
David Majnemer9d168222016-08-05 17:44:54 +00003992 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003993 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003994 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003995 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003996 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3997 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003998 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3999 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004000 }
4001 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4002 if (ME->isArrow() &&
4003 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004004 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 }
4006 }
4007 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008
Alexey Bataeve3727102018-04-18 15:57:46 +00004009 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004010 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004011 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004012 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004013 << S->getSourceRange();
4014 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004015 return true;
4016}
4017
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004018/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004019/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004020static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004022 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004023 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004024 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004025 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004026 if ((Ctor->isCopyOrMoveConstructor() ||
4027 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4028 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004029 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004030 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4031 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004032 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004034 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004035 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4036 return getCanonicalDecl(ME->getMemberDecl());
4037 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004038}
4039
Alexey Bataeve3727102018-04-18 15:57:46 +00004040bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041 // Check test-expr for canonical form, save upper-bound UB, flags for
4042 // less/greater and for strict/non-strict comparison.
4043 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4044 // var relational-op b
4045 // b relational-op var
4046 //
4047 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004049 return true;
4050 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004051 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004052 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004053 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004054 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004055 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4056 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4058 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4059 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004060 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4061 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004062 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4063 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4064 BO->getSourceRange(), BO->getOperatorLoc());
4065 }
David Majnemer9d168222016-08-05 17:44:54 +00004066 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067 if (CE->getNumArgs() == 2) {
4068 auto Op = CE->getOperator();
4069 switch (Op) {
4070 case OO_Greater:
4071 case OO_GreaterEqual:
4072 case OO_Less:
4073 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004074 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4075 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004076 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4077 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004078 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4079 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004080 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4081 CE->getOperatorLoc());
4082 break;
4083 default:
4084 break;
4085 }
4086 }
4087 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004088 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004089 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004090 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004091 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004092 return true;
4093}
4094
Alexey Bataeve3727102018-04-18 15:57:46 +00004095bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 // RHS of canonical loop form increment can be:
4097 // var + incr
4098 // incr + var
4099 // var - incr
4100 //
4101 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004102 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 if (BO->isAdditiveOp()) {
4104 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004105 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4106 return setStep(BO->getRHS(), !IsAdd);
4107 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4108 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 }
David Majnemer9d168222016-08-05 17:44:54 +00004110 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004111 bool IsAdd = CE->getOperator() == OO_Plus;
4112 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004113 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4114 return setStep(CE->getArg(1), !IsAdd);
4115 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4116 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 }
4118 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004119 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004120 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004121 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004122 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004123 return true;
4124}
4125
Alexey Bataeve3727102018-04-18 15:57:46 +00004126bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 // Check incr-expr for canonical loop form and return true if it
4128 // does not conform.
4129 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4130 // ++var
4131 // var++
4132 // --var
4133 // var--
4134 // var += incr
4135 // var -= incr
4136 // var = var + incr
4137 // var = incr + var
4138 // var = var - incr
4139 //
4140 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004141 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004142 return true;
4143 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004144 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4145 if (!ExprTemp->cleanupsHaveSideEffects())
4146 S = ExprTemp->getSubExpr();
4147
Alexander Musmana5f070a2014-10-01 06:03:56 +00004148 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004149 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004150 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004151 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004152 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4153 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004154 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004155 (UO->isDecrementOp() ? -1 : 1))
4156 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004157 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004158 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 switch (BO->getOpcode()) {
4160 case BO_AddAssign:
4161 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004162 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4163 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004164 break;
4165 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004166 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4167 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004168 break;
4169 default:
4170 break;
4171 }
David Majnemer9d168222016-08-05 17:44:54 +00004172 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004173 switch (CE->getOperator()) {
4174 case OO_PlusPlus:
4175 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004176 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4177 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004178 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004179 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004180 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4181 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004182 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004183 break;
4184 case OO_PlusEqual:
4185 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004186 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4187 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188 break;
4189 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004190 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4191 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004192 break;
4193 default:
4194 break;
4195 }
4196 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004197 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004198 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004199 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004200 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004201 return true;
4202}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004203
Alexey Bataev5a3af132016-03-29 08:58:54 +00004204static ExprResult
4205tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004206 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004207 if (SemaRef.CurContext->isDependentContext())
4208 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004209 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4210 return SemaRef.PerformImplicitConversion(
4211 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4212 /*AllowExplicit=*/true);
4213 auto I = Captures.find(Capture);
4214 if (I != Captures.end())
4215 return buildCapture(SemaRef, Capture, I->second);
4216 DeclRefExpr *Ref = nullptr;
4217 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4218 Captures[Capture] = Ref;
4219 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004220}
4221
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004222/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004223Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004224 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004225 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004226 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004227 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004228 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004229 SemaRef.getLangOpts().CPlusPlus) {
4230 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004231 Expr *UBExpr = TestIsLessOp ? UB : LB;
4232 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004233 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4234 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004235 if (!Upper || !Lower)
4236 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004237
4238 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4239
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004240 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004241 // BuildBinOp already emitted error, this one is to point user to upper
4242 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004243 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004244 << Upper->getSourceRange() << Lower->getSourceRange();
4245 return nullptr;
4246 }
4247 }
4248
4249 if (!Diff.isUsable())
4250 return nullptr;
4251
4252 // Upper - Lower [- 1]
4253 if (TestIsStrictOp)
4254 Diff = SemaRef.BuildBinOp(
4255 S, DefaultLoc, BO_Sub, Diff.get(),
4256 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4257 if (!Diff.isUsable())
4258 return nullptr;
4259
4260 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004261 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004262 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004263 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004264 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004265 if (!Diff.isUsable())
4266 return nullptr;
4267
4268 // Parentheses (for dumping/debugging purposes only).
4269 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4270 if (!Diff.isUsable())
4271 return nullptr;
4272
4273 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004274 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004275 if (!Diff.isUsable())
4276 return nullptr;
4277
Alexander Musman174b3ca2014-10-06 11:16:29 +00004278 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004279 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004280 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004281 bool UseVarType = VarType->hasIntegerRepresentation() &&
4282 C.getTypeSize(Type) > C.getTypeSize(VarType);
4283 if (!Type->isIntegerType() || UseVarType) {
4284 unsigned NewSize =
4285 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4286 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4287 : Type->hasSignedIntegerRepresentation();
4288 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004289 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4290 Diff = SemaRef.PerformImplicitConversion(
4291 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4292 if (!Diff.isUsable())
4293 return nullptr;
4294 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004295 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004296 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004297 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4298 if (NewSize != C.getTypeSize(Type)) {
4299 if (NewSize < C.getTypeSize(Type)) {
4300 assert(NewSize == 64 && "incorrect loop var size");
4301 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4302 << InitSrcRange << ConditionSrcRange;
4303 }
4304 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004305 NewSize, Type->hasSignedIntegerRepresentation() ||
4306 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004307 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4308 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4309 Sema::AA_Converting, true);
4310 if (!Diff.isUsable())
4311 return nullptr;
4312 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004313 }
4314 }
4315
Alexander Musmana5f070a2014-10-01 06:03:56 +00004316 return Diff.get();
4317}
4318
Alexey Bataeve3727102018-04-18 15:57:46 +00004319Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004320 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004321 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004322 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4323 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4324 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004325
Alexey Bataeve3727102018-04-18 15:57:46 +00004326 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4327 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004328 if (!NewLB.isUsable() || !NewUB.isUsable())
4329 return nullptr;
4330
Alexey Bataeve3727102018-04-18 15:57:46 +00004331 ExprResult CondExpr =
4332 SemaRef.BuildBinOp(S, DefaultLoc,
4333 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4334 : (TestIsStrictOp ? BO_GT : BO_GE),
4335 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004336 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004337 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4338 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004339 CondExpr = SemaRef.PerformImplicitConversion(
4340 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4341 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004342 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004343 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4344 // Otherwise use original loop conditon and evaluate it in runtime.
4345 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4346}
4347
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004348/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004349DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004350 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4351 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004352 auto *VD = dyn_cast<VarDecl>(LCDecl);
4353 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004354 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4355 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004356 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004357 const DSAStackTy::DSAVarData Data =
4358 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004359 // If the loop control decl is explicitly marked as private, do not mark it
4360 // as captured again.
4361 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4362 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004363 return Ref;
4364 }
4365 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004366 DefaultLoc);
4367}
4368
Alexey Bataeve3727102018-04-18 15:57:46 +00004369Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004370 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004371 QualType Type = LCDecl->getType().getNonReferenceType();
4372 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004373 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4374 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4375 isa<VarDecl>(LCDecl)
4376 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4377 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004378 if (PrivateVar->isInvalidDecl())
4379 return nullptr;
4380 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4381 }
4382 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383}
4384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004385/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004386Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004387
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004388/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004389Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004390
Alexey Bataevf138fda2018-08-13 19:04:24 +00004391Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4392 Scope *S, Expr *Counter,
4393 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4394 Expr *Inc, OverloadedOperatorKind OOK) {
4395 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4396 if (!Cnt)
4397 return nullptr;
4398 if (Inc) {
4399 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4400 "Expected only + or - operations for depend clauses.");
4401 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4402 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4403 if (!Cnt)
4404 return nullptr;
4405 }
4406 ExprResult Diff;
4407 QualType VarType = LCDecl->getType().getNonReferenceType();
4408 if (VarType->isIntegerType() || VarType->isPointerType() ||
4409 SemaRef.getLangOpts().CPlusPlus) {
4410 // Upper - Lower
4411 Expr *Upper =
4412 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4413 Expr *Lower =
4414 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4415 if (!Upper || !Lower)
4416 return nullptr;
4417
4418 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4419
4420 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4421 // BuildBinOp already emitted error, this one is to point user to upper
4422 // and lower bound, and to tell what is passed to 'operator-'.
4423 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4424 << Upper->getSourceRange() << Lower->getSourceRange();
4425 return nullptr;
4426 }
4427 }
4428
4429 if (!Diff.isUsable())
4430 return nullptr;
4431
4432 // Parentheses (for dumping/debugging purposes only).
4433 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4434 if (!Diff.isUsable())
4435 return nullptr;
4436
4437 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4438 if (!NewStep.isUsable())
4439 return nullptr;
4440 // (Upper - Lower) / Step
4441 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4442 if (!Diff.isUsable())
4443 return nullptr;
4444
4445 return Diff.get();
4446}
4447
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004448/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004449struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004450 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004451 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004452 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004453 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004454 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004455 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004456 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004457 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004458 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004459 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004460 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004461 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004462 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004463 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004464 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004465 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004466 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004467 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004468 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004469 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004470 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004471 SourceRange IncSrcRange;
4472};
4473
Alexey Bataev23b69422014-06-18 07:08:49 +00004474} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004475
Alexey Bataev9c821032015-04-30 04:23:23 +00004476void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4477 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4478 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004479 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4480 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004481 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4482 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004483 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4484 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004485 auto *VD = dyn_cast<VarDecl>(D);
4486 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004487 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004488 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004489 } else {
4490 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4491 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004492 VD = cast<VarDecl>(Ref->getDecl());
4493 }
4494 }
4495 DSAStack->addLoopControlVariable(D, VD);
4496 }
4497 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004498 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004499 }
4500}
4501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004502/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004503/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004504static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004505 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4506 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004507 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4508 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004509 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004510 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004511 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004512 // OpenMP [2.6, Canonical Loop Form]
4513 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004514 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004515 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004516 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004517 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004518 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004519 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004520 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004521 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4522 SemaRef.Diag(DSA.getConstructLoc(),
4523 diag::note_omp_collapse_ordered_expr)
4524 << 2 << CollapseLoopCountExpr->getSourceRange()
4525 << OrderedLoopCountExpr->getSourceRange();
4526 else if (CollapseLoopCountExpr)
4527 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4528 diag::note_omp_collapse_ordered_expr)
4529 << 0 << CollapseLoopCountExpr->getSourceRange();
4530 else
4531 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4532 diag::note_omp_collapse_ordered_expr)
4533 << 1 << OrderedLoopCountExpr->getSourceRange();
4534 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004535 return true;
4536 }
4537 assert(For->getBody());
4538
4539 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4540
4541 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004542 Stmt *Init = For->getInit();
4543 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004545
4546 bool HasErrors = false;
4547
4548 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004549 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4550 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004551
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004552 // OpenMP [2.6, Canonical Loop Form]
4553 // Var is one of the following:
4554 // A variable of signed or unsigned integer type.
4555 // For C++, a variable of a random access iterator type.
4556 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004557 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004558 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4559 !VarType->isPointerType() &&
4560 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004561 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004562 << SemaRef.getLangOpts().CPlusPlus;
4563 HasErrors = true;
4564 }
4565
4566 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4567 // a Construct
4568 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4569 // parallel for construct is (are) private.
4570 // The loop iteration variable in the associated for-loop of a simd
4571 // construct with just one associated for-loop is linear with a
4572 // constant-linear-step that is the increment of the associated for-loop.
4573 // Exclude loop var from the list of variables with implicitly defined data
4574 // sharing attributes.
4575 VarsWithImplicitDSA.erase(LCDecl);
4576
4577 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4578 // in a Construct, C/C++].
4579 // The loop iteration variable in the associated for-loop of a simd
4580 // construct with just one associated for-loop may be listed in a linear
4581 // clause with a constant-linear-step that is the increment of the
4582 // associated for-loop.
4583 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4584 // parallel for construct may be listed in a private or lastprivate clause.
4585 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4586 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4587 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004588 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004589 isOpenMPSimdDirective(DKind)
4590 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4591 : OMPC_private;
4592 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4593 DVar.CKind != PredeterminedCKind) ||
4594 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4595 isOpenMPDistributeDirective(DKind)) &&
4596 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4597 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4598 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004599 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004600 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4601 << getOpenMPClauseName(PredeterminedCKind);
4602 if (DVar.RefExpr == nullptr)
4603 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004604 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004605 HasErrors = true;
4606 } else if (LoopDeclRefExpr != nullptr) {
4607 // Make the loop iteration variable private (for worksharing constructs),
4608 // linear (for simd directives with the only one associated loop) or
4609 // lastprivate (for simd directives with several collapsed or ordered
4610 // loops).
4611 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004612 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4613 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004614 /*FromParent=*/false);
4615 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4616 }
4617
4618 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4619
4620 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004621 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004622
4623 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004624 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004625 }
4626
Alexey Bataeve3727102018-04-18 15:57:46 +00004627 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004628 return HasErrors;
4629
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004631 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004632 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4633 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004634 DSA.getCurScope(),
4635 (isOpenMPWorksharingDirective(DKind) ||
4636 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4637 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004638 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4639 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4640 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4641 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4642 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4643 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4644 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4645 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004646
Alexey Bataev62dbb972015-04-22 11:59:37 +00004647 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4648 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004649 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004650 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004651 ResultIterSpace.CounterInit == nullptr ||
4652 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004653 if (!HasErrors && DSA.isOrderedRegion()) {
4654 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4655 if (CurrentNestedLoopCount <
4656 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4657 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4658 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4659 DSA.getOrderedRegionParam().second->setLoopCounter(
4660 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4661 }
4662 }
4663 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4664 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4665 // Erroneous case - clause has some problems.
4666 continue;
4667 }
4668 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4669 Pair.second.size() <= CurrentNestedLoopCount) {
4670 // Erroneous case - clause has some problems.
4671 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4672 continue;
4673 }
4674 Expr *CntValue;
4675 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4676 CntValue = ISC.buildOrderedLoopData(
4677 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4678 Pair.first->getDependencyLoc());
4679 else
4680 CntValue = ISC.buildOrderedLoopData(
4681 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4682 Pair.first->getDependencyLoc(),
4683 Pair.second[CurrentNestedLoopCount].first,
4684 Pair.second[CurrentNestedLoopCount].second);
4685 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4686 }
4687 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004688
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004689 return HasErrors;
4690}
4691
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004692/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004693static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004694buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004695 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004696 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004697 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004698 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004699 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004700 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004701 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004702 VarRef.get()->getType())) {
4703 NewStart = SemaRef.PerformImplicitConversion(
4704 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4705 /*AllowExplicit=*/true);
4706 if (!NewStart.isUsable())
4707 return ExprError();
4708 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004709
Alexey Bataeve3727102018-04-18 15:57:46 +00004710 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004711 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4712 return Init;
4713}
4714
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004715/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004716static ExprResult buildCounterUpdate(
4717 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4718 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4719 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004720 // Add parentheses (for debugging purposes only).
4721 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4722 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4723 !Step.isUsable())
4724 return ExprError();
4725
Alexey Bataev5a3af132016-03-29 08:58:54 +00004726 ExprResult NewStep = Step;
4727 if (Captures)
4728 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004729 if (NewStep.isInvalid())
4730 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004731 ExprResult Update =
4732 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004733 if (!Update.isUsable())
4734 return ExprError();
4735
Alexey Bataevc0214e02016-02-16 12:13:49 +00004736 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4737 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004738 ExprResult NewStart = Start;
4739 if (Captures)
4740 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004741 if (NewStart.isInvalid())
4742 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004743
Alexey Bataevc0214e02016-02-16 12:13:49 +00004744 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4745 ExprResult SavedUpdate = Update;
4746 ExprResult UpdateVal;
4747 if (VarRef.get()->getType()->isOverloadableType() ||
4748 NewStart.get()->getType()->isOverloadableType() ||
4749 Update.get()->getType()->isOverloadableType()) {
4750 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4751 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4752 Update =
4753 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4754 if (Update.isUsable()) {
4755 UpdateVal =
4756 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4757 VarRef.get(), SavedUpdate.get());
4758 if (UpdateVal.isUsable()) {
4759 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4760 UpdateVal.get());
4761 }
4762 }
4763 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4764 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004765
Alexey Bataevc0214e02016-02-16 12:13:49 +00004766 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4767 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4768 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4769 NewStart.get(), SavedUpdate.get());
4770 if (!Update.isUsable())
4771 return ExprError();
4772
Alexey Bataev11481f52016-02-17 10:29:05 +00004773 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4774 VarRef.get()->getType())) {
4775 Update = SemaRef.PerformImplicitConversion(
4776 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4777 if (!Update.isUsable())
4778 return ExprError();
4779 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004780
4781 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4782 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004783 return Update;
4784}
4785
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004786/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004787/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004788static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004789 if (E == nullptr)
4790 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004791 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004792 QualType OldType = E->getType();
4793 unsigned HasBits = C.getTypeSize(OldType);
4794 if (HasBits >= Bits)
4795 return ExprResult(E);
4796 // OK to convert to signed, because new type has more bits than old.
4797 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4798 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4799 true);
4800}
4801
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004802/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004803/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004804static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004805 if (E == nullptr)
4806 return false;
4807 llvm::APSInt Result;
4808 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4809 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4810 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004811}
4812
Alexey Bataev5a3af132016-03-29 08:58:54 +00004813/// Build preinits statement for the given declarations.
4814static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004815 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004816 if (!PreInits.empty()) {
4817 return new (Context) DeclStmt(
4818 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4819 SourceLocation(), SourceLocation());
4820 }
4821 return nullptr;
4822}
4823
4824/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004825static Stmt *
4826buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004827 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004828 if (!Captures.empty()) {
4829 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004830 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004831 PreInits.push_back(Pair.second->getDecl());
4832 return buildPreInits(Context, PreInits);
4833 }
4834 return nullptr;
4835}
4836
4837/// Build postupdate expression for the given list of postupdates expressions.
4838static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4839 Expr *PostUpdate = nullptr;
4840 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004841 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004842 Expr *ConvE = S.BuildCStyleCastExpr(
4843 E->getExprLoc(),
4844 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4845 E->getExprLoc(), E)
4846 .get();
4847 PostUpdate = PostUpdate
4848 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4849 PostUpdate, ConvE)
4850 .get()
4851 : ConvE;
4852 }
4853 }
4854 return PostUpdate;
4855}
4856
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004857/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004858/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4859/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004860static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00004861checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004862 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4863 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00004864 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004865 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004866 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004867 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004868 // Found 'collapse' clause - calculate collapse number.
4869 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004870 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004871 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004872 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004873 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004874 if (OrderedLoopCountExpr) {
4875 // Found 'ordered' clause - calculate collapse number.
4876 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004877 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4878 if (Result.getLimitedValue() < NestedLoopCount) {
4879 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4880 diag::err_omp_wrong_ordered_loop_count)
4881 << OrderedLoopCountExpr->getSourceRange();
4882 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4883 diag::note_collapse_loop_count)
4884 << CollapseLoopCountExpr->getSourceRange();
4885 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004886 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004887 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004888 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004889 // This is helper routine for loop directives (e.g., 'for', 'simd',
4890 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00004891 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004892 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004893 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004894 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004895 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00004896 if (checkOpenMPIterationSpace(
4897 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4898 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4899 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4900 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004901 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004902 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004903 // OpenMP [2.8.1, simd construct, Restrictions]
4904 // All loops associated with the construct must be perfectly nested; that
4905 // is, there must be no intervening code nor any OpenMP directive between
4906 // any two loops.
4907 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004908 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004909 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
4910 if (checkOpenMPIterationSpace(
4911 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4912 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4913 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4914 Captures))
4915 return 0;
4916 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
4917 // Handle initialization of captured loop iterator variables.
4918 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
4919 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
4920 Captures[DRE] = DRE;
4921 }
4922 }
4923 // Move on to the next nested for loop, or to the loop body.
4924 // OpenMP [2.8.1, simd construct, Restrictions]
4925 // All loops associated with the construct must be perfectly nested; that
4926 // is, there must be no intervening code nor any OpenMP directive between
4927 // any two loops.
4928 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4929 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004930
Alexander Musmana5f070a2014-10-01 06:03:56 +00004931 Built.clear(/* size */ NestedLoopCount);
4932
4933 if (SemaRef.CurContext->isDependentContext())
4934 return NestedLoopCount;
4935
4936 // An example of what is generated for the following code:
4937 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004938 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004939 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004940 // for (k = 0; k < NK; ++k)
4941 // for (j = J0; j < NJ; j+=2) {
4942 // <loop body>
4943 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004944 //
4945 // We generate the code below.
4946 // Note: the loop body may be outlined in CodeGen.
4947 // Note: some counters may be C++ classes, operator- is used to find number of
4948 // iterations and operator+= to calculate counter value.
4949 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4950 // or i64 is currently supported).
4951 //
4952 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4953 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4954 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4955 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4956 // // similar updates for vars in clauses (e.g. 'linear')
4957 // <loop body (using local i and j)>
4958 // }
4959 // i = NI; // assign final values of counters
4960 // j = NJ;
4961 //
4962
4963 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4964 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004965 // Precondition tests if there is at least one iteration (all conditions are
4966 // true).
4967 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00004968 Expr *N0 = IterSpaces[0].NumIterations;
4969 ExprResult LastIteration32 =
4970 widenIterationCount(/*Bits=*/32,
4971 SemaRef
4972 .PerformImplicitConversion(
4973 N0->IgnoreImpCasts(), N0->getType(),
4974 Sema::AA_Converting, /*AllowExplicit=*/true)
4975 .get(),
4976 SemaRef);
4977 ExprResult LastIteration64 = widenIterationCount(
4978 /*Bits=*/64,
4979 SemaRef
4980 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
4981 Sema::AA_Converting,
4982 /*AllowExplicit=*/true)
4983 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004984 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004985
4986 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4987 return NestedLoopCount;
4988
Alexey Bataeve3727102018-04-18 15:57:46 +00004989 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004990 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4991
4992 Scope *CurScope = DSA.getCurScope();
4993 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004994 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00004995 PreCond =
4996 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4997 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00004998 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004999 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005000 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005001 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5002 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005003 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005004 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005005 SemaRef
5006 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5007 Sema::AA_Converting,
5008 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005009 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005010 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005011 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005012 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005013 SemaRef
5014 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5015 Sema::AA_Converting,
5016 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005017 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005018 }
5019
5020 // Choose either the 32-bit or 64-bit version.
5021 ExprResult LastIteration = LastIteration64;
5022 if (LastIteration32.isUsable() &&
5023 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5024 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005025 fitsInto(
5026 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005027 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5028 LastIteration64.get(), SemaRef)))
5029 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005030 QualType VType = LastIteration.get()->getType();
5031 QualType RealVType = VType;
5032 QualType StrideVType = VType;
5033 if (isOpenMPTaskLoopDirective(DKind)) {
5034 VType =
5035 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5036 StrideVType =
5037 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5038 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039
5040 if (!LastIteration.isUsable())
5041 return 0;
5042
5043 // Save the number of iterations.
5044 ExprResult NumIterations = LastIteration;
5045 {
5046 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005047 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5048 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005049 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5050 if (!LastIteration.isUsable())
5051 return 0;
5052 }
5053
5054 // Calculate the last iteration number beforehand instead of doing this on
5055 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5056 llvm::APSInt Result;
5057 bool IsConstant =
5058 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5059 ExprResult CalcLastIteration;
5060 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005061 ExprResult SaveRef =
5062 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005063 LastIteration = SaveRef;
5064
5065 // Prepare SaveRef + 1.
5066 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005067 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005068 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5069 if (!NumIterations.isUsable())
5070 return 0;
5071 }
5072
5073 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5074
David Majnemer9d168222016-08-05 17:44:54 +00005075 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005076 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005077 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5078 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005079 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005080 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5081 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005082 SemaRef.AddInitializerToDecl(LBDecl,
5083 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5084 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005085
5086 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005087 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5088 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005089 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005090 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005091
5092 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5093 // This will be used to implement clause 'lastprivate'.
5094 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005095 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5096 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005097 SemaRef.AddInitializerToDecl(ILDecl,
5098 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5099 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005100
5101 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005102 VarDecl *STDecl =
5103 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5104 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005105 SemaRef.AddInitializerToDecl(STDecl,
5106 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5107 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005108
5109 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005110 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005111 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5112 UB.get(), LastIteration.get());
5113 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005114 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5115 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005116 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5117 CondOp.get());
5118 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005119
5120 // If we have a combined directive that combines 'distribute', 'for' or
5121 // 'simd' we need to be able to access the bounds of the schedule of the
5122 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5123 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5124 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005125 // Lower bound variable, initialized with zero.
5126 VarDecl *CombLBDecl =
5127 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5128 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5129 SemaRef.AddInitializerToDecl(
5130 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5131 /*DirectInit*/ false);
5132
5133 // Upper bound variable, initialized with last iteration number.
5134 VarDecl *CombUBDecl =
5135 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5136 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5137 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5138 /*DirectInit*/ false);
5139
5140 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5141 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5142 ExprResult CombCondOp =
5143 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5144 LastIteration.get(), CombUB.get());
5145 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5146 CombCondOp.get());
5147 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5148
Alexey Bataeve3727102018-04-18 15:57:46 +00005149 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005150 // We expect to have at least 2 more parameters than the 'parallel'
5151 // directive does - the lower and upper bounds of the previous schedule.
5152 assert(CD->getNumParams() >= 4 &&
5153 "Unexpected number of parameters in loop combined directive");
5154
5155 // Set the proper type for the bounds given what we learned from the
5156 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005157 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5158 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005159
5160 // Previous lower and upper bounds are obtained from the region
5161 // parameters.
5162 PrevLB =
5163 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5164 PrevUB =
5165 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5166 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005167 }
5168
5169 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005170 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005171 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005172 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005173 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5174 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005175 Expr *RHS =
5176 (isOpenMPWorksharingDirective(DKind) ||
5177 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5178 ? LB.get()
5179 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005180 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5181 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005182
5183 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5184 Expr *CombRHS =
5185 (isOpenMPWorksharingDirective(DKind) ||
5186 isOpenMPTaskLoopDirective(DKind) ||
5187 isOpenMPDistributeDirective(DKind))
5188 ? CombLB.get()
5189 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5190 CombInit =
5191 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5192 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5193 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005194 }
5195
Alexander Musmanc6388682014-12-15 07:07:06 +00005196 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005197 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005198 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005199 (isOpenMPWorksharingDirective(DKind) ||
5200 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005201 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5202 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5203 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005204 ExprResult CombCond;
5205 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5206 CombCond =
5207 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5208 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005209 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005210 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005211 ExprResult Inc =
5212 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5213 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5214 if (!Inc.isUsable())
5215 return 0;
5216 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005217 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5218 if (!Inc.isUsable())
5219 return 0;
5220
5221 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5222 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005223 // In combined construct, add combined version that use CombLB and CombUB
5224 // base variables for the update
5225 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005226 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5227 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005228 // LB + ST
5229 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5230 if (!NextLB.isUsable())
5231 return 0;
5232 // LB = LB + ST
5233 NextLB =
5234 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5235 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5236 if (!NextLB.isUsable())
5237 return 0;
5238 // UB + ST
5239 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5240 if (!NextUB.isUsable())
5241 return 0;
5242 // UB = UB + ST
5243 NextUB =
5244 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5245 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5246 if (!NextUB.isUsable())
5247 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005248 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5249 CombNextLB =
5250 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5251 if (!NextLB.isUsable())
5252 return 0;
5253 // LB = LB + ST
5254 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5255 CombNextLB.get());
5256 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5257 if (!CombNextLB.isUsable())
5258 return 0;
5259 // UB + ST
5260 CombNextUB =
5261 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5262 if (!CombNextUB.isUsable())
5263 return 0;
5264 // UB = UB + ST
5265 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5266 CombNextUB.get());
5267 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5268 if (!CombNextUB.isUsable())
5269 return 0;
5270 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005271 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005272
Carlo Bertolliffafe102017-04-20 00:39:39 +00005273 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005274 // directive with for as IV = IV + ST; ensure upper bound expression based
5275 // on PrevUB instead of NumIterations - used to implement 'for' when found
5276 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005277 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005278 ExprResult DistCond, DistInc, PrevEUB;
5279 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5280 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5281 assert(DistCond.isUsable() && "distribute cond expr was not built");
5282
5283 DistInc =
5284 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5285 assert(DistInc.isUsable() && "distribute inc expr was not built");
5286 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5287 DistInc.get());
5288 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5289 assert(DistInc.isUsable() && "distribute inc expr was not built");
5290
5291 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5292 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005293 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005294 ExprResult IsUBGreater =
5295 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5296 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5297 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5298 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5299 CondOp.get());
5300 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5301 }
5302
Alexander Musmana5f070a2014-10-01 06:03:56 +00005303 // Build updates and final values of the loop counters.
5304 bool HasErrors = false;
5305 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005306 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005307 Built.Updates.resize(NestedLoopCount);
5308 Built.Finals.resize(NestedLoopCount);
5309 {
5310 ExprResult Div;
5311 // Go from inner nested loop to outer.
5312 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5313 LoopIterationSpace &IS = IterSpaces[Cnt];
5314 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5315 // Build: Iter = (IV / Div) % IS.NumIters
5316 // where Div is product of previous iterations' IS.NumIters.
5317 ExprResult Iter;
5318 if (Div.isUsable()) {
5319 Iter =
5320 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5321 } else {
5322 Iter = IV;
5323 assert((Cnt == (int)NestedLoopCount - 1) &&
5324 "unusable div expected on first iteration only");
5325 }
5326
5327 if (Cnt != 0 && Iter.isUsable())
5328 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5329 IS.NumIterations);
5330 if (!Iter.isUsable()) {
5331 HasErrors = true;
5332 break;
5333 }
5334
Alexey Bataev39f915b82015-05-08 10:41:21 +00005335 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005336 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005337 DeclRefExpr *CounterVar = buildDeclRefExpr(
5338 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5339 /*RefersToCapture=*/true);
5340 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005341 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005342 if (!Init.isUsable()) {
5343 HasErrors = true;
5344 break;
5345 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005346 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005347 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5348 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005349 if (!Update.isUsable()) {
5350 HasErrors = true;
5351 break;
5352 }
5353
5354 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005355 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005356 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005357 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005358 if (!Final.isUsable()) {
5359 HasErrors = true;
5360 break;
5361 }
5362
5363 // Build Div for the next iteration: Div <- Div * IS.NumIters
5364 if (Cnt != 0) {
5365 if (Div.isUnset())
5366 Div = IS.NumIterations;
5367 else
5368 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5369 IS.NumIterations);
5370
5371 // Add parentheses (for debugging purposes only).
5372 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005373 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005374 if (!Div.isUsable()) {
5375 HasErrors = true;
5376 break;
5377 }
5378 }
5379 if (!Update.isUsable() || !Final.isUsable()) {
5380 HasErrors = true;
5381 break;
5382 }
5383 // Save results
5384 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005385 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005386 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005387 Built.Updates[Cnt] = Update.get();
5388 Built.Finals[Cnt] = Final.get();
5389 }
5390 }
5391
5392 if (HasErrors)
5393 return 0;
5394
5395 // Save results
5396 Built.IterationVarRef = IV.get();
5397 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005398 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005399 Built.CalcLastIteration =
5400 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005401 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005402 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005403 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005404 Built.Init = Init.get();
5405 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005406 Built.LB = LB.get();
5407 Built.UB = UB.get();
5408 Built.IL = IL.get();
5409 Built.ST = ST.get();
5410 Built.EUB = EUB.get();
5411 Built.NLB = NextLB.get();
5412 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005413 Built.PrevLB = PrevLB.get();
5414 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005415 Built.DistInc = DistInc.get();
5416 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005417 Built.DistCombinedFields.LB = CombLB.get();
5418 Built.DistCombinedFields.UB = CombUB.get();
5419 Built.DistCombinedFields.EUB = CombEUB.get();
5420 Built.DistCombinedFields.Init = CombInit.get();
5421 Built.DistCombinedFields.Cond = CombCond.get();
5422 Built.DistCombinedFields.NLB = CombNextLB.get();
5423 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005424
Alexey Bataevabfc0692014-06-25 06:52:00 +00005425 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005426}
5427
Alexey Bataev10e775f2015-07-30 11:36:16 +00005428static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005429 auto CollapseClauses =
5430 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5431 if (CollapseClauses.begin() != CollapseClauses.end())
5432 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005433 return nullptr;
5434}
5435
Alexey Bataev10e775f2015-07-30 11:36:16 +00005436static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005437 auto OrderedClauses =
5438 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5439 if (OrderedClauses.begin() != OrderedClauses.end())
5440 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005441 return nullptr;
5442}
5443
Kelvin Lic5609492016-07-15 04:39:07 +00005444static bool checkSimdlenSafelenSpecified(Sema &S,
5445 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005446 const OMPSafelenClause *Safelen = nullptr;
5447 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005448
Alexey Bataeve3727102018-04-18 15:57:46 +00005449 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005450 if (Clause->getClauseKind() == OMPC_safelen)
5451 Safelen = cast<OMPSafelenClause>(Clause);
5452 else if (Clause->getClauseKind() == OMPC_simdlen)
5453 Simdlen = cast<OMPSimdlenClause>(Clause);
5454 if (Safelen && Simdlen)
5455 break;
5456 }
5457
5458 if (Simdlen && Safelen) {
5459 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005460 const Expr *SimdlenLength = Simdlen->getSimdlen();
5461 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005462 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5463 SimdlenLength->isInstantiationDependent() ||
5464 SimdlenLength->containsUnexpandedParameterPack())
5465 return false;
5466 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5467 SafelenLength->isInstantiationDependent() ||
5468 SafelenLength->containsUnexpandedParameterPack())
5469 return false;
5470 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5471 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5472 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5473 // If both simdlen and safelen clauses are specified, the value of the
5474 // simdlen parameter must be less than or equal to the value of the safelen
5475 // parameter.
5476 if (SimdlenRes > SafelenRes) {
5477 S.Diag(SimdlenLength->getExprLoc(),
5478 diag::err_omp_wrong_simdlen_safelen_values)
5479 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5480 return true;
5481 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005482 }
5483 return false;
5484}
5485
Alexey Bataeve3727102018-04-18 15:57:46 +00005486StmtResult
5487Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5488 SourceLocation StartLoc, SourceLocation EndLoc,
5489 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005490 if (!AStmt)
5491 return StmtError();
5492
5493 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005494 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005495 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5496 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005497 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005498 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5499 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005500 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005501 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005502
Alexander Musmana5f070a2014-10-01 06:03:56 +00005503 assert((CurContext->isDependentContext() || B.builtAll()) &&
5504 "omp simd loop exprs were not built");
5505
Alexander Musman3276a272015-03-21 10:12:56 +00005506 if (!CurContext->isDependentContext()) {
5507 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005508 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005509 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005510 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005511 B.NumIterations, *this, CurScope,
5512 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005513 return StmtError();
5514 }
5515 }
5516
Kelvin Lic5609492016-07-15 04:39:07 +00005517 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005518 return StmtError();
5519
Reid Kleckner87a31802018-03-12 21:43:02 +00005520 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005521 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5522 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005523}
5524
Alexey Bataeve3727102018-04-18 15:57:46 +00005525StmtResult
5526Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5527 SourceLocation StartLoc, SourceLocation EndLoc,
5528 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005529 if (!AStmt)
5530 return StmtError();
5531
5532 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005533 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005534 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5535 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005536 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005537 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5538 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005539 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005540 return StmtError();
5541
Alexander Musmana5f070a2014-10-01 06:03:56 +00005542 assert((CurContext->isDependentContext() || B.builtAll()) &&
5543 "omp for loop exprs were not built");
5544
Alexey Bataev54acd402015-08-04 11:18:19 +00005545 if (!CurContext->isDependentContext()) {
5546 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005547 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005548 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005549 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005550 B.NumIterations, *this, CurScope,
5551 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005552 return StmtError();
5553 }
5554 }
5555
Reid Kleckner87a31802018-03-12 21:43:02 +00005556 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005557 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005558 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005559}
5560
Alexander Musmanf82886e2014-09-18 05:12:34 +00005561StmtResult Sema::ActOnOpenMPForSimdDirective(
5562 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005563 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005564 if (!AStmt)
5565 return StmtError();
5566
5567 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005568 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005569 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5570 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005571 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005572 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005573 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5574 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005575 if (NestedLoopCount == 0)
5576 return StmtError();
5577
Alexander Musmanc6388682014-12-15 07:07:06 +00005578 assert((CurContext->isDependentContext() || B.builtAll()) &&
5579 "omp for simd loop exprs were not built");
5580
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005581 if (!CurContext->isDependentContext()) {
5582 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005583 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005584 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005585 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005586 B.NumIterations, *this, CurScope,
5587 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005588 return StmtError();
5589 }
5590 }
5591
Kelvin Lic5609492016-07-15 04:39:07 +00005592 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005593 return StmtError();
5594
Reid Kleckner87a31802018-03-12 21:43:02 +00005595 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005596 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5597 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005598}
5599
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005600StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5601 Stmt *AStmt,
5602 SourceLocation StartLoc,
5603 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005604 if (!AStmt)
5605 return StmtError();
5606
5607 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005608 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005609 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005610 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005611 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005612 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005613 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005614 return StmtError();
5615 // All associated statements must be '#pragma omp section' except for
5616 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005617 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005618 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5619 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005620 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005621 diag::err_omp_sections_substmt_not_section);
5622 return StmtError();
5623 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005624 cast<OMPSectionDirective>(SectionStmt)
5625 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005626 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005627 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005628 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005629 return StmtError();
5630 }
5631
Reid Kleckner87a31802018-03-12 21:43:02 +00005632 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005633
Alexey Bataev25e5b442015-09-15 12:52:43 +00005634 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5635 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005636}
5637
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005638StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5639 SourceLocation StartLoc,
5640 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005641 if (!AStmt)
5642 return StmtError();
5643
5644 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005645
Reid Kleckner87a31802018-03-12 21:43:02 +00005646 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005647 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005648
Alexey Bataev25e5b442015-09-15 12:52:43 +00005649 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5650 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005651}
5652
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005653StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5654 Stmt *AStmt,
5655 SourceLocation StartLoc,
5656 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005657 if (!AStmt)
5658 return StmtError();
5659
5660 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005661
Reid Kleckner87a31802018-03-12 21:43:02 +00005662 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005663
Alexey Bataev3255bf32015-01-19 05:20:46 +00005664 // OpenMP [2.7.3, single Construct, Restrictions]
5665 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005666 const OMPClause *Nowait = nullptr;
5667 const OMPClause *Copyprivate = nullptr;
5668 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005669 if (Clause->getClauseKind() == OMPC_nowait)
5670 Nowait = Clause;
5671 else if (Clause->getClauseKind() == OMPC_copyprivate)
5672 Copyprivate = Clause;
5673 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005674 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005675 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005676 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005677 return StmtError();
5678 }
5679 }
5680
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005681 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5682}
5683
Alexander Musman80c22892014-07-17 08:54:58 +00005684StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5685 SourceLocation StartLoc,
5686 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005687 if (!AStmt)
5688 return StmtError();
5689
5690 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005691
Reid Kleckner87a31802018-03-12 21:43:02 +00005692 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005693
5694 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5695}
5696
Alexey Bataev28c75412015-12-15 08:19:24 +00005697StmtResult Sema::ActOnOpenMPCriticalDirective(
5698 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5699 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005700 if (!AStmt)
5701 return StmtError();
5702
5703 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005704
Alexey Bataev28c75412015-12-15 08:19:24 +00005705 bool ErrorFound = false;
5706 llvm::APSInt Hint;
5707 SourceLocation HintLoc;
5708 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005709 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005710 if (C->getClauseKind() == OMPC_hint) {
5711 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005712 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005713 ErrorFound = true;
5714 }
5715 Expr *E = cast<OMPHintClause>(C)->getHint();
5716 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005717 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005718 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005719 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005720 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005721 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005722 }
5723 }
5724 }
5725 if (ErrorFound)
5726 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005727 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005728 if (Pair.first && DirName.getName() && !DependentHint) {
5729 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5730 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005731 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005732 Diag(HintLoc, diag::note_omp_critical_hint_here)
5733 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005734 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005735 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005736 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005737 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005738 << 1
5739 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5740 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005741 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005742 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005743 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005744 }
5745 }
5746
Reid Kleckner87a31802018-03-12 21:43:02 +00005747 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005748
Alexey Bataev28c75412015-12-15 08:19:24 +00005749 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5750 Clauses, AStmt);
5751 if (!Pair.first && DirName.getName() && !DependentHint)
5752 DSAStack->addCriticalWithHint(Dir, Hint);
5753 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005754}
5755
Alexey Bataev4acb8592014-07-07 13:01:15 +00005756StmtResult Sema::ActOnOpenMPParallelForDirective(
5757 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005758 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005759 if (!AStmt)
5760 return StmtError();
5761
Alexey Bataeve3727102018-04-18 15:57:46 +00005762 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005763 // 1.2.2 OpenMP Language Terminology
5764 // Structured block - An executable statement with a single entry at the
5765 // top and a single exit at the bottom.
5766 // The point of exit cannot be a branch out of the structured block.
5767 // longjmp() and throw() must not violate the entry/exit criteria.
5768 CS->getCapturedDecl()->setNothrow();
5769
Alexander Musmanc6388682014-12-15 07:07:06 +00005770 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005771 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5772 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005773 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005774 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005775 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5776 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005777 if (NestedLoopCount == 0)
5778 return StmtError();
5779
Alexander Musmana5f070a2014-10-01 06:03:56 +00005780 assert((CurContext->isDependentContext() || B.builtAll()) &&
5781 "omp parallel for loop exprs were not built");
5782
Alexey Bataev54acd402015-08-04 11:18:19 +00005783 if (!CurContext->isDependentContext()) {
5784 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005785 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005786 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005787 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005788 B.NumIterations, *this, CurScope,
5789 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005790 return StmtError();
5791 }
5792 }
5793
Reid Kleckner87a31802018-03-12 21:43:02 +00005794 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005795 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005796 NestedLoopCount, Clauses, AStmt, B,
5797 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005798}
5799
Alexander Musmane4e893b2014-09-23 09:33:00 +00005800StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5801 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005802 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005803 if (!AStmt)
5804 return StmtError();
5805
Alexey Bataeve3727102018-04-18 15:57:46 +00005806 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005807 // 1.2.2 OpenMP Language Terminology
5808 // Structured block - An executable statement with a single entry at the
5809 // top and a single exit at the bottom.
5810 // The point of exit cannot be a branch out of the structured block.
5811 // longjmp() and throw() must not violate the entry/exit criteria.
5812 CS->getCapturedDecl()->setNothrow();
5813
Alexander Musmanc6388682014-12-15 07:07:06 +00005814 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005815 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5816 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005817 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005818 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005819 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5820 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005821 if (NestedLoopCount == 0)
5822 return StmtError();
5823
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005824 if (!CurContext->isDependentContext()) {
5825 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005826 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005827 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005828 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005829 B.NumIterations, *this, CurScope,
5830 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005831 return StmtError();
5832 }
5833 }
5834
Kelvin Lic5609492016-07-15 04:39:07 +00005835 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005836 return StmtError();
5837
Reid Kleckner87a31802018-03-12 21:43:02 +00005838 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005839 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005840 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005841}
5842
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005843StmtResult
5844Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5845 Stmt *AStmt, SourceLocation StartLoc,
5846 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005847 if (!AStmt)
5848 return StmtError();
5849
5850 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005851 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005852 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005853 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005854 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005855 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005856 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005857 return StmtError();
5858 // All associated statements must be '#pragma omp section' except for
5859 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005860 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005861 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5862 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005863 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005864 diag::err_omp_parallel_sections_substmt_not_section);
5865 return StmtError();
5866 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005867 cast<OMPSectionDirective>(SectionStmt)
5868 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005869 }
5870 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005871 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005872 diag::err_omp_parallel_sections_not_compound_stmt);
5873 return StmtError();
5874 }
5875
Reid Kleckner87a31802018-03-12 21:43:02 +00005876 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005877
Alexey Bataev25e5b442015-09-15 12:52:43 +00005878 return OMPParallelSectionsDirective::Create(
5879 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005880}
5881
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005882StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5883 Stmt *AStmt, SourceLocation StartLoc,
5884 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005885 if (!AStmt)
5886 return StmtError();
5887
David Majnemer9d168222016-08-05 17:44:54 +00005888 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005889 // 1.2.2 OpenMP Language Terminology
5890 // Structured block - An executable statement with a single entry at the
5891 // top and a single exit at the bottom.
5892 // The point of exit cannot be a branch out of the structured block.
5893 // longjmp() and throw() must not violate the entry/exit criteria.
5894 CS->getCapturedDecl()->setNothrow();
5895
Reid Kleckner87a31802018-03-12 21:43:02 +00005896 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005897
Alexey Bataev25e5b442015-09-15 12:52:43 +00005898 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5899 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005900}
5901
Alexey Bataev68446b72014-07-18 07:47:19 +00005902StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5903 SourceLocation EndLoc) {
5904 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5905}
5906
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005907StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5908 SourceLocation EndLoc) {
5909 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5910}
5911
Alexey Bataev2df347a2014-07-18 10:17:07 +00005912StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5913 SourceLocation EndLoc) {
5914 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5915}
5916
Alexey Bataev169d96a2017-07-18 20:17:46 +00005917StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5918 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005919 SourceLocation StartLoc,
5920 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005921 if (!AStmt)
5922 return StmtError();
5923
5924 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005925
Reid Kleckner87a31802018-03-12 21:43:02 +00005926 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005927
Alexey Bataev169d96a2017-07-18 20:17:46 +00005928 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005929 AStmt,
5930 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005931}
5932
Alexey Bataev6125da92014-07-21 11:26:11 +00005933StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5934 SourceLocation StartLoc,
5935 SourceLocation EndLoc) {
5936 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5937 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5938}
5939
Alexey Bataev346265e2015-09-25 10:37:12 +00005940StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5941 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005942 SourceLocation StartLoc,
5943 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005944 const OMPClause *DependFound = nullptr;
5945 const OMPClause *DependSourceClause = nullptr;
5946 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005947 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005948 const OMPThreadsClause *TC = nullptr;
5949 const OMPSIMDClause *SC = nullptr;
5950 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005951 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5952 DependFound = C;
5953 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5954 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005955 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00005956 << getOpenMPDirectiveName(OMPD_ordered)
5957 << getOpenMPClauseName(OMPC_depend) << 2;
5958 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005959 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00005960 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00005961 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005962 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005963 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005964 << 0;
5965 ErrorFound = true;
5966 }
5967 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5968 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005969 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005970 << 1;
5971 ErrorFound = true;
5972 }
5973 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005974 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005975 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00005976 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005977 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005978 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005979 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005980 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005981 if (!ErrorFound && !SC &&
5982 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005983 // OpenMP [2.8.1,simd Construct, Restrictions]
5984 // An ordered construct with the simd clause is the only OpenMP construct
5985 // that can appear in the simd region.
5986 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005987 ErrorFound = true;
5988 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005989 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00005990 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5991 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005992 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005993 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00005994 diag::err_omp_ordered_directive_without_param);
5995 ErrorFound = true;
5996 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005997 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005998 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00005999 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6000 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006001 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006002 ErrorFound = true;
6003 }
6004 }
6005 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006006 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006007
6008 if (AStmt) {
6009 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6010
Reid Kleckner87a31802018-03-12 21:43:02 +00006011 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006012 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006013
6014 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006015}
6016
Alexey Bataev1d160b12015-03-13 12:27:31 +00006017namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006018/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006019/// construct.
6020class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006021 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006022 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006023 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006024 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006025 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006026 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006027 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006028 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006029 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006030 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006031 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006032 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006033 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006034 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006035 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006036 /// expression.
6037 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006038 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006039 /// part.
6040 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006041 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006042 NoError
6043 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006044 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006045 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006046 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006047 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006048 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006049 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006050 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006051 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006052 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006053 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6054 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6055 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006056 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006057 /// important for non-associative operations.
6058 bool IsXLHSInRHSPart;
6059 BinaryOperatorKind Op;
6060 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006061 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006062 /// if it is a prefix unary operation.
6063 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006064
6065public:
6066 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006067 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006068 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006069 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006070 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006071 /// expression. If DiagId and NoteId == 0, then only check is performed
6072 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006073 /// \param DiagId Diagnostic which should be emitted if error is found.
6074 /// \param NoteId Diagnostic note for the main error message.
6075 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006076 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006077 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006078 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006079 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006080 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006081 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006082 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6083 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6084 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006085 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006086 /// false otherwise.
6087 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6088
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006089 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006090 /// if it is a prefix unary operation.
6091 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6092
Alexey Bataev1d160b12015-03-13 12:27:31 +00006093private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006094 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6095 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006096};
6097} // namespace
6098
6099bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6100 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6101 ExprAnalysisErrorCode ErrorFound = NoError;
6102 SourceLocation ErrorLoc, NoteLoc;
6103 SourceRange ErrorRange, NoteRange;
6104 // Allowed constructs are:
6105 // x = x binop expr;
6106 // x = expr binop x;
6107 if (AtomicBinOp->getOpcode() == BO_Assign) {
6108 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006109 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006110 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6111 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6112 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6113 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006114 Op = AtomicInnerBinOp->getOpcode();
6115 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006116 Expr *LHS = AtomicInnerBinOp->getLHS();
6117 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006118 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6119 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6120 /*Canonical=*/true);
6121 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6122 /*Canonical=*/true);
6123 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6124 /*Canonical=*/true);
6125 if (XId == LHSId) {
6126 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006127 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006128 } else if (XId == RHSId) {
6129 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006130 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006131 } else {
6132 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6133 ErrorRange = AtomicInnerBinOp->getSourceRange();
6134 NoteLoc = X->getExprLoc();
6135 NoteRange = X->getSourceRange();
6136 ErrorFound = NotAnUpdateExpression;
6137 }
6138 } else {
6139 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6140 ErrorRange = AtomicInnerBinOp->getSourceRange();
6141 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6142 NoteRange = SourceRange(NoteLoc, NoteLoc);
6143 ErrorFound = NotABinaryOperator;
6144 }
6145 } else {
6146 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6147 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6148 ErrorFound = NotABinaryExpression;
6149 }
6150 } else {
6151 ErrorLoc = AtomicBinOp->getExprLoc();
6152 ErrorRange = AtomicBinOp->getSourceRange();
6153 NoteLoc = AtomicBinOp->getOperatorLoc();
6154 NoteRange = SourceRange(NoteLoc, NoteLoc);
6155 ErrorFound = NotAnAssignmentOp;
6156 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006157 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006158 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6159 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6160 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006161 }
6162 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006163 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006164 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006165}
6166
6167bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6168 unsigned NoteId) {
6169 ExprAnalysisErrorCode ErrorFound = NoError;
6170 SourceLocation ErrorLoc, NoteLoc;
6171 SourceRange ErrorRange, NoteRange;
6172 // Allowed constructs are:
6173 // x++;
6174 // x--;
6175 // ++x;
6176 // --x;
6177 // x binop= expr;
6178 // x = x binop expr;
6179 // x = expr binop x;
6180 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6181 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6182 if (AtomicBody->getType()->isScalarType() ||
6183 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006184 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006185 AtomicBody->IgnoreParenImpCasts())) {
6186 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006187 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006188 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006189 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006190 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006191 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006192 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006193 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6194 AtomicBody->IgnoreParenImpCasts())) {
6195 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006196 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006197 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006198 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006199 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006200 // Check for Unary Operation
6201 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006202 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006203 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6204 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006205 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006206 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6207 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006208 } else {
6209 ErrorFound = NotAnUnaryIncDecExpression;
6210 ErrorLoc = AtomicUnaryOp->getExprLoc();
6211 ErrorRange = AtomicUnaryOp->getSourceRange();
6212 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6213 NoteRange = SourceRange(NoteLoc, NoteLoc);
6214 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006215 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006216 ErrorFound = NotABinaryOrUnaryExpression;
6217 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6218 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6219 }
6220 } else {
6221 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006222 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006223 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6224 }
6225 } else {
6226 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006227 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006228 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6229 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006230 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006231 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6232 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6233 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006234 }
6235 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006236 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006237 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006238 // Build an update expression of form 'OpaqueValueExpr(x) binop
6239 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6240 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6241 auto *OVEX = new (SemaRef.getASTContext())
6242 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6243 auto *OVEExpr = new (SemaRef.getASTContext())
6244 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006245 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006246 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6247 IsXLHSInRHSPart ? OVEExpr : OVEX);
6248 if (Update.isInvalid())
6249 return true;
6250 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6251 Sema::AA_Casting);
6252 if (Update.isInvalid())
6253 return true;
6254 UpdateExpr = Update.get();
6255 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006256 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006257}
6258
Alexey Bataev0162e452014-07-22 10:10:35 +00006259StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6260 Stmt *AStmt,
6261 SourceLocation StartLoc,
6262 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006263 if (!AStmt)
6264 return StmtError();
6265
David Majnemer9d168222016-08-05 17:44:54 +00006266 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006267 // 1.2.2 OpenMP Language Terminology
6268 // Structured block - An executable statement with a single entry at the
6269 // top and a single exit at the bottom.
6270 // The point of exit cannot be a branch out of the structured block.
6271 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006272 OpenMPClauseKind AtomicKind = OMPC_unknown;
6273 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006274 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006275 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006276 C->getClauseKind() == OMPC_update ||
6277 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006278 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006279 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006280 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006281 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6282 << getOpenMPClauseName(AtomicKind);
6283 } else {
6284 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006285 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006286 }
6287 }
6288 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006289
Alexey Bataeve3727102018-04-18 15:57:46 +00006290 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006291 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6292 Body = EWC->getSubExpr();
6293
Alexey Bataev62cec442014-11-18 10:14:22 +00006294 Expr *X = nullptr;
6295 Expr *V = nullptr;
6296 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006297 Expr *UE = nullptr;
6298 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006299 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006300 // OpenMP [2.12.6, atomic Construct]
6301 // In the next expressions:
6302 // * x and v (as applicable) are both l-value expressions with scalar type.
6303 // * During the execution of an atomic region, multiple syntactic
6304 // occurrences of x must designate the same storage location.
6305 // * Neither of v and expr (as applicable) may access the storage location
6306 // designated by x.
6307 // * Neither of x and expr (as applicable) may access the storage location
6308 // designated by v.
6309 // * expr is an expression with scalar type.
6310 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6311 // * binop, binop=, ++, and -- are not overloaded operators.
6312 // * The expression x binop expr must be numerically equivalent to x binop
6313 // (expr). This requirement is satisfied if the operators in expr have
6314 // precedence greater than binop, or by using parentheses around expr or
6315 // subexpressions of expr.
6316 // * The expression expr binop x must be numerically equivalent to (expr)
6317 // binop x. This requirement is satisfied if the operators in expr have
6318 // precedence equal to or greater than binop, or by using parentheses around
6319 // expr or subexpressions of expr.
6320 // * For forms that allow multiple occurrences of x, the number of times
6321 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006322 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006323 enum {
6324 NotAnExpression,
6325 NotAnAssignmentOp,
6326 NotAScalarType,
6327 NotAnLValue,
6328 NoError
6329 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006330 SourceLocation ErrorLoc, NoteLoc;
6331 SourceRange ErrorRange, NoteRange;
6332 // If clause is read:
6333 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006334 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6335 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006336 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6337 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6338 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6339 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6340 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6341 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6342 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006343 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006344 ErrorFound = NotAnLValue;
6345 ErrorLoc = AtomicBinOp->getExprLoc();
6346 ErrorRange = AtomicBinOp->getSourceRange();
6347 NoteLoc = NotLValueExpr->getExprLoc();
6348 NoteRange = NotLValueExpr->getSourceRange();
6349 }
6350 } else if (!X->isInstantiationDependent() ||
6351 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006352 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006353 (X->isInstantiationDependent() || X->getType()->isScalarType())
6354 ? V
6355 : X;
6356 ErrorFound = NotAScalarType;
6357 ErrorLoc = AtomicBinOp->getExprLoc();
6358 ErrorRange = AtomicBinOp->getSourceRange();
6359 NoteLoc = NotScalarExpr->getExprLoc();
6360 NoteRange = NotScalarExpr->getSourceRange();
6361 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006362 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006363 ErrorFound = NotAnAssignmentOp;
6364 ErrorLoc = AtomicBody->getExprLoc();
6365 ErrorRange = AtomicBody->getSourceRange();
6366 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6367 : AtomicBody->getExprLoc();
6368 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6369 : AtomicBody->getSourceRange();
6370 }
6371 } else {
6372 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006373 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006374 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006375 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006376 if (ErrorFound != NoError) {
6377 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6378 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006379 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6380 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006381 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006382 }
6383 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006384 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006385 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006386 enum {
6387 NotAnExpression,
6388 NotAnAssignmentOp,
6389 NotAScalarType,
6390 NotAnLValue,
6391 NoError
6392 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006393 SourceLocation ErrorLoc, NoteLoc;
6394 SourceRange ErrorRange, NoteRange;
6395 // If clause is write:
6396 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006397 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6398 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006399 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6400 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006401 X = AtomicBinOp->getLHS();
6402 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006403 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6404 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6405 if (!X->isLValue()) {
6406 ErrorFound = NotAnLValue;
6407 ErrorLoc = AtomicBinOp->getExprLoc();
6408 ErrorRange = AtomicBinOp->getSourceRange();
6409 NoteLoc = X->getExprLoc();
6410 NoteRange = X->getSourceRange();
6411 }
6412 } else if (!X->isInstantiationDependent() ||
6413 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006414 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006415 (X->isInstantiationDependent() || X->getType()->isScalarType())
6416 ? E
6417 : X;
6418 ErrorFound = NotAScalarType;
6419 ErrorLoc = AtomicBinOp->getExprLoc();
6420 ErrorRange = AtomicBinOp->getSourceRange();
6421 NoteLoc = NotScalarExpr->getExprLoc();
6422 NoteRange = NotScalarExpr->getSourceRange();
6423 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006424 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006425 ErrorFound = NotAnAssignmentOp;
6426 ErrorLoc = AtomicBody->getExprLoc();
6427 ErrorRange = AtomicBody->getSourceRange();
6428 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6429 : AtomicBody->getExprLoc();
6430 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6431 : AtomicBody->getSourceRange();
6432 }
6433 } else {
6434 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006435 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006436 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006437 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006438 if (ErrorFound != NoError) {
6439 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6440 << ErrorRange;
6441 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6442 << NoteRange;
6443 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006444 }
6445 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006446 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006447 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006448 // If clause is update:
6449 // x++;
6450 // x--;
6451 // ++x;
6452 // --x;
6453 // x binop= expr;
6454 // x = x binop expr;
6455 // x = expr binop x;
6456 OpenMPAtomicUpdateChecker Checker(*this);
6457 if (Checker.checkStatement(
6458 Body, (AtomicKind == OMPC_update)
6459 ? diag::err_omp_atomic_update_not_expression_statement
6460 : diag::err_omp_atomic_not_expression_statement,
6461 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006462 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006463 if (!CurContext->isDependentContext()) {
6464 E = Checker.getExpr();
6465 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006466 UE = Checker.getUpdateExpr();
6467 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006468 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006469 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006470 enum {
6471 NotAnAssignmentOp,
6472 NotACompoundStatement,
6473 NotTwoSubstatements,
6474 NotASpecificExpression,
6475 NoError
6476 } ErrorFound = NoError;
6477 SourceLocation ErrorLoc, NoteLoc;
6478 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006479 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006480 // If clause is a capture:
6481 // v = x++;
6482 // v = x--;
6483 // v = ++x;
6484 // v = --x;
6485 // v = x binop= expr;
6486 // v = x = x binop expr;
6487 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006488 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006489 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6490 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6491 V = AtomicBinOp->getLHS();
6492 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6493 OpenMPAtomicUpdateChecker Checker(*this);
6494 if (Checker.checkStatement(
6495 Body, diag::err_omp_atomic_capture_not_expression_statement,
6496 diag::note_omp_atomic_update))
6497 return StmtError();
6498 E = Checker.getExpr();
6499 X = Checker.getX();
6500 UE = Checker.getUpdateExpr();
6501 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6502 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006503 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006504 ErrorLoc = AtomicBody->getExprLoc();
6505 ErrorRange = AtomicBody->getSourceRange();
6506 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6507 : AtomicBody->getExprLoc();
6508 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6509 : AtomicBody->getSourceRange();
6510 ErrorFound = NotAnAssignmentOp;
6511 }
6512 if (ErrorFound != NoError) {
6513 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6514 << ErrorRange;
6515 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6516 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006517 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006518 if (CurContext->isDependentContext())
6519 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006520 } else {
6521 // If clause is a capture:
6522 // { v = x; x = expr; }
6523 // { v = x; x++; }
6524 // { v = x; x--; }
6525 // { v = x; ++x; }
6526 // { v = x; --x; }
6527 // { v = x; x binop= expr; }
6528 // { v = x; x = x binop expr; }
6529 // { v = x; x = expr binop x; }
6530 // { x++; v = x; }
6531 // { x--; v = x; }
6532 // { ++x; v = x; }
6533 // { --x; v = x; }
6534 // { x binop= expr; v = x; }
6535 // { x = x binop expr; v = x; }
6536 // { x = expr binop x; v = x; }
6537 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6538 // Check that this is { expr1; expr2; }
6539 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006540 Stmt *First = CS->body_front();
6541 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006542 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6543 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6544 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6545 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6546 // Need to find what subexpression is 'v' and what is 'x'.
6547 OpenMPAtomicUpdateChecker Checker(*this);
6548 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6549 BinaryOperator *BinOp = nullptr;
6550 if (IsUpdateExprFound) {
6551 BinOp = dyn_cast<BinaryOperator>(First);
6552 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6553 }
6554 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6555 // { v = x; x++; }
6556 // { v = x; x--; }
6557 // { v = x; ++x; }
6558 // { v = x; --x; }
6559 // { v = x; x binop= expr; }
6560 // { v = x; x = x binop expr; }
6561 // { v = x; x = expr binop x; }
6562 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006563 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006564 llvm::FoldingSetNodeID XId, PossibleXId;
6565 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6566 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6567 IsUpdateExprFound = XId == PossibleXId;
6568 if (IsUpdateExprFound) {
6569 V = BinOp->getLHS();
6570 X = Checker.getX();
6571 E = Checker.getExpr();
6572 UE = Checker.getUpdateExpr();
6573 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006574 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006575 }
6576 }
6577 if (!IsUpdateExprFound) {
6578 IsUpdateExprFound = !Checker.checkStatement(First);
6579 BinOp = nullptr;
6580 if (IsUpdateExprFound) {
6581 BinOp = dyn_cast<BinaryOperator>(Second);
6582 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6583 }
6584 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6585 // { x++; v = x; }
6586 // { x--; v = x; }
6587 // { ++x; v = x; }
6588 // { --x; v = x; }
6589 // { x binop= expr; v = x; }
6590 // { x = x binop expr; v = x; }
6591 // { x = expr binop x; v = x; }
6592 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006593 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006594 llvm::FoldingSetNodeID XId, PossibleXId;
6595 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6596 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6597 IsUpdateExprFound = XId == PossibleXId;
6598 if (IsUpdateExprFound) {
6599 V = BinOp->getLHS();
6600 X = Checker.getX();
6601 E = Checker.getExpr();
6602 UE = Checker.getUpdateExpr();
6603 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006604 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006605 }
6606 }
6607 }
6608 if (!IsUpdateExprFound) {
6609 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006610 auto *FirstExpr = dyn_cast<Expr>(First);
6611 auto *SecondExpr = dyn_cast<Expr>(Second);
6612 if (!FirstExpr || !SecondExpr ||
6613 !(FirstExpr->isInstantiationDependent() ||
6614 SecondExpr->isInstantiationDependent())) {
6615 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6616 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006617 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006618 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006619 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006620 NoteRange = ErrorRange = FirstBinOp
6621 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006622 : SourceRange(ErrorLoc, ErrorLoc);
6623 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006624 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6625 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6626 ErrorFound = NotAnAssignmentOp;
6627 NoteLoc = ErrorLoc = SecondBinOp
6628 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006629 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006630 NoteRange = ErrorRange =
6631 SecondBinOp ? SecondBinOp->getSourceRange()
6632 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006633 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006634 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006635 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006636 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006637 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6638 llvm::FoldingSetNodeID X1Id, X2Id;
6639 PossibleXRHSInFirst->Profile(X1Id, Context,
6640 /*Canonical=*/true);
6641 PossibleXLHSInSecond->Profile(X2Id, Context,
6642 /*Canonical=*/true);
6643 IsUpdateExprFound = X1Id == X2Id;
6644 if (IsUpdateExprFound) {
6645 V = FirstBinOp->getLHS();
6646 X = SecondBinOp->getLHS();
6647 E = SecondBinOp->getRHS();
6648 UE = nullptr;
6649 IsXLHSInRHSPart = false;
6650 IsPostfixUpdate = true;
6651 } else {
6652 ErrorFound = NotASpecificExpression;
6653 ErrorLoc = FirstBinOp->getExprLoc();
6654 ErrorRange = FirstBinOp->getSourceRange();
6655 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6656 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6657 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006658 }
6659 }
6660 }
6661 }
6662 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006663 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006664 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006665 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006666 ErrorFound = NotTwoSubstatements;
6667 }
6668 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006669 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006670 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006671 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006672 ErrorFound = NotACompoundStatement;
6673 }
6674 if (ErrorFound != NoError) {
6675 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6676 << ErrorRange;
6677 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6678 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006679 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006680 if (CurContext->isDependentContext())
6681 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006682 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006683 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006684
Reid Kleckner87a31802018-03-12 21:43:02 +00006685 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006686
Alexey Bataev62cec442014-11-18 10:14:22 +00006687 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006688 X, V, E, UE, IsXLHSInRHSPart,
6689 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006690}
6691
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006692StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6693 Stmt *AStmt,
6694 SourceLocation StartLoc,
6695 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006696 if (!AStmt)
6697 return StmtError();
6698
Alexey Bataeve3727102018-04-18 15:57:46 +00006699 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006700 // 1.2.2 OpenMP Language Terminology
6701 // Structured block - An executable statement with a single entry at the
6702 // top and a single exit at the bottom.
6703 // The point of exit cannot be a branch out of the structured block.
6704 // longjmp() and throw() must not violate the entry/exit criteria.
6705 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006706 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6707 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6708 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6709 // 1.2.2 OpenMP Language Terminology
6710 // Structured block - An executable statement with a single entry at the
6711 // top and a single exit at the bottom.
6712 // The point of exit cannot be a branch out of the structured block.
6713 // longjmp() and throw() must not violate the entry/exit criteria.
6714 CS->getCapturedDecl()->setNothrow();
6715 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006716
Alexey Bataev13314bf2014-10-09 04:18:56 +00006717 // OpenMP [2.16, Nesting of Regions]
6718 // If specified, a teams construct must be contained within a target
6719 // construct. That target construct must contain no statements or directives
6720 // outside of the teams construct.
6721 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006722 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006723 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006724 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006725 auto I = CS->body_begin();
6726 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006727 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006728 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6729 OMPTeamsFound = false;
6730 break;
6731 }
6732 ++I;
6733 }
6734 assert(I != CS->body_end() && "Not found statement");
6735 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006736 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006737 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006738 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006739 }
6740 if (!OMPTeamsFound) {
6741 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6742 Diag(DSAStack->getInnerTeamsRegionLoc(),
6743 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006744 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006745 << isa<OMPExecutableDirective>(S);
6746 return StmtError();
6747 }
6748 }
6749
Reid Kleckner87a31802018-03-12 21:43:02 +00006750 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006751
6752 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6753}
6754
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006755StmtResult
6756Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6757 Stmt *AStmt, SourceLocation StartLoc,
6758 SourceLocation EndLoc) {
6759 if (!AStmt)
6760 return StmtError();
6761
Alexey Bataeve3727102018-04-18 15:57:46 +00006762 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006763 // 1.2.2 OpenMP Language Terminology
6764 // Structured block - An executable statement with a single entry at the
6765 // top and a single exit at the bottom.
6766 // The point of exit cannot be a branch out of the structured block.
6767 // longjmp() and throw() must not violate the entry/exit criteria.
6768 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006769 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6770 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6771 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6772 // 1.2.2 OpenMP Language Terminology
6773 // Structured block - An executable statement with a single entry at the
6774 // top and a single exit at the bottom.
6775 // The point of exit cannot be a branch out of the structured block.
6776 // longjmp() and throw() must not violate the entry/exit criteria.
6777 CS->getCapturedDecl()->setNothrow();
6778 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006779
Reid Kleckner87a31802018-03-12 21:43:02 +00006780 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006781
6782 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6783 AStmt);
6784}
6785
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006786StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6787 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006788 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006789 if (!AStmt)
6790 return StmtError();
6791
Alexey Bataeve3727102018-04-18 15:57:46 +00006792 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006793 // 1.2.2 OpenMP Language Terminology
6794 // Structured block - An executable statement with a single entry at the
6795 // top and a single exit at the bottom.
6796 // The point of exit cannot be a branch out of the structured block.
6797 // longjmp() and throw() must not violate the entry/exit criteria.
6798 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006799 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6800 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6801 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6802 // 1.2.2 OpenMP Language Terminology
6803 // Structured block - An executable statement with a single entry at the
6804 // top and a single exit at the bottom.
6805 // The point of exit cannot be a branch out of the structured block.
6806 // longjmp() and throw() must not violate the entry/exit criteria.
6807 CS->getCapturedDecl()->setNothrow();
6808 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006809
6810 OMPLoopDirective::HelperExprs B;
6811 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6812 // define the nested loops number.
6813 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006814 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006815 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006816 VarsWithImplicitDSA, B);
6817 if (NestedLoopCount == 0)
6818 return StmtError();
6819
6820 assert((CurContext->isDependentContext() || B.builtAll()) &&
6821 "omp target parallel for loop exprs were not built");
6822
6823 if (!CurContext->isDependentContext()) {
6824 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006825 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006826 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006827 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006828 B.NumIterations, *this, CurScope,
6829 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006830 return StmtError();
6831 }
6832 }
6833
Reid Kleckner87a31802018-03-12 21:43:02 +00006834 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006835 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6836 NestedLoopCount, Clauses, AStmt,
6837 B, DSAStack->isCancelRegion());
6838}
6839
Alexey Bataev95b64a92017-05-30 16:00:04 +00006840/// Check for existence of a map clause in the list of clauses.
6841static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6842 const OpenMPClauseKind K) {
6843 return llvm::any_of(
6844 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6845}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006846
Alexey Bataev95b64a92017-05-30 16:00:04 +00006847template <typename... Params>
6848static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6849 const Params... ClauseTypes) {
6850 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006851}
6852
Michael Wong65f367f2015-07-21 13:44:28 +00006853StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6854 Stmt *AStmt,
6855 SourceLocation StartLoc,
6856 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006857 if (!AStmt)
6858 return StmtError();
6859
6860 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6861
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006862 // OpenMP [2.10.1, Restrictions, p. 97]
6863 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006864 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6865 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6866 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006867 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006868 return StmtError();
6869 }
6870
Reid Kleckner87a31802018-03-12 21:43:02 +00006871 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00006872
6873 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6874 AStmt);
6875}
6876
Samuel Antaodf67fc42016-01-19 19:15:56 +00006877StmtResult
6878Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6879 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006880 SourceLocation EndLoc, Stmt *AStmt) {
6881 if (!AStmt)
6882 return StmtError();
6883
Alexey Bataeve3727102018-04-18 15:57:46 +00006884 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006885 // 1.2.2 OpenMP Language Terminology
6886 // Structured block - An executable statement with a single entry at the
6887 // top and a single exit at the bottom.
6888 // The point of exit cannot be a branch out of the structured block.
6889 // longjmp() and throw() must not violate the entry/exit criteria.
6890 CS->getCapturedDecl()->setNothrow();
6891 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6892 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6893 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6894 // 1.2.2 OpenMP Language Terminology
6895 // Structured block - An executable statement with a single entry at the
6896 // top and a single exit at the bottom.
6897 // The point of exit cannot be a branch out of the structured block.
6898 // longjmp() and throw() must not violate the entry/exit criteria.
6899 CS->getCapturedDecl()->setNothrow();
6900 }
6901
Samuel Antaodf67fc42016-01-19 19:15:56 +00006902 // OpenMP [2.10.2, Restrictions, p. 99]
6903 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006904 if (!hasClauses(Clauses, OMPC_map)) {
6905 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6906 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006907 return StmtError();
6908 }
6909
Alexey Bataev7828b252017-11-21 17:08:48 +00006910 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6911 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006912}
6913
Samuel Antao72590762016-01-19 20:04:50 +00006914StmtResult
6915Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6916 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006917 SourceLocation EndLoc, Stmt *AStmt) {
6918 if (!AStmt)
6919 return StmtError();
6920
Alexey Bataeve3727102018-04-18 15:57:46 +00006921 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006922 // 1.2.2 OpenMP Language Terminology
6923 // Structured block - An executable statement with a single entry at the
6924 // top and a single exit at the bottom.
6925 // The point of exit cannot be a branch out of the structured block.
6926 // longjmp() and throw() must not violate the entry/exit criteria.
6927 CS->getCapturedDecl()->setNothrow();
6928 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6929 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6930 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6931 // 1.2.2 OpenMP Language Terminology
6932 // Structured block - An executable statement with a single entry at the
6933 // top and a single exit at the bottom.
6934 // The point of exit cannot be a branch out of the structured block.
6935 // longjmp() and throw() must not violate the entry/exit criteria.
6936 CS->getCapturedDecl()->setNothrow();
6937 }
6938
Samuel Antao72590762016-01-19 20:04:50 +00006939 // OpenMP [2.10.3, Restrictions, p. 102]
6940 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006941 if (!hasClauses(Clauses, OMPC_map)) {
6942 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6943 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006944 return StmtError();
6945 }
6946
Alexey Bataev7828b252017-11-21 17:08:48 +00006947 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6948 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006949}
6950
Samuel Antao686c70c2016-05-26 17:30:50 +00006951StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6952 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006953 SourceLocation EndLoc,
6954 Stmt *AStmt) {
6955 if (!AStmt)
6956 return StmtError();
6957
Alexey Bataeve3727102018-04-18 15:57:46 +00006958 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006959 // 1.2.2 OpenMP Language Terminology
6960 // Structured block - An executable statement with a single entry at the
6961 // top and a single exit at the bottom.
6962 // The point of exit cannot be a branch out of the structured block.
6963 // longjmp() and throw() must not violate the entry/exit criteria.
6964 CS->getCapturedDecl()->setNothrow();
6965 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6966 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6967 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6968 // 1.2.2 OpenMP Language Terminology
6969 // Structured block - An executable statement with a single entry at the
6970 // top and a single exit at the bottom.
6971 // The point of exit cannot be a branch out of the structured block.
6972 // longjmp() and throw() must not violate the entry/exit criteria.
6973 CS->getCapturedDecl()->setNothrow();
6974 }
6975
Alexey Bataev95b64a92017-05-30 16:00:04 +00006976 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006977 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6978 return StmtError();
6979 }
Alexey Bataev7828b252017-11-21 17:08:48 +00006980 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6981 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00006982}
6983
Alexey Bataev13314bf2014-10-09 04:18:56 +00006984StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6985 Stmt *AStmt, SourceLocation StartLoc,
6986 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006987 if (!AStmt)
6988 return StmtError();
6989
Alexey Bataeve3727102018-04-18 15:57:46 +00006990 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006991 // 1.2.2 OpenMP Language Terminology
6992 // Structured block - An executable statement with a single entry at the
6993 // top and a single exit at the bottom.
6994 // The point of exit cannot be a branch out of the structured block.
6995 // longjmp() and throw() must not violate the entry/exit criteria.
6996 CS->getCapturedDecl()->setNothrow();
6997
Reid Kleckner87a31802018-03-12 21:43:02 +00006998 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00006999
Alexey Bataevceabd412017-11-30 18:01:54 +00007000 DSAStack->setParentTeamsRegionLoc(StartLoc);
7001
Alexey Bataev13314bf2014-10-09 04:18:56 +00007002 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7003}
7004
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007005StmtResult
7006Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7007 SourceLocation EndLoc,
7008 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007009 if (DSAStack->isParentNowaitRegion()) {
7010 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7011 return StmtError();
7012 }
7013 if (DSAStack->isParentOrderedRegion()) {
7014 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7015 return StmtError();
7016 }
7017 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7018 CancelRegion);
7019}
7020
Alexey Bataev87933c72015-09-18 08:07:34 +00007021StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7022 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007023 SourceLocation EndLoc,
7024 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007025 if (DSAStack->isParentNowaitRegion()) {
7026 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7027 return StmtError();
7028 }
7029 if (DSAStack->isParentOrderedRegion()) {
7030 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7031 return StmtError();
7032 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007033 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007034 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7035 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007036}
7037
Alexey Bataev382967a2015-12-08 12:06:20 +00007038static bool checkGrainsizeNumTasksClauses(Sema &S,
7039 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007040 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007041 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007042 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007043 if (C->getClauseKind() == OMPC_grainsize ||
7044 C->getClauseKind() == OMPC_num_tasks) {
7045 if (!PrevClause)
7046 PrevClause = C;
7047 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007048 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007049 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7050 << getOpenMPClauseName(C->getClauseKind())
7051 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007052 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007053 diag::note_omp_previous_grainsize_num_tasks)
7054 << getOpenMPClauseName(PrevClause->getClauseKind());
7055 ErrorFound = true;
7056 }
7057 }
7058 }
7059 return ErrorFound;
7060}
7061
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007062static bool checkReductionClauseWithNogroup(Sema &S,
7063 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007064 const OMPClause *ReductionClause = nullptr;
7065 const OMPClause *NogroupClause = nullptr;
7066 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007067 if (C->getClauseKind() == OMPC_reduction) {
7068 ReductionClause = C;
7069 if (NogroupClause)
7070 break;
7071 continue;
7072 }
7073 if (C->getClauseKind() == OMPC_nogroup) {
7074 NogroupClause = C;
7075 if (ReductionClause)
7076 break;
7077 continue;
7078 }
7079 }
7080 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007081 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7082 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007083 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007084 return true;
7085 }
7086 return false;
7087}
7088
Alexey Bataev49f6e782015-12-01 04:18:41 +00007089StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7090 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007091 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007092 if (!AStmt)
7093 return StmtError();
7094
7095 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7096 OMPLoopDirective::HelperExprs B;
7097 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7098 // define the nested loops number.
7099 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007100 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007101 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007102 VarsWithImplicitDSA, B);
7103 if (NestedLoopCount == 0)
7104 return StmtError();
7105
7106 assert((CurContext->isDependentContext() || B.builtAll()) &&
7107 "omp for loop exprs were not built");
7108
Alexey Bataev382967a2015-12-08 12:06:20 +00007109 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7110 // The grainsize clause and num_tasks clause are mutually exclusive and may
7111 // not appear on the same taskloop directive.
7112 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7113 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007114 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7115 // If a reduction clause is present on the taskloop directive, the nogroup
7116 // clause must not be specified.
7117 if (checkReductionClauseWithNogroup(*this, Clauses))
7118 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007119
Reid Kleckner87a31802018-03-12 21:43:02 +00007120 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007121 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7122 NestedLoopCount, Clauses, AStmt, B);
7123}
7124
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007125StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7126 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007127 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007128 if (!AStmt)
7129 return StmtError();
7130
7131 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7132 OMPLoopDirective::HelperExprs B;
7133 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7134 // define the nested loops number.
7135 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007136 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007137 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7138 VarsWithImplicitDSA, B);
7139 if (NestedLoopCount == 0)
7140 return StmtError();
7141
7142 assert((CurContext->isDependentContext() || B.builtAll()) &&
7143 "omp for loop exprs were not built");
7144
Alexey Bataev5a3af132016-03-29 08:58:54 +00007145 if (!CurContext->isDependentContext()) {
7146 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007147 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007148 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007149 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007150 B.NumIterations, *this, CurScope,
7151 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007152 return StmtError();
7153 }
7154 }
7155
Alexey Bataev382967a2015-12-08 12:06:20 +00007156 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7157 // The grainsize clause and num_tasks clause are mutually exclusive and may
7158 // not appear on the same taskloop directive.
7159 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7160 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007161 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7162 // If a reduction clause is present on the taskloop directive, the nogroup
7163 // clause must not be specified.
7164 if (checkReductionClauseWithNogroup(*this, Clauses))
7165 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007166 if (checkSimdlenSafelenSpecified(*this, Clauses))
7167 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007168
Reid Kleckner87a31802018-03-12 21:43:02 +00007169 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007170 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7171 NestedLoopCount, Clauses, AStmt, B);
7172}
7173
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007174StmtResult Sema::ActOnOpenMPDistributeDirective(
7175 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007176 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007177 if (!AStmt)
7178 return StmtError();
7179
7180 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7181 OMPLoopDirective::HelperExprs B;
7182 // In presence of clause 'collapse' with number of loops, it will
7183 // define the nested loops number.
7184 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007185 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007186 nullptr /*ordered not a clause on distribute*/, AStmt,
7187 *this, *DSAStack, VarsWithImplicitDSA, B);
7188 if (NestedLoopCount == 0)
7189 return StmtError();
7190
7191 assert((CurContext->isDependentContext() || B.builtAll()) &&
7192 "omp for loop exprs were not built");
7193
Reid Kleckner87a31802018-03-12 21:43:02 +00007194 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007195 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7196 NestedLoopCount, Clauses, AStmt, B);
7197}
7198
Carlo Bertolli9925f152016-06-27 14:55:37 +00007199StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7200 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007201 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007202 if (!AStmt)
7203 return StmtError();
7204
Alexey Bataeve3727102018-04-18 15:57:46 +00007205 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007206 // 1.2.2 OpenMP Language Terminology
7207 // Structured block - An executable statement with a single entry at the
7208 // top and a single exit at the bottom.
7209 // The point of exit cannot be a branch out of the structured block.
7210 // longjmp() and throw() must not violate the entry/exit criteria.
7211 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007212 for (int ThisCaptureLevel =
7213 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7214 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7215 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7216 // 1.2.2 OpenMP Language Terminology
7217 // Structured block - An executable statement with a single entry at the
7218 // top and a single exit at the bottom.
7219 // The point of exit cannot be a branch out of the structured block.
7220 // longjmp() and throw() must not violate the entry/exit criteria.
7221 CS->getCapturedDecl()->setNothrow();
7222 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007223
7224 OMPLoopDirective::HelperExprs B;
7225 // In presence of clause 'collapse' with number of loops, it will
7226 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007227 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007228 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007229 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007230 VarsWithImplicitDSA, B);
7231 if (NestedLoopCount == 0)
7232 return StmtError();
7233
7234 assert((CurContext->isDependentContext() || B.builtAll()) &&
7235 "omp for loop exprs were not built");
7236
Reid Kleckner87a31802018-03-12 21:43:02 +00007237 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007238 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007239 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7240 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007241}
7242
Kelvin Li4a39add2016-07-05 05:00:15 +00007243StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7244 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007245 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007246 if (!AStmt)
7247 return StmtError();
7248
Alexey Bataeve3727102018-04-18 15:57:46 +00007249 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007250 // 1.2.2 OpenMP Language Terminology
7251 // Structured block - An executable statement with a single entry at the
7252 // top and a single exit at the bottom.
7253 // The point of exit cannot be a branch out of the structured block.
7254 // longjmp() and throw() must not violate the entry/exit criteria.
7255 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007256 for (int ThisCaptureLevel =
7257 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7258 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7259 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7260 // 1.2.2 OpenMP Language Terminology
7261 // Structured block - An executable statement with a single entry at the
7262 // top and a single exit at the bottom.
7263 // The point of exit cannot be a branch out of the structured block.
7264 // longjmp() and throw() must not violate the entry/exit criteria.
7265 CS->getCapturedDecl()->setNothrow();
7266 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007267
7268 OMPLoopDirective::HelperExprs B;
7269 // In presence of clause 'collapse' with number of loops, it will
7270 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007271 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007272 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007273 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007274 VarsWithImplicitDSA, B);
7275 if (NestedLoopCount == 0)
7276 return StmtError();
7277
7278 assert((CurContext->isDependentContext() || B.builtAll()) &&
7279 "omp for loop exprs were not built");
7280
Alexey Bataev438388c2017-11-22 18:34:02 +00007281 if (!CurContext->isDependentContext()) {
7282 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007283 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007284 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7285 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7286 B.NumIterations, *this, CurScope,
7287 DSAStack))
7288 return StmtError();
7289 }
7290 }
7291
Kelvin Lic5609492016-07-15 04:39:07 +00007292 if (checkSimdlenSafelenSpecified(*this, Clauses))
7293 return StmtError();
7294
Reid Kleckner87a31802018-03-12 21:43:02 +00007295 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007296 return OMPDistributeParallelForSimdDirective::Create(
7297 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7298}
7299
Kelvin Li787f3fc2016-07-06 04:45:38 +00007300StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7301 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007302 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007303 if (!AStmt)
7304 return StmtError();
7305
Alexey Bataeve3727102018-04-18 15:57:46 +00007306 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007307 // 1.2.2 OpenMP Language Terminology
7308 // Structured block - An executable statement with a single entry at the
7309 // top and a single exit at the bottom.
7310 // The point of exit cannot be a branch out of the structured block.
7311 // longjmp() and throw() must not violate the entry/exit criteria.
7312 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007313 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7314 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7315 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7316 // 1.2.2 OpenMP Language Terminology
7317 // Structured block - An executable statement with a single entry at the
7318 // top and a single exit at the bottom.
7319 // The point of exit cannot be a branch out of the structured block.
7320 // longjmp() and throw() must not violate the entry/exit criteria.
7321 CS->getCapturedDecl()->setNothrow();
7322 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007323
7324 OMPLoopDirective::HelperExprs B;
7325 // In presence of clause 'collapse' with number of loops, it will
7326 // define the nested loops number.
7327 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007328 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007329 nullptr /*ordered not a clause on distribute*/, CS, *this,
7330 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007331 if (NestedLoopCount == 0)
7332 return StmtError();
7333
7334 assert((CurContext->isDependentContext() || B.builtAll()) &&
7335 "omp for loop exprs were not built");
7336
Alexey Bataev438388c2017-11-22 18:34:02 +00007337 if (!CurContext->isDependentContext()) {
7338 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007339 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007340 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7341 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7342 B.NumIterations, *this, CurScope,
7343 DSAStack))
7344 return StmtError();
7345 }
7346 }
7347
Kelvin Lic5609492016-07-15 04:39:07 +00007348 if (checkSimdlenSafelenSpecified(*this, Clauses))
7349 return StmtError();
7350
Reid Kleckner87a31802018-03-12 21:43:02 +00007351 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007352 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7353 NestedLoopCount, Clauses, AStmt, B);
7354}
7355
Kelvin Lia579b912016-07-14 02:54:56 +00007356StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7357 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007358 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007359 if (!AStmt)
7360 return StmtError();
7361
Alexey Bataeve3727102018-04-18 15:57:46 +00007362 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007363 // 1.2.2 OpenMP Language Terminology
7364 // Structured block - An executable statement with a single entry at the
7365 // top and a single exit at the bottom.
7366 // The point of exit cannot be a branch out of the structured block.
7367 // longjmp() and throw() must not violate the entry/exit criteria.
7368 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007369 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7370 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7371 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7372 // 1.2.2 OpenMP Language Terminology
7373 // Structured block - An executable statement with a single entry at the
7374 // top and a single exit at the bottom.
7375 // The point of exit cannot be a branch out of the structured block.
7376 // longjmp() and throw() must not violate the entry/exit criteria.
7377 CS->getCapturedDecl()->setNothrow();
7378 }
Kelvin Lia579b912016-07-14 02:54:56 +00007379
7380 OMPLoopDirective::HelperExprs B;
7381 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7382 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007383 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007384 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007385 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007386 VarsWithImplicitDSA, B);
7387 if (NestedLoopCount == 0)
7388 return StmtError();
7389
7390 assert((CurContext->isDependentContext() || B.builtAll()) &&
7391 "omp target parallel for simd loop exprs were not built");
7392
7393 if (!CurContext->isDependentContext()) {
7394 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007395 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007396 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007397 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7398 B.NumIterations, *this, CurScope,
7399 DSAStack))
7400 return StmtError();
7401 }
7402 }
Kelvin Lic5609492016-07-15 04:39:07 +00007403 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007404 return StmtError();
7405
Reid Kleckner87a31802018-03-12 21:43:02 +00007406 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007407 return OMPTargetParallelForSimdDirective::Create(
7408 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7409}
7410
Kelvin Li986330c2016-07-20 22:57:10 +00007411StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7412 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007413 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007414 if (!AStmt)
7415 return StmtError();
7416
Alexey Bataeve3727102018-04-18 15:57:46 +00007417 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007418 // 1.2.2 OpenMP Language Terminology
7419 // Structured block - An executable statement with a single entry at the
7420 // top and a single exit at the bottom.
7421 // The point of exit cannot be a branch out of the structured block.
7422 // longjmp() and throw() must not violate the entry/exit criteria.
7423 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007424 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7425 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7426 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7427 // 1.2.2 OpenMP Language Terminology
7428 // Structured block - An executable statement with a single entry at the
7429 // top and a single exit at the bottom.
7430 // The point of exit cannot be a branch out of the structured block.
7431 // longjmp() and throw() must not violate the entry/exit criteria.
7432 CS->getCapturedDecl()->setNothrow();
7433 }
7434
Kelvin Li986330c2016-07-20 22:57:10 +00007435 OMPLoopDirective::HelperExprs B;
7436 // In presence of clause 'collapse' with number of loops, it will define the
7437 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007438 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007439 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007440 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007441 VarsWithImplicitDSA, B);
7442 if (NestedLoopCount == 0)
7443 return StmtError();
7444
7445 assert((CurContext->isDependentContext() || B.builtAll()) &&
7446 "omp target simd loop exprs were not built");
7447
7448 if (!CurContext->isDependentContext()) {
7449 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007450 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007451 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007452 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7453 B.NumIterations, *this, CurScope,
7454 DSAStack))
7455 return StmtError();
7456 }
7457 }
7458
7459 if (checkSimdlenSafelenSpecified(*this, Clauses))
7460 return StmtError();
7461
Reid Kleckner87a31802018-03-12 21:43:02 +00007462 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007463 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7464 NestedLoopCount, Clauses, AStmt, B);
7465}
7466
Kelvin Li02532872016-08-05 14:37:37 +00007467StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7468 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007469 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007470 if (!AStmt)
7471 return StmtError();
7472
Alexey Bataeve3727102018-04-18 15:57:46 +00007473 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007474 // 1.2.2 OpenMP Language Terminology
7475 // Structured block - An executable statement with a single entry at the
7476 // top and a single exit at the bottom.
7477 // The point of exit cannot be a branch out of the structured block.
7478 // longjmp() and throw() must not violate the entry/exit criteria.
7479 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007480 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7481 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7482 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7483 // 1.2.2 OpenMP Language Terminology
7484 // Structured block - An executable statement with a single entry at the
7485 // top and a single exit at the bottom.
7486 // The point of exit cannot be a branch out of the structured block.
7487 // longjmp() and throw() must not violate the entry/exit criteria.
7488 CS->getCapturedDecl()->setNothrow();
7489 }
Kelvin Li02532872016-08-05 14:37:37 +00007490
7491 OMPLoopDirective::HelperExprs B;
7492 // In presence of clause 'collapse' with number of loops, it will
7493 // define the nested loops number.
7494 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007495 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007496 nullptr /*ordered not a clause on distribute*/, CS, *this,
7497 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007498 if (NestedLoopCount == 0)
7499 return StmtError();
7500
7501 assert((CurContext->isDependentContext() || B.builtAll()) &&
7502 "omp teams distribute loop exprs were not built");
7503
Reid Kleckner87a31802018-03-12 21:43:02 +00007504 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007505
7506 DSAStack->setParentTeamsRegionLoc(StartLoc);
7507
David Majnemer9d168222016-08-05 17:44:54 +00007508 return OMPTeamsDistributeDirective::Create(
7509 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007510}
7511
Kelvin Li4e325f72016-10-25 12:50:55 +00007512StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7513 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007514 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007515 if (!AStmt)
7516 return StmtError();
7517
Alexey Bataeve3727102018-04-18 15:57:46 +00007518 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007519 // 1.2.2 OpenMP Language Terminology
7520 // Structured block - An executable statement with a single entry at the
7521 // top and a single exit at the bottom.
7522 // The point of exit cannot be a branch out of the structured block.
7523 // longjmp() and throw() must not violate the entry/exit criteria.
7524 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007525 for (int ThisCaptureLevel =
7526 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7527 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7528 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7529 // 1.2.2 OpenMP Language Terminology
7530 // Structured block - An executable statement with a single entry at the
7531 // top and a single exit at the bottom.
7532 // The point of exit cannot be a branch out of the structured block.
7533 // longjmp() and throw() must not violate the entry/exit criteria.
7534 CS->getCapturedDecl()->setNothrow();
7535 }
7536
Kelvin Li4e325f72016-10-25 12:50:55 +00007537
7538 OMPLoopDirective::HelperExprs B;
7539 // In presence of clause 'collapse' with number of loops, it will
7540 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007541 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007542 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007543 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007544 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007545
7546 if (NestedLoopCount == 0)
7547 return StmtError();
7548
7549 assert((CurContext->isDependentContext() || B.builtAll()) &&
7550 "omp teams distribute simd loop exprs were not built");
7551
7552 if (!CurContext->isDependentContext()) {
7553 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007554 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007555 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7556 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7557 B.NumIterations, *this, CurScope,
7558 DSAStack))
7559 return StmtError();
7560 }
7561 }
7562
7563 if (checkSimdlenSafelenSpecified(*this, Clauses))
7564 return StmtError();
7565
Reid Kleckner87a31802018-03-12 21:43:02 +00007566 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007567
7568 DSAStack->setParentTeamsRegionLoc(StartLoc);
7569
Kelvin Li4e325f72016-10-25 12:50:55 +00007570 return OMPTeamsDistributeSimdDirective::Create(
7571 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7572}
7573
Kelvin Li579e41c2016-11-30 23:51:03 +00007574StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7575 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007576 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007577 if (!AStmt)
7578 return StmtError();
7579
Alexey Bataeve3727102018-04-18 15:57:46 +00007580 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007581 // 1.2.2 OpenMP Language Terminology
7582 // Structured block - An executable statement with a single entry at the
7583 // top and a single exit at the bottom.
7584 // The point of exit cannot be a branch out of the structured block.
7585 // longjmp() and throw() must not violate the entry/exit criteria.
7586 CS->getCapturedDecl()->setNothrow();
7587
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007588 for (int ThisCaptureLevel =
7589 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7590 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7591 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7592 // 1.2.2 OpenMP Language Terminology
7593 // Structured block - An executable statement with a single entry at the
7594 // top and a single exit at the bottom.
7595 // The point of exit cannot be a branch out of the structured block.
7596 // longjmp() and throw() must not violate the entry/exit criteria.
7597 CS->getCapturedDecl()->setNothrow();
7598 }
7599
Kelvin Li579e41c2016-11-30 23:51:03 +00007600 OMPLoopDirective::HelperExprs B;
7601 // In presence of clause 'collapse' with number of loops, it will
7602 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007603 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007604 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007605 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007606 VarsWithImplicitDSA, B);
7607
7608 if (NestedLoopCount == 0)
7609 return StmtError();
7610
7611 assert((CurContext->isDependentContext() || B.builtAll()) &&
7612 "omp for loop exprs were not built");
7613
7614 if (!CurContext->isDependentContext()) {
7615 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007616 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007617 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7618 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7619 B.NumIterations, *this, CurScope,
7620 DSAStack))
7621 return StmtError();
7622 }
7623 }
7624
7625 if (checkSimdlenSafelenSpecified(*this, Clauses))
7626 return StmtError();
7627
Reid Kleckner87a31802018-03-12 21:43:02 +00007628 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007629
7630 DSAStack->setParentTeamsRegionLoc(StartLoc);
7631
Kelvin Li579e41c2016-11-30 23:51:03 +00007632 return OMPTeamsDistributeParallelForSimdDirective::Create(
7633 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7634}
7635
Kelvin Li7ade93f2016-12-09 03:24:30 +00007636StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7637 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007638 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007639 if (!AStmt)
7640 return StmtError();
7641
Alexey Bataeve3727102018-04-18 15:57:46 +00007642 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007643 // 1.2.2 OpenMP Language Terminology
7644 // Structured block - An executable statement with a single entry at the
7645 // top and a single exit at the bottom.
7646 // The point of exit cannot be a branch out of the structured block.
7647 // longjmp() and throw() must not violate the entry/exit criteria.
7648 CS->getCapturedDecl()->setNothrow();
7649
Carlo Bertolli62fae152017-11-20 20:46:39 +00007650 for (int ThisCaptureLevel =
7651 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7652 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7653 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7654 // 1.2.2 OpenMP Language Terminology
7655 // Structured block - An executable statement with a single entry at the
7656 // top and a single exit at the bottom.
7657 // The point of exit cannot be a branch out of the structured block.
7658 // longjmp() and throw() must not violate the entry/exit criteria.
7659 CS->getCapturedDecl()->setNothrow();
7660 }
7661
Kelvin Li7ade93f2016-12-09 03:24:30 +00007662 OMPLoopDirective::HelperExprs B;
7663 // In presence of clause 'collapse' with number of loops, it will
7664 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007665 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007666 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007667 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007668 VarsWithImplicitDSA, B);
7669
7670 if (NestedLoopCount == 0)
7671 return StmtError();
7672
7673 assert((CurContext->isDependentContext() || B.builtAll()) &&
7674 "omp for loop exprs were not built");
7675
Reid Kleckner87a31802018-03-12 21:43:02 +00007676 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007677
7678 DSAStack->setParentTeamsRegionLoc(StartLoc);
7679
Kelvin Li7ade93f2016-12-09 03:24:30 +00007680 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007681 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7682 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007683}
7684
Kelvin Libf594a52016-12-17 05:48:59 +00007685StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7686 Stmt *AStmt,
7687 SourceLocation StartLoc,
7688 SourceLocation EndLoc) {
7689 if (!AStmt)
7690 return StmtError();
7691
Alexey Bataeve3727102018-04-18 15:57:46 +00007692 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007693 // 1.2.2 OpenMP Language Terminology
7694 // Structured block - An executable statement with a single entry at the
7695 // top and a single exit at the bottom.
7696 // The point of exit cannot be a branch out of the structured block.
7697 // longjmp() and throw() must not violate the entry/exit criteria.
7698 CS->getCapturedDecl()->setNothrow();
7699
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007700 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7701 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7702 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7703 // 1.2.2 OpenMP Language Terminology
7704 // Structured block - An executable statement with a single entry at the
7705 // top and a single exit at the bottom.
7706 // The point of exit cannot be a branch out of the structured block.
7707 // longjmp() and throw() must not violate the entry/exit criteria.
7708 CS->getCapturedDecl()->setNothrow();
7709 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007710 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007711
7712 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7713 AStmt);
7714}
7715
Kelvin Li83c451e2016-12-25 04:52:54 +00007716StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7717 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007718 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007719 if (!AStmt)
7720 return StmtError();
7721
Alexey Bataeve3727102018-04-18 15:57:46 +00007722 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007723 // 1.2.2 OpenMP Language Terminology
7724 // Structured block - An executable statement with a single entry at the
7725 // top and a single exit at the bottom.
7726 // The point of exit cannot be a branch out of the structured block.
7727 // longjmp() and throw() must not violate the entry/exit criteria.
7728 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007729 for (int ThisCaptureLevel =
7730 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7731 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7732 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7733 // 1.2.2 OpenMP Language Terminology
7734 // Structured block - An executable statement with a single entry at the
7735 // top and a single exit at the bottom.
7736 // The point of exit cannot be a branch out of the structured block.
7737 // longjmp() and throw() must not violate the entry/exit criteria.
7738 CS->getCapturedDecl()->setNothrow();
7739 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007740
7741 OMPLoopDirective::HelperExprs B;
7742 // In presence of clause 'collapse' with number of loops, it will
7743 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007744 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007745 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7746 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007747 VarsWithImplicitDSA, B);
7748 if (NestedLoopCount == 0)
7749 return StmtError();
7750
7751 assert((CurContext->isDependentContext() || B.builtAll()) &&
7752 "omp target teams distribute loop exprs were not built");
7753
Reid Kleckner87a31802018-03-12 21:43:02 +00007754 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007755 return OMPTargetTeamsDistributeDirective::Create(
7756 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7757}
7758
Kelvin Li80e8f562016-12-29 22:16:30 +00007759StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7760 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007761 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007762 if (!AStmt)
7763 return StmtError();
7764
Alexey Bataeve3727102018-04-18 15:57:46 +00007765 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007766 // 1.2.2 OpenMP Language Terminology
7767 // Structured block - An executable statement with a single entry at the
7768 // top and a single exit at the bottom.
7769 // The point of exit cannot be a branch out of the structured block.
7770 // longjmp() and throw() must not violate the entry/exit criteria.
7771 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007772 for (int ThisCaptureLevel =
7773 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7774 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7775 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7776 // 1.2.2 OpenMP Language Terminology
7777 // Structured block - An executable statement with a single entry at the
7778 // top and a single exit at the bottom.
7779 // The point of exit cannot be a branch out of the structured block.
7780 // longjmp() and throw() must not violate the entry/exit criteria.
7781 CS->getCapturedDecl()->setNothrow();
7782 }
7783
Kelvin Li80e8f562016-12-29 22:16:30 +00007784 OMPLoopDirective::HelperExprs B;
7785 // In presence of clause 'collapse' with number of loops, it will
7786 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007787 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007788 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7789 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007790 VarsWithImplicitDSA, B);
7791 if (NestedLoopCount == 0)
7792 return StmtError();
7793
7794 assert((CurContext->isDependentContext() || B.builtAll()) &&
7795 "omp target teams distribute parallel for loop exprs were not built");
7796
Alexey Bataev647dd842018-01-15 20:59:40 +00007797 if (!CurContext->isDependentContext()) {
7798 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007799 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007800 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7801 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7802 B.NumIterations, *this, CurScope,
7803 DSAStack))
7804 return StmtError();
7805 }
7806 }
7807
Reid Kleckner87a31802018-03-12 21:43:02 +00007808 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007809 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007810 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7811 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007812}
7813
Kelvin Li1851df52017-01-03 05:23:48 +00007814StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7815 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007816 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007817 if (!AStmt)
7818 return StmtError();
7819
Alexey Bataeve3727102018-04-18 15:57:46 +00007820 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007821 // 1.2.2 OpenMP Language Terminology
7822 // Structured block - An executable statement with a single entry at the
7823 // top and a single exit at the bottom.
7824 // The point of exit cannot be a branch out of the structured block.
7825 // longjmp() and throw() must not violate the entry/exit criteria.
7826 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007827 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7828 OMPD_target_teams_distribute_parallel_for_simd);
7829 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7830 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7831 // 1.2.2 OpenMP Language Terminology
7832 // Structured block - An executable statement with a single entry at the
7833 // top and a single exit at the bottom.
7834 // The point of exit cannot be a branch out of the structured block.
7835 // longjmp() and throw() must not violate the entry/exit criteria.
7836 CS->getCapturedDecl()->setNothrow();
7837 }
Kelvin Li1851df52017-01-03 05:23:48 +00007838
7839 OMPLoopDirective::HelperExprs B;
7840 // In presence of clause 'collapse' with number of loops, it will
7841 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007842 unsigned NestedLoopCount =
7843 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00007844 getCollapseNumberExpr(Clauses),
7845 nullptr /*ordered not a clause on distribute*/, CS, *this,
7846 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00007847 if (NestedLoopCount == 0)
7848 return StmtError();
7849
7850 assert((CurContext->isDependentContext() || B.builtAll()) &&
7851 "omp target teams distribute parallel for simd loop exprs were not "
7852 "built");
7853
7854 if (!CurContext->isDependentContext()) {
7855 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007856 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00007857 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7858 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7859 B.NumIterations, *this, CurScope,
7860 DSAStack))
7861 return StmtError();
7862 }
7863 }
7864
Alexey Bataev438388c2017-11-22 18:34:02 +00007865 if (checkSimdlenSafelenSpecified(*this, Clauses))
7866 return StmtError();
7867
Reid Kleckner87a31802018-03-12 21:43:02 +00007868 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00007869 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7870 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7871}
7872
Kelvin Lida681182017-01-10 18:08:18 +00007873StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7874 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007875 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00007876 if (!AStmt)
7877 return StmtError();
7878
7879 auto *CS = cast<CapturedStmt>(AStmt);
7880 // 1.2.2 OpenMP Language Terminology
7881 // Structured block - An executable statement with a single entry at the
7882 // top and a single exit at the bottom.
7883 // The point of exit cannot be a branch out of the structured block.
7884 // longjmp() and throw() must not violate the entry/exit criteria.
7885 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007886 for (int ThisCaptureLevel =
7887 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7888 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7889 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7890 // 1.2.2 OpenMP Language Terminology
7891 // Structured block - An executable statement with a single entry at the
7892 // top and a single exit at the bottom.
7893 // The point of exit cannot be a branch out of the structured block.
7894 // longjmp() and throw() must not violate the entry/exit criteria.
7895 CS->getCapturedDecl()->setNothrow();
7896 }
Kelvin Lida681182017-01-10 18:08:18 +00007897
7898 OMPLoopDirective::HelperExprs B;
7899 // In presence of clause 'collapse' with number of loops, it will
7900 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007901 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00007902 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007903 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007904 VarsWithImplicitDSA, B);
7905 if (NestedLoopCount == 0)
7906 return StmtError();
7907
7908 assert((CurContext->isDependentContext() || B.builtAll()) &&
7909 "omp target teams distribute simd loop exprs were not built");
7910
Alexey Bataev438388c2017-11-22 18:34:02 +00007911 if (!CurContext->isDependentContext()) {
7912 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007913 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007914 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7915 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7916 B.NumIterations, *this, CurScope,
7917 DSAStack))
7918 return StmtError();
7919 }
7920 }
7921
7922 if (checkSimdlenSafelenSpecified(*this, Clauses))
7923 return StmtError();
7924
Reid Kleckner87a31802018-03-12 21:43:02 +00007925 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00007926 return OMPTargetTeamsDistributeSimdDirective::Create(
7927 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7928}
7929
Alexey Bataeved09d242014-05-28 05:53:51 +00007930OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007931 SourceLocation StartLoc,
7932 SourceLocation LParenLoc,
7933 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007934 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007935 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007936 case OMPC_final:
7937 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7938 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007939 case OMPC_num_threads:
7940 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7941 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007942 case OMPC_safelen:
7943 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7944 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007945 case OMPC_simdlen:
7946 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7947 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007948 case OMPC_collapse:
7949 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7950 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007951 case OMPC_ordered:
7952 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7953 break;
Michael Wonge710d542015-08-07 16:16:36 +00007954 case OMPC_device:
7955 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7956 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007957 case OMPC_num_teams:
7958 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7959 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007960 case OMPC_thread_limit:
7961 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7962 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007963 case OMPC_priority:
7964 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7965 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007966 case OMPC_grainsize:
7967 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7968 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007969 case OMPC_num_tasks:
7970 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7971 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007972 case OMPC_hint:
7973 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7974 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007975 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007976 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007977 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007978 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007979 case OMPC_private:
7980 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007981 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007982 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007983 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00007984 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00007985 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007986 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007987 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007988 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007989 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007990 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007991 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007992 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007993 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007994 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007995 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007996 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007997 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007998 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007999 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008000 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008001 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008002 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008003 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008004 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008005 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008006 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008007 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008008 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008009 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008010 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008011 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008012 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008013 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008014 case OMPC_unified_shared_memory:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008015 llvm_unreachable("Clause is not allowed.");
8016 }
8017 return Res;
8018}
8019
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008020// An OpenMP directive such as 'target parallel' has two captured regions:
8021// for the 'target' and 'parallel' respectively. This function returns
8022// the region in which to capture expressions associated with a clause.
8023// A return value of OMPD_unknown signifies that the expression should not
8024// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008025static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8026 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8027 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008028 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008029 switch (CKind) {
8030 case OMPC_if:
8031 switch (DKind) {
8032 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008033 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008034 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008035 // If this clause applies to the nested 'parallel' region, capture within
8036 // the 'target' region, otherwise do not capture.
8037 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8038 CaptureRegion = OMPD_target;
8039 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008040 case OMPD_target_teams_distribute_parallel_for:
8041 case OMPD_target_teams_distribute_parallel_for_simd:
8042 // If this clause applies to the nested 'parallel' region, capture within
8043 // the 'teams' region, otherwise do not capture.
8044 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8045 CaptureRegion = OMPD_teams;
8046 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008047 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008048 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008049 CaptureRegion = OMPD_teams;
8050 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008051 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008052 case OMPD_target_enter_data:
8053 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008054 CaptureRegion = OMPD_task;
8055 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008056 case OMPD_cancel:
8057 case OMPD_parallel:
8058 case OMPD_parallel_sections:
8059 case OMPD_parallel_for:
8060 case OMPD_parallel_for_simd:
8061 case OMPD_target:
8062 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008063 case OMPD_target_teams:
8064 case OMPD_target_teams_distribute:
8065 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008066 case OMPD_distribute_parallel_for:
8067 case OMPD_distribute_parallel_for_simd:
8068 case OMPD_task:
8069 case OMPD_taskloop:
8070 case OMPD_taskloop_simd:
8071 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008072 // Do not capture if-clause expressions.
8073 break;
8074 case OMPD_threadprivate:
8075 case OMPD_taskyield:
8076 case OMPD_barrier:
8077 case OMPD_taskwait:
8078 case OMPD_cancellation_point:
8079 case OMPD_flush:
8080 case OMPD_declare_reduction:
8081 case OMPD_declare_simd:
8082 case OMPD_declare_target:
8083 case OMPD_end_declare_target:
8084 case OMPD_teams:
8085 case OMPD_simd:
8086 case OMPD_for:
8087 case OMPD_for_simd:
8088 case OMPD_sections:
8089 case OMPD_section:
8090 case OMPD_single:
8091 case OMPD_master:
8092 case OMPD_critical:
8093 case OMPD_taskgroup:
8094 case OMPD_distribute:
8095 case OMPD_ordered:
8096 case OMPD_atomic:
8097 case OMPD_distribute_simd:
8098 case OMPD_teams_distribute:
8099 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008100 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008101 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8102 case OMPD_unknown:
8103 llvm_unreachable("Unknown OpenMP directive");
8104 }
8105 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008106 case OMPC_num_threads:
8107 switch (DKind) {
8108 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008109 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008110 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008111 CaptureRegion = OMPD_target;
8112 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008113 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008114 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008115 case OMPD_target_teams_distribute_parallel_for:
8116 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008117 CaptureRegion = OMPD_teams;
8118 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008119 case OMPD_parallel:
8120 case OMPD_parallel_sections:
8121 case OMPD_parallel_for:
8122 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008123 case OMPD_distribute_parallel_for:
8124 case OMPD_distribute_parallel_for_simd:
8125 // Do not capture num_threads-clause expressions.
8126 break;
8127 case OMPD_target_data:
8128 case OMPD_target_enter_data:
8129 case OMPD_target_exit_data:
8130 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008131 case OMPD_target:
8132 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008133 case OMPD_target_teams:
8134 case OMPD_target_teams_distribute:
8135 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008136 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008137 case OMPD_task:
8138 case OMPD_taskloop:
8139 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008140 case OMPD_threadprivate:
8141 case OMPD_taskyield:
8142 case OMPD_barrier:
8143 case OMPD_taskwait:
8144 case OMPD_cancellation_point:
8145 case OMPD_flush:
8146 case OMPD_declare_reduction:
8147 case OMPD_declare_simd:
8148 case OMPD_declare_target:
8149 case OMPD_end_declare_target:
8150 case OMPD_teams:
8151 case OMPD_simd:
8152 case OMPD_for:
8153 case OMPD_for_simd:
8154 case OMPD_sections:
8155 case OMPD_section:
8156 case OMPD_single:
8157 case OMPD_master:
8158 case OMPD_critical:
8159 case OMPD_taskgroup:
8160 case OMPD_distribute:
8161 case OMPD_ordered:
8162 case OMPD_atomic:
8163 case OMPD_distribute_simd:
8164 case OMPD_teams_distribute:
8165 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008166 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008167 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8168 case OMPD_unknown:
8169 llvm_unreachable("Unknown OpenMP directive");
8170 }
8171 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008172 case OMPC_num_teams:
8173 switch (DKind) {
8174 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008175 case OMPD_target_teams_distribute:
8176 case OMPD_target_teams_distribute_simd:
8177 case OMPD_target_teams_distribute_parallel_for:
8178 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008179 CaptureRegion = OMPD_target;
8180 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008181 case OMPD_teams_distribute_parallel_for:
8182 case OMPD_teams_distribute_parallel_for_simd:
8183 case OMPD_teams:
8184 case OMPD_teams_distribute:
8185 case OMPD_teams_distribute_simd:
8186 // Do not capture num_teams-clause expressions.
8187 break;
8188 case OMPD_distribute_parallel_for:
8189 case OMPD_distribute_parallel_for_simd:
8190 case OMPD_task:
8191 case OMPD_taskloop:
8192 case OMPD_taskloop_simd:
8193 case OMPD_target_data:
8194 case OMPD_target_enter_data:
8195 case OMPD_target_exit_data:
8196 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008197 case OMPD_cancel:
8198 case OMPD_parallel:
8199 case OMPD_parallel_sections:
8200 case OMPD_parallel_for:
8201 case OMPD_parallel_for_simd:
8202 case OMPD_target:
8203 case OMPD_target_simd:
8204 case OMPD_target_parallel:
8205 case OMPD_target_parallel_for:
8206 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008207 case OMPD_threadprivate:
8208 case OMPD_taskyield:
8209 case OMPD_barrier:
8210 case OMPD_taskwait:
8211 case OMPD_cancellation_point:
8212 case OMPD_flush:
8213 case OMPD_declare_reduction:
8214 case OMPD_declare_simd:
8215 case OMPD_declare_target:
8216 case OMPD_end_declare_target:
8217 case OMPD_simd:
8218 case OMPD_for:
8219 case OMPD_for_simd:
8220 case OMPD_sections:
8221 case OMPD_section:
8222 case OMPD_single:
8223 case OMPD_master:
8224 case OMPD_critical:
8225 case OMPD_taskgroup:
8226 case OMPD_distribute:
8227 case OMPD_ordered:
8228 case OMPD_atomic:
8229 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008230 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008231 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8232 case OMPD_unknown:
8233 llvm_unreachable("Unknown OpenMP directive");
8234 }
8235 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008236 case OMPC_thread_limit:
8237 switch (DKind) {
8238 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008239 case OMPD_target_teams_distribute:
8240 case OMPD_target_teams_distribute_simd:
8241 case OMPD_target_teams_distribute_parallel_for:
8242 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008243 CaptureRegion = OMPD_target;
8244 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008245 case OMPD_teams_distribute_parallel_for:
8246 case OMPD_teams_distribute_parallel_for_simd:
8247 case OMPD_teams:
8248 case OMPD_teams_distribute:
8249 case OMPD_teams_distribute_simd:
8250 // Do not capture thread_limit-clause expressions.
8251 break;
8252 case OMPD_distribute_parallel_for:
8253 case OMPD_distribute_parallel_for_simd:
8254 case OMPD_task:
8255 case OMPD_taskloop:
8256 case OMPD_taskloop_simd:
8257 case OMPD_target_data:
8258 case OMPD_target_enter_data:
8259 case OMPD_target_exit_data:
8260 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008261 case OMPD_cancel:
8262 case OMPD_parallel:
8263 case OMPD_parallel_sections:
8264 case OMPD_parallel_for:
8265 case OMPD_parallel_for_simd:
8266 case OMPD_target:
8267 case OMPD_target_simd:
8268 case OMPD_target_parallel:
8269 case OMPD_target_parallel_for:
8270 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008271 case OMPD_threadprivate:
8272 case OMPD_taskyield:
8273 case OMPD_barrier:
8274 case OMPD_taskwait:
8275 case OMPD_cancellation_point:
8276 case OMPD_flush:
8277 case OMPD_declare_reduction:
8278 case OMPD_declare_simd:
8279 case OMPD_declare_target:
8280 case OMPD_end_declare_target:
8281 case OMPD_simd:
8282 case OMPD_for:
8283 case OMPD_for_simd:
8284 case OMPD_sections:
8285 case OMPD_section:
8286 case OMPD_single:
8287 case OMPD_master:
8288 case OMPD_critical:
8289 case OMPD_taskgroup:
8290 case OMPD_distribute:
8291 case OMPD_ordered:
8292 case OMPD_atomic:
8293 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008294 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008295 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8296 case OMPD_unknown:
8297 llvm_unreachable("Unknown OpenMP directive");
8298 }
8299 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008300 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008301 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008302 case OMPD_parallel_for:
8303 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008304 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008305 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008306 case OMPD_teams_distribute_parallel_for:
8307 case OMPD_teams_distribute_parallel_for_simd:
8308 case OMPD_target_parallel_for:
8309 case OMPD_target_parallel_for_simd:
8310 case OMPD_target_teams_distribute_parallel_for:
8311 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008312 CaptureRegion = OMPD_parallel;
8313 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008314 case OMPD_for:
8315 case OMPD_for_simd:
8316 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008317 break;
8318 case OMPD_task:
8319 case OMPD_taskloop:
8320 case OMPD_taskloop_simd:
8321 case OMPD_target_data:
8322 case OMPD_target_enter_data:
8323 case OMPD_target_exit_data:
8324 case OMPD_target_update:
8325 case OMPD_teams:
8326 case OMPD_teams_distribute:
8327 case OMPD_teams_distribute_simd:
8328 case OMPD_target_teams_distribute:
8329 case OMPD_target_teams_distribute_simd:
8330 case OMPD_target:
8331 case OMPD_target_simd:
8332 case OMPD_target_parallel:
8333 case OMPD_cancel:
8334 case OMPD_parallel:
8335 case OMPD_parallel_sections:
8336 case OMPD_threadprivate:
8337 case OMPD_taskyield:
8338 case OMPD_barrier:
8339 case OMPD_taskwait:
8340 case OMPD_cancellation_point:
8341 case OMPD_flush:
8342 case OMPD_declare_reduction:
8343 case OMPD_declare_simd:
8344 case OMPD_declare_target:
8345 case OMPD_end_declare_target:
8346 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008347 case OMPD_sections:
8348 case OMPD_section:
8349 case OMPD_single:
8350 case OMPD_master:
8351 case OMPD_critical:
8352 case OMPD_taskgroup:
8353 case OMPD_distribute:
8354 case OMPD_ordered:
8355 case OMPD_atomic:
8356 case OMPD_distribute_simd:
8357 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008358 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008359 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8360 case OMPD_unknown:
8361 llvm_unreachable("Unknown OpenMP directive");
8362 }
8363 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008364 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008365 switch (DKind) {
8366 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008367 case OMPD_teams_distribute_parallel_for_simd:
8368 case OMPD_teams_distribute:
8369 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008370 case OMPD_target_teams_distribute_parallel_for:
8371 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008372 case OMPD_target_teams_distribute:
8373 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008374 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008375 break;
8376 case OMPD_distribute_parallel_for:
8377 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008378 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008379 case OMPD_distribute_simd:
8380 // Do not capture thread_limit-clause expressions.
8381 break;
8382 case OMPD_parallel_for:
8383 case OMPD_parallel_for_simd:
8384 case OMPD_target_parallel_for_simd:
8385 case OMPD_target_parallel_for:
8386 case OMPD_task:
8387 case OMPD_taskloop:
8388 case OMPD_taskloop_simd:
8389 case OMPD_target_data:
8390 case OMPD_target_enter_data:
8391 case OMPD_target_exit_data:
8392 case OMPD_target_update:
8393 case OMPD_teams:
8394 case OMPD_target:
8395 case OMPD_target_simd:
8396 case OMPD_target_parallel:
8397 case OMPD_cancel:
8398 case OMPD_parallel:
8399 case OMPD_parallel_sections:
8400 case OMPD_threadprivate:
8401 case OMPD_taskyield:
8402 case OMPD_barrier:
8403 case OMPD_taskwait:
8404 case OMPD_cancellation_point:
8405 case OMPD_flush:
8406 case OMPD_declare_reduction:
8407 case OMPD_declare_simd:
8408 case OMPD_declare_target:
8409 case OMPD_end_declare_target:
8410 case OMPD_simd:
8411 case OMPD_for:
8412 case OMPD_for_simd:
8413 case OMPD_sections:
8414 case OMPD_section:
8415 case OMPD_single:
8416 case OMPD_master:
8417 case OMPD_critical:
8418 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008419 case OMPD_ordered:
8420 case OMPD_atomic:
8421 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008422 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008423 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8424 case OMPD_unknown:
8425 llvm_unreachable("Unknown OpenMP directive");
8426 }
8427 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008428 case OMPC_device:
8429 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008430 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008431 case OMPD_target_enter_data:
8432 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008433 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008434 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008435 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008436 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008437 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008438 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008439 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008440 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008441 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008442 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008443 CaptureRegion = OMPD_task;
8444 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008445 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008446 // Do not capture device-clause expressions.
8447 break;
8448 case OMPD_teams_distribute_parallel_for:
8449 case OMPD_teams_distribute_parallel_for_simd:
8450 case OMPD_teams:
8451 case OMPD_teams_distribute:
8452 case OMPD_teams_distribute_simd:
8453 case OMPD_distribute_parallel_for:
8454 case OMPD_distribute_parallel_for_simd:
8455 case OMPD_task:
8456 case OMPD_taskloop:
8457 case OMPD_taskloop_simd:
8458 case OMPD_cancel:
8459 case OMPD_parallel:
8460 case OMPD_parallel_sections:
8461 case OMPD_parallel_for:
8462 case OMPD_parallel_for_simd:
8463 case OMPD_threadprivate:
8464 case OMPD_taskyield:
8465 case OMPD_barrier:
8466 case OMPD_taskwait:
8467 case OMPD_cancellation_point:
8468 case OMPD_flush:
8469 case OMPD_declare_reduction:
8470 case OMPD_declare_simd:
8471 case OMPD_declare_target:
8472 case OMPD_end_declare_target:
8473 case OMPD_simd:
8474 case OMPD_for:
8475 case OMPD_for_simd:
8476 case OMPD_sections:
8477 case OMPD_section:
8478 case OMPD_single:
8479 case OMPD_master:
8480 case OMPD_critical:
8481 case OMPD_taskgroup:
8482 case OMPD_distribute:
8483 case OMPD_ordered:
8484 case OMPD_atomic:
8485 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008486 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008487 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8488 case OMPD_unknown:
8489 llvm_unreachable("Unknown OpenMP directive");
8490 }
8491 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008492 case OMPC_firstprivate:
8493 case OMPC_lastprivate:
8494 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008495 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008496 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008497 case OMPC_linear:
8498 case OMPC_default:
8499 case OMPC_proc_bind:
8500 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008501 case OMPC_safelen:
8502 case OMPC_simdlen:
8503 case OMPC_collapse:
8504 case OMPC_private:
8505 case OMPC_shared:
8506 case OMPC_aligned:
8507 case OMPC_copyin:
8508 case OMPC_copyprivate:
8509 case OMPC_ordered:
8510 case OMPC_nowait:
8511 case OMPC_untied:
8512 case OMPC_mergeable:
8513 case OMPC_threadprivate:
8514 case OMPC_flush:
8515 case OMPC_read:
8516 case OMPC_write:
8517 case OMPC_update:
8518 case OMPC_capture:
8519 case OMPC_seq_cst:
8520 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008521 case OMPC_threads:
8522 case OMPC_simd:
8523 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008524 case OMPC_priority:
8525 case OMPC_grainsize:
8526 case OMPC_nogroup:
8527 case OMPC_num_tasks:
8528 case OMPC_hint:
8529 case OMPC_defaultmap:
8530 case OMPC_unknown:
8531 case OMPC_uniform:
8532 case OMPC_to:
8533 case OMPC_from:
8534 case OMPC_use_device_ptr:
8535 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008536 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008537 case OMPC_unified_shared_memory:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008538 llvm_unreachable("Unexpected OpenMP clause.");
8539 }
8540 return CaptureRegion;
8541}
8542
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008543OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8544 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008545 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008546 SourceLocation NameModifierLoc,
8547 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008548 SourceLocation EndLoc) {
8549 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008550 Stmt *HelperValStmt = nullptr;
8551 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008552 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8553 !Condition->isInstantiationDependent() &&
8554 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008555 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008556 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008557 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008558
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008559 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008560
8561 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8562 CaptureRegion =
8563 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008564 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008565 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008566 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008567 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8568 HelperValStmt = buildPreInits(Context, Captures);
8569 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008570 }
8571
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008572 return new (Context)
8573 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8574 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008575}
8576
Alexey Bataev3778b602014-07-17 07:32:53 +00008577OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8578 SourceLocation StartLoc,
8579 SourceLocation LParenLoc,
8580 SourceLocation EndLoc) {
8581 Expr *ValExpr = Condition;
8582 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8583 !Condition->isInstantiationDependent() &&
8584 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008585 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008586 if (Val.isInvalid())
8587 return nullptr;
8588
Richard Smith03a4aa32016-06-23 19:02:52 +00008589 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008590 }
8591
8592 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8593}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008594ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8595 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008596 if (!Op)
8597 return ExprError();
8598
8599 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8600 public:
8601 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008602 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008603 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8604 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008605 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8606 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008607 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8608 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008609 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8610 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008611 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8612 QualType T,
8613 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008614 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8615 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008616 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8617 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008618 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008619 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008620 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008621 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8622 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008623 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8624 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008625 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8626 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008627 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008628 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008629 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008630 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8631 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008632 llvm_unreachable("conversion functions are permitted");
8633 }
8634 } ConvertDiagnoser;
8635 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8636}
8637
Alexey Bataeve3727102018-04-18 15:57:46 +00008638static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008639 OpenMPClauseKind CKind,
8640 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008641 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8642 !ValExpr->isInstantiationDependent()) {
8643 SourceLocation Loc = ValExpr->getExprLoc();
8644 ExprResult Value =
8645 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8646 if (Value.isInvalid())
8647 return false;
8648
8649 ValExpr = Value.get();
8650 // The expression must evaluate to a non-negative integer value.
8651 llvm::APSInt Result;
8652 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008653 Result.isSigned() &&
8654 !((!StrictlyPositive && Result.isNonNegative()) ||
8655 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008656 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008657 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8658 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008659 return false;
8660 }
8661 }
8662 return true;
8663}
8664
Alexey Bataev568a8332014-03-06 06:15:19 +00008665OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8666 SourceLocation StartLoc,
8667 SourceLocation LParenLoc,
8668 SourceLocation EndLoc) {
8669 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008670 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008671
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008672 // OpenMP [2.5, Restrictions]
8673 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008674 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008675 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008676 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008677
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008678 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008679 OpenMPDirectiveKind CaptureRegion =
8680 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8681 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008682 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008683 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008684 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8685 HelperValStmt = buildPreInits(Context, Captures);
8686 }
8687
8688 return new (Context) OMPNumThreadsClause(
8689 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008690}
8691
Alexey Bataev62c87d22014-03-21 04:51:18 +00008692ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008693 OpenMPClauseKind CKind,
8694 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008695 if (!E)
8696 return ExprError();
8697 if (E->isValueDependent() || E->isTypeDependent() ||
8698 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008699 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008700 llvm::APSInt Result;
8701 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8702 if (ICE.isInvalid())
8703 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008704 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8705 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008706 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008707 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8708 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008709 return ExprError();
8710 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008711 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8712 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8713 << E->getSourceRange();
8714 return ExprError();
8715 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008716 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8717 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008718 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008719 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008720 return ICE;
8721}
8722
8723OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8724 SourceLocation LParenLoc,
8725 SourceLocation EndLoc) {
8726 // OpenMP [2.8.1, simd construct, Description]
8727 // The parameter of the safelen clause must be a constant
8728 // positive integer expression.
8729 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8730 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008731 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008732 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008733 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008734}
8735
Alexey Bataev66b15b52015-08-21 11:14:16 +00008736OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8737 SourceLocation LParenLoc,
8738 SourceLocation EndLoc) {
8739 // OpenMP [2.8.1, simd construct, Description]
8740 // The parameter of the simdlen clause must be a constant
8741 // positive integer expression.
8742 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8743 if (Simdlen.isInvalid())
8744 return nullptr;
8745 return new (Context)
8746 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8747}
8748
Alexander Musman64d33f12014-06-04 07:53:32 +00008749OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8750 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008751 SourceLocation LParenLoc,
8752 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008753 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008754 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008755 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008756 // The parameter of the collapse clause must be a constant
8757 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008758 ExprResult NumForLoopsResult =
8759 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8760 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008761 return nullptr;
8762 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008763 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008764}
8765
Alexey Bataev10e775f2015-07-30 11:36:16 +00008766OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8767 SourceLocation EndLoc,
8768 SourceLocation LParenLoc,
8769 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008770 // OpenMP [2.7.1, loop construct, Description]
8771 // OpenMP [2.8.1, simd construct, Description]
8772 // OpenMP [2.9.6, distribute construct, Description]
8773 // The parameter of the ordered clause must be a constant
8774 // positive integer expression if any.
8775 if (NumForLoops && LParenLoc.isValid()) {
8776 ExprResult NumForLoopsResult =
8777 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8778 if (NumForLoopsResult.isInvalid())
8779 return nullptr;
8780 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008781 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008782 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008783 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008784 auto *Clause = OMPOrderedClause::Create(
8785 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8786 StartLoc, LParenLoc, EndLoc);
8787 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8788 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008789}
8790
Alexey Bataeved09d242014-05-28 05:53:51 +00008791OMPClause *Sema::ActOnOpenMPSimpleClause(
8792 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8793 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008794 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008795 switch (Kind) {
8796 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008797 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008798 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8799 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008800 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008801 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008802 Res = ActOnOpenMPProcBindClause(
8803 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8804 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008805 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008806 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008807 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008808 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008809 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008810 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008811 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008812 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008813 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008814 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008815 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008816 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008817 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008818 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008819 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008820 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008821 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008822 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008823 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008824 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008825 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008826 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008827 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008828 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008829 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008830 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008831 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008832 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008833 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008834 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008835 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008836 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008837 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008838 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008839 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008840 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008841 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008842 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008843 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008844 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008845 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008846 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008847 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008848 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008849 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008850 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008851 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008852 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008853 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008854 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008855 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008856 case OMPC_unified_shared_memory:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008857 llvm_unreachable("Clause is not allowed.");
8858 }
8859 return Res;
8860}
8861
Alexey Bataev6402bca2015-12-28 07:25:51 +00008862static std::string
8863getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8864 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008865 SmallString<256> Buffer;
8866 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00008867 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8868 unsigned Skipped = Exclude.size();
8869 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00008870 for (unsigned I = First; I < Last; ++I) {
8871 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008872 --Skipped;
8873 continue;
8874 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008875 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
8876 if (I == Bound - Skipped)
8877 Out << " or ";
8878 else if (I != Bound + 1 - Skipped)
8879 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00008880 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008881 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00008882}
8883
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008884OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8885 SourceLocation KindKwLoc,
8886 SourceLocation StartLoc,
8887 SourceLocation LParenLoc,
8888 SourceLocation EndLoc) {
8889 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008890 static_assert(OMPC_DEFAULT_unknown > 0,
8891 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008892 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008893 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8894 /*Last=*/OMPC_DEFAULT_unknown)
8895 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008896 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008897 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008898 switch (Kind) {
8899 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008900 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008901 break;
8902 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008903 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008904 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008905 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008906 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008907 break;
8908 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008909 return new (Context)
8910 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008911}
8912
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008913OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8914 SourceLocation KindKwLoc,
8915 SourceLocation StartLoc,
8916 SourceLocation LParenLoc,
8917 SourceLocation EndLoc) {
8918 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008919 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008920 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8921 /*Last=*/OMPC_PROC_BIND_unknown)
8922 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008923 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008924 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008925 return new (Context)
8926 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008927}
8928
Alexey Bataev56dafe82014-06-20 07:16:17 +00008929OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008930 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008931 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008932 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008933 SourceLocation EndLoc) {
8934 OMPClause *Res = nullptr;
8935 switch (Kind) {
8936 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008937 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8938 assert(Argument.size() == NumberOfElements &&
8939 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008940 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008941 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8942 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8943 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8944 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8945 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008946 break;
8947 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008948 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8949 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8950 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8951 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008952 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008953 case OMPC_dist_schedule:
8954 Res = ActOnOpenMPDistScheduleClause(
8955 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8956 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8957 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008958 case OMPC_defaultmap:
8959 enum { Modifier, DefaultmapKind };
8960 Res = ActOnOpenMPDefaultmapClause(
8961 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8962 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008963 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8964 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008965 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008966 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008967 case OMPC_num_threads:
8968 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008969 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008970 case OMPC_collapse:
8971 case OMPC_default:
8972 case OMPC_proc_bind:
8973 case OMPC_private:
8974 case OMPC_firstprivate:
8975 case OMPC_lastprivate:
8976 case OMPC_shared:
8977 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008978 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008979 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008980 case OMPC_linear:
8981 case OMPC_aligned:
8982 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008983 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008984 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008985 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008986 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008987 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008988 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008989 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008990 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008991 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008992 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008993 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008994 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008995 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008996 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008997 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008998 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008999 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009000 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009001 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009002 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009003 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009004 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009005 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009006 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009007 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009008 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009009 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009010 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009011 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009012 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009013 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009014 case OMPC_unified_shared_memory:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009015 llvm_unreachable("Clause is not allowed.");
9016 }
9017 return Res;
9018}
9019
Alexey Bataev6402bca2015-12-28 07:25:51 +00009020static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9021 OpenMPScheduleClauseModifier M2,
9022 SourceLocation M1Loc, SourceLocation M2Loc) {
9023 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9024 SmallVector<unsigned, 2> Excluded;
9025 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9026 Excluded.push_back(M2);
9027 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9028 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9029 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9030 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9031 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9032 << getListOfPossibleValues(OMPC_schedule,
9033 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9034 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9035 Excluded)
9036 << getOpenMPClauseName(OMPC_schedule);
9037 return true;
9038 }
9039 return false;
9040}
9041
Alexey Bataev56dafe82014-06-20 07:16:17 +00009042OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009043 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009044 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009045 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9046 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9047 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9048 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9049 return nullptr;
9050 // OpenMP, 2.7.1, Loop Construct, Restrictions
9051 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9052 // but not both.
9053 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9054 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9055 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9056 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9057 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9058 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9059 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9060 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9061 return nullptr;
9062 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009063 if (Kind == OMPC_SCHEDULE_unknown) {
9064 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009065 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9066 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9067 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9068 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9069 Exclude);
9070 } else {
9071 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9072 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009073 }
9074 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9075 << Values << getOpenMPClauseName(OMPC_schedule);
9076 return nullptr;
9077 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009078 // OpenMP, 2.7.1, Loop Construct, Restrictions
9079 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9080 // schedule(guided).
9081 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9082 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9083 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9084 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9085 diag::err_omp_schedule_nonmonotonic_static);
9086 return nullptr;
9087 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009088 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009089 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009090 if (ChunkSize) {
9091 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9092 !ChunkSize->isInstantiationDependent() &&
9093 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009094 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009095 ExprResult Val =
9096 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9097 if (Val.isInvalid())
9098 return nullptr;
9099
9100 ValExpr = Val.get();
9101
9102 // OpenMP [2.7.1, Restrictions]
9103 // chunk_size must be a loop invariant integer expression with a positive
9104 // value.
9105 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009106 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9107 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9108 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009109 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009110 return nullptr;
9111 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009112 } else if (getOpenMPCaptureRegionForClause(
9113 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9114 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009115 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009116 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009117 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009118 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9119 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009120 }
9121 }
9122 }
9123
Alexey Bataev6402bca2015-12-28 07:25:51 +00009124 return new (Context)
9125 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009126 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009127}
9128
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009129OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9130 SourceLocation StartLoc,
9131 SourceLocation EndLoc) {
9132 OMPClause *Res = nullptr;
9133 switch (Kind) {
9134 case OMPC_ordered:
9135 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9136 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009137 case OMPC_nowait:
9138 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9139 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009140 case OMPC_untied:
9141 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9142 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009143 case OMPC_mergeable:
9144 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9145 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009146 case OMPC_read:
9147 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9148 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009149 case OMPC_write:
9150 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9151 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009152 case OMPC_update:
9153 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9154 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009155 case OMPC_capture:
9156 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9157 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009158 case OMPC_seq_cst:
9159 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9160 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009161 case OMPC_threads:
9162 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9163 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009164 case OMPC_simd:
9165 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9166 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009167 case OMPC_nogroup:
9168 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9169 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009170 case OMPC_unified_address:
9171 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9172 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009173 case OMPC_unified_shared_memory:
9174 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9175 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009176 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009177 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009178 case OMPC_num_threads:
9179 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009180 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009181 case OMPC_collapse:
9182 case OMPC_schedule:
9183 case OMPC_private:
9184 case OMPC_firstprivate:
9185 case OMPC_lastprivate:
9186 case OMPC_shared:
9187 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009188 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009189 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009190 case OMPC_linear:
9191 case OMPC_aligned:
9192 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009193 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009194 case OMPC_default:
9195 case OMPC_proc_bind:
9196 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009197 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009198 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009199 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009200 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009201 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009202 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009203 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009204 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009205 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009206 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009207 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009208 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009209 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009210 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009211 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009212 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009213 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009214 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009215 llvm_unreachable("Clause is not allowed.");
9216 }
9217 return Res;
9218}
9219
Alexey Bataev236070f2014-06-20 11:19:47 +00009220OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9221 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009222 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009223 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9224}
9225
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009226OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9227 SourceLocation EndLoc) {
9228 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9229}
9230
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009231OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9232 SourceLocation EndLoc) {
9233 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9234}
9235
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009236OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9237 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009238 return new (Context) OMPReadClause(StartLoc, EndLoc);
9239}
9240
Alexey Bataevdea47612014-07-23 07:46:59 +00009241OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9242 SourceLocation EndLoc) {
9243 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9244}
9245
Alexey Bataev67a4f222014-07-23 10:25:33 +00009246OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9247 SourceLocation EndLoc) {
9248 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9249}
9250
Alexey Bataev459dec02014-07-24 06:46:57 +00009251OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9252 SourceLocation EndLoc) {
9253 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9254}
9255
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009256OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9257 SourceLocation EndLoc) {
9258 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9259}
9260
Alexey Bataev346265e2015-09-25 10:37:12 +00009261OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9262 SourceLocation EndLoc) {
9263 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9264}
9265
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009266OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9267 SourceLocation EndLoc) {
9268 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9269}
9270
Alexey Bataevb825de12015-12-07 10:51:44 +00009271OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9272 SourceLocation EndLoc) {
9273 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9274}
9275
Kelvin Li1408f912018-09-26 04:28:39 +00009276OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9277 SourceLocation EndLoc) {
9278 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9279}
9280
Patrick Lyster4a370b92018-10-01 13:47:43 +00009281OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9282 SourceLocation EndLoc) {
9283 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9284}
9285
Alexey Bataevc5e02582014-06-16 07:08:35 +00009286OMPClause *Sema::ActOnOpenMPVarListClause(
9287 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9288 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9289 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009290 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009291 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9292 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9293 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009294 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009295 switch (Kind) {
9296 case OMPC_private:
9297 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9298 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009299 case OMPC_firstprivate:
9300 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9301 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009302 case OMPC_lastprivate:
9303 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9304 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009305 case OMPC_shared:
9306 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9307 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009308 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009309 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9310 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009311 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009312 case OMPC_task_reduction:
9313 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9314 EndLoc, ReductionIdScopeSpec,
9315 ReductionId);
9316 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009317 case OMPC_in_reduction:
9318 Res =
9319 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9320 EndLoc, ReductionIdScopeSpec, ReductionId);
9321 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009322 case OMPC_linear:
9323 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009324 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009325 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009326 case OMPC_aligned:
9327 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9328 ColonLoc, EndLoc);
9329 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009330 case OMPC_copyin:
9331 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9332 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009333 case OMPC_copyprivate:
9334 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9335 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009336 case OMPC_flush:
9337 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9338 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009339 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009340 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009341 StartLoc, LParenLoc, EndLoc);
9342 break;
9343 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009344 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9345 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9346 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009347 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009348 case OMPC_to:
9349 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9350 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009351 case OMPC_from:
9352 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9353 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009354 case OMPC_use_device_ptr:
9355 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9356 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009357 case OMPC_is_device_ptr:
9358 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9359 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009360 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009361 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009362 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009363 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009364 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009365 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009366 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009367 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009368 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009369 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009370 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009371 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009372 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009373 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009374 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009375 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009376 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009377 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009378 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009379 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009380 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009381 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009382 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009383 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009384 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009385 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009386 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009387 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009388 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009389 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009390 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009391 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009392 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009393 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009394 case OMPC_unified_shared_memory:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009395 llvm_unreachable("Clause is not allowed.");
9396 }
9397 return Res;
9398}
9399
Alexey Bataev90c228f2016-02-08 09:29:13 +00009400ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009401 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009402 ExprResult Res = BuildDeclRefExpr(
9403 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9404 if (!Res.isUsable())
9405 return ExprError();
9406 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9407 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9408 if (!Res.isUsable())
9409 return ExprError();
9410 }
9411 if (VK != VK_LValue && Res.get()->isGLValue()) {
9412 Res = DefaultLvalueConversion(Res.get());
9413 if (!Res.isUsable())
9414 return ExprError();
9415 }
9416 return Res;
9417}
9418
Alexey Bataev60da77e2016-02-29 05:54:20 +00009419static std::pair<ValueDecl *, bool>
9420getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9421 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009422 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9423 RefExpr->containsUnexpandedParameterPack())
9424 return std::make_pair(nullptr, true);
9425
Alexey Bataevd985eda2016-02-10 11:29:16 +00009426 // OpenMP [3.1, C/C++]
9427 // A list item is a variable name.
9428 // OpenMP [2.9.3.3, Restrictions, p.1]
9429 // A variable that is part of another variable (as an array or
9430 // structure element) cannot appear in a private clause.
9431 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009432 enum {
9433 NoArrayExpr = -1,
9434 ArraySubscript = 0,
9435 OMPArraySection = 1
9436 } IsArrayExpr = NoArrayExpr;
9437 if (AllowArraySection) {
9438 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009439 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009440 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9441 Base = TempASE->getBase()->IgnoreParenImpCasts();
9442 RefExpr = Base;
9443 IsArrayExpr = ArraySubscript;
9444 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009445 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009446 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9447 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9448 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9449 Base = TempASE->getBase()->IgnoreParenImpCasts();
9450 RefExpr = Base;
9451 IsArrayExpr = OMPArraySection;
9452 }
9453 }
9454 ELoc = RefExpr->getExprLoc();
9455 ERange = RefExpr->getSourceRange();
9456 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009457 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9458 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9459 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9460 (S.getCurrentThisType().isNull() || !ME ||
9461 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9462 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009463 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009464 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9465 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009466 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009467 S.Diag(ELoc,
9468 AllowArraySection
9469 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9470 : diag::err_omp_expected_var_name_member_expr)
9471 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9472 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009473 return std::make_pair(nullptr, false);
9474 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009475 return std::make_pair(
9476 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009477}
9478
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009479OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9480 SourceLocation StartLoc,
9481 SourceLocation LParenLoc,
9482 SourceLocation EndLoc) {
9483 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009484 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009485 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009486 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009487 SourceLocation ELoc;
9488 SourceRange ERange;
9489 Expr *SimpleRefExpr = RefExpr;
9490 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009491 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009492 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009493 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009494 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009495 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009496 ValueDecl *D = Res.first;
9497 if (!D)
9498 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009499
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009500 QualType Type = D->getType();
9501 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009502
9503 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9504 // A variable that appears in a private clause must not have an incomplete
9505 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009506 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009507 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009508 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009509
Alexey Bataev758e55e2013-09-06 18:03:48 +00009510 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9511 // in a Construct]
9512 // Variables with the predetermined data-sharing attributes may not be
9513 // listed in data-sharing attributes clauses, except for the cases
9514 // listed below. For these exceptions only, listing a predetermined
9515 // variable in a data-sharing attribute clause is allowed and overrides
9516 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009517 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009518 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009519 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9520 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009521 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009522 continue;
9523 }
9524
Alexey Bataeve3727102018-04-18 15:57:46 +00009525 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009526 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009527 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009528 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009529 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9530 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009531 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009532 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009533 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009534 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009535 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009536 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009537 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009538 continue;
9539 }
9540
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009541 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9542 // A list item cannot appear in both a map clause and a data-sharing
9543 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009544 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009545 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009546 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009547 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009548 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9549 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9550 ConflictKind = WhereFoundClauseKind;
9551 return true;
9552 })) {
9553 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009554 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009555 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009556 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009557 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009558 continue;
9559 }
9560 }
9561
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009562 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9563 // A variable of class type (or array thereof) that appears in a private
9564 // clause requires an accessible, unambiguous default constructor for the
9565 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009566 // Generate helper private variable and initialize it with the default
9567 // value. The address of the original variable is replaced by the address of
9568 // the new private variable in CodeGen. This new variable is not added to
9569 // IdResolver, so the code in the OpenMP region uses original variable for
9570 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009571 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009572 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009573 buildVarDecl(*this, ELoc, Type, D->getName(),
9574 D->hasAttrs() ? &D->getAttrs() : nullptr,
9575 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009576 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009577 if (VDPrivate->isInvalidDecl())
9578 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009579 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009580 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009581
Alexey Bataev90c228f2016-02-08 09:29:13 +00009582 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009583 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009584 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009585 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009586 Vars.push_back((VD || CurContext->isDependentContext())
9587 ? RefExpr->IgnoreParens()
9588 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009589 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009590 }
9591
Alexey Bataeved09d242014-05-28 05:53:51 +00009592 if (Vars.empty())
9593 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009594
Alexey Bataev03b340a2014-10-21 03:16:40 +00009595 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9596 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009597}
9598
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009599namespace {
9600class DiagsUninitializedSeveretyRAII {
9601private:
9602 DiagnosticsEngine &Diags;
9603 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009604 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009605
9606public:
9607 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9608 bool IsIgnored)
9609 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9610 if (!IsIgnored) {
9611 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9612 /*Map*/ diag::Severity::Ignored, Loc);
9613 }
9614 }
9615 ~DiagsUninitializedSeveretyRAII() {
9616 if (!IsIgnored)
9617 Diags.popMappings(SavedLoc);
9618 }
9619};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009620}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009621
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009622OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9623 SourceLocation StartLoc,
9624 SourceLocation LParenLoc,
9625 SourceLocation EndLoc) {
9626 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009627 SmallVector<Expr *, 8> PrivateCopies;
9628 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009629 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009630 bool IsImplicitClause =
9631 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009632 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009633
Alexey Bataeve3727102018-04-18 15:57:46 +00009634 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009635 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009636 SourceLocation ELoc;
9637 SourceRange ERange;
9638 Expr *SimpleRefExpr = RefExpr;
9639 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009640 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009641 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009642 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009643 PrivateCopies.push_back(nullptr);
9644 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009645 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009646 ValueDecl *D = Res.first;
9647 if (!D)
9648 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009649
Alexey Bataev60da77e2016-02-29 05:54:20 +00009650 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009651 QualType Type = D->getType();
9652 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009653
9654 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9655 // A variable that appears in a private clause must not have an incomplete
9656 // type or a reference type.
9657 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009658 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009659 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009660 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009661
9662 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9663 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009664 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009665 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009666 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009667
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009668 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009669 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009670 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009671 DSAStackTy::DSAVarData DVar =
9672 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009673 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009674 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009675 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009676 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9677 // A list item that specifies a given variable may not appear in more
9678 // than one clause on the same directive, except that a variable may be
9679 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009680 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9681 // A list item may appear in a firstprivate or lastprivate clause but not
9682 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009683 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009684 (isOpenMPDistributeDirective(CurrDir) ||
9685 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009686 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009687 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009688 << getOpenMPClauseName(DVar.CKind)
9689 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009690 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009691 continue;
9692 }
9693
9694 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9695 // in a Construct]
9696 // Variables with the predetermined data-sharing attributes may not be
9697 // listed in data-sharing attributes clauses, except for the cases
9698 // listed below. For these exceptions only, listing a predetermined
9699 // variable in a data-sharing attribute clause is allowed and overrides
9700 // the variable's predetermined data-sharing attributes.
9701 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9702 // in a Construct, C/C++, p.2]
9703 // Variables with const-qualified type having no mutable member may be
9704 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009705 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009706 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9707 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009708 << getOpenMPClauseName(DVar.CKind)
9709 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009710 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009711 continue;
9712 }
9713
9714 // OpenMP [2.9.3.4, Restrictions, p.2]
9715 // A list item that is private within a parallel region must not appear
9716 // in a firstprivate clause on a worksharing construct if any of the
9717 // worksharing regions arising from the worksharing construct ever bind
9718 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009719 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9720 // A list item that is private within a teams region must not appear in a
9721 // firstprivate clause on a distribute construct if any of the distribute
9722 // regions arising from the distribute construct ever bind to any of the
9723 // teams regions arising from the teams construct.
9724 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9725 // A list item that appears in a reduction clause of a teams construct
9726 // must not appear in a firstprivate clause on a distribute construct if
9727 // any of the distribute regions arising from the distribute construct
9728 // ever bind to any of the teams regions arising from the teams construct.
9729 if ((isOpenMPWorksharingDirective(CurrDir) ||
9730 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009731 !isOpenMPParallelDirective(CurrDir) &&
9732 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009733 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009734 if (DVar.CKind != OMPC_shared &&
9735 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009736 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009737 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009738 Diag(ELoc, diag::err_omp_required_access)
9739 << getOpenMPClauseName(OMPC_firstprivate)
9740 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009741 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009742 continue;
9743 }
9744 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009745 // OpenMP [2.9.3.4, Restrictions, p.3]
9746 // A list item that appears in a reduction clause of a parallel construct
9747 // must not appear in a firstprivate clause on a worksharing or task
9748 // construct if any of the worksharing or task regions arising from the
9749 // worksharing or task construct ever bind to any of the parallel regions
9750 // arising from the parallel construct.
9751 // OpenMP [2.9.3.4, Restrictions, p.4]
9752 // A list item that appears in a reduction clause in worksharing
9753 // construct must not appear in a firstprivate clause in a task construct
9754 // encountered during execution of any of the worksharing regions arising
9755 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009756 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009757 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009758 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9759 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009760 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009761 isOpenMPWorksharingDirective(K) ||
9762 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009763 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009764 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009765 if (DVar.CKind == OMPC_reduction &&
9766 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009767 isOpenMPWorksharingDirective(DVar.DKind) ||
9768 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009769 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9770 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009771 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009772 continue;
9773 }
9774 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009775
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009776 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9777 // A list item cannot appear in both a map clause and a data-sharing
9778 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009779 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009780 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009781 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009782 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009783 [&ConflictKind](
9784 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9785 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009786 ConflictKind = WhereFoundClauseKind;
9787 return true;
9788 })) {
9789 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009790 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009791 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009792 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009793 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009794 continue;
9795 }
9796 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009797 }
9798
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009799 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009800 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009801 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009802 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9803 << getOpenMPClauseName(OMPC_firstprivate) << Type
9804 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9805 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009806 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009807 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009808 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009809 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009810 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009811 continue;
9812 }
9813
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009814 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009815 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009816 buildVarDecl(*this, ELoc, Type, D->getName(),
9817 D->hasAttrs() ? &D->getAttrs() : nullptr,
9818 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009819 // Generate helper private variable and initialize it with the value of the
9820 // original variable. The address of the original variable is replaced by
9821 // the address of the new private variable in the CodeGen. This new variable
9822 // is not added to IdResolver, so the code in the OpenMP region uses
9823 // original variable for proper diagnostics and variable capturing.
9824 Expr *VDInitRefExpr = nullptr;
9825 // For arrays generate initializer for single element and replace it by the
9826 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009827 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009828 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009829 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009830 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009831 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009832 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009833 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9834 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009835 InitializedEntity Entity =
9836 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009837 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9838
9839 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9840 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9841 if (Result.isInvalid())
9842 VDPrivate->setInvalidDecl();
9843 else
9844 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009845 // Remove temp variable declaration.
9846 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009847 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00009848 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9849 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +00009850 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9851 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009852 AddInitializerToDecl(VDPrivate,
9853 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009854 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009855 }
9856 if (VDPrivate->isInvalidDecl()) {
9857 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009858 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009859 diag::note_omp_task_predetermined_firstprivate_here);
9860 }
9861 continue;
9862 }
9863 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009864 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009865 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9866 RefExpr->getExprLoc());
9867 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009868 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009869 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009870 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009871 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009872 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +00009873 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009874 ExprCaptures.push_back(Ref->getDecl());
9875 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009876 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009877 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009878 Vars.push_back((VD || CurContext->isDependentContext())
9879 ? RefExpr->IgnoreParens()
9880 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009881 PrivateCopies.push_back(VDPrivateRefExpr);
9882 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009883 }
9884
Alexey Bataeved09d242014-05-28 05:53:51 +00009885 if (Vars.empty())
9886 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009887
9888 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009889 Vars, PrivateCopies, Inits,
9890 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009891}
9892
Alexander Musman1bb328c2014-06-04 13:06:39 +00009893OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9894 SourceLocation StartLoc,
9895 SourceLocation LParenLoc,
9896 SourceLocation EndLoc) {
9897 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009898 SmallVector<Expr *, 8> SrcExprs;
9899 SmallVector<Expr *, 8> DstExprs;
9900 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009901 SmallVector<Decl *, 4> ExprCaptures;
9902 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +00009903 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009904 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009905 SourceLocation ELoc;
9906 SourceRange ERange;
9907 Expr *SimpleRefExpr = RefExpr;
9908 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009909 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009910 // It will be analyzed later.
9911 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009912 SrcExprs.push_back(nullptr);
9913 DstExprs.push_back(nullptr);
9914 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009915 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009916 ValueDecl *D = Res.first;
9917 if (!D)
9918 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009919
Alexey Bataev74caaf22016-02-20 04:09:36 +00009920 QualType Type = D->getType();
9921 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009922
9923 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9924 // A variable that appears in a lastprivate clause must not have an
9925 // incomplete type or a reference type.
9926 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009927 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009928 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009929 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009930
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009931 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009932 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9933 // in a Construct]
9934 // Variables with the predetermined data-sharing attributes may not be
9935 // listed in data-sharing attributes clauses, except for the cases
9936 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009937 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9938 // A list item may appear in a firstprivate or lastprivate clause but not
9939 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +00009940 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009941 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009942 (isOpenMPDistributeDirective(CurrDir) ||
9943 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009944 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9945 Diag(ELoc, diag::err_omp_wrong_dsa)
9946 << getOpenMPClauseName(DVar.CKind)
9947 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009948 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009949 continue;
9950 }
9951
Alexey Bataevf29276e2014-06-18 04:14:57 +00009952 // OpenMP [2.14.3.5, Restrictions, p.2]
9953 // A list item that is private within a parallel region, or that appears in
9954 // the reduction clause of a parallel construct, must not appear in a
9955 // lastprivate clause on a worksharing construct if any of the corresponding
9956 // worksharing regions ever binds to any of the corresponding parallel
9957 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009958 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009959 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009960 !isOpenMPParallelDirective(CurrDir) &&
9961 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009962 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009963 if (DVar.CKind != OMPC_shared) {
9964 Diag(ELoc, diag::err_omp_required_access)
9965 << getOpenMPClauseName(OMPC_lastprivate)
9966 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009967 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009968 continue;
9969 }
9970 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009971
Alexander Musman1bb328c2014-06-04 13:06:39 +00009972 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009973 // A variable of class type (or array thereof) that appears in a
9974 // lastprivate clause requires an accessible, unambiguous default
9975 // constructor for the class type, unless the list item is also specified
9976 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009977 // A variable of class type (or array thereof) that appears in a
9978 // lastprivate clause requires an accessible, unambiguous copy assignment
9979 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009980 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009981 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
9982 Type.getUnqualifiedType(), ".lastprivate.src",
9983 D->hasAttrs() ? &D->getAttrs() : nullptr);
9984 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +00009985 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009986 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009987 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009988 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +00009989 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009990 // For arrays generate assignment operation for single element and replace
9991 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009992 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
9993 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009994 if (AssignmentOp.isInvalid())
9995 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009996 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009997 /*DiscardedValue=*/true);
9998 if (AssignmentOp.isInvalid())
9999 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010000
Alexey Bataev74caaf22016-02-20 04:09:36 +000010001 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010002 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010003 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010004 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010005 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010006 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010007 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010008 ExprCaptures.push_back(Ref->getDecl());
10009 }
10010 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010011 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010012 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010013 ExprResult RefRes = DefaultLvalueConversion(Ref);
10014 if (!RefRes.isUsable())
10015 continue;
10016 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010017 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10018 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010019 if (!PostUpdateRes.isUsable())
10020 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010021 ExprPostUpdates.push_back(
10022 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010023 }
10024 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010025 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010026 Vars.push_back((VD || CurContext->isDependentContext())
10027 ? RefExpr->IgnoreParens()
10028 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010029 SrcExprs.push_back(PseudoSrcExpr);
10030 DstExprs.push_back(PseudoDstExpr);
10031 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010032 }
10033
10034 if (Vars.empty())
10035 return nullptr;
10036
10037 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010038 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010039 buildPreInits(Context, ExprCaptures),
10040 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010041}
10042
Alexey Bataev758e55e2013-09-06 18:03:48 +000010043OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10044 SourceLocation StartLoc,
10045 SourceLocation LParenLoc,
10046 SourceLocation EndLoc) {
10047 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010048 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010049 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010050 SourceLocation ELoc;
10051 SourceRange ERange;
10052 Expr *SimpleRefExpr = RefExpr;
10053 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010054 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010055 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010056 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010057 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010058 ValueDecl *D = Res.first;
10059 if (!D)
10060 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010061
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010062 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010063 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10064 // in a Construct]
10065 // Variables with the predetermined data-sharing attributes may not be
10066 // listed in data-sharing attributes clauses, except for the cases
10067 // listed below. For these exceptions only, listing a predetermined
10068 // variable in a data-sharing attribute clause is allowed and overrides
10069 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010070 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010071 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10072 DVar.RefExpr) {
10073 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10074 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010075 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010076 continue;
10077 }
10078
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010079 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010080 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010081 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010082 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010083 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10084 ? RefExpr->IgnoreParens()
10085 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010086 }
10087
Alexey Bataeved09d242014-05-28 05:53:51 +000010088 if (Vars.empty())
10089 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010090
10091 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10092}
10093
Alexey Bataevc5e02582014-06-16 07:08:35 +000010094namespace {
10095class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10096 DSAStackTy *Stack;
10097
10098public:
10099 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010100 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10101 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010102 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10103 return false;
10104 if (DVar.CKind != OMPC_unknown)
10105 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010106 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010107 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010108 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010109 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010110 }
10111 return false;
10112 }
10113 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010114 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010115 if (Child && Visit(Child))
10116 return true;
10117 }
10118 return false;
10119 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010120 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010121};
Alexey Bataev23b69422014-06-18 07:08:49 +000010122} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010123
Alexey Bataev60da77e2016-02-29 05:54:20 +000010124namespace {
10125// Transform MemberExpression for specified FieldDecl of current class to
10126// DeclRefExpr to specified OMPCapturedExprDecl.
10127class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10128 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010129 ValueDecl *Field = nullptr;
10130 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010131
10132public:
10133 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10134 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10135
10136 ExprResult TransformMemberExpr(MemberExpr *E) {
10137 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10138 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010139 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010140 return CapturedExpr;
10141 }
10142 return BaseTransform::TransformMemberExpr(E);
10143 }
10144 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10145};
10146} // namespace
10147
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010148template <typename T, typename U>
10149static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10150 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010151 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010152 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010153 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010154 return Res;
10155 }
10156 }
10157 return T();
10158}
10159
Alexey Bataev43b90b72018-09-12 16:31:59 +000010160static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10161 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10162
10163 for (auto RD : D->redecls()) {
10164 // Don't bother with extra checks if we already know this one isn't visible.
10165 if (RD == D)
10166 continue;
10167
10168 auto ND = cast<NamedDecl>(RD);
10169 if (LookupResult::isVisible(SemaRef, ND))
10170 return ND;
10171 }
10172
10173 return nullptr;
10174}
10175
10176static void
10177argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10178 SourceLocation Loc, QualType Ty,
10179 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10180 // Find all of the associated namespaces and classes based on the
10181 // arguments we have.
10182 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10183 Sema::AssociatedClassSet AssociatedClasses;
10184 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10185 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10186 AssociatedClasses);
10187
10188 // C++ [basic.lookup.argdep]p3:
10189 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10190 // and let Y be the lookup set produced by argument dependent
10191 // lookup (defined as follows). If X contains [...] then Y is
10192 // empty. Otherwise Y is the set of declarations found in the
10193 // namespaces associated with the argument types as described
10194 // below. The set of declarations found by the lookup of the name
10195 // is the union of X and Y.
10196 //
10197 // Here, we compute Y and add its members to the overloaded
10198 // candidate set.
10199 for (auto *NS : AssociatedNamespaces) {
10200 // When considering an associated namespace, the lookup is the
10201 // same as the lookup performed when the associated namespace is
10202 // used as a qualifier (3.4.3.2) except that:
10203 //
10204 // -- Any using-directives in the associated namespace are
10205 // ignored.
10206 //
10207 // -- Any namespace-scope friend functions declared in
10208 // associated classes are visible within their respective
10209 // namespaces even if they are not visible during an ordinary
10210 // lookup (11.4).
10211 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10212 for (auto *D : R) {
10213 auto *Underlying = D;
10214 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10215 Underlying = USD->getTargetDecl();
10216
10217 if (!isa<OMPDeclareReductionDecl>(Underlying))
10218 continue;
10219
10220 if (!SemaRef.isVisible(D)) {
10221 D = findAcceptableDecl(SemaRef, D);
10222 if (!D)
10223 continue;
10224 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10225 Underlying = USD->getTargetDecl();
10226 }
10227 Lookups.emplace_back();
10228 Lookups.back().addDecl(Underlying);
10229 }
10230 }
10231}
10232
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010233static ExprResult
10234buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10235 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10236 const DeclarationNameInfo &ReductionId, QualType Ty,
10237 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10238 if (ReductionIdScopeSpec.isInvalid())
10239 return ExprError();
10240 SmallVector<UnresolvedSet<8>, 4> Lookups;
10241 if (S) {
10242 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10243 Lookup.suppressDiagnostics();
10244 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010245 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010246 do {
10247 S = S->getParent();
10248 } while (S && !S->isDeclScope(D));
10249 if (S)
10250 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010251 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010252 Lookups.back().append(Lookup.begin(), Lookup.end());
10253 Lookup.clear();
10254 }
10255 } else if (auto *ULE =
10256 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10257 Lookups.push_back(UnresolvedSet<8>());
10258 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010259 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010260 if (D == PrevD)
10261 Lookups.push_back(UnresolvedSet<8>());
10262 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10263 Lookups.back().addDecl(DRD);
10264 PrevD = D;
10265 }
10266 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010267 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10268 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010269 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010270 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010271 return !D->isInvalidDecl() &&
10272 (D->getType()->isDependentType() ||
10273 D->getType()->isInstantiationDependentType() ||
10274 D->getType()->containsUnexpandedParameterPack());
10275 })) {
10276 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010277 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010278 if (Set.empty())
10279 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010280 ResSet.append(Set.begin(), Set.end());
10281 // The last item marks the end of all declarations at the specified scope.
10282 ResSet.addDecl(Set[Set.size() - 1]);
10283 }
10284 return UnresolvedLookupExpr::Create(
10285 SemaRef.Context, /*NamingClass=*/nullptr,
10286 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10287 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10288 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010289 // Lookup inside the classes.
10290 // C++ [over.match.oper]p3:
10291 // For a unary operator @ with an operand of a type whose
10292 // cv-unqualified version is T1, and for a binary operator @ with
10293 // a left operand of a type whose cv-unqualified version is T1 and
10294 // a right operand of a type whose cv-unqualified version is T2,
10295 // three sets of candidate functions, designated member
10296 // candidates, non-member candidates and built-in candidates, are
10297 // constructed as follows:
10298 // -- If T1 is a complete class type or a class currently being
10299 // defined, the set of member candidates is the result of the
10300 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10301 // the set of member candidates is empty.
10302 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10303 Lookup.suppressDiagnostics();
10304 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10305 // Complete the type if it can be completed.
10306 // If the type is neither complete nor being defined, bail out now.
10307 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10308 TyRec->getDecl()->getDefinition()) {
10309 Lookup.clear();
10310 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10311 if (Lookup.empty()) {
10312 Lookups.emplace_back();
10313 Lookups.back().append(Lookup.begin(), Lookup.end());
10314 }
10315 }
10316 }
10317 // Perform ADL.
10318 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010319 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10320 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10321 if (!D->isInvalidDecl() &&
10322 SemaRef.Context.hasSameType(D->getType(), Ty))
10323 return D;
10324 return nullptr;
10325 }))
10326 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10327 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10328 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10329 if (!D->isInvalidDecl() &&
10330 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10331 !Ty.isMoreQualifiedThan(D->getType()))
10332 return D;
10333 return nullptr;
10334 })) {
10335 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10336 /*DetectVirtual=*/false);
10337 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10338 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10339 VD->getType().getUnqualifiedType()))) {
10340 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10341 /*DiagID=*/0) !=
10342 Sema::AR_inaccessible) {
10343 SemaRef.BuildBasePathArray(Paths, BasePath);
10344 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10345 }
10346 }
10347 }
10348 }
10349 if (ReductionIdScopeSpec.isSet()) {
10350 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10351 return ExprError();
10352 }
10353 return ExprEmpty();
10354}
10355
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010356namespace {
10357/// Data for the reduction-based clauses.
10358struct ReductionData {
10359 /// List of original reduction items.
10360 SmallVector<Expr *, 8> Vars;
10361 /// List of private copies of the reduction items.
10362 SmallVector<Expr *, 8> Privates;
10363 /// LHS expressions for the reduction_op expressions.
10364 SmallVector<Expr *, 8> LHSs;
10365 /// RHS expressions for the reduction_op expressions.
10366 SmallVector<Expr *, 8> RHSs;
10367 /// Reduction operation expression.
10368 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010369 /// Taskgroup descriptors for the corresponding reduction items in
10370 /// in_reduction clauses.
10371 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010372 /// List of captures for clause.
10373 SmallVector<Decl *, 4> ExprCaptures;
10374 /// List of postupdate expressions.
10375 SmallVector<Expr *, 4> ExprPostUpdates;
10376 ReductionData() = delete;
10377 /// Reserves required memory for the reduction data.
10378 ReductionData(unsigned Size) {
10379 Vars.reserve(Size);
10380 Privates.reserve(Size);
10381 LHSs.reserve(Size);
10382 RHSs.reserve(Size);
10383 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010384 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010385 ExprCaptures.reserve(Size);
10386 ExprPostUpdates.reserve(Size);
10387 }
10388 /// Stores reduction item and reduction operation only (required for dependent
10389 /// reduction item).
10390 void push(Expr *Item, Expr *ReductionOp) {
10391 Vars.emplace_back(Item);
10392 Privates.emplace_back(nullptr);
10393 LHSs.emplace_back(nullptr);
10394 RHSs.emplace_back(nullptr);
10395 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010396 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010397 }
10398 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010399 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10400 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010401 Vars.emplace_back(Item);
10402 Privates.emplace_back(Private);
10403 LHSs.emplace_back(LHS);
10404 RHSs.emplace_back(RHS);
10405 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010406 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010407 }
10408};
10409} // namespace
10410
Alexey Bataeve3727102018-04-18 15:57:46 +000010411static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010412 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10413 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10414 const Expr *Length = OASE->getLength();
10415 if (Length == nullptr) {
10416 // For array sections of the form [1:] or [:], we would need to analyze
10417 // the lower bound...
10418 if (OASE->getColonLoc().isValid())
10419 return false;
10420
10421 // This is an array subscript which has implicit length 1!
10422 SingleElement = true;
10423 ArraySizes.push_back(llvm::APSInt::get(1));
10424 } else {
10425 llvm::APSInt ConstantLengthValue;
10426 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10427 return false;
10428
10429 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10430 ArraySizes.push_back(ConstantLengthValue);
10431 }
10432
10433 // Get the base of this array section and walk up from there.
10434 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10435
10436 // We require length = 1 for all array sections except the right-most to
10437 // guarantee that the memory region is contiguous and has no holes in it.
10438 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10439 Length = TempOASE->getLength();
10440 if (Length == nullptr) {
10441 // For array sections of the form [1:] or [:], we would need to analyze
10442 // the lower bound...
10443 if (OASE->getColonLoc().isValid())
10444 return false;
10445
10446 // This is an array subscript which has implicit length 1!
10447 ArraySizes.push_back(llvm::APSInt::get(1));
10448 } else {
10449 llvm::APSInt ConstantLengthValue;
10450 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10451 ConstantLengthValue.getSExtValue() != 1)
10452 return false;
10453
10454 ArraySizes.push_back(ConstantLengthValue);
10455 }
10456 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10457 }
10458
10459 // If we have a single element, we don't need to add the implicit lengths.
10460 if (!SingleElement) {
10461 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10462 // Has implicit length 1!
10463 ArraySizes.push_back(llvm::APSInt::get(1));
10464 Base = TempASE->getBase()->IgnoreParenImpCasts();
10465 }
10466 }
10467
10468 // This array section can be privatized as a single value or as a constant
10469 // sized array.
10470 return true;
10471}
10472
Alexey Bataeve3727102018-04-18 15:57:46 +000010473static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010474 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10475 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10476 SourceLocation ColonLoc, SourceLocation EndLoc,
10477 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010478 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010479 DeclarationName DN = ReductionId.getName();
10480 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010481 BinaryOperatorKind BOK = BO_Comma;
10482
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010483 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010484 // OpenMP [2.14.3.6, reduction clause]
10485 // C
10486 // reduction-identifier is either an identifier or one of the following
10487 // operators: +, -, *, &, |, ^, && and ||
10488 // C++
10489 // reduction-identifier is either an id-expression or one of the following
10490 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010491 switch (OOK) {
10492 case OO_Plus:
10493 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010494 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010495 break;
10496 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010497 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010498 break;
10499 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010500 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010501 break;
10502 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010503 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010504 break;
10505 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010506 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010507 break;
10508 case OO_AmpAmp:
10509 BOK = BO_LAnd;
10510 break;
10511 case OO_PipePipe:
10512 BOK = BO_LOr;
10513 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010514 case OO_New:
10515 case OO_Delete:
10516 case OO_Array_New:
10517 case OO_Array_Delete:
10518 case OO_Slash:
10519 case OO_Percent:
10520 case OO_Tilde:
10521 case OO_Exclaim:
10522 case OO_Equal:
10523 case OO_Less:
10524 case OO_Greater:
10525 case OO_LessEqual:
10526 case OO_GreaterEqual:
10527 case OO_PlusEqual:
10528 case OO_MinusEqual:
10529 case OO_StarEqual:
10530 case OO_SlashEqual:
10531 case OO_PercentEqual:
10532 case OO_CaretEqual:
10533 case OO_AmpEqual:
10534 case OO_PipeEqual:
10535 case OO_LessLess:
10536 case OO_GreaterGreater:
10537 case OO_LessLessEqual:
10538 case OO_GreaterGreaterEqual:
10539 case OO_EqualEqual:
10540 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010541 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010542 case OO_PlusPlus:
10543 case OO_MinusMinus:
10544 case OO_Comma:
10545 case OO_ArrowStar:
10546 case OO_Arrow:
10547 case OO_Call:
10548 case OO_Subscript:
10549 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010550 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010551 case NUM_OVERLOADED_OPERATORS:
10552 llvm_unreachable("Unexpected reduction identifier");
10553 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010554 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010555 if (II->isStr("max"))
10556 BOK = BO_GT;
10557 else if (II->isStr("min"))
10558 BOK = BO_LT;
10559 }
10560 break;
10561 }
10562 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010563 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010564 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010565 else
10566 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010567 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010568
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010569 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10570 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010571 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010572 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010573 // OpenMP [2.1, C/C++]
10574 // A list item is a variable or array section, subject to the restrictions
10575 // specified in Section 2.4 on page 42 and in each of the sections
10576 // describing clauses and directives for which a list appears.
10577 // OpenMP [2.14.3.3, Restrictions, p.1]
10578 // A variable that is part of another variable (as an array or
10579 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010580 if (!FirstIter && IR != ER)
10581 ++IR;
10582 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010583 SourceLocation ELoc;
10584 SourceRange ERange;
10585 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010586 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010587 /*AllowArraySection=*/true);
10588 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010589 // Try to find 'declare reduction' corresponding construct before using
10590 // builtin/overloaded operators.
10591 QualType Type = Context.DependentTy;
10592 CXXCastPath BasePath;
10593 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010594 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010595 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010596 Expr *ReductionOp = nullptr;
10597 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010598 (DeclareReductionRef.isUnset() ||
10599 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010600 ReductionOp = DeclareReductionRef.get();
10601 // It will be analyzed later.
10602 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010603 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010604 ValueDecl *D = Res.first;
10605 if (!D)
10606 continue;
10607
Alexey Bataev88202be2017-07-27 13:20:36 +000010608 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010609 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010610 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10611 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010612 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010613 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010614 } else if (OASE) {
10615 QualType BaseType =
10616 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10617 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010618 Type = ATy->getElementType();
10619 else
10620 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010621 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010622 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010623 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010624 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010625 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010626
Alexey Bataevc5e02582014-06-16 07:08:35 +000010627 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10628 // A variable that appears in a private clause must not have an incomplete
10629 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010630 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010631 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010632 continue;
10633 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010634 // A list item that appears in a reduction clause must not be
10635 // const-qualified.
10636 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010637 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010638 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010639 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10640 VarDecl::DeclarationOnly;
10641 S.Diag(D->getLocation(),
10642 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010643 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010644 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010645 continue;
10646 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010647
10648 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010649 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10650 // If a list-item is a reference type then it must bind to the same object
10651 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010652 if (!ASE && !OASE) {
10653 if (VD) {
10654 VarDecl *VDDef = VD->getDefinition();
10655 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10656 DSARefChecker Check(Stack);
10657 if (Check.Visit(VDDef->getInit())) {
10658 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10659 << getOpenMPClauseName(ClauseKind) << ERange;
10660 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10661 continue;
10662 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010663 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010664 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010665
Alexey Bataevbc529672018-09-28 19:33:14 +000010666 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10667 // in a Construct]
10668 // Variables with the predetermined data-sharing attributes may not be
10669 // listed in data-sharing attributes clauses, except for the cases
10670 // listed below. For these exceptions only, listing a predetermined
10671 // variable in a data-sharing attribute clause is allowed and overrides
10672 // the variable's predetermined data-sharing attributes.
10673 // OpenMP [2.14.3.6, Restrictions, p.3]
10674 // Any number of reduction clauses can be specified on the directive,
10675 // but a list item can appear only once in the reduction clauses for that
10676 // directive.
10677 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10678 if (DVar.CKind == OMPC_reduction) {
10679 S.Diag(ELoc, diag::err_omp_once_referenced)
10680 << getOpenMPClauseName(ClauseKind);
10681 if (DVar.RefExpr)
10682 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10683 continue;
10684 }
10685 if (DVar.CKind != OMPC_unknown) {
10686 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10687 << getOpenMPClauseName(DVar.CKind)
10688 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010689 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010690 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010691 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010692
10693 // OpenMP [2.14.3.6, Restrictions, p.1]
10694 // A list item that appears in a reduction clause of a worksharing
10695 // construct must be shared in the parallel regions to which any of the
10696 // worksharing regions arising from the worksharing construct bind.
10697 if (isOpenMPWorksharingDirective(CurrDir) &&
10698 !isOpenMPParallelDirective(CurrDir) &&
10699 !isOpenMPTeamsDirective(CurrDir)) {
10700 DVar = Stack->getImplicitDSA(D, true);
10701 if (DVar.CKind != OMPC_shared) {
10702 S.Diag(ELoc, diag::err_omp_required_access)
10703 << getOpenMPClauseName(OMPC_reduction)
10704 << getOpenMPClauseName(OMPC_shared);
10705 reportOriginalDsa(S, Stack, D, DVar);
10706 continue;
10707 }
10708 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010709 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010710
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010711 // Try to find 'declare reduction' corresponding construct before using
10712 // builtin/overloaded operators.
10713 CXXCastPath BasePath;
10714 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010715 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010716 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10717 if (DeclareReductionRef.isInvalid())
10718 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010719 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010720 (DeclareReductionRef.isUnset() ||
10721 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010722 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010723 continue;
10724 }
10725 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10726 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010727 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010728 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010729 << Type << ReductionIdRange;
10730 continue;
10731 }
10732
10733 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10734 // The type of a list item that appears in a reduction clause must be valid
10735 // for the reduction-identifier. For a max or min reduction in C, the type
10736 // of the list item must be an allowed arithmetic data type: char, int,
10737 // float, double, or _Bool, possibly modified with long, short, signed, or
10738 // unsigned. For a max or min reduction in C++, the type of the list item
10739 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10740 // double, or bool, possibly modified with long, short, signed, or unsigned.
10741 if (DeclareReductionRef.isUnset()) {
10742 if ((BOK == BO_GT || BOK == BO_LT) &&
10743 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010744 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10745 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010746 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010747 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010748 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10749 VarDecl::DeclarationOnly;
10750 S.Diag(D->getLocation(),
10751 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010752 << D;
10753 }
10754 continue;
10755 }
10756 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010757 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010758 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10759 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010760 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010761 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10762 VarDecl::DeclarationOnly;
10763 S.Diag(D->getLocation(),
10764 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010765 << D;
10766 }
10767 continue;
10768 }
10769 }
10770
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010771 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010772 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10773 D->hasAttrs() ? &D->getAttrs() : nullptr);
10774 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10775 D->hasAttrs() ? &D->getAttrs() : nullptr);
10776 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010777
10778 // Try if we can determine constant lengths for all array sections and avoid
10779 // the VLA.
10780 bool ConstantLengthOASE = false;
10781 if (OASE) {
10782 bool SingleElement;
10783 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010784 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010785 Context, OASE, SingleElement, ArraySizes);
10786
10787 // If we don't have a single element, we must emit a constant array type.
10788 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010789 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010790 PrivateTy = Context.getConstantArrayType(
10791 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010792 }
10793 }
10794
10795 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010796 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010797 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010798 if (!Context.getTargetInfo().isVLASupported() &&
10799 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10800 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10801 S.Diag(ELoc, diag::note_vla_unsupported);
10802 continue;
10803 }
David Majnemer9d168222016-08-05 17:44:54 +000010804 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010805 // Create pseudo array type for private copy. The size for this array will
10806 // be generated during codegen.
10807 // For array subscripts or single variables Private Ty is the same as Type
10808 // (type of the variable or single array element).
10809 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010810 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010811 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010812 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010813 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000010814 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010815 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010816 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010817 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000010818 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010819 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10820 D->hasAttrs() ? &D->getAttrs() : nullptr,
10821 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010822 // Add initializer for private variable.
10823 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010824 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10825 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010826 if (DeclareReductionRef.isUsable()) {
10827 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10828 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10829 if (DRD->getInitializer()) {
10830 Init = DRDRef;
10831 RHSVD->setInit(DRDRef);
10832 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010833 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010834 } else {
10835 switch (BOK) {
10836 case BO_Add:
10837 case BO_Xor:
10838 case BO_Or:
10839 case BO_LOr:
10840 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10841 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010842 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010843 break;
10844 case BO_Mul:
10845 case BO_LAnd:
10846 if (Type->isScalarType() || Type->isAnyComplexType()) {
10847 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010848 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010849 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010850 break;
10851 case BO_And: {
10852 // '&' reduction op - initializer is '~0'.
10853 QualType OrigType = Type;
10854 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10855 Type = ComplexTy->getElementType();
10856 if (Type->isRealFloatingType()) {
10857 llvm::APFloat InitValue =
10858 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10859 /*isIEEE=*/true);
10860 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10861 Type, ELoc);
10862 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010863 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010864 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10865 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10866 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10867 }
10868 if (Init && OrigType->isAnyComplexType()) {
10869 // Init = 0xFFFF + 0xFFFFi;
10870 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010871 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010872 }
10873 Type = OrigType;
10874 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010875 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010876 case BO_LT:
10877 case BO_GT: {
10878 // 'min' reduction op - initializer is 'Largest representable number in
10879 // the reduction list item type'.
10880 // 'max' reduction op - initializer is 'Least representable number in
10881 // the reduction list item type'.
10882 if (Type->isIntegerType() || Type->isPointerType()) {
10883 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000010884 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010885 QualType IntTy =
10886 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10887 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010888 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10889 : llvm::APInt::getMinValue(Size)
10890 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10891 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010892 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10893 if (Type->isPointerType()) {
10894 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010895 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010896 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010897 if (CastExpr.isInvalid())
10898 continue;
10899 Init = CastExpr.get();
10900 }
10901 } else if (Type->isRealFloatingType()) {
10902 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10903 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10904 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10905 Type, ELoc);
10906 }
10907 break;
10908 }
10909 case BO_PtrMemD:
10910 case BO_PtrMemI:
10911 case BO_MulAssign:
10912 case BO_Div:
10913 case BO_Rem:
10914 case BO_Sub:
10915 case BO_Shl:
10916 case BO_Shr:
10917 case BO_LE:
10918 case BO_GE:
10919 case BO_EQ:
10920 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010921 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010922 case BO_AndAssign:
10923 case BO_XorAssign:
10924 case BO_OrAssign:
10925 case BO_Assign:
10926 case BO_AddAssign:
10927 case BO_SubAssign:
10928 case BO_DivAssign:
10929 case BO_RemAssign:
10930 case BO_ShlAssign:
10931 case BO_ShrAssign:
10932 case BO_Comma:
10933 llvm_unreachable("Unexpected reduction operation");
10934 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010935 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010936 if (Init && DeclareReductionRef.isUnset())
10937 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10938 else if (!Init)
10939 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010940 if (RHSVD->isInvalidDecl())
10941 continue;
10942 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010943 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10944 << Type << ReductionIdRange;
10945 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10946 VarDecl::DeclarationOnly;
10947 S.Diag(D->getLocation(),
10948 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010949 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010950 continue;
10951 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010952 // Store initializer for single element in private copy. Will be used during
10953 // codegen.
10954 PrivateVD->setInit(RHSVD->getInit());
10955 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000010956 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010957 ExprResult ReductionOp;
10958 if (DeclareReductionRef.isUsable()) {
10959 QualType RedTy = DeclareReductionRef.get()->getType();
10960 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010961 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10962 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010963 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010964 LHS = S.DefaultLvalueConversion(LHS.get());
10965 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010966 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10967 CK_UncheckedDerivedToBase, LHS.get(),
10968 &BasePath, LHS.get()->getValueKind());
10969 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10970 CK_UncheckedDerivedToBase, RHS.get(),
10971 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010972 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010973 FunctionProtoType::ExtProtoInfo EPI;
10974 QualType Params[] = {PtrRedTy, PtrRedTy};
10975 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10976 auto *OVE = new (Context) OpaqueValueExpr(
10977 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010978 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010979 Expr *Args[] = {LHS.get(), RHS.get()};
10980 ReductionOp = new (Context)
10981 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10982 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010983 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010984 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010985 if (ReductionOp.isUsable()) {
10986 if (BOK != BO_LT && BOK != BO_GT) {
10987 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010988 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010989 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010990 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000010991 auto *ConditionalOp = new (Context)
10992 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10993 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010994 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010995 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010996 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010997 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010998 if (ReductionOp.isUsable())
10999 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011000 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011001 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011002 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011003 }
11004
Alexey Bataevfa312f32017-07-21 18:48:21 +000011005 // OpenMP [2.15.4.6, Restrictions, p.2]
11006 // A list item that appears in an in_reduction clause of a task construct
11007 // must appear in a task_reduction clause of a construct associated with a
11008 // taskgroup region that includes the participating task in its taskgroup
11009 // set. The construct associated with the innermost region that meets this
11010 // condition must specify the same reduction-identifier as the in_reduction
11011 // clause.
11012 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011013 SourceRange ParentSR;
11014 BinaryOperatorKind ParentBOK;
11015 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011016 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011017 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011018 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11019 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011020 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011021 Stack->getTopMostTaskgroupReductionData(
11022 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011023 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11024 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11025 if (!IsParentBOK && !IsParentReductionOp) {
11026 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11027 continue;
11028 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011029 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11030 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11031 IsParentReductionOp) {
11032 bool EmitError = true;
11033 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11034 llvm::FoldingSetNodeID RedId, ParentRedId;
11035 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11036 DeclareReductionRef.get()->Profile(RedId, Context,
11037 /*Canonical=*/true);
11038 EmitError = RedId != ParentRedId;
11039 }
11040 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011041 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011042 diag::err_omp_reduction_identifier_mismatch)
11043 << ReductionIdRange << RefExpr->getSourceRange();
11044 S.Diag(ParentSR.getBegin(),
11045 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011046 << ParentSR
11047 << (IsParentBOK ? ParentBOKDSA.RefExpr
11048 : ParentReductionOpDSA.RefExpr)
11049 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011050 continue;
11051 }
11052 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011053 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11054 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011055 }
11056
Alexey Bataev60da77e2016-02-29 05:54:20 +000011057 DeclRefExpr *Ref = nullptr;
11058 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011059 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011060 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011061 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011062 VarsExpr =
11063 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11064 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011065 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011066 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011067 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011068 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011069 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011070 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011071 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011072 if (!RefRes.isUsable())
11073 continue;
11074 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011075 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11076 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011077 if (!PostUpdateRes.isUsable())
11078 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011079 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11080 Stack->getCurrentDirective() == OMPD_taskgroup) {
11081 S.Diag(RefExpr->getExprLoc(),
11082 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011083 << RefExpr->getSourceRange();
11084 continue;
11085 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011086 RD.ExprPostUpdates.emplace_back(
11087 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011088 }
11089 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011090 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011091 // All reduction items are still marked as reduction (to do not increase
11092 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011093 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011094 if (CurrDir == OMPD_taskgroup) {
11095 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011096 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11097 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011098 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011099 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011100 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011101 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11102 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011103 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011104 return RD.Vars.empty();
11105}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011106
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011107OMPClause *Sema::ActOnOpenMPReductionClause(
11108 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11109 SourceLocation ColonLoc, SourceLocation EndLoc,
11110 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11111 ArrayRef<Expr *> UnresolvedReductions) {
11112 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011113 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011114 StartLoc, LParenLoc, ColonLoc, EndLoc,
11115 ReductionIdScopeSpec, ReductionId,
11116 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011117 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011118
Alexey Bataevc5e02582014-06-16 07:08:35 +000011119 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011120 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11121 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11122 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11123 buildPreInits(Context, RD.ExprCaptures),
11124 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011125}
11126
Alexey Bataev169d96a2017-07-18 20:17:46 +000011127OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11128 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11129 SourceLocation ColonLoc, SourceLocation EndLoc,
11130 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11131 ArrayRef<Expr *> UnresolvedReductions) {
11132 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011133 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11134 StartLoc, LParenLoc, ColonLoc, EndLoc,
11135 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011136 UnresolvedReductions, RD))
11137 return nullptr;
11138
11139 return OMPTaskReductionClause::Create(
11140 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11141 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11142 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11143 buildPreInits(Context, RD.ExprCaptures),
11144 buildPostUpdate(*this, RD.ExprPostUpdates));
11145}
11146
Alexey Bataevfa312f32017-07-21 18:48:21 +000011147OMPClause *Sema::ActOnOpenMPInReductionClause(
11148 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11149 SourceLocation ColonLoc, SourceLocation EndLoc,
11150 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11151 ArrayRef<Expr *> UnresolvedReductions) {
11152 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011153 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011154 StartLoc, LParenLoc, ColonLoc, EndLoc,
11155 ReductionIdScopeSpec, ReductionId,
11156 UnresolvedReductions, RD))
11157 return nullptr;
11158
11159 return OMPInReductionClause::Create(
11160 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11161 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011162 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011163 buildPreInits(Context, RD.ExprCaptures),
11164 buildPostUpdate(*this, RD.ExprPostUpdates));
11165}
11166
Alexey Bataevecba70f2016-04-12 11:02:11 +000011167bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11168 SourceLocation LinLoc) {
11169 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11170 LinKind == OMPC_LINEAR_unknown) {
11171 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11172 return true;
11173 }
11174 return false;
11175}
11176
Alexey Bataeve3727102018-04-18 15:57:46 +000011177bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011178 OpenMPLinearClauseKind LinKind,
11179 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011180 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011181 // A variable must not have an incomplete type or a reference type.
11182 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11183 return true;
11184 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11185 !Type->isReferenceType()) {
11186 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11187 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11188 return true;
11189 }
11190 Type = Type.getNonReferenceType();
11191
11192 // A list item must not be const-qualified.
11193 if (Type.isConstant(Context)) {
11194 Diag(ELoc, diag::err_omp_const_variable)
11195 << getOpenMPClauseName(OMPC_linear);
11196 if (D) {
11197 bool IsDecl =
11198 !VD ||
11199 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11200 Diag(D->getLocation(),
11201 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11202 << D;
11203 }
11204 return true;
11205 }
11206
11207 // A list item must be of integral or pointer type.
11208 Type = Type.getUnqualifiedType().getCanonicalType();
11209 const auto *Ty = Type.getTypePtrOrNull();
11210 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11211 !Ty->isPointerType())) {
11212 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11213 if (D) {
11214 bool IsDecl =
11215 !VD ||
11216 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11217 Diag(D->getLocation(),
11218 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11219 << D;
11220 }
11221 return true;
11222 }
11223 return false;
11224}
11225
Alexey Bataev182227b2015-08-20 10:54:39 +000011226OMPClause *Sema::ActOnOpenMPLinearClause(
11227 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11228 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11229 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011230 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011231 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011232 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011233 SmallVector<Decl *, 4> ExprCaptures;
11234 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011235 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011236 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011237 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011238 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011239 SourceLocation ELoc;
11240 SourceRange ERange;
11241 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011242 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011243 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011244 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011245 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011246 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011247 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011248 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011249 ValueDecl *D = Res.first;
11250 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011251 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011252
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011253 QualType Type = D->getType();
11254 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011255
11256 // OpenMP [2.14.3.7, linear clause]
11257 // A list-item cannot appear in more than one linear clause.
11258 // A list-item that appears in a linear clause cannot appear in any
11259 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011260 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011261 if (DVar.RefExpr) {
11262 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11263 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011264 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011265 continue;
11266 }
11267
Alexey Bataevecba70f2016-04-12 11:02:11 +000011268 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011269 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011270 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011271
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011272 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011273 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011274 buildVarDecl(*this, ELoc, Type, D->getName(),
11275 D->hasAttrs() ? &D->getAttrs() : nullptr,
11276 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011277 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011278 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011279 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011280 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011281 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011282 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011283 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011284 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011285 ExprCaptures.push_back(Ref->getDecl());
11286 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11287 ExprResult RefRes = DefaultLvalueConversion(Ref);
11288 if (!RefRes.isUsable())
11289 continue;
11290 ExprResult PostUpdateRes =
11291 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11292 SimpleRefExpr, RefRes.get());
11293 if (!PostUpdateRes.isUsable())
11294 continue;
11295 ExprPostUpdates.push_back(
11296 IgnoredValueConversions(PostUpdateRes.get()).get());
11297 }
11298 }
11299 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011300 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011301 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011302 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011303 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011304 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011305 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011306 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011307
11308 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011309 Vars.push_back((VD || CurContext->isDependentContext())
11310 ? RefExpr->IgnoreParens()
11311 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011312 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011313 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011314 }
11315
11316 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011317 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011318
11319 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011320 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011321 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11322 !Step->isInstantiationDependent() &&
11323 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011324 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011325 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011326 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011327 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011328 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011329
Alexander Musman3276a272015-03-21 10:12:56 +000011330 // Build var to save the step value.
11331 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011332 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011333 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011334 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011335 ExprResult CalcStep =
11336 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011337 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011338
Alexander Musman8dba6642014-04-22 13:09:42 +000011339 // Warn about zero linear step (it would be probably better specified as
11340 // making corresponding variables 'const').
11341 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011342 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11343 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011344 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11345 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011346 if (!IsConstant && CalcStep.isUsable()) {
11347 // Calculate the step beforehand instead of doing this on each iteration.
11348 // (This is not used if the number of iterations may be kfold-ed).
11349 CalcStepExpr = CalcStep.get();
11350 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011351 }
11352
Alexey Bataev182227b2015-08-20 10:54:39 +000011353 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11354 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011355 StepExpr, CalcStepExpr,
11356 buildPreInits(Context, ExprCaptures),
11357 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011358}
11359
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011360static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11361 Expr *NumIterations, Sema &SemaRef,
11362 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011363 // Walk the vars and build update/final expressions for the CodeGen.
11364 SmallVector<Expr *, 8> Updates;
11365 SmallVector<Expr *, 8> Finals;
11366 Expr *Step = Clause.getStep();
11367 Expr *CalcStep = Clause.getCalcStep();
11368 // OpenMP [2.14.3.7, linear clause]
11369 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011370 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011371 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011372 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011373 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11374 bool HasErrors = false;
11375 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011376 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011377 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11378 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011379 SourceLocation ELoc;
11380 SourceRange ERange;
11381 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011382 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011383 ValueDecl *D = Res.first;
11384 if (Res.second || !D) {
11385 Updates.push_back(nullptr);
11386 Finals.push_back(nullptr);
11387 HasErrors = true;
11388 continue;
11389 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011390 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011391 // OpenMP [2.15.11, distribute simd Construct]
11392 // A list item may not appear in a linear clause, unless it is the loop
11393 // iteration variable.
11394 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11395 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11396 SemaRef.Diag(ELoc,
11397 diag::err_omp_linear_distribute_var_non_loop_iteration);
11398 Updates.push_back(nullptr);
11399 Finals.push_back(nullptr);
11400 HasErrors = true;
11401 continue;
11402 }
Alexander Musman3276a272015-03-21 10:12:56 +000011403 Expr *InitExpr = *CurInit;
11404
11405 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011406 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011407 Expr *CapturedRef;
11408 if (LinKind == OMPC_LINEAR_uval)
11409 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11410 else
11411 CapturedRef =
11412 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11413 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11414 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011415
11416 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011417 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011418 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011419 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011420 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011421 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011422 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011423 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011424 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011425 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011426
11427 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011428 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011429 if (!Info.first)
11430 Final =
11431 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11432 InitExpr, NumIterations, Step, /*Subtract=*/false);
11433 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011434 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011435 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011436 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011437
Alexander Musman3276a272015-03-21 10:12:56 +000011438 if (!Update.isUsable() || !Final.isUsable()) {
11439 Updates.push_back(nullptr);
11440 Finals.push_back(nullptr);
11441 HasErrors = true;
11442 } else {
11443 Updates.push_back(Update.get());
11444 Finals.push_back(Final.get());
11445 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011446 ++CurInit;
11447 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011448 }
11449 Clause.setUpdates(Updates);
11450 Clause.setFinals(Finals);
11451 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011452}
11453
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011454OMPClause *Sema::ActOnOpenMPAlignedClause(
11455 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11456 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011457 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011458 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011459 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11460 SourceLocation ELoc;
11461 SourceRange ERange;
11462 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011463 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011464 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011465 // It will be analyzed later.
11466 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011467 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011468 ValueDecl *D = Res.first;
11469 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011470 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011471
Alexey Bataev1efd1662016-03-29 10:59:56 +000011472 QualType QType = D->getType();
11473 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011474
11475 // OpenMP [2.8.1, simd construct, Restrictions]
11476 // The type of list items appearing in the aligned clause must be
11477 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011478 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011479 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011480 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011481 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011482 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011483 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011484 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011485 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011486 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011487 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011488 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011489 continue;
11490 }
11491
11492 // OpenMP [2.8.1, simd construct, Restrictions]
11493 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011494 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011495 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011496 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11497 << getOpenMPClauseName(OMPC_aligned);
11498 continue;
11499 }
11500
Alexey Bataev1efd1662016-03-29 10:59:56 +000011501 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011502 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011503 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11504 Vars.push_back(DefaultFunctionArrayConversion(
11505 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11506 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011507 }
11508
11509 // OpenMP [2.8.1, simd construct, Description]
11510 // The parameter of the aligned clause, alignment, must be a constant
11511 // positive integer expression.
11512 // If no optional parameter is specified, implementation-defined default
11513 // alignments for SIMD instructions on the target platforms are assumed.
11514 if (Alignment != nullptr) {
11515 ExprResult AlignResult =
11516 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11517 if (AlignResult.isInvalid())
11518 return nullptr;
11519 Alignment = AlignResult.get();
11520 }
11521 if (Vars.empty())
11522 return nullptr;
11523
11524 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11525 EndLoc, Vars, Alignment);
11526}
11527
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011528OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11529 SourceLocation StartLoc,
11530 SourceLocation LParenLoc,
11531 SourceLocation EndLoc) {
11532 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011533 SmallVector<Expr *, 8> SrcExprs;
11534 SmallVector<Expr *, 8> DstExprs;
11535 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011536 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011537 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11538 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011539 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011540 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011541 SrcExprs.push_back(nullptr);
11542 DstExprs.push_back(nullptr);
11543 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011544 continue;
11545 }
11546
Alexey Bataeved09d242014-05-28 05:53:51 +000011547 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011548 // OpenMP [2.1, C/C++]
11549 // A list item is a variable name.
11550 // OpenMP [2.14.4.1, Restrictions, p.1]
11551 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011552 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011553 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011554 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11555 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011556 continue;
11557 }
11558
11559 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011560 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011561
11562 QualType Type = VD->getType();
11563 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11564 // It will be analyzed later.
11565 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011566 SrcExprs.push_back(nullptr);
11567 DstExprs.push_back(nullptr);
11568 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011569 continue;
11570 }
11571
11572 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11573 // A list item that appears in a copyin clause must be threadprivate.
11574 if (!DSAStack->isThreadPrivate(VD)) {
11575 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011576 << getOpenMPClauseName(OMPC_copyin)
11577 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011578 continue;
11579 }
11580
11581 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11582 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011583 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011584 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011585 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11586 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011587 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011588 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011589 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011590 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011591 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011592 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011593 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011594 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011595 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011596 // For arrays generate assignment operation for single element and replace
11597 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011598 ExprResult AssignmentOp =
11599 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11600 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011601 if (AssignmentOp.isInvalid())
11602 continue;
11603 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11604 /*DiscardedValue=*/true);
11605 if (AssignmentOp.isInvalid())
11606 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011607
11608 DSAStack->addDSA(VD, DE, OMPC_copyin);
11609 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011610 SrcExprs.push_back(PseudoSrcExpr);
11611 DstExprs.push_back(PseudoDstExpr);
11612 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011613 }
11614
Alexey Bataeved09d242014-05-28 05:53:51 +000011615 if (Vars.empty())
11616 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011617
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011618 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11619 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011620}
11621
Alexey Bataevbae9a792014-06-27 10:37:06 +000011622OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11623 SourceLocation StartLoc,
11624 SourceLocation LParenLoc,
11625 SourceLocation EndLoc) {
11626 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011627 SmallVector<Expr *, 8> SrcExprs;
11628 SmallVector<Expr *, 8> DstExprs;
11629 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011630 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011631 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11632 SourceLocation ELoc;
11633 SourceRange ERange;
11634 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011635 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011636 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011637 // It will be analyzed later.
11638 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011639 SrcExprs.push_back(nullptr);
11640 DstExprs.push_back(nullptr);
11641 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011642 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011643 ValueDecl *D = Res.first;
11644 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011645 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011646
Alexey Bataeve122da12016-03-17 10:50:17 +000011647 QualType Type = D->getType();
11648 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011649
11650 // OpenMP [2.14.4.2, Restrictions, p.2]
11651 // A list item that appears in a copyprivate clause may not appear in a
11652 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011653 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011654 DSAStackTy::DSAVarData DVar =
11655 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011656 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11657 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011658 Diag(ELoc, diag::err_omp_wrong_dsa)
11659 << getOpenMPClauseName(DVar.CKind)
11660 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011661 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011662 continue;
11663 }
11664
11665 // OpenMP [2.11.4.2, Restrictions, p.1]
11666 // All list items that appear in a copyprivate clause must be either
11667 // threadprivate or private in the enclosing context.
11668 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011669 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011670 if (DVar.CKind == OMPC_shared) {
11671 Diag(ELoc, diag::err_omp_required_access)
11672 << getOpenMPClauseName(OMPC_copyprivate)
11673 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011674 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011675 continue;
11676 }
11677 }
11678 }
11679
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011680 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011681 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011682 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011683 << getOpenMPClauseName(OMPC_copyprivate) << Type
11684 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011685 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011686 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011687 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011688 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011690 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011691 continue;
11692 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011693
Alexey Bataevbae9a792014-06-27 10:37:06 +000011694 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11695 // A variable of class type (or array thereof) that appears in a
11696 // copyin clause requires an accessible, unambiguous copy assignment
11697 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011698 Type = Context.getBaseElementType(Type.getNonReferenceType())
11699 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011700 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011701 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011702 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011703 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11704 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011705 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011706 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011707 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11708 ExprResult AssignmentOp = BuildBinOp(
11709 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011710 if (AssignmentOp.isInvalid())
11711 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011712 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011713 /*DiscardedValue=*/true);
11714 if (AssignmentOp.isInvalid())
11715 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011716
11717 // No need to mark vars as copyprivate, they are already threadprivate or
11718 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011719 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011720 Vars.push_back(
11721 VD ? RefExpr->IgnoreParens()
11722 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011723 SrcExprs.push_back(PseudoSrcExpr);
11724 DstExprs.push_back(PseudoDstExpr);
11725 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011726 }
11727
11728 if (Vars.empty())
11729 return nullptr;
11730
Alexey Bataeva63048e2015-03-23 06:18:07 +000011731 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11732 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011733}
11734
Alexey Bataev6125da92014-07-21 11:26:11 +000011735OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11736 SourceLocation StartLoc,
11737 SourceLocation LParenLoc,
11738 SourceLocation EndLoc) {
11739 if (VarList.empty())
11740 return nullptr;
11741
11742 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11743}
Alexey Bataevdea47612014-07-23 07:46:59 +000011744
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011745OMPClause *
11746Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11747 SourceLocation DepLoc, SourceLocation ColonLoc,
11748 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11749 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011750 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011751 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011752 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011753 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011754 return nullptr;
11755 }
11756 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011757 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11758 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011759 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011760 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011761 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11762 /*Last=*/OMPC_DEPEND_unknown, Except)
11763 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011764 return nullptr;
11765 }
11766 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011767 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011768 llvm::APSInt DepCounter(/*BitWidth=*/32);
11769 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011770 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11771 if (const Expr *OrderedCountExpr =
11772 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011773 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11774 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011775 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011776 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011777 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011778 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11779 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11780 // It will be analyzed later.
11781 Vars.push_back(RefExpr);
11782 continue;
11783 }
11784
11785 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011786 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011787 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000011788 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011789 DepCounter >= TotalDepCount) {
11790 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11791 continue;
11792 }
11793 ++DepCounter;
11794 // OpenMP [2.13.9, Summary]
11795 // depend(dependence-type : vec), where dependence-type is:
11796 // 'sink' and where vec is the iteration vector, which has the form:
11797 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11798 // where n is the value specified by the ordered clause in the loop
11799 // directive, xi denotes the loop iteration variable of the i-th nested
11800 // loop associated with the loop directive, and di is a constant
11801 // non-negative integer.
11802 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011803 // It will be analyzed later.
11804 Vars.push_back(RefExpr);
11805 continue;
11806 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011807 SimpleExpr = SimpleExpr->IgnoreImplicit();
11808 OverloadedOperatorKind OOK = OO_None;
11809 SourceLocation OOLoc;
11810 Expr *LHS = SimpleExpr;
11811 Expr *RHS = nullptr;
11812 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11813 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11814 OOLoc = BO->getOperatorLoc();
11815 LHS = BO->getLHS()->IgnoreParenImpCasts();
11816 RHS = BO->getRHS()->IgnoreParenImpCasts();
11817 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11818 OOK = OCE->getOperator();
11819 OOLoc = OCE->getOperatorLoc();
11820 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11821 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11822 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11823 OOK = MCE->getMethodDecl()
11824 ->getNameInfo()
11825 .getName()
11826 .getCXXOverloadedOperator();
11827 OOLoc = MCE->getCallee()->getExprLoc();
11828 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11829 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011830 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011831 SourceLocation ELoc;
11832 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000011833 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011834 if (Res.second) {
11835 // It will be analyzed later.
11836 Vars.push_back(RefExpr);
11837 }
11838 ValueDecl *D = Res.first;
11839 if (!D)
11840 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011841
Alexey Bataev17daedf2018-02-15 22:42:57 +000011842 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11843 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11844 continue;
11845 }
11846 if (RHS) {
11847 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11848 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11849 if (RHSRes.isInvalid())
11850 continue;
11851 }
11852 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011853 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011854 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011855 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000011856 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000011857 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000011858 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11859 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000011860 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000011861 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000011862 continue;
11863 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011864 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011865 } else {
11866 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11867 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11868 (ASE &&
11869 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11870 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11871 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11872 << RefExpr->getSourceRange();
11873 continue;
11874 }
11875 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11876 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11877 ExprResult Res =
11878 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11879 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11880 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11881 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11882 << RefExpr->getSourceRange();
11883 continue;
11884 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011885 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011886 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011887 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011888
11889 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11890 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011891 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011892 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11893 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11894 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11895 }
11896 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11897 Vars.empty())
11898 return nullptr;
11899
Alexey Bataev8b427062016-05-25 12:36:08 +000011900 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000011901 DepKind, DepLoc, ColonLoc, Vars,
11902 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000011903 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11904 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000011905 DSAStack->addDoacrossDependClause(C, OpsOffs);
11906 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011907}
Michael Wonge710d542015-08-07 16:16:36 +000011908
11909OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11910 SourceLocation LParenLoc,
11911 SourceLocation EndLoc) {
11912 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011913 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011914
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011915 // OpenMP [2.9.1, Restrictions]
11916 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000011917 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000011918 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011919 return nullptr;
11920
Alexey Bataev931e19b2017-10-02 16:32:39 +000011921 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011922 OpenMPDirectiveKind CaptureRegion =
11923 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11924 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011925 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011926 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011927 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11928 HelperValStmt = buildPreInits(Context, Captures);
11929 }
11930
Alexey Bataev8451efa2018-01-15 19:06:12 +000011931 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11932 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011933}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011934
Alexey Bataeve3727102018-04-18 15:57:46 +000011935static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000011936 DSAStackTy *Stack, QualType QTy,
11937 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000011938 NamedDecl *ND;
11939 if (QTy->isIncompleteType(&ND)) {
11940 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11941 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011942 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000011943 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11944 !QTy.isTrivialType(SemaRef.Context))
11945 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011946 return true;
11947}
11948
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011949/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011950/// (array section or array subscript) does NOT specify the whole size of the
11951/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011952static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011953 const Expr *E,
11954 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011955 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011956
11957 // If this is an array subscript, it refers to the whole size if the size of
11958 // the dimension is constant and equals 1. Also, an array section assumes the
11959 // format of an array subscript if no colon is used.
11960 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011961 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011962 return ATy->getSize().getSExtValue() != 1;
11963 // Size can't be evaluated statically.
11964 return false;
11965 }
11966
11967 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000011968 const Expr *LowerBound = OASE->getLowerBound();
11969 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011970
11971 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000011972 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011973 if (LowerBound) {
11974 llvm::APSInt ConstLowerBound;
11975 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11976 return false; // Can't get the integer value as a constant.
11977 if (ConstLowerBound.getSExtValue())
11978 return true;
11979 }
11980
11981 // If we don't have a length we covering the whole dimension.
11982 if (!Length)
11983 return false;
11984
11985 // If the base is a pointer, we don't have a way to get the size of the
11986 // pointee.
11987 if (BaseQTy->isPointerType())
11988 return false;
11989
11990 // We can only check if the length is the same as the size of the dimension
11991 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000011992 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011993 if (!CATy)
11994 return false;
11995
11996 llvm::APSInt ConstLength;
11997 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11998 return false; // Can't get the integer value as a constant.
11999
12000 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12001}
12002
12003// Return true if it can be proven that the provided array expression (array
12004// section or array subscript) does NOT specify a single element of the array
12005// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012006static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012007 const Expr *E,
12008 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012009 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012010
12011 // An array subscript always refer to a single element. Also, an array section
12012 // assumes the format of an array subscript if no colon is used.
12013 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12014 return false;
12015
12016 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012017 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012018
12019 // If we don't have a length we have to check if the array has unitary size
12020 // for this dimension. Also, we should always expect a length if the base type
12021 // is pointer.
12022 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012023 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012024 return ATy->getSize().getSExtValue() != 1;
12025 // We cannot assume anything.
12026 return false;
12027 }
12028
12029 // Check if the length evaluates to 1.
12030 llvm::APSInt ConstLength;
12031 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12032 return false; // Can't get the integer value as a constant.
12033
12034 return ConstLength.getSExtValue() != 1;
12035}
12036
Samuel Antao661c0902016-05-26 17:39:58 +000012037// Return the expression of the base of the mappable expression or null if it
12038// cannot be determined and do all the necessary checks to see if the expression
12039// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012040// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012041static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012042 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012043 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012044 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012045 SourceLocation ELoc = E->getExprLoc();
12046 SourceRange ERange = E->getSourceRange();
12047
12048 // The base of elements of list in a map clause have to be either:
12049 // - a reference to variable or field.
12050 // - a member expression.
12051 // - an array expression.
12052 //
12053 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12054 // reference to 'r'.
12055 //
12056 // If we have:
12057 //
12058 // struct SS {
12059 // Bla S;
12060 // foo() {
12061 // #pragma omp target map (S.Arr[:12]);
12062 // }
12063 // }
12064 //
12065 // We want to retrieve the member expression 'this->S';
12066
Alexey Bataeve3727102018-04-18 15:57:46 +000012067 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012068
Samuel Antao5de996e2016-01-22 20:21:36 +000012069 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12070 // If a list item is an array section, it must specify contiguous storage.
12071 //
12072 // For this restriction it is sufficient that we make sure only references
12073 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012074 // exist except in the rightmost expression (unless they cover the whole
12075 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012076 //
12077 // r.ArrS[3:5].Arr[6:7]
12078 //
12079 // r.ArrS[3:5].x
12080 //
12081 // but these would be valid:
12082 // r.ArrS[3].Arr[6:7]
12083 //
12084 // r.ArrS[3].x
12085
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012086 bool AllowUnitySizeArraySection = true;
12087 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012088
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012089 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012090 E = E->IgnoreParenImpCasts();
12091
12092 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12093 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012094 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012095
12096 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012097
12098 // If we got a reference to a declaration, we should not expect any array
12099 // section before that.
12100 AllowUnitySizeArraySection = false;
12101 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012102
12103 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012104 CurComponents.emplace_back(CurE, CurE->getDecl());
12105 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012106 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012107
12108 if (isa<CXXThisExpr>(BaseE))
12109 // We found a base expression: this->Val.
12110 RelevantExpr = CurE;
12111 else
12112 E = BaseE;
12113
12114 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012115 if (!NoDiagnose) {
12116 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12117 << CurE->getSourceRange();
12118 return nullptr;
12119 }
12120 if (RelevantExpr)
12121 return nullptr;
12122 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012123 }
12124
12125 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12126
12127 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12128 // A bit-field cannot appear in a map clause.
12129 //
12130 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012131 if (!NoDiagnose) {
12132 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12133 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12134 return nullptr;
12135 }
12136 if (RelevantExpr)
12137 return nullptr;
12138 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012139 }
12140
12141 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12142 // If the type of a list item is a reference to a type T then the type
12143 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012144 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012145
12146 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12147 // A list item cannot be a variable that is a member of a structure with
12148 // a union type.
12149 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012150 if (CurType->isUnionType()) {
12151 if (!NoDiagnose) {
12152 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12153 << CurE->getSourceRange();
12154 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012155 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012156 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012157 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012158
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012159 // If we got a member expression, we should not expect any array section
12160 // before that:
12161 //
12162 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12163 // If a list item is an element of a structure, only the rightmost symbol
12164 // of the variable reference can be an array section.
12165 //
12166 AllowUnitySizeArraySection = false;
12167 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012168
12169 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012170 CurComponents.emplace_back(CurE, FD);
12171 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012172 E = CurE->getBase()->IgnoreParenImpCasts();
12173
12174 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012175 if (!NoDiagnose) {
12176 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12177 << 0 << CurE->getSourceRange();
12178 return nullptr;
12179 }
12180 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012181 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012182
12183 // If we got an array subscript that express the whole dimension we
12184 // can have any array expressions before. If it only expressing part of
12185 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012186 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012187 E->getType()))
12188 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012189
12190 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012191 CurComponents.emplace_back(CurE, nullptr);
12192 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012193 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012194 E = CurE->getBase()->IgnoreParenImpCasts();
12195
Alexey Bataev27041fa2017-12-05 15:22:49 +000012196 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012197 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12198
Samuel Antao5de996e2016-01-22 20:21:36 +000012199 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12200 // If the type of a list item is a reference to a type T then the type
12201 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012202 if (CurType->isReferenceType())
12203 CurType = CurType->getPointeeType();
12204
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012205 bool IsPointer = CurType->isAnyPointerType();
12206
12207 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012208 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12209 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012210 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012211 }
12212
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012213 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012214 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012215 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012216 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012217
Samuel Antaodab51bb2016-07-18 23:22:11 +000012218 if (AllowWholeSizeArraySection) {
12219 // Any array section is currently allowed. Allowing a whole size array
12220 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012221 //
12222 // If this array section refers to the whole dimension we can still
12223 // accept other array sections before this one, except if the base is a
12224 // pointer. Otherwise, only unitary sections are accepted.
12225 if (NotWhole || IsPointer)
12226 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012227 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012228 // A unity or whole array section is not allowed and that is not
12229 // compatible with the properties of the current array section.
12230 SemaRef.Diag(
12231 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12232 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012233 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012234 }
Samuel Antao90927002016-04-26 14:54:23 +000012235
12236 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012237 CurComponents.emplace_back(CurE, nullptr);
12238 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012239 if (!NoDiagnose) {
12240 // If nothing else worked, this is not a valid map clause expression.
12241 SemaRef.Diag(
12242 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12243 << ERange;
12244 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012245 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012246 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012247 }
12248
12249 return RelevantExpr;
12250}
12251
12252// Return true if expression E associated with value VD has conflicts with other
12253// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012254static bool checkMapConflicts(
12255 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012256 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012257 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12258 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012259 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012260 SourceLocation ELoc = E->getExprLoc();
12261 SourceRange ERange = E->getSourceRange();
12262
12263 // In order to easily check the conflicts we need to match each component of
12264 // the expression under test with the components of the expressions that are
12265 // already in the stack.
12266
Samuel Antao5de996e2016-01-22 20:21:36 +000012267 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012268 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012269 "Map clause expression with unexpected base!");
12270
12271 // Variables to help detecting enclosing problems in data environment nests.
12272 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012273 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012274
Samuel Antao90927002016-04-26 14:54:23 +000012275 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12276 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012277 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12278 ERange, CKind, &EnclosingExpr,
12279 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12280 StackComponents,
12281 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012282 assert(!StackComponents.empty() &&
12283 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012284 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012285 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012286 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012287
Samuel Antao90927002016-04-26 14:54:23 +000012288 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012289 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012290
Samuel Antao5de996e2016-01-22 20:21:36 +000012291 // Expressions must start from the same base. Here we detect at which
12292 // point both expressions diverge from each other and see if we can
12293 // detect if the memory referred to both expressions is contiguous and
12294 // do not overlap.
12295 auto CI = CurComponents.rbegin();
12296 auto CE = CurComponents.rend();
12297 auto SI = StackComponents.rbegin();
12298 auto SE = StackComponents.rend();
12299 for (; CI != CE && SI != SE; ++CI, ++SI) {
12300
12301 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12302 // At most one list item can be an array item derived from a given
12303 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012304 if (CurrentRegionOnly &&
12305 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12306 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12307 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12308 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12309 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012310 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012311 << CI->getAssociatedExpression()->getSourceRange();
12312 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12313 diag::note_used_here)
12314 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012315 return true;
12316 }
12317
12318 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012319 if (CI->getAssociatedExpression()->getStmtClass() !=
12320 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012321 break;
12322
12323 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012324 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012325 break;
12326 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012327 // Check if the extra components of the expressions in the enclosing
12328 // data environment are redundant for the current base declaration.
12329 // If they are, the maps completely overlap, which is legal.
12330 for (; SI != SE; ++SI) {
12331 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012332 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012333 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012334 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012335 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012336 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012337 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012338 Type =
12339 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12340 }
12341 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012342 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012343 SemaRef, SI->getAssociatedExpression(), Type))
12344 break;
12345 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012346
12347 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12348 // List items of map clauses in the same construct must not share
12349 // original storage.
12350 //
12351 // If the expressions are exactly the same or one is a subset of the
12352 // other, it means they are sharing storage.
12353 if (CI == CE && SI == SE) {
12354 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012355 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012356 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012357 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012358 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012359 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12360 << ERange;
12361 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012362 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12363 << RE->getSourceRange();
12364 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012365 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012366 // If we find the same expression in the enclosing data environment,
12367 // that is legal.
12368 IsEnclosedByDataEnvironmentExpr = true;
12369 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012370 }
12371
Samuel Antao90927002016-04-26 14:54:23 +000012372 QualType DerivedType =
12373 std::prev(CI)->getAssociatedDeclaration()->getType();
12374 SourceLocation DerivedLoc =
12375 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012376
12377 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12378 // If the type of a list item is a reference to a type T then the type
12379 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012380 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012381
12382 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12383 // A variable for which the type is pointer and an array section
12384 // derived from that variable must not appear as list items of map
12385 // clauses of the same construct.
12386 //
12387 // Also, cover one of the cases in:
12388 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12389 // If any part of the original storage of a list item has corresponding
12390 // storage in the device data environment, all of the original storage
12391 // must have corresponding storage in the device data environment.
12392 //
12393 if (DerivedType->isAnyPointerType()) {
12394 if (CI == CE || SI == SE) {
12395 SemaRef.Diag(
12396 DerivedLoc,
12397 diag::err_omp_pointer_mapped_along_with_derived_section)
12398 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012399 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12400 << RE->getSourceRange();
12401 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012402 }
12403 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012404 SI->getAssociatedExpression()->getStmtClass() ||
12405 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12406 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012407 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012408 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012409 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012410 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12411 << RE->getSourceRange();
12412 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012413 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012414 }
12415
12416 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12417 // List items of map clauses in the same construct must not share
12418 // original storage.
12419 //
12420 // An expression is a subset of the other.
12421 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012422 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012423 if (CI != CE || SI != SE) {
12424 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12425 // a pointer.
12426 auto Begin =
12427 CI != CE ? CurComponents.begin() : StackComponents.begin();
12428 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12429 auto It = Begin;
12430 while (It != End && !It->getAssociatedDeclaration())
12431 std::advance(It, 1);
12432 assert(It != End &&
12433 "Expected at least one component with the declaration.");
12434 if (It != Begin && It->getAssociatedDeclaration()
12435 ->getType()
12436 .getCanonicalType()
12437 ->isAnyPointerType()) {
12438 IsEnclosedByDataEnvironmentExpr = false;
12439 EnclosingExpr = nullptr;
12440 return false;
12441 }
12442 }
Samuel Antao661c0902016-05-26 17:39:58 +000012443 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012444 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012445 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012446 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12447 << ERange;
12448 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012449 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12450 << RE->getSourceRange();
12451 return true;
12452 }
12453
12454 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012455 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012456 if (!CurrentRegionOnly && SI != SE)
12457 EnclosingExpr = RE;
12458
12459 // The current expression is a subset of the expression in the data
12460 // environment.
12461 IsEnclosedByDataEnvironmentExpr |=
12462 (!CurrentRegionOnly && CI != CE && SI == SE);
12463
12464 return false;
12465 });
12466
12467 if (CurrentRegionOnly)
12468 return FoundError;
12469
12470 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12471 // If any part of the original storage of a list item has corresponding
12472 // storage in the device data environment, all of the original storage must
12473 // have corresponding storage in the device data environment.
12474 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12475 // If a list item is an element of a structure, and a different element of
12476 // the structure has a corresponding list item in the device data environment
12477 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012478 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012479 // data environment prior to the task encountering the construct.
12480 //
12481 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12482 SemaRef.Diag(ELoc,
12483 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12484 << ERange;
12485 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12486 << EnclosingExpr->getSourceRange();
12487 return true;
12488 }
12489
12490 return FoundError;
12491}
12492
Samuel Antao661c0902016-05-26 17:39:58 +000012493namespace {
12494// Utility struct that gathers all the related lists associated with a mappable
12495// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012496struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012497 // The list of expressions.
12498 ArrayRef<Expr *> VarList;
12499 // The list of processed expressions.
12500 SmallVector<Expr *, 16> ProcessedVarList;
12501 // The mappble components for each expression.
12502 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12503 // The base declaration of the variable.
12504 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12505
12506 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12507 // We have a list of components and base declarations for each entry in the
12508 // variable list.
12509 VarComponents.reserve(VarList.size());
12510 VarBaseDeclarations.reserve(VarList.size());
12511 }
12512};
12513}
12514
12515// Check the validity of the provided variable list for the provided clause kind
12516// \a CKind. In the check process the valid expressions, and mappable expression
12517// components and variables are extracted and used to fill \a Vars,
12518// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12519// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12520static void
12521checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12522 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12523 SourceLocation StartLoc,
12524 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12525 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012526 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12527 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012528 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012529
Samuel Antao90927002016-04-26 14:54:23 +000012530 // Keep track of the mappable components and base declarations in this clause.
12531 // Each entry in the list is going to have a list of components associated. We
12532 // record each set of the components so that we can build the clause later on.
12533 // In the end we should have the same amount of declarations and component
12534 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012535
Alexey Bataeve3727102018-04-18 15:57:46 +000012536 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012537 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012538 SourceLocation ELoc = RE->getExprLoc();
12539
Alexey Bataeve3727102018-04-18 15:57:46 +000012540 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012541
12542 if (VE->isValueDependent() || VE->isTypeDependent() ||
12543 VE->isInstantiationDependent() ||
12544 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012545 // We can only analyze this information once the missing information is
12546 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012547 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012548 continue;
12549 }
12550
Alexey Bataeve3727102018-04-18 15:57:46 +000012551 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012552
Samuel Antao5de996e2016-01-22 20:21:36 +000012553 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012554 SemaRef.Diag(ELoc,
12555 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012556 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012557 continue;
12558 }
12559
Samuel Antao90927002016-04-26 14:54:23 +000012560 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12561 ValueDecl *CurDeclaration = nullptr;
12562
12563 // Obtain the array or member expression bases if required. Also, fill the
12564 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012565 const Expr *BE = checkMapClauseExpressionBase(
12566 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012567 if (!BE)
12568 continue;
12569
Samuel Antao90927002016-04-26 14:54:23 +000012570 assert(!CurComponents.empty() &&
12571 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012572
Samuel Antao90927002016-04-26 14:54:23 +000012573 // For the following checks, we rely on the base declaration which is
12574 // expected to be associated with the last component. The declaration is
12575 // expected to be a variable or a field (if 'this' is being mapped).
12576 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12577 assert(CurDeclaration && "Null decl on map clause.");
12578 assert(
12579 CurDeclaration->isCanonicalDecl() &&
12580 "Expecting components to have associated only canonical declarations.");
12581
12582 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012583 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012584
12585 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012586 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012587
12588 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012589 // threadprivate variables cannot appear in a map clause.
12590 // OpenMP 4.5 [2.10.5, target update Construct]
12591 // threadprivate variables cannot appear in a from clause.
12592 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012593 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012594 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12595 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012596 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012597 continue;
12598 }
12599
Samuel Antao5de996e2016-01-22 20:21:36 +000012600 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12601 // A list item cannot appear in both a map clause and a data-sharing
12602 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012603
Samuel Antao5de996e2016-01-22 20:21:36 +000012604 // Check conflicts with other map clause expressions. We check the conflicts
12605 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012606 // environment, because the restrictions are different. We only have to
12607 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012608 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012609 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012610 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012611 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012612 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012613 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012614 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012615
Samuel Antao661c0902016-05-26 17:39:58 +000012616 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012617 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12618 // If the type of a list item is a reference to a type T then the type will
12619 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012620 auto I = llvm::find_if(
12621 CurComponents,
12622 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12623 return MC.getAssociatedDeclaration();
12624 });
12625 assert(I != CurComponents.end() && "Null decl on map clause.");
12626 QualType Type =
12627 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012628
Samuel Antao661c0902016-05-26 17:39:58 +000012629 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12630 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012631 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012632 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012633 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012634 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012635 continue;
12636
Samuel Antao661c0902016-05-26 17:39:58 +000012637 if (CKind == OMPC_map) {
12638 // target enter data
12639 // OpenMP [2.10.2, Restrictions, p. 99]
12640 // A map-type must be specified in all map clauses and must be either
12641 // to or alloc.
12642 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12643 if (DKind == OMPD_target_enter_data &&
12644 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12645 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12646 << (IsMapTypeImplicit ? 1 : 0)
12647 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12648 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012649 continue;
12650 }
Samuel Antao661c0902016-05-26 17:39:58 +000012651
12652 // target exit_data
12653 // OpenMP [2.10.3, Restrictions, p. 102]
12654 // A map-type must be specified in all map clauses and must be either
12655 // from, release, or delete.
12656 if (DKind == OMPD_target_exit_data &&
12657 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12658 MapType == OMPC_MAP_delete)) {
12659 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12660 << (IsMapTypeImplicit ? 1 : 0)
12661 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12662 << getOpenMPDirectiveName(DKind);
12663 continue;
12664 }
12665
12666 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12667 // A list item cannot appear in both a map clause and a data-sharing
12668 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012669 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12670 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012671 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012672 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012673 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012674 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012675 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012676 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012677 continue;
12678 }
12679 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012680 }
12681
Samuel Antao90927002016-04-26 14:54:23 +000012682 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012683 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012684
12685 // Store the components in the stack so that they can be used to check
12686 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012687 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12688 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012689
12690 // Save the components and declaration to create the clause. For purposes of
12691 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012692 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012693 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12694 MVLI.VarComponents.back().append(CurComponents.begin(),
12695 CurComponents.end());
12696 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12697 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012698 }
Samuel Antao661c0902016-05-26 17:39:58 +000012699}
12700
12701OMPClause *
12702Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12703 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12704 SourceLocation MapLoc, SourceLocation ColonLoc,
12705 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12706 SourceLocation LParenLoc, SourceLocation EndLoc) {
12707 MappableVarListInfo MVLI(VarList);
12708 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12709 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012710
Samuel Antao5de996e2016-01-22 20:21:36 +000012711 // We need to produce a map clause even if we don't have variables so that
12712 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012713 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12714 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12715 MVLI.VarComponents, MapTypeModifier, MapType,
12716 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012717}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012718
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012719QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12720 TypeResult ParsedType) {
12721 assert(ParsedType.isUsable());
12722
12723 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12724 if (ReductionType.isNull())
12725 return QualType();
12726
12727 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12728 // A type name in a declare reduction directive cannot be a function type, an
12729 // array type, a reference type, or a type qualified with const, volatile or
12730 // restrict.
12731 if (ReductionType.hasQualifiers()) {
12732 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12733 return QualType();
12734 }
12735
12736 if (ReductionType->isFunctionType()) {
12737 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12738 return QualType();
12739 }
12740 if (ReductionType->isReferenceType()) {
12741 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12742 return QualType();
12743 }
12744 if (ReductionType->isArrayType()) {
12745 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12746 return QualType();
12747 }
12748 return ReductionType;
12749}
12750
12751Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12752 Scope *S, DeclContext *DC, DeclarationName Name,
12753 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12754 AccessSpecifier AS, Decl *PrevDeclInScope) {
12755 SmallVector<Decl *, 8> Decls;
12756 Decls.reserve(ReductionTypes.size());
12757
12758 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012759 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012760 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12761 // A reduction-identifier may not be re-declared in the current scope for the
12762 // same type or for a type that is compatible according to the base language
12763 // rules.
12764 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12765 OMPDeclareReductionDecl *PrevDRD = nullptr;
12766 bool InCompoundScope = true;
12767 if (S != nullptr) {
12768 // Find previous declaration with the same name not referenced in other
12769 // declarations.
12770 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12771 InCompoundScope =
12772 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12773 LookupName(Lookup, S);
12774 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12775 /*AllowInlineNamespace=*/false);
12776 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012777 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012778 while (Filter.hasNext()) {
12779 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12780 if (InCompoundScope) {
12781 auto I = UsedAsPrevious.find(PrevDecl);
12782 if (I == UsedAsPrevious.end())
12783 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012784 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012785 UsedAsPrevious[D] = true;
12786 }
12787 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12788 PrevDecl->getLocation();
12789 }
12790 Filter.done();
12791 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012792 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012793 if (!PrevData.second) {
12794 PrevDRD = PrevData.first;
12795 break;
12796 }
12797 }
12798 }
12799 } else if (PrevDeclInScope != nullptr) {
12800 auto *PrevDRDInScope = PrevDRD =
12801 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12802 do {
12803 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12804 PrevDRDInScope->getLocation();
12805 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12806 } while (PrevDRDInScope != nullptr);
12807 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012808 for (const auto &TyData : ReductionTypes) {
12809 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012810 bool Invalid = false;
12811 if (I != PreviousRedeclTypes.end()) {
12812 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12813 << TyData.first;
12814 Diag(I->second, diag::note_previous_definition);
12815 Invalid = true;
12816 }
12817 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12818 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12819 Name, TyData.first, PrevDRD);
12820 DC->addDecl(DRD);
12821 DRD->setAccess(AS);
12822 Decls.push_back(DRD);
12823 if (Invalid)
12824 DRD->setInvalidDecl();
12825 else
12826 PrevDRD = DRD;
12827 }
12828
12829 return DeclGroupPtrTy::make(
12830 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12831}
12832
12833void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12834 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12835
12836 // Enter new function scope.
12837 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012838 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012839 getCurFunction()->setHasOMPDeclareReductionCombiner();
12840
12841 if (S != nullptr)
12842 PushDeclContext(S, DRD);
12843 else
12844 CurContext = DRD;
12845
Faisal Valid143a0c2017-04-01 21:30:49 +000012846 PushExpressionEvaluationContext(
12847 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012848
12849 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012850 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12851 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12852 // uses semantics of argument handles by value, but it should be passed by
12853 // reference. C lang does not support references, so pass all parameters as
12854 // pointers.
12855 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012856 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012857 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012858 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12859 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12860 // uses semantics of argument handles by value, but it should be passed by
12861 // reference. C lang does not support references, so pass all parameters as
12862 // pointers.
12863 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012864 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012865 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12866 if (S != nullptr) {
12867 PushOnScopeChains(OmpInParm, S);
12868 PushOnScopeChains(OmpOutParm, S);
12869 } else {
12870 DRD->addDecl(OmpInParm);
12871 DRD->addDecl(OmpOutParm);
12872 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000012873 Expr *InE =
12874 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
12875 Expr *OutE =
12876 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
12877 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012878}
12879
12880void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12881 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12882 DiscardCleanupsInEvaluationContext();
12883 PopExpressionEvaluationContext();
12884
12885 PopDeclContext();
12886 PopFunctionScopeInfo();
12887
12888 if (Combiner != nullptr)
12889 DRD->setCombiner(Combiner);
12890 else
12891 DRD->setInvalidDecl();
12892}
12893
Alexey Bataev070f43a2017-09-06 14:49:58 +000012894VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012895 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12896
12897 // Enter new function scope.
12898 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012899 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012900
12901 if (S != nullptr)
12902 PushDeclContext(S, DRD);
12903 else
12904 CurContext = DRD;
12905
Faisal Valid143a0c2017-04-01 21:30:49 +000012906 PushExpressionEvaluationContext(
12907 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012908
12909 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012910 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12911 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12912 // uses semantics of argument handles by value, but it should be passed by
12913 // reference. C lang does not support references, so pass all parameters as
12914 // pointers.
12915 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012916 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012917 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012918 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12919 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12920 // uses semantics of argument handles by value, but it should be passed by
12921 // reference. C lang does not support references, so pass all parameters as
12922 // pointers.
12923 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012924 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012925 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012926 if (S != nullptr) {
12927 PushOnScopeChains(OmpPrivParm, S);
12928 PushOnScopeChains(OmpOrigParm, S);
12929 } else {
12930 DRD->addDecl(OmpPrivParm);
12931 DRD->addDecl(OmpOrigParm);
12932 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000012933 Expr *OrigE =
12934 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
12935 Expr *PrivE =
12936 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
12937 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000012938 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012939}
12940
Alexey Bataev070f43a2017-09-06 14:49:58 +000012941void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12942 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012943 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12944 DiscardCleanupsInEvaluationContext();
12945 PopExpressionEvaluationContext();
12946
12947 PopDeclContext();
12948 PopFunctionScopeInfo();
12949
Alexey Bataev070f43a2017-09-06 14:49:58 +000012950 if (Initializer != nullptr) {
12951 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12952 } else if (OmpPrivParm->hasInit()) {
12953 DRD->setInitializer(OmpPrivParm->getInit(),
12954 OmpPrivParm->isDirectInit()
12955 ? OMPDeclareReductionDecl::DirectInit
12956 : OMPDeclareReductionDecl::CopyInit);
12957 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012958 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000012959 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012960}
12961
12962Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12963 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012964 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012965 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012966 if (S)
12967 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
12968 /*AddToContext=*/false);
12969 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012970 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000012971 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012972 }
12973 return DeclReductions;
12974}
12975
David Majnemer9d168222016-08-05 17:44:54 +000012976OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000012977 SourceLocation StartLoc,
12978 SourceLocation LParenLoc,
12979 SourceLocation EndLoc) {
12980 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012981 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012982
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012983 // OpenMP [teams Constrcut, Restrictions]
12984 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012985 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000012986 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012987 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012988
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012989 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012990 OpenMPDirectiveKind CaptureRegion =
12991 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12992 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012993 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012994 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012995 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12996 HelperValStmt = buildPreInits(Context, Captures);
12997 }
12998
12999 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13000 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013001}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013002
13003OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13004 SourceLocation StartLoc,
13005 SourceLocation LParenLoc,
13006 SourceLocation EndLoc) {
13007 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013008 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013009
13010 // OpenMP [teams Constrcut, Restrictions]
13011 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013012 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013013 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013014 return nullptr;
13015
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013016 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013017 OpenMPDirectiveKind CaptureRegion =
13018 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13019 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013020 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013021 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013022 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13023 HelperValStmt = buildPreInits(Context, Captures);
13024 }
13025
13026 return new (Context) OMPThreadLimitClause(
13027 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013028}
Alexey Bataeva0569352015-12-01 10:17:31 +000013029
13030OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13031 SourceLocation StartLoc,
13032 SourceLocation LParenLoc,
13033 SourceLocation EndLoc) {
13034 Expr *ValExpr = Priority;
13035
13036 // OpenMP [2.9.1, task Constrcut]
13037 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013038 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013039 /*StrictlyPositive=*/false))
13040 return nullptr;
13041
13042 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13043}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013044
13045OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13046 SourceLocation StartLoc,
13047 SourceLocation LParenLoc,
13048 SourceLocation EndLoc) {
13049 Expr *ValExpr = Grainsize;
13050
13051 // OpenMP [2.9.2, taskloop Constrcut]
13052 // The parameter of the grainsize clause must be a positive integer
13053 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013054 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013055 /*StrictlyPositive=*/true))
13056 return nullptr;
13057
13058 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13059}
Alexey Bataev382967a2015-12-08 12:06:20 +000013060
13061OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13062 SourceLocation StartLoc,
13063 SourceLocation LParenLoc,
13064 SourceLocation EndLoc) {
13065 Expr *ValExpr = NumTasks;
13066
13067 // OpenMP [2.9.2, taskloop Constrcut]
13068 // The parameter of the num_tasks clause must be a positive integer
13069 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013070 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013071 /*StrictlyPositive=*/true))
13072 return nullptr;
13073
13074 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13075}
13076
Alexey Bataev28c75412015-12-15 08:19:24 +000013077OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13078 SourceLocation LParenLoc,
13079 SourceLocation EndLoc) {
13080 // OpenMP [2.13.2, critical construct, Description]
13081 // ... where hint-expression is an integer constant expression that evaluates
13082 // to a valid lock hint.
13083 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13084 if (HintExpr.isInvalid())
13085 return nullptr;
13086 return new (Context)
13087 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13088}
13089
Carlo Bertollib4adf552016-01-15 18:50:31 +000013090OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13091 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13092 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13093 SourceLocation EndLoc) {
13094 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13095 std::string Values;
13096 Values += "'";
13097 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13098 Values += "'";
13099 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13100 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13101 return nullptr;
13102 }
13103 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013104 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013105 if (ChunkSize) {
13106 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13107 !ChunkSize->isInstantiationDependent() &&
13108 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013109 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013110 ExprResult Val =
13111 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13112 if (Val.isInvalid())
13113 return nullptr;
13114
13115 ValExpr = Val.get();
13116
13117 // OpenMP [2.7.1, Restrictions]
13118 // chunk_size must be a loop invariant integer expression with a positive
13119 // value.
13120 llvm::APSInt Result;
13121 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13122 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13123 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13124 << "dist_schedule" << ChunkSize->getSourceRange();
13125 return nullptr;
13126 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013127 } else if (getOpenMPCaptureRegionForClause(
13128 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13129 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013130 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013131 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013132 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013133 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13134 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013135 }
13136 }
13137 }
13138
13139 return new (Context)
13140 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013141 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013142}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013143
13144OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13145 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13146 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13147 SourceLocation KindLoc, SourceLocation EndLoc) {
13148 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013149 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013150 std::string Value;
13151 SourceLocation Loc;
13152 Value += "'";
13153 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13154 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013155 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013156 Loc = MLoc;
13157 } else {
13158 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013159 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013160 Loc = KindLoc;
13161 }
13162 Value += "'";
13163 Diag(Loc, diag::err_omp_unexpected_clause_value)
13164 << Value << getOpenMPClauseName(OMPC_defaultmap);
13165 return nullptr;
13166 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013167 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013168
13169 return new (Context)
13170 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13171}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013172
13173bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13174 DeclContext *CurLexicalContext = getCurLexicalContext();
13175 if (!CurLexicalContext->isFileContext() &&
13176 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013177 !CurLexicalContext->isExternCXXContext() &&
13178 !isa<CXXRecordDecl>(CurLexicalContext) &&
13179 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13180 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13181 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013182 Diag(Loc, diag::err_omp_region_not_file_context);
13183 return false;
13184 }
Kelvin Libc38e632018-09-10 02:07:09 +000013185 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013186 return true;
13187}
13188
13189void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013190 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013191 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013192 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013193}
13194
David Majnemer9d168222016-08-05 17:44:54 +000013195void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13196 CXXScopeSpec &ScopeSpec,
13197 const DeclarationNameInfo &Id,
13198 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13199 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013200 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13201 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13202
13203 if (Lookup.isAmbiguous())
13204 return;
13205 Lookup.suppressDiagnostics();
13206
13207 if (!Lookup.isSingleResult()) {
13208 if (TypoCorrection Corrected =
13209 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13210 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13211 CTK_ErrorRecovery)) {
13212 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13213 << Id.getName());
13214 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13215 return;
13216 }
13217
13218 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13219 return;
13220 }
13221
13222 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013223 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13224 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013225 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13226 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013227 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13228 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13229 cast<ValueDecl>(ND));
13230 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013231 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013232 ND->addAttr(A);
13233 if (ASTMutationListener *ML = Context.getASTMutationListener())
13234 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013235 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013236 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013237 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13238 << Id.getName();
13239 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013240 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013241 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013242 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013243}
13244
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013245static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13246 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013247 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013248 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013249 auto *VD = cast<VarDecl>(D);
13250 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13251 return;
13252 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13253 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013254}
13255
13256static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13257 Sema &SemaRef, DSAStackTy *Stack,
13258 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013259 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13260 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13261 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013262}
13263
Kelvin Li1ce87c72017-12-12 20:08:12 +000013264void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13265 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013266 if (!D || D->isInvalidDecl())
13267 return;
13268 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013269 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013270 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013271 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013272 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13273 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013274 return;
13275 // 2.10.6: threadprivate variable cannot appear in a declare target
13276 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013277 if (DSAStack->isThreadPrivate(VD)) {
13278 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013279 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013280 return;
13281 }
13282 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013283 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13284 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013285 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013286 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13287 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13288 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013289 assert(IdLoc.isValid() && "Source location is expected");
13290 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13291 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13292 return;
13293 }
13294 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013295 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13296 // Problem if any with var declared with incomplete type will be reported
13297 // as normal, so no need to check it here.
13298 if ((E || !VD->getType()->isIncompleteType()) &&
13299 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13300 return;
13301 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13302 // Checking declaration inside declare target region.
13303 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13304 isa<FunctionTemplateDecl>(D)) {
13305 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13306 Context, OMPDeclareTargetDeclAttr::MT_To);
13307 D->addAttr(A);
13308 if (ASTMutationListener *ML = Context.getASTMutationListener())
13309 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13310 }
13311 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013312 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013313 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013314 if (!E)
13315 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013316 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13317}
Samuel Antao661c0902016-05-26 17:39:58 +000013318
13319OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13320 SourceLocation StartLoc,
13321 SourceLocation LParenLoc,
13322 SourceLocation EndLoc) {
13323 MappableVarListInfo MVLI(VarList);
13324 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13325 if (MVLI.ProcessedVarList.empty())
13326 return nullptr;
13327
13328 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13329 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13330 MVLI.VarComponents);
13331}
Samuel Antaoec172c62016-05-26 17:49:04 +000013332
13333OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13334 SourceLocation StartLoc,
13335 SourceLocation LParenLoc,
13336 SourceLocation EndLoc) {
13337 MappableVarListInfo MVLI(VarList);
13338 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13339 if (MVLI.ProcessedVarList.empty())
13340 return nullptr;
13341
13342 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13343 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13344 MVLI.VarComponents);
13345}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013346
13347OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13348 SourceLocation StartLoc,
13349 SourceLocation LParenLoc,
13350 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013351 MappableVarListInfo MVLI(VarList);
13352 SmallVector<Expr *, 8> PrivateCopies;
13353 SmallVector<Expr *, 8> Inits;
13354
Alexey Bataeve3727102018-04-18 15:57:46 +000013355 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013356 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13357 SourceLocation ELoc;
13358 SourceRange ERange;
13359 Expr *SimpleRefExpr = RefExpr;
13360 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13361 if (Res.second) {
13362 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013363 MVLI.ProcessedVarList.push_back(RefExpr);
13364 PrivateCopies.push_back(nullptr);
13365 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013366 }
13367 ValueDecl *D = Res.first;
13368 if (!D)
13369 continue;
13370
13371 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013372 Type = Type.getNonReferenceType().getUnqualifiedType();
13373
13374 auto *VD = dyn_cast<VarDecl>(D);
13375
13376 // Item should be a pointer or reference to pointer.
13377 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013378 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13379 << 0 << RefExpr->getSourceRange();
13380 continue;
13381 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013382
13383 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013384 auto VDPrivate =
13385 buildVarDecl(*this, ELoc, Type, D->getName(),
13386 D->hasAttrs() ? &D->getAttrs() : nullptr,
13387 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013388 if (VDPrivate->isInvalidDecl())
13389 continue;
13390
13391 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013392 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013393 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13394
13395 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013396 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013397 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013398 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13399 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013400 AddInitializerToDecl(VDPrivate,
13401 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013402 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013403
13404 // If required, build a capture to implement the privatization initialized
13405 // with the current list item value.
13406 DeclRefExpr *Ref = nullptr;
13407 if (!VD)
13408 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13409 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13410 PrivateCopies.push_back(VDPrivateRefExpr);
13411 Inits.push_back(VDInitRefExpr);
13412
13413 // We need to add a data sharing attribute for this variable to make sure it
13414 // is correctly captured. A variable that shows up in a use_device_ptr has
13415 // similar properties of a first private variable.
13416 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13417
13418 // Create a mappable component for the list item. List items in this clause
13419 // only need a component.
13420 MVLI.VarBaseDeclarations.push_back(D);
13421 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13422 MVLI.VarComponents.back().push_back(
13423 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013424 }
13425
Samuel Antaocc10b852016-07-28 14:23:26 +000013426 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013427 return nullptr;
13428
Samuel Antaocc10b852016-07-28 14:23:26 +000013429 return OMPUseDevicePtrClause::Create(
13430 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13431 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013432}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013433
13434OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13435 SourceLocation StartLoc,
13436 SourceLocation LParenLoc,
13437 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013438 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013439 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013440 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013441 SourceLocation ELoc;
13442 SourceRange ERange;
13443 Expr *SimpleRefExpr = RefExpr;
13444 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13445 if (Res.second) {
13446 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013447 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013448 }
13449 ValueDecl *D = Res.first;
13450 if (!D)
13451 continue;
13452
13453 QualType Type = D->getType();
13454 // item should be a pointer or array or reference to pointer or array
13455 if (!Type.getNonReferenceType()->isPointerType() &&
13456 !Type.getNonReferenceType()->isArrayType()) {
13457 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13458 << 0 << RefExpr->getSourceRange();
13459 continue;
13460 }
Samuel Antao6890b092016-07-28 14:25:09 +000013461
13462 // Check if the declaration in the clause does not show up in any data
13463 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013464 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013465 if (isOpenMPPrivate(DVar.CKind)) {
13466 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13467 << getOpenMPClauseName(DVar.CKind)
13468 << getOpenMPClauseName(OMPC_is_device_ptr)
13469 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013470 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013471 continue;
13472 }
13473
Alexey Bataeve3727102018-04-18 15:57:46 +000013474 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013475 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013476 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013477 [&ConflictExpr](
13478 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13479 OpenMPClauseKind) -> bool {
13480 ConflictExpr = R.front().getAssociatedExpression();
13481 return true;
13482 })) {
13483 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13484 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13485 << ConflictExpr->getSourceRange();
13486 continue;
13487 }
13488
13489 // Store the components in the stack so that they can be used to check
13490 // against other clauses later on.
13491 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13492 DSAStack->addMappableExpressionComponents(
13493 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13494
13495 // Record the expression we've just processed.
13496 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13497
13498 // Create a mappable component for the list item. List items in this clause
13499 // only need a component. We use a null declaration to signal fields in
13500 // 'this'.
13501 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13502 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13503 "Unexpected device pointer expression!");
13504 MVLI.VarBaseDeclarations.push_back(
13505 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13506 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13507 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013508 }
13509
Samuel Antao6890b092016-07-28 14:25:09 +000013510 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013511 return nullptr;
13512
Samuel Antao6890b092016-07-28 14:25:09 +000013513 return OMPIsDevicePtrClause::Create(
13514 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13515 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013516}