blob: e1a4c420d4024625e83e728e66b92454600577f4 [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 Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
Alexey Bataeve3727102018-04-18 15:57:46 +000078 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000079 OpenMPClauseKind Attributes = OMPC_unknown;
80 /// Pointer to a reference expression and a flag which shows that the
81 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000082 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 };
Alexey Bataeve3727102018-04-18 15:57:46 +000085 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
86 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
87 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
88 using LoopControlVariablesMapTy =
89 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000090 /// Struct that associates a component with the clause kind where they are
91 /// found.
92 struct MappedExprComponentTy {
93 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
94 OpenMPClauseKind Kind = OMPC_unknown;
95 };
Alexey Bataeve3727102018-04-18 15:57:46 +000096 using MappedExprComponentsTy =
97 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
98 using CriticalsWithHintsTy =
99 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
100 using DoacrossDependMapTy =
101 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
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 Bataeve3727102018-04-18 15:57:46 +0000140 llvm::PointerIntPair<const Expr *, 1, bool> 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
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000181 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000182
Alexey Bataevaac108a2015-06-23 04:51:00 +0000183 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000184 OpenMPClauseKind getClauseParsingMode() const {
185 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
186 return ClauseKindMode;
187 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000188 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000190 bool isForceVarCapturing() const { return ForceCapturing; }
191 void setForceVarCapturing(bool V) { ForceCapturing = V; }
192
Alexey Bataev758e55e2013-09-06 18:03:48 +0000193 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000194 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000195 if (Stack.empty() ||
196 Stack.back().second != CurrentNonCapturingFunctionScope)
197 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
198 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
199 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200 }
201
202 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000203 assert(!Stack.back().first.empty() &&
204 "Data-sharing attributes stack is empty!");
205 Stack.back().first.pop_back();
206 }
207
208 /// Start new OpenMP region stack in new non-capturing function.
209 void pushFunction() {
210 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
211 assert(!isa<CapturingScopeInfo>(CurFnScope));
212 CurrentNonCapturingFunctionScope = CurFnScope;
213 }
214 /// Pop region stack for non-capturing function.
215 void popFunction(const FunctionScopeInfo *OldFSI) {
216 if (!Stack.empty() && Stack.back().second == OldFSI) {
217 assert(Stack.back().first.empty());
218 Stack.pop_back();
219 }
220 CurrentNonCapturingFunctionScope = nullptr;
221 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
222 if (!isa<CapturingScopeInfo>(FSI)) {
223 CurrentNonCapturingFunctionScope = FSI;
224 break;
225 }
226 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227 }
228
Alexey Bataeve3727102018-04-18 15:57:46 +0000229 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000230 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000231 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000232 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000233 getCriticalWithHint(const DeclarationNameInfo &Name) const {
234 auto I = Criticals.find(Name.getAsString());
235 if (I != Criticals.end())
236 return I->second;
237 return std::make_pair(nullptr, llvm::APSInt());
238 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000239 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000240 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000241 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000242 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000244 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000245 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000246 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000247 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000248 /// \return The index of the loop control variable in the list of associated
249 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000250 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000251 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000252 /// parent region.
253 /// \return The index of the loop control variable in the list of associated
254 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000255 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000256 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000257 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000258 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000259
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000260 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000261 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000262 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
Alexey Bataevfa312f32017-07-21 18:48:21 +0000264 /// Adds additional information for the reduction items with the reduction id
265 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000266 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000267 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000268 /// Adds additional information for the reduction items with the reduction id
269 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000270 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000271 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000272 /// Returns the location and reduction operation from the innermost parent
273 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000274 const DSAVarData
275 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
276 BinaryOperatorKind &BOK,
277 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000278 /// Returns the location and reduction operation from the innermost parent
279 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000280 const DSAVarData
281 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
282 const Expr *&ReductionRef,
283 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000284 /// Return reduction reference expression for the current taskgroup.
285 Expr *getTaskgroupReductionRef() const {
286 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
287 "taskgroup reference expression requested for non taskgroup "
288 "directive.");
289 return Stack.back().first.back().TaskgroupReductionRef;
290 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000291 /// Checks if the given \p VD declaration is actually a taskgroup reduction
292 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000293 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000294 return Stack.back().first[Level].TaskgroupReductionRef &&
295 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
296 ->getDecl() == VD;
297 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000299 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000301 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000303 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000304 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000305 /// match specified \a CPred predicate in any directive which matches \a DPred
306 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000307 const DSAVarData
308 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
309 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
310 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000311 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000312 /// match specified \a CPred predicate in any innermost directive which
313 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000314 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000315 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000316 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
317 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000318 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000319 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000320 /// attributes which match specified \a CPred predicate at the specified
321 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000322 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000323 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000324 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000325
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000326 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000327 /// specified \a DPred predicate.
328 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000329 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000330 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000331
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000332 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000333 bool hasDirective(
334 const llvm::function_ref<bool(
335 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
336 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000337 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000338
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000339 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000341 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000342 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000343 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000344 OpenMPDirectiveKind getDirective(unsigned Level) const {
345 assert(!isStackEmpty() && "No directive at specified level.");
346 return Stack.back().first[Level].Directive;
347 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000348 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000349 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000350 if (isStackEmpty() || Stack.back().first.size() == 1)
351 return OMPD_unknown;
352 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000353 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000354
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000355 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000356 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000357 assert(!isStackEmpty());
358 Stack.back().first.back().DefaultAttr = DSA_none;
359 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000360 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000361 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000362 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000363 assert(!isStackEmpty());
364 Stack.back().first.back().DefaultAttr = DSA_shared;
365 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000366 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000367 /// Set default data mapping attribute to 'tofrom:scalar'.
368 void setDefaultDMAToFromScalar(SourceLocation Loc) {
369 assert(!isStackEmpty());
370 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
371 Stack.back().first.back().DefaultMapAttrLoc = Loc;
372 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373
374 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000375 return isStackEmpty() ? DSA_unspecified
376 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000378 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000379 return isStackEmpty() ? SourceLocation()
380 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000381 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000382 DefaultMapAttributes getDefaultDMA() const {
383 return isStackEmpty() ? DMA_unspecified
384 : Stack.back().first.back().DefaultMapAttr;
385 }
386 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
387 return Stack.back().first[Level].DefaultMapAttr;
388 }
389 SourceLocation getDefaultDMALocation() const {
390 return isStackEmpty() ? SourceLocation()
391 : Stack.back().first.back().DefaultMapAttrLoc;
392 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000394 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000395 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000396 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000397 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000398 }
399
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000400 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataeve3727102018-04-18 15:57:46 +0000401 void setOrderedRegion(bool IsOrdered, const Expr *Param) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000402 assert(!isStackEmpty());
403 Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
404 Stack.back().first.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000405 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000406 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000407 /// 'ordered' clause), false - otherwise.
408 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000409 if (isStackEmpty() || Stack.back().first.size() == 1)
410 return false;
411 return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000412 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000413 /// Returns optional parameter for the ordered region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000414 const Expr *getParentOrderedRegionParam() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000415 if (isStackEmpty() || Stack.back().first.size() == 1)
416 return nullptr;
417 return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
Alexey Bataev346265e2015-09-25 10:37:12 +0000418 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000419 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000420 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000421 assert(!isStackEmpty());
422 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000423 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000424 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000425 /// 'nowait' clause), false - otherwise.
426 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000427 if (isStackEmpty() || Stack.back().first.size() == 1)
428 return false;
429 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000430 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000431 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000432 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000433 if (!isStackEmpty() && Stack.back().first.size() > 1) {
434 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
435 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
436 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000437 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000438 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000439 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000440 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000441 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000442
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000443 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000444 void setAssociatedLoops(unsigned Val) {
445 assert(!isStackEmpty());
446 Stack.back().first.back().AssociatedLoops = Val;
447 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000448 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000449 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000450 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000451 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000453 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000454 /// region.
455 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000456 if (!isStackEmpty() && Stack.back().first.size() > 1) {
457 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
458 TeamsRegionLoc;
459 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000460 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000461 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000462 bool hasInnerTeamsRegion() const {
463 return getInnerTeamsRegionLoc().isValid();
464 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000465 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000466 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000467 return isStackEmpty() ? SourceLocation()
468 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000469 }
470
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000471 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000472 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000473 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000474 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000475 return isStackEmpty() ? SourceLocation()
476 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000477 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000478
Samuel Antao4c8035b2016-12-12 18:00:20 +0000479 /// Do the check specified in \a Check to all component lists and return true
480 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000481 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000482 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000483 const llvm::function_ref<
484 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000485 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000486 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000487 if (isStackEmpty())
488 return false;
489 auto SI = Stack.back().first.rbegin();
490 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000491
492 if (SI == SE)
493 return false;
494
Alexey Bataeve3727102018-04-18 15:57:46 +0000495 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000496 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000497 else
498 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000499
500 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000501 auto MI = SI->MappedExprComponents.find(VD);
502 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000503 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
504 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000505 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000506 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000507 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000508 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000509 }
510
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000511 /// Do the check specified in \a Check to all component lists at a given level
512 /// and return true if any issue is found.
513 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000514 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000515 const llvm::function_ref<
516 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000517 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000518 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000519 if (isStackEmpty())
520 return false;
521
522 auto StartI = Stack.back().first.begin();
523 auto EndI = Stack.back().first.end();
524 if (std::distance(StartI, EndI) <= (int)Level)
525 return false;
526 std::advance(StartI, Level);
527
528 auto MI = StartI->MappedExprComponents.find(VD);
529 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000530 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
531 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000532 if (Check(L, MI->second.Kind))
533 return true;
534 return false;
535 }
536
Samuel Antao4c8035b2016-12-12 18:00:20 +0000537 /// Create a new mappable expression component list associated with a given
538 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000539 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000540 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000541 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
542 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000543 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000544 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000545 MappedExprComponentTy &MEC =
546 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000547 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000548 MEC.Components.resize(MEC.Components.size() + 1);
549 MEC.Components.back().append(Components.begin(), Components.end());
550 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000551 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000552
553 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000554 assert(!isStackEmpty());
555 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000556 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000557 void addDoacrossDependClause(OMPDependClause *C,
558 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000559 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000560 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000561 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000562 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000563 }
564 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
565 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000566 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000567 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000568 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000569 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000570 return llvm::make_range(Ref.begin(), Ref.end());
571 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000572 return llvm::make_range(StackElem.DoacrossDepends.end(),
573 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000574 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000575};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000577 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
578 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000579}
Alexey Bataeve3727102018-04-18 15:57:46 +0000580
Alexey Bataeved09d242014-05-28 05:53:51 +0000581} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000582
Alexey Bataeve3727102018-04-18 15:57:46 +0000583static const Expr *getExprAsWritten(const Expr *E) {
584 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000585 E = ExprTemp->getSubExpr();
586
Alexey Bataeve3727102018-04-18 15:57:46 +0000587 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000588 E = MTE->GetTemporaryExpr();
589
Alexey Bataeve3727102018-04-18 15:57:46 +0000590 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000591 E = Binder->getSubExpr();
592
Alexey Bataeve3727102018-04-18 15:57:46 +0000593 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000594 E = ICE->getSubExprAsWritten();
595 return E->IgnoreParens();
596}
597
Alexey Bataeve3727102018-04-18 15:57:46 +0000598static Expr *getExprAsWritten(Expr *E) {
599 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
600}
601
602static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
603 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
604 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000605 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000606 const auto *VD = dyn_cast<VarDecl>(D);
607 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000608 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000609 VD = VD->getCanonicalDecl();
610 D = VD;
611 } else {
612 assert(FD);
613 FD = FD->getCanonicalDecl();
614 D = FD;
615 }
616 return D;
617}
618
Alexey Bataeve3727102018-04-18 15:57:46 +0000619static ValueDecl *getCanonicalDecl(ValueDecl *D) {
620 return const_cast<ValueDecl *>(
621 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
622}
623
624DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
625 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000626 D = getCanonicalDecl(D);
627 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000628 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000629 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000630 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000631 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
632 // in a region but not in construct]
633 // File-scope or namespace-scope variables referenced in called routines
634 // in the region are shared unless they appear in a threadprivate
635 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000636 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000637 DVar.CKind = OMPC_shared;
638
639 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
640 // in a region but not in construct]
641 // Variables with static storage duration that are declared in called
642 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000643 if (VD && VD->hasGlobalStorage())
644 DVar.CKind = OMPC_shared;
645
646 // Non-static data members are shared by default.
647 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000648 DVar.CKind = OMPC_shared;
649
Alexey Bataev758e55e2013-09-06 18:03:48 +0000650 return DVar;
651 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000652
Alexey Bataevec3da872014-01-31 05:15:34 +0000653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
654 // in a Construct, C/C++, predetermined, p.1]
655 // Variables with automatic storage duration that are declared in a scope
656 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000657 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
658 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000659 DVar.CKind = OMPC_private;
660 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000661 }
662
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000663 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 // Explicitly specified attributes and local variables with predetermined
665 // attributes.
666 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000667 const DSAInfo &Data = Iter->SharingMap.lookup(D);
668 DVar.RefExpr = Data.RefExpr.getPointer();
669 DVar.PrivateCopy = Data.PrivateCopy;
670 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000671 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000672 return DVar;
673 }
674
675 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
676 // in a Construct, C/C++, implicitly determined, p.1]
677 // In a parallel or task construct, the data-sharing attributes of these
678 // variables are determined by the default clause, if present.
679 switch (Iter->DefaultAttr) {
680 case DSA_shared:
681 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000682 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 return DVar;
684 case DSA_none:
685 return DVar;
686 case DSA_unspecified:
687 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
688 // in a Construct, implicitly determined, p.2]
689 // In a parallel construct, if no default clause is present, these
690 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000691 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000692 if (isOpenMPParallelDirective(DVar.DKind) ||
693 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000694 DVar.CKind = OMPC_shared;
695 return DVar;
696 }
697
698 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
699 // in a Construct, implicitly determined, p.4]
700 // In a task construct, if no default clause is present, a variable that in
701 // the enclosing context is determined to be shared by all implicit tasks
702 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000703 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000704 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000705 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000706 do {
707 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000708 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000709 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000710 // In a task construct, if no default clause is present, a variable
711 // whose data-sharing attribute is not determined by the rules above is
712 // firstprivate.
713 DVarTemp = getDSA(I, D);
714 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000715 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000716 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 return DVar;
718 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000719 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000720 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000721 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000722 return DVar;
723 }
724 }
725 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
726 // in a Construct, implicitly determined, p.3]
727 // For constructs other than task, if no default clause is present, these
728 // variables inherit their data-sharing attributes from the enclosing
729 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000730 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000731}
732
Alexey Bataeve3727102018-04-18 15:57:46 +0000733const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
734 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000735 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000736 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000737 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000738 auto It = StackElem.AlignedMap.find(D);
739 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000740 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000741 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000742 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000743 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000744 assert(It->second && "Unexpected nullptr expr in the aligned map");
745 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000746}
747
Alexey Bataeve3727102018-04-18 15:57:46 +0000748void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000749 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000750 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000751 SharingMapTy &StackElem = Stack.back().first.back();
752 StackElem.LCVMap.try_emplace(
753 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000754}
755
Alexey Bataeve3727102018-04-18 15:57:46 +0000756const DSAStackTy::LCDeclInfo
757DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000758 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000759 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000760 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000761 auto It = StackElem.LCVMap.find(D);
762 if (It != StackElem.LCVMap.end())
763 return It->second;
764 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000765}
766
Alexey Bataeve3727102018-04-18 15:57:46 +0000767const DSAStackTy::LCDeclInfo
768DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000769 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
770 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000772 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000773 auto It = StackElem.LCVMap.find(D);
774 if (It != StackElem.LCVMap.end())
775 return It->second;
776 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000777}
778
Alexey Bataeve3727102018-04-18 15:57:46 +0000779const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000780 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
781 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000782 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000783 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000784 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000785 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000786 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000787 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000788 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000789}
790
Alexey Bataeve3727102018-04-18 15:57:46 +0000791void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000792 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000794 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000795 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000796 Data.Attributes = A;
797 Data.RefExpr.setPointer(E);
798 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000799 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000800 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000801 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000802 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
803 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
804 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
805 (isLoopControlVariable(D).first && A == OMPC_private));
806 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
807 Data.RefExpr.setInt(/*IntVal=*/true);
808 return;
809 }
810 const bool IsLastprivate =
811 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
812 Data.Attributes = A;
813 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
814 Data.PrivateCopy = PrivateCopy;
815 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000816 DSAInfo &Data =
817 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000818 Data.Attributes = A;
819 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
820 Data.PrivateCopy = nullptr;
821 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000822 }
823}
824
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000825/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000826static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000827 StringRef Name, const AttrVec *Attrs = nullptr,
828 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000829 DeclContext *DC = SemaRef.CurContext;
830 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
831 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000832 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000833 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
834 if (Attrs) {
835 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
836 I != E; ++I)
837 Decl->addAttr(*I);
838 }
839 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000840 if (OrigRef) {
841 Decl->addAttr(
842 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
843 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000844 return Decl;
845}
846
847static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
848 SourceLocation Loc,
849 bool RefersToCapture = false) {
850 D->setReferenced();
851 D->markUsed(S.Context);
852 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
853 SourceLocation(), D, RefersToCapture, Loc, Ty,
854 VK_LValue);
855}
856
Alexey Bataeve3727102018-04-18 15:57:46 +0000857void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000858 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000859 D = getCanonicalDecl(D);
860 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000861 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000862 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000863 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000864 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000865 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000866 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000867 "Additional reduction info may be specified only once for reduction "
868 "items.");
869 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000870 Expr *&TaskgroupReductionRef =
871 Stack.back().first.back().TaskgroupReductionRef;
872 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000873 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
874 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000875 TaskgroupReductionRef =
876 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000877 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000878}
879
Alexey Bataeve3727102018-04-18 15:57:46 +0000880void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000881 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000882 D = getCanonicalDecl(D);
883 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000884 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000885 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000886 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000887 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000888 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000889 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000890 "Additional reduction info may be specified only once for reduction "
891 "items.");
892 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000893 Expr *&TaskgroupReductionRef =
894 Stack.back().first.back().TaskgroupReductionRef;
895 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000896 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
897 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000898 TaskgroupReductionRef =
899 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000900 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000901}
902
Alexey Bataeve3727102018-04-18 15:57:46 +0000903const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
904 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
905 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000906 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000907 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
908 if (Stack.back().first.empty())
909 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000910 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
911 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000912 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000913 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000914 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000915 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000916 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000917 if (!ReductionData.ReductionOp ||
918 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000919 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000920 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000921 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000922 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
923 "expression for the descriptor is not "
924 "set.");
925 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000926 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
927 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000928 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000929 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000930}
931
Alexey Bataeve3727102018-04-18 15:57:46 +0000932const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
933 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
934 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000935 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000936 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
937 if (Stack.back().first.empty())
938 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000939 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
940 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000941 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000942 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000943 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000944 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000945 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000946 if (!ReductionData.ReductionOp ||
947 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000948 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000949 SR = ReductionData.ReductionRange;
950 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000951 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
952 "expression for the descriptor is not "
953 "set.");
954 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000955 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
956 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000957 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000958 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000959}
960
Alexey Bataeve3727102018-04-18 15:57:46 +0000961bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000962 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +0000963 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000964 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000965 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +0000966 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
967 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +0000968 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000969 if (I == E)
970 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000971 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000972 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000973 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +0000974 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000975 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000977 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978}
979
Alexey Bataeve3727102018-04-18 15:57:46 +0000980const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
981 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000983 DSAVarData DVar;
984
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000985 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000986 auto TI = Threadprivates.find(D);
987 if (TI != Threadprivates.end()) {
988 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000989 DVar.CKind = OMPC_threadprivate;
990 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +0000991 }
992 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +0000993 DVar.RefExpr = buildDeclRefExpr(
994 SemaRef, VD, D->getType().getNonReferenceType(),
995 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
996 DVar.CKind = OMPC_threadprivate;
997 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +0000998 return DVar;
999 }
1000 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1001 // in a Construct, C/C++, predetermined, p.1]
1002 // Variables appearing in threadprivate directives are threadprivate.
1003 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1004 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1005 SemaRef.getLangOpts().OpenMPUseTLS &&
1006 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1007 (VD && VD->getStorageClass() == SC_Register &&
1008 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1009 DVar.RefExpr = buildDeclRefExpr(
1010 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1011 DVar.CKind = OMPC_threadprivate;
1012 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1013 return DVar;
1014 }
1015 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1016 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1017 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001018 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001019 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1020 [](const SharingMapTy &Data) {
1021 return isOpenMPTargetExecutionDirective(Data.Directive);
1022 });
1023 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001024 iterator ParentIterTarget = std::next(IterTarget, 1);
1025 for (iterator Iter = Stack.back().first.rbegin();
1026 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001027 if (isOpenMPLocal(VD, Iter)) {
1028 DVar.RefExpr =
1029 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1030 D->getLocation());
1031 DVar.CKind = OMPC_threadprivate;
1032 return DVar;
1033 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001034 }
1035 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1036 auto DSAIter = IterTarget->SharingMap.find(D);
1037 if (DSAIter != IterTarget->SharingMap.end() &&
1038 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1039 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1040 DVar.CKind = OMPC_threadprivate;
1041 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001042 }
1043 iterator End = Stack.back().first.rend();
1044 if (!SemaRef.isOpenMPCapturedByRef(
1045 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001046 DVar.RefExpr =
1047 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1048 IterTarget->ConstructLoc);
1049 DVar.CKind = OMPC_threadprivate;
1050 return DVar;
1051 }
1052 }
1053 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001054 }
1055
Alexey Bataev4b465392017-04-26 15:06:24 +00001056 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001057 // Not in OpenMP execution region and top scope was already checked.
1058 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001059
Alexey Bataev758e55e2013-09-06 18:03:48 +00001060 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001061 // in a Construct, C/C++, predetermined, p.4]
1062 // Static data members are shared.
1063 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1064 // in a Construct, C/C++, predetermined, p.7]
1065 // Variables with static storage duration that are declared in a scope
1066 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001067 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001068 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001069 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001070 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001071 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001072
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001073 DVar.CKind = OMPC_shared;
1074 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075 }
1076
1077 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001078 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1079 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1081 // in a Construct, C/C++, predetermined, p.6]
1082 // Variables with const qualified type having no mutable member are
1083 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001084 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001085 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001086 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1087 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001088 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001089 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001090 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1091 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001092 // Variables with const-qualified type having no mutable member may be
1093 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001094 DSAVarData DVarTemp =
1095 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1096 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001097 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001098 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001099
Alexey Bataev758e55e2013-09-06 18:03:48 +00001100 DVar.CKind = OMPC_shared;
1101 return DVar;
1102 }
1103
Alexey Bataev758e55e2013-09-06 18:03:48 +00001104 // Explicitly specified attributes and local variables with predetermined
1105 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001106 iterator I = Stack.back().first.rbegin();
1107 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001108 if (FromParent && I != EndI)
1109 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001110 auto It = I->SharingMap.find(D);
1111 if (It != I->SharingMap.end()) {
1112 const DSAInfo &Data = It->getSecond();
1113 DVar.RefExpr = Data.RefExpr.getPointer();
1114 DVar.PrivateCopy = Data.PrivateCopy;
1115 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001116 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001117 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001118 }
1119
1120 return DVar;
1121}
1122
Alexey Bataeve3727102018-04-18 15:57:46 +00001123const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1124 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001125 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001126 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001127 return getDSA(I, D);
1128 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001129 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001130 iterator StartI = Stack.back().first.rbegin();
1131 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001132 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001133 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001134 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001135}
1136
Alexey Bataeve3727102018-04-18 15:57:46 +00001137const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001138DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001139 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1140 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001141 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001142 if (isStackEmpty())
1143 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001144 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001145 iterator I = Stack.back().first.rbegin();
1146 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001147 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001148 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001149 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001150 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001151 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001152 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001153 DSAVarData DVar = getDSA(NewI, D);
1154 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001155 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001156 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001157 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001158}
1159
Alexey Bataeve3727102018-04-18 15:57:46 +00001160const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001161 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1162 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001163 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001164 if (isStackEmpty())
1165 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001166 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001167 iterator StartI = Stack.back().first.rbegin();
1168 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001169 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001170 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001171 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001172 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001173 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001174 DSAVarData DVar = getDSA(NewI, D);
1175 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001176}
1177
Alexey Bataevaac108a2015-06-23 04:51:00 +00001178bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001179 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1180 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001181 if (isStackEmpty())
1182 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001183 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001184 auto StartI = Stack.back().first.begin();
1185 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001186 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001187 return false;
1188 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001189 auto I = StartI->SharingMap.find(D);
1190 return (I != StartI->SharingMap.end()) &&
1191 I->getSecond().RefExpr.getPointer() &&
1192 CPred(I->getSecond().Attributes) &&
1193 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001194}
1195
Samuel Antao4be30e92015-10-02 17:14:03 +00001196bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001197 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1198 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001199 if (isStackEmpty())
1200 return false;
1201 auto StartI = Stack.back().first.begin();
1202 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001203 if (std::distance(StartI, EndI) <= (int)Level)
1204 return false;
1205 std::advance(StartI, Level);
1206 return DPred(StartI->Directive);
1207}
1208
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001209bool DSAStackTy::hasDirective(
1210 const llvm::function_ref<bool(OpenMPDirectiveKind,
1211 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001212 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001213 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001214 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001215 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001216 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001217 auto StartI = std::next(Stack.back().first.rbegin());
1218 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001219 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001220 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001221 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1222 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1223 return true;
1224 }
1225 return false;
1226}
1227
Alexey Bataev758e55e2013-09-06 18:03:48 +00001228void Sema::InitDataSharingAttributesStack() {
1229 VarDataSharingAttributesStack = new DSAStackTy(*this);
1230}
1231
1232#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1233
Alexey Bataev4b465392017-04-26 15:06:24 +00001234void Sema::pushOpenMPFunctionRegion() {
1235 DSAStack->pushFunction();
1236}
1237
1238void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1239 DSAStack->popFunction(OldFSI);
1240}
1241
Alexey Bataev92327c52018-03-26 16:40:55 +00001242static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
1243isDeclareTargetDeclaration(const ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001244 for (const Decl *D : VD->redecls()) {
Alexey Bataev92327c52018-03-26 16:40:55 +00001245 if (!D->hasAttrs())
1246 continue;
1247 if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
1248 return Attr->getMapType();
1249 }
1250 return llvm::None;
1251}
1252
Alexey Bataeve3727102018-04-18 15:57:46 +00001253bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001254 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1255
Alexey Bataeve3727102018-04-18 15:57:46 +00001256 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001257 bool IsByRef = true;
1258
1259 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001260 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001261 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001262
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001263 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001264 // This table summarizes how a given variable should be passed to the device
1265 // given its type and the clauses where it appears. This table is based on
1266 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1267 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1268 //
1269 // =========================================================================
1270 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1271 // | |(tofrom:scalar)| | pvt | | | |
1272 // =========================================================================
1273 // | scl | | | | - | | bycopy|
1274 // | scl | | - | x | - | - | bycopy|
1275 // | scl | | x | - | - | - | null |
1276 // | scl | x | | | - | | byref |
1277 // | scl | x | - | x | - | - | bycopy|
1278 // | scl | x | x | - | - | - | null |
1279 // | scl | | - | - | - | x | byref |
1280 // | scl | x | - | - | - | x | byref |
1281 //
1282 // | agg | n.a. | | | - | | byref |
1283 // | agg | n.a. | - | x | - | - | byref |
1284 // | agg | n.a. | x | - | - | - | null |
1285 // | agg | n.a. | - | - | - | x | byref |
1286 // | agg | n.a. | - | - | - | x[] | byref |
1287 //
1288 // | ptr | n.a. | | | - | | bycopy|
1289 // | ptr | n.a. | - | x | - | - | bycopy|
1290 // | ptr | n.a. | x | - | - | - | null |
1291 // | ptr | n.a. | - | - | - | x | byref |
1292 // | ptr | n.a. | - | - | - | x[] | bycopy|
1293 // | ptr | n.a. | - | - | x | | bycopy|
1294 // | ptr | n.a. | - | - | x | x | bycopy|
1295 // | ptr | n.a. | - | - | x | x[] | bycopy|
1296 // =========================================================================
1297 // Legend:
1298 // scl - scalar
1299 // ptr - pointer
1300 // agg - aggregate
1301 // x - applies
1302 // - - invalid in this combination
1303 // [] - mapped with an array section
1304 // byref - should be mapped by reference
1305 // byval - should be mapped by value
1306 // null - initialize a local variable to null on the device
1307 //
1308 // Observations:
1309 // - All scalar declarations that show up in a map clause have to be passed
1310 // by reference, because they may have been mapped in the enclosing data
1311 // environment.
1312 // - If the scalar value does not fit the size of uintptr, it has to be
1313 // passed by reference, regardless the result in the table above.
1314 // - For pointers mapped by value that have either an implicit map or an
1315 // array section, the runtime library may pass the NULL value to the
1316 // device instead of the value passed to it by the compiler.
1317
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001318 if (Ty->isReferenceType())
1319 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001320
1321 // Locate map clauses and see if the variable being captured is referred to
1322 // in any of those clauses. Here we only care about variables, not fields,
1323 // because fields are part of aggregates.
1324 bool IsVariableUsedInMapClause = false;
1325 bool IsVariableAssociatedWithSection = false;
1326
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001327 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001328 D, Level,
1329 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1330 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001331 MapExprComponents,
1332 OpenMPClauseKind WhereFoundClauseKind) {
1333 // Only the map clause information influences how a variable is
1334 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001335 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001336 if (WhereFoundClauseKind != OMPC_map)
1337 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001338
1339 auto EI = MapExprComponents.rbegin();
1340 auto EE = MapExprComponents.rend();
1341
1342 assert(EI != EE && "Invalid map expression!");
1343
1344 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1345 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1346
1347 ++EI;
1348 if (EI == EE)
1349 return false;
1350
1351 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1352 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1353 isa<MemberExpr>(EI->getAssociatedExpression())) {
1354 IsVariableAssociatedWithSection = true;
1355 // There is nothing more we need to know about this variable.
1356 return true;
1357 }
1358
1359 // Keep looking for more map info.
1360 return false;
1361 });
1362
1363 if (IsVariableUsedInMapClause) {
1364 // If variable is identified in a map clause it is always captured by
1365 // reference except if it is a pointer that is dereferenced somehow.
1366 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1367 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001368 // By default, all the data that has a scalar type is mapped by copy
1369 // (except for reduction variables).
1370 IsByRef =
1371 !Ty->isScalarType() ||
1372 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1373 DSAStack->hasExplicitDSA(
1374 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001375 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001376 }
1377
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001378 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001379 IsByRef =
1380 !DSAStack->hasExplicitDSA(
1381 D,
1382 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1383 Level, /*NotLastprivate=*/true) &&
1384 // If the variable is artificial and must be captured by value - try to
1385 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001386 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1387 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001388 }
1389
Samuel Antao86ace552016-04-27 22:40:57 +00001390 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001391 // and alignment, because the runtime library only deals with uintptr types.
1392 // If it does not fit the uintptr size, we need to pass the data by reference
1393 // instead.
1394 if (!IsByRef &&
1395 (Ctx.getTypeSizeInChars(Ty) >
1396 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001397 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001398 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001399 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001400
1401 return IsByRef;
1402}
1403
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001404unsigned Sema::getOpenMPNestingLevel() const {
1405 assert(getLangOpts().OpenMP);
1406 return DSAStack->getNestingLevel();
1407}
1408
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001409bool Sema::isInOpenMPTargetExecutionDirective() const {
1410 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1411 !DSAStack->isClauseParsingMode()) ||
1412 DSAStack->hasDirective(
1413 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1414 SourceLocation) -> bool {
1415 return isOpenMPTargetExecutionDirective(K);
1416 },
1417 false);
1418}
1419
Alexey Bataeve3727102018-04-18 15:57:46 +00001420VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) const {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001421 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001422 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001423
1424 // If we are attempting to capture a global variable in a directive with
1425 // 'target' we return true so that this global is also mapped to the device.
1426 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001427 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001428 if (VD && !VD->hasLocalStorage() && isInOpenMPTargetExecutionDirective()) {
1429 // If the declaration is enclosed in a 'declare target' directive,
1430 // then it should not be captured.
1431 //
Alexey Bataev92327c52018-03-26 16:40:55 +00001432 if (isDeclareTargetDeclaration(VD))
1433 return nullptr;
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001434 return VD;
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001435 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001436
Alexey Bataev48977c32015-08-04 08:10:48 +00001437 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1438 (!DSAStack->isClauseParsingMode() ||
1439 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001440 auto &&Info = DSAStack->isLoopControlVariable(D);
1441 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001442 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001443 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001444 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001445 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001446 DSAStackTy::DSAVarData DVarPrivate =
1447 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001448 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001449 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001450 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1451 [](OpenMPDirectiveKind) { return true; },
1452 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001453 if (DVarPrivate.CKind != OMPC_unknown)
1454 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001455 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001456 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001457}
1458
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001459void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1460 unsigned Level) const {
1461 SmallVector<OpenMPDirectiveKind, 4> Regions;
1462 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1463 FunctionScopesIndex -= Regions.size();
1464}
1465
Alexey Bataeve3727102018-04-18 15:57:46 +00001466bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001467 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1468 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001469 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001470 (DSAStack->isClauseParsingMode() &&
1471 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001472 // Consider taskgroup reduction descriptor variable a private to avoid
1473 // possible capture in the region.
1474 (DSAStack->hasExplicitDirective(
1475 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1476 Level) &&
1477 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001478}
1479
Alexey Bataeve3727102018-04-18 15:57:46 +00001480void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1481 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001482 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1483 D = getCanonicalDecl(D);
1484 OpenMPClauseKind OMPC = OMPC_unknown;
1485 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1486 const unsigned NewLevel = I - 1;
1487 if (DSAStack->hasExplicitDSA(D,
1488 [&OMPC](const OpenMPClauseKind K) {
1489 if (isOpenMPPrivate(K)) {
1490 OMPC = K;
1491 return true;
1492 }
1493 return false;
1494 },
1495 NewLevel))
1496 break;
1497 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1498 D, NewLevel,
1499 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1500 OpenMPClauseKind) { return true; })) {
1501 OMPC = OMPC_map;
1502 break;
1503 }
1504 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1505 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001506 OMPC = OMPC_map;
1507 if (D->getType()->isScalarType() &&
1508 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1509 DefaultMapAttributes::DMA_tofrom_scalar)
1510 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001511 break;
1512 }
1513 }
1514 if (OMPC != OMPC_unknown)
1515 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1516}
1517
Alexey Bataeve3727102018-04-18 15:57:46 +00001518bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1519 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001520 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1521 // Return true if the current level is no longer enclosed in a target region.
1522
Alexey Bataeve3727102018-04-18 15:57:46 +00001523 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001524 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001525 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1526 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001527}
1528
Alexey Bataeved09d242014-05-28 05:53:51 +00001529void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001530
1531void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1532 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001533 Scope *CurScope, SourceLocation Loc) {
1534 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001535 PushExpressionEvaluationContext(
1536 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001537}
1538
Alexey Bataevaac108a2015-06-23 04:51:00 +00001539void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1540 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001541}
1542
Alexey Bataevaac108a2015-06-23 04:51:00 +00001543void Sema::EndOpenMPClause() {
1544 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001545}
1546
Alexey Bataev758e55e2013-09-06 18:03:48 +00001547void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001548 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1549 // A variable of class type (or array thereof) that appears in a lastprivate
1550 // clause requires an accessible, unambiguous default constructor for the
1551 // class type, unless the list item is also specified in a firstprivate
1552 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001553 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1554 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001555 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1556 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001557 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001558 if (DE->isValueDependent() || DE->isTypeDependent()) {
1559 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001560 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001561 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001562 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001563 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001564 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001565 const DSAStackTy::DSAVarData DVar =
1566 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001567 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001568 // Generate helper private variable and initialize it with the
1569 // default value. The address of the original variable is replaced
1570 // by the address of the new private variable in CodeGen. This new
1571 // variable is not added to IdResolver, so the code in the OpenMP
1572 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001573 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001574 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001575 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001576 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001577 if (VDPrivate->isInvalidDecl())
1578 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001579 PrivateCopies.push_back(buildDeclRefExpr(
1580 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001581 } else {
1582 // The variable is also a firstprivate, so initialization sequence
1583 // for private copy is generated already.
1584 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001585 }
1586 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001587 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001588 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001589 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001590 }
1591 }
1592 }
1593
Alexey Bataev758e55e2013-09-06 18:03:48 +00001594 DSAStack->pop();
1595 DiscardCleanupsInEvaluationContext();
1596 PopExpressionEvaluationContext();
1597}
1598
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001599static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1600 Expr *NumIterations, Sema &SemaRef,
1601 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001602
Alexey Bataeva769e072013-03-22 06:34:35 +00001603namespace {
1604
Alexey Bataeve3727102018-04-18 15:57:46 +00001605class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001606private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001607 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001608
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001609public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001610 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001611 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001612 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001613 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001614 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001615 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1616 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001617 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001618 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001619 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001620};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001621
Alexey Bataeve3727102018-04-18 15:57:46 +00001622class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001623private:
1624 Sema &SemaRef;
1625
1626public:
1627 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1628 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1629 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001630 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001631 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1632 SemaRef.getCurScope());
1633 }
1634 return false;
1635 }
1636};
1637
Alexey Bataeved09d242014-05-28 05:53:51 +00001638} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001639
1640ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1641 CXXScopeSpec &ScopeSpec,
1642 const DeclarationNameInfo &Id) {
1643 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1644 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1645
1646 if (Lookup.isAmbiguous())
1647 return ExprError();
1648
1649 VarDecl *VD;
1650 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001651 if (TypoCorrection Corrected = CorrectTypo(
1652 Id, LookupOrdinaryName, CurScope, nullptr,
1653 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001654 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001655 PDiag(Lookup.empty()
1656 ? diag::err_undeclared_var_use_suggest
1657 : diag::err_omp_expected_var_arg_suggest)
1658 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001659 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001660 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001661 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1662 : diag::err_omp_expected_var_arg)
1663 << Id.getName();
1664 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001665 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001666 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1667 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1668 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1669 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001670 }
1671 Lookup.suppressDiagnostics();
1672
1673 // OpenMP [2.9.2, Syntax, C/C++]
1674 // Variables must be file-scope, namespace-scope, or static block-scope.
1675 if (!VD->hasGlobalStorage()) {
1676 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001677 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1678 bool IsDecl =
1679 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001680 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001681 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1682 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001683 return ExprError();
1684 }
1685
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001686 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001687 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001688 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1689 // A threadprivate directive for file-scope variables must appear outside
1690 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001691 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1692 !getCurLexicalContext()->isTranslationUnit()) {
1693 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001694 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1695 bool IsDecl =
1696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1697 Diag(VD->getLocation(),
1698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1699 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001700 return ExprError();
1701 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001702 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1703 // A threadprivate directive for static class member variables must appear
1704 // in the class definition, in the same scope in which the member
1705 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001706 if (CanonicalVD->isStaticDataMember() &&
1707 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1708 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001709 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1710 bool IsDecl =
1711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1712 Diag(VD->getLocation(),
1713 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1714 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001715 return ExprError();
1716 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001717 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1718 // A threadprivate directive for namespace-scope variables must appear
1719 // outside any definition or declaration other than the namespace
1720 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001721 if (CanonicalVD->getDeclContext()->isNamespace() &&
1722 (!getCurLexicalContext()->isFileContext() ||
1723 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1724 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001725 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1726 bool IsDecl =
1727 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1728 Diag(VD->getLocation(),
1729 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1730 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001731 return ExprError();
1732 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001733 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1734 // A threadprivate directive for static block-scope variables must appear
1735 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001736 if (CanonicalVD->isStaticLocal() && CurScope &&
1737 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001738 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001739 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1740 bool IsDecl =
1741 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1742 Diag(VD->getLocation(),
1743 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1744 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001745 return ExprError();
1746 }
1747
1748 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1749 // A threadprivate directive must lexically precede all references to any
1750 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001751 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001752 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001753 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001754 return ExprError();
1755 }
1756
1757 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001758 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1759 SourceLocation(), VD,
1760 /*RefersToEnclosingVariableOrCapture=*/false,
1761 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001762}
1763
Alexey Bataeved09d242014-05-28 05:53:51 +00001764Sema::DeclGroupPtrTy
1765Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1766 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001767 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001768 CurContext->addDecl(D);
1769 return DeclGroupPtrTy::make(DeclGroupRef(D));
1770 }
David Blaikie0403cb12016-01-15 23:43:25 +00001771 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001772}
1773
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001774namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001775class LocalVarRefChecker final
1776 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001777 Sema &SemaRef;
1778
1779public:
1780 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001781 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001782 if (VD->hasLocalStorage()) {
1783 SemaRef.Diag(E->getLocStart(),
1784 diag::err_omp_local_var_in_threadprivate_init)
1785 << E->getSourceRange();
1786 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1787 << VD << VD->getSourceRange();
1788 return true;
1789 }
1790 }
1791 return false;
1792 }
1793 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001794 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001795 if (Child && Visit(Child))
1796 return true;
1797 }
1798 return false;
1799 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001800 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001801};
1802} // namespace
1803
Alexey Bataeved09d242014-05-28 05:53:51 +00001804OMPThreadPrivateDecl *
1805Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001806 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001807 for (Expr *RefExpr : VarList) {
1808 auto *DE = cast<DeclRefExpr>(RefExpr);
1809 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001810 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001811
Alexey Bataev376b4a42016-02-09 09:41:09 +00001812 // Mark variable as used.
1813 VD->setReferenced();
1814 VD->markUsed(Context);
1815
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001816 QualType QType = VD->getType();
1817 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1818 // It will be analyzed later.
1819 Vars.push_back(DE);
1820 continue;
1821 }
1822
Alexey Bataeva769e072013-03-22 06:34:35 +00001823 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1824 // A threadprivate variable must not have an incomplete type.
1825 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001826 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001827 continue;
1828 }
1829
1830 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1831 // A threadprivate variable must not have a reference type.
1832 if (VD->getType()->isReferenceType()) {
1833 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001834 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1835 bool IsDecl =
1836 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1837 Diag(VD->getLocation(),
1838 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1839 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001840 continue;
1841 }
1842
Samuel Antaof8b50122015-07-13 22:54:53 +00001843 // Check if this is a TLS variable. If TLS is not being supported, produce
1844 // the corresponding diagnostic.
1845 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1846 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1847 getLangOpts().OpenMPUseTLS &&
1848 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001849 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1850 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001851 Diag(ILoc, diag::err_omp_var_thread_local)
1852 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001853 bool IsDecl =
1854 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1855 Diag(VD->getLocation(),
1856 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1857 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001858 continue;
1859 }
1860
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001861 // Check if initial value of threadprivate variable reference variable with
1862 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00001863 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001864 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001865 if (Checker.Visit(Init))
1866 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001867 }
1868
Alexey Bataeved09d242014-05-28 05:53:51 +00001869 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001870 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001871 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1872 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00001873 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00001874 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001875 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001876 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001877 if (!Vars.empty()) {
1878 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1879 Vars);
1880 D->setAccess(AS_public);
1881 }
1882 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001883}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001884
Alexey Bataeve3727102018-04-18 15:57:46 +00001885static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
1886 const ValueDecl *D,
1887 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001888 bool IsLoopIterVar = false) {
1889 if (DVar.RefExpr) {
1890 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1891 << getOpenMPClauseName(DVar.CKind);
1892 return;
1893 }
1894 enum {
1895 PDSA_StaticMemberShared,
1896 PDSA_StaticLocalVarShared,
1897 PDSA_LoopIterVarPrivate,
1898 PDSA_LoopIterVarLinear,
1899 PDSA_LoopIterVarLastprivate,
1900 PDSA_ConstVarShared,
1901 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001902 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001903 PDSA_LocalVarPrivate,
1904 PDSA_Implicit
1905 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001906 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001907 auto ReportLoc = D->getLocation();
1908 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001909 if (IsLoopIterVar) {
1910 if (DVar.CKind == OMPC_private)
1911 Reason = PDSA_LoopIterVarPrivate;
1912 else if (DVar.CKind == OMPC_lastprivate)
1913 Reason = PDSA_LoopIterVarLastprivate;
1914 else
1915 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001916 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1917 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001918 Reason = PDSA_TaskVarFirstprivate;
1919 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001920 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001921 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001922 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001923 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001924 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001925 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001926 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001927 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001928 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001929 ReportHint = true;
1930 Reason = PDSA_LocalVarPrivate;
1931 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001932 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001933 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001934 << Reason << ReportHint
1935 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1936 } else if (DVar.ImplicitDSALoc.isValid()) {
1937 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1938 << getOpenMPClauseName(DVar.CKind);
1939 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001940}
1941
Alexey Bataev758e55e2013-09-06 18:03:48 +00001942namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001943class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001944 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001945 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00001946 bool ErrorFound = false;
1947 CapturedStmt *CS = nullptr;
1948 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
1949 llvm::SmallVector<Expr *, 4> ImplicitMap;
1950 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
1951 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00001952
Alexey Bataev758e55e2013-09-06 18:03:48 +00001953public:
1954 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001955 if (E->isTypeDependent() || E->isValueDependent() ||
1956 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1957 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001958 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001959 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001960 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00001961 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00001962 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001963
Alexey Bataeve3727102018-04-18 15:57:46 +00001964 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001965 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001966 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00001967 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001968
Alexey Bataevafe50572017-10-06 17:00:28 +00001969 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00001970 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
1971 isDeclareTargetDeclaration(VD);
1972 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
1973 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00001974 return;
1975
Alexey Bataeve3727102018-04-18 15:57:46 +00001976 SourceLocation ELoc = E->getExprLoc();
1977 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001978 // The default(none) clause requires that each variable that is referenced
1979 // in the construct, and does not have a predetermined data-sharing
1980 // attribute, must have its data-sharing attribute explicitly determined
1981 // by being listed in a data-sharing attribute clause.
1982 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001983 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001984 VarsWithInheritedDSA.count(VD) == 0) {
1985 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001986 return;
1987 }
1988
Alexey Bataevf47c4b42017-09-26 13:47:31 +00001989 if (isOpenMPTargetExecutionDirective(DKind) &&
1990 !Stack->isLoopControlVariable(VD).first) {
1991 if (!Stack->checkMappableExprComponentListsForDecl(
1992 VD, /*CurrentRegionOnly=*/true,
1993 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
1994 StackComponents,
1995 OpenMPClauseKind) {
1996 // Variable is used if it has been marked as an array, array
1997 // section or the variable iself.
1998 return StackComponents.size() == 1 ||
1999 std::all_of(
2000 std::next(StackComponents.rbegin()),
2001 StackComponents.rend(),
2002 [](const OMPClauseMappableExprCommon::
2003 MappableComponent &MC) {
2004 return MC.getAssociatedDeclaration() ==
2005 nullptr &&
2006 (isa<OMPArraySectionExpr>(
2007 MC.getAssociatedExpression()) ||
2008 isa<ArraySubscriptExpr>(
2009 MC.getAssociatedExpression()));
2010 });
2011 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002012 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002013 // By default lambdas are captured as firstprivates.
2014 if (const auto *RD =
2015 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002016 IsFirstprivate = RD->isLambda();
2017 IsFirstprivate =
2018 IsFirstprivate ||
2019 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002020 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002021 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002022 ImplicitFirstprivate.emplace_back(E);
2023 else
2024 ImplicitMap.emplace_back(E);
2025 return;
2026 }
2027 }
2028
Alexey Bataev758e55e2013-09-06 18:03:48 +00002029 // OpenMP [2.9.3.6, Restrictions, p.2]
2030 // A list item that appears in a reduction clause of the innermost
2031 // enclosing worksharing or parallel construct may not be accessed in an
2032 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002033 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002034 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2035 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002036 return isOpenMPParallelDirective(K) ||
2037 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2038 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002039 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002040 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002041 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002042 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002043 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002044 return;
2045 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002046
2047 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002048 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002049 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2050 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002051 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002052 }
2053 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002054 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002055 if (E->isTypeDependent() || E->isValueDependent() ||
2056 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2057 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002058 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002059 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002060 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002061 if (!FD)
2062 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002063 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002064 // Check if the variable has explicit DSA set and stop analysis if it
2065 // so.
2066 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2067 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002068
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002069 if (isOpenMPTargetExecutionDirective(DKind) &&
2070 !Stack->isLoopControlVariable(FD).first &&
2071 !Stack->checkMappableExprComponentListsForDecl(
2072 FD, /*CurrentRegionOnly=*/true,
2073 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2074 StackComponents,
2075 OpenMPClauseKind) {
2076 return isa<CXXThisExpr>(
2077 cast<MemberExpr>(
2078 StackComponents.back().getAssociatedExpression())
2079 ->getBase()
2080 ->IgnoreParens());
2081 })) {
2082 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2083 // A bit-field cannot appear in a map clause.
2084 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002085 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002086 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002087 ImplicitMap.emplace_back(E);
2088 return;
2089 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002090
Alexey Bataeve3727102018-04-18 15:57:46 +00002091 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002092 // OpenMP [2.9.3.6, Restrictions, p.2]
2093 // A list item that appears in a reduction clause of the innermost
2094 // enclosing worksharing or parallel construct may not be accessed in
2095 // an explicit task.
2096 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002097 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2098 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002099 return isOpenMPParallelDirective(K) ||
2100 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2101 },
2102 /*FromParent=*/true);
2103 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2104 ErrorFound = true;
2105 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002106 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002107 return;
2108 }
2109
2110 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002111 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002112 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2113 !Stack->isLoopControlVariable(FD).first)
2114 ImplicitFirstprivate.push_back(E);
2115 return;
2116 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002117 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002118 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002119 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002120 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002121 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002122 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002123 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2124 if (!Stack->checkMappableExprComponentListsForDecl(
2125 VD, /*CurrentRegionOnly=*/true,
2126 [&CurComponents](
2127 OMPClauseMappableExprCommon::MappableExprComponentListRef
2128 StackComponents,
2129 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002130 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002131 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002132 for (const auto &SC : llvm::reverse(StackComponents)) {
2133 // Do both expressions have the same kind?
2134 if (CCI->getAssociatedExpression()->getStmtClass() !=
2135 SC.getAssociatedExpression()->getStmtClass())
2136 if (!(isa<OMPArraySectionExpr>(
2137 SC.getAssociatedExpression()) &&
2138 isa<ArraySubscriptExpr>(
2139 CCI->getAssociatedExpression())))
2140 return false;
2141
Alexey Bataeve3727102018-04-18 15:57:46 +00002142 const Decl *CCD = CCI->getAssociatedDeclaration();
2143 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002144 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2145 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2146 if (SCD != CCD)
2147 return false;
2148 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002149 if (CCI == CCE)
2150 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002151 }
2152 return true;
2153 })) {
2154 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002155 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002156 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002157 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002158 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002159 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002160 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002161 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002162 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002163 // for task|target directives.
2164 // Skip analysis of arguments of implicitly defined map clause for target
2165 // directives.
2166 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2167 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002168 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002169 if (CC)
2170 Visit(CC);
2171 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002172 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002174 }
2175 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002176 for (Stmt *C : S->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002177 if (C && !isa<OMPExecutableDirective>(C))
2178 Visit(C);
2179 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002180 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002181
Alexey Bataeve3727102018-04-18 15:57:46 +00002182 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002183 ArrayRef<Expr *> getImplicitFirstprivate() const {
2184 return ImplicitFirstprivate;
2185 }
2186 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002187 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002188 return VarsWithInheritedDSA;
2189 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002190
Alexey Bataev7ff55242014-06-19 09:13:45 +00002191 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2192 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002193};
Alexey Bataeved09d242014-05-28 05:53:51 +00002194} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002195
Alexey Bataevbae9a792014-06-27 10:37:06 +00002196void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002197 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002198 case OMPD_parallel:
2199 case OMPD_parallel_for:
2200 case OMPD_parallel_for_simd:
2201 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002202 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002203 case OMPD_teams_distribute:
2204 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002205 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002206 QualType KmpInt32PtrTy =
2207 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002208 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002209 std::make_pair(".global_tid.", KmpInt32PtrTy),
2210 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2211 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002212 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002213 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2214 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002215 break;
2216 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002217 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002218 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002219 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002220 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002221 case OMPD_target_teams_distribute:
2222 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002223 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2224 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2225 QualType KmpInt32PtrTy =
2226 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2227 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002228 FunctionProtoType::ExtProtoInfo EPI;
2229 EPI.Variadic = true;
2230 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2231 Sema::CapturedParamNameType Params[] = {
2232 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002233 std::make_pair(".part_id.", KmpInt32PtrTy),
2234 std::make_pair(".privates.", VoidPtrTy),
2235 std::make_pair(
2236 ".copy_fn.",
2237 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002238 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2239 std::make_pair(StringRef(), QualType()) // __context with shared vars
2240 };
2241 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2242 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002243 // Mark this captured region as inlined, because we don't use outlined
2244 // function directly.
2245 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2246 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002247 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002248 Sema::CapturedParamNameType ParamsTarget[] = {
2249 std::make_pair(StringRef(), QualType()) // __context with shared vars
2250 };
2251 // Start a captured region for 'target' with no implicit parameters.
2252 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2253 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002254 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002255 std::make_pair(".global_tid.", KmpInt32PtrTy),
2256 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2257 std::make_pair(StringRef(), QualType()) // __context with shared vars
2258 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002259 // Start a captured region for 'teams' or 'parallel'. Both regions have
2260 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002261 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002262 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002263 break;
2264 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002265 case OMPD_target:
2266 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002267 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2268 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2269 QualType KmpInt32PtrTy =
2270 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2271 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002272 FunctionProtoType::ExtProtoInfo EPI;
2273 EPI.Variadic = true;
2274 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2275 Sema::CapturedParamNameType Params[] = {
2276 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002277 std::make_pair(".part_id.", KmpInt32PtrTy),
2278 std::make_pair(".privates.", VoidPtrTy),
2279 std::make_pair(
2280 ".copy_fn.",
2281 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002282 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2283 std::make_pair(StringRef(), QualType()) // __context with shared vars
2284 };
2285 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2286 Params);
2287 // Mark this captured region as inlined, because we don't use outlined
2288 // function directly.
2289 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2290 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002291 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002292 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2293 std::make_pair(StringRef(), QualType()));
2294 break;
2295 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002296 case OMPD_simd:
2297 case OMPD_for:
2298 case OMPD_for_simd:
2299 case OMPD_sections:
2300 case OMPD_section:
2301 case OMPD_single:
2302 case OMPD_master:
2303 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002304 case OMPD_taskgroup:
2305 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002306 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002307 case OMPD_ordered:
2308 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002309 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002310 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002311 std::make_pair(StringRef(), QualType()) // __context with shared vars
2312 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002313 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2314 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002315 break;
2316 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002317 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002318 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2319 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2320 QualType KmpInt32PtrTy =
2321 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2322 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002323 FunctionProtoType::ExtProtoInfo EPI;
2324 EPI.Variadic = true;
2325 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002326 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002327 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002328 std::make_pair(".part_id.", KmpInt32PtrTy),
2329 std::make_pair(".privates.", VoidPtrTy),
2330 std::make_pair(
2331 ".copy_fn.",
2332 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002333 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002334 std::make_pair(StringRef(), QualType()) // __context with shared vars
2335 };
2336 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2337 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002338 // Mark this captured region as inlined, because we don't use outlined
2339 // function directly.
2340 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2341 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002342 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002343 break;
2344 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002345 case OMPD_taskloop:
2346 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002347 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002348 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2349 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002350 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002351 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2352 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002353 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002354 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2355 .withConst();
2356 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2357 QualType KmpInt32PtrTy =
2358 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2359 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002360 FunctionProtoType::ExtProtoInfo EPI;
2361 EPI.Variadic = true;
2362 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002363 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002364 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002365 std::make_pair(".part_id.", KmpInt32PtrTy),
2366 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002367 std::make_pair(
2368 ".copy_fn.",
2369 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2370 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2371 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002372 std::make_pair(".ub.", KmpUInt64Ty),
2373 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002374 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002375 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002376 std::make_pair(StringRef(), QualType()) // __context with shared vars
2377 };
2378 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2379 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002380 // Mark this captured region as inlined, because we don't use outlined
2381 // function directly.
2382 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2383 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002384 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002385 break;
2386 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002387 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002388 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002389 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002390 QualType KmpInt32PtrTy =
2391 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2392 Sema::CapturedParamNameType Params[] = {
2393 std::make_pair(".global_tid.", KmpInt32PtrTy),
2394 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002395 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2396 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002397 std::make_pair(StringRef(), QualType()) // __context with shared vars
2398 };
2399 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2400 Params);
2401 break;
2402 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002403 case OMPD_target_teams_distribute_parallel_for:
2404 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002405 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002406 QualType KmpInt32PtrTy =
2407 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002408 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002409
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002410 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002411 FunctionProtoType::ExtProtoInfo EPI;
2412 EPI.Variadic = true;
2413 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2414 Sema::CapturedParamNameType Params[] = {
2415 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002416 std::make_pair(".part_id.", KmpInt32PtrTy),
2417 std::make_pair(".privates.", VoidPtrTy),
2418 std::make_pair(
2419 ".copy_fn.",
2420 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002421 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2422 std::make_pair(StringRef(), QualType()) // __context with shared vars
2423 };
2424 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2425 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002426 // Mark this captured region as inlined, because we don't use outlined
2427 // function directly.
2428 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2429 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002430 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002431 Sema::CapturedParamNameType ParamsTarget[] = {
2432 std::make_pair(StringRef(), QualType()) // __context with shared vars
2433 };
2434 // Start a captured region for 'target' with no implicit parameters.
2435 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2436 ParamsTarget);
2437
2438 Sema::CapturedParamNameType ParamsTeams[] = {
2439 std::make_pair(".global_tid.", KmpInt32PtrTy),
2440 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2441 std::make_pair(StringRef(), QualType()) // __context with shared vars
2442 };
2443 // Start a captured region for 'target' with no implicit parameters.
2444 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2445 ParamsTeams);
2446
2447 Sema::CapturedParamNameType ParamsParallel[] = {
2448 std::make_pair(".global_tid.", KmpInt32PtrTy),
2449 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002450 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2451 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002452 std::make_pair(StringRef(), QualType()) // __context with shared vars
2453 };
2454 // Start a captured region for 'teams' or 'parallel'. Both regions have
2455 // the same implicit parameters.
2456 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2457 ParamsParallel);
2458 break;
2459 }
2460
Alexey Bataev46506272017-12-05 17:41:34 +00002461 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002462 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002463 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002464 QualType KmpInt32PtrTy =
2465 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2466
2467 Sema::CapturedParamNameType ParamsTeams[] = {
2468 std::make_pair(".global_tid.", KmpInt32PtrTy),
2469 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2470 std::make_pair(StringRef(), QualType()) // __context with shared vars
2471 };
2472 // Start a captured region for 'target' with no implicit parameters.
2473 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2474 ParamsTeams);
2475
2476 Sema::CapturedParamNameType ParamsParallel[] = {
2477 std::make_pair(".global_tid.", KmpInt32PtrTy),
2478 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002479 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2480 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002481 std::make_pair(StringRef(), QualType()) // __context with shared vars
2482 };
2483 // Start a captured region for 'teams' or 'parallel'. Both regions have
2484 // the same implicit parameters.
2485 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2486 ParamsParallel);
2487 break;
2488 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002489 case OMPD_target_update:
2490 case OMPD_target_enter_data:
2491 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002492 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2493 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2494 QualType KmpInt32PtrTy =
2495 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2496 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002497 FunctionProtoType::ExtProtoInfo EPI;
2498 EPI.Variadic = true;
2499 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2500 Sema::CapturedParamNameType Params[] = {
2501 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002502 std::make_pair(".part_id.", KmpInt32PtrTy),
2503 std::make_pair(".privates.", VoidPtrTy),
2504 std::make_pair(
2505 ".copy_fn.",
2506 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002507 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2508 std::make_pair(StringRef(), QualType()) // __context with shared vars
2509 };
2510 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2511 Params);
2512 // Mark this captured region as inlined, because we don't use outlined
2513 // function directly.
2514 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2515 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002516 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002517 break;
2518 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002519 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002520 case OMPD_taskyield:
2521 case OMPD_barrier:
2522 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002523 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002524 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002525 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002526 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002527 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002528 case OMPD_declare_target:
2529 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00002530 llvm_unreachable("OpenMP Directive is not allowed");
2531 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002532 llvm_unreachable("Unknown OpenMP directive");
2533 }
2534}
2535
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002536int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2537 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2538 getOpenMPCaptureRegions(CaptureRegions, DKind);
2539 return CaptureRegions.size();
2540}
2541
Alexey Bataev3392d762016-02-16 11:18:12 +00002542static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002543 Expr *CaptureExpr, bool WithInit,
2544 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002545 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002546 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002547 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002548 QualType Ty = Init->getType();
2549 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002550 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002551 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002552 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002553 Ty = C.getPointerType(Ty);
2554 ExprResult Res =
2555 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2556 if (!Res.isUsable())
2557 return nullptr;
2558 Init = Res.get();
2559 }
Alexey Bataev61205072016-03-02 04:57:40 +00002560 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002561 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002562 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2563 CaptureExpr->getLocStart());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002564 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002565 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002566 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002567 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002568 return CED;
2569}
2570
Alexey Bataev61205072016-03-02 04:57:40 +00002571static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2572 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002573 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002574 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002575 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002576 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002577 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2578 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002579 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002580 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002581}
2582
Alexey Bataev5a3af132016-03-29 08:58:54 +00002583static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002584 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002585 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002586 OMPCapturedExprDecl *CD = buildCaptureDecl(
2587 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2588 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002589 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2590 CaptureExpr->getExprLoc());
2591 }
2592 ExprResult Res = Ref;
2593 if (!S.getLangOpts().CPlusPlus &&
2594 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002595 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002596 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002597 if (!Res.isUsable())
2598 return ExprError();
2599 }
2600 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002601}
2602
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002603namespace {
2604// OpenMP directives parsed in this section are represented as a
2605// CapturedStatement with an associated statement. If a syntax error
2606// is detected during the parsing of the associated statement, the
2607// compiler must abort processing and close the CapturedStatement.
2608//
2609// Combined directives such as 'target parallel' have more than one
2610// nested CapturedStatements. This RAII ensures that we unwind out
2611// of all the nested CapturedStatements when an error is found.
2612class CaptureRegionUnwinderRAII {
2613private:
2614 Sema &S;
2615 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002616 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002617
2618public:
2619 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2620 OpenMPDirectiveKind DKind)
2621 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2622 ~CaptureRegionUnwinderRAII() {
2623 if (ErrorFound) {
2624 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2625 while (--ThisCaptureLevel >= 0)
2626 S.ActOnCapturedRegionError();
2627 }
2628 }
2629};
2630} // namespace
2631
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002632StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2633 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002634 bool ErrorFound = false;
2635 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2636 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002637 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002638 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002639 return StmtError();
2640 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002641
Alexey Bataev2ba67042017-11-28 21:11:44 +00002642 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2643 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002644 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002645 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002646 SmallVector<const OMPLinearClause *, 4> LCs;
2647 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002648 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002649 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002650 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2651 Clause->getClauseKind() == OMPC_in_reduction) {
2652 // Capture taskgroup task_reduction descriptors inside the tasking regions
2653 // with the corresponding in_reduction items.
2654 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002655 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002656 if (E)
2657 MarkDeclarationsReferencedInExpr(E);
2658 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002659 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002660 Clause->getClauseKind() == OMPC_copyprivate ||
2661 (getLangOpts().OpenMPUseTLS &&
2662 getASTContext().getTargetInfo().isTLSSupported() &&
2663 Clause->getClauseKind() == OMPC_copyin)) {
2664 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002665 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002666 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002667 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002668 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002669 }
2670 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002671 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002672 } else if (CaptureRegions.size() > 1 ||
2673 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002674 if (auto *C = OMPClauseWithPreInit::get(Clause))
2675 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002676 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002677 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002678 MarkDeclarationsReferencedInExpr(E);
2679 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002680 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002681 if (Clause->getClauseKind() == OMPC_schedule)
2682 SC = cast<OMPScheduleClause>(Clause);
2683 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002684 OC = cast<OMPOrderedClause>(Clause);
2685 else if (Clause->getClauseKind() == OMPC_linear)
2686 LCs.push_back(cast<OMPLinearClause>(Clause));
2687 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002688 // OpenMP, 2.7.1 Loop Construct, Restrictions
2689 // The nonmonotonic modifier cannot be specified if an ordered clause is
2690 // specified.
2691 if (SC &&
2692 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2693 SC->getSecondScheduleModifier() ==
2694 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2695 OC) {
2696 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2697 ? SC->getFirstScheduleModifierLoc()
2698 : SC->getSecondScheduleModifierLoc(),
2699 diag::err_omp_schedule_nonmonotonic_ordered)
2700 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2701 ErrorFound = true;
2702 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002703 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002704 for (const OMPLinearClause *C : LCs) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002705 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2706 << SourceRange(OC->getLocStart(), OC->getLocEnd());
2707 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002708 ErrorFound = true;
2709 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002710 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2711 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2712 OC->getNumForLoops()) {
2713 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2714 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2715 ErrorFound = true;
2716 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002717 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002718 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002719 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002720 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002721 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002722 // Mark all variables in private list clauses as used in inner region.
2723 // Required for proper codegen of combined directives.
2724 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002725 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002726 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002727 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2728 // Find the particular capture region for the clause if the
2729 // directive is a combined one with multiple capture regions.
2730 // If the directive is not a combined one, the capture region
2731 // associated with the clause is OMPD_unknown and is generated
2732 // only once.
2733 if (CaptureRegion == ThisCaptureRegion ||
2734 CaptureRegion == OMPD_unknown) {
2735 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002736 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002737 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2738 }
2739 }
2740 }
2741 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002742 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002743 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002744 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002745}
2746
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002747static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2748 OpenMPDirectiveKind CancelRegion,
2749 SourceLocation StartLoc) {
2750 // CancelRegion is only needed for cancel and cancellation_point.
2751 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2752 return false;
2753
2754 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2755 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2756 return false;
2757
2758 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2759 << getOpenMPDirectiveName(CancelRegion);
2760 return true;
2761}
2762
Alexey Bataeve3727102018-04-18 15:57:46 +00002763static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002764 OpenMPDirectiveKind CurrentRegion,
2765 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002766 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002767 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002768 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002769 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2770 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002771 bool NestingProhibited = false;
2772 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002773 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002774 enum {
2775 NoRecommend,
2776 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002777 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002778 ShouldBeInTargetRegion,
2779 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002780 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002781 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002782 // OpenMP [2.16, Nesting of Regions]
2783 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002784 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002785 // An ordered construct with the simd clause is the only OpenMP
2786 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002787 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002788 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2789 // message.
2790 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2791 ? diag::err_omp_prohibited_region_simd
2792 : diag::warn_omp_nesting_simd);
2793 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002794 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002795 if (ParentRegion == OMPD_atomic) {
2796 // OpenMP [2.16, Nesting of Regions]
2797 // OpenMP constructs may not be nested inside an atomic region.
2798 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2799 return true;
2800 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002801 if (CurrentRegion == OMPD_section) {
2802 // OpenMP [2.7.2, sections Construct, Restrictions]
2803 // Orphaned section directives are prohibited. That is, the section
2804 // directives must appear within the sections construct and must not be
2805 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002806 if (ParentRegion != OMPD_sections &&
2807 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002808 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2809 << (ParentRegion != OMPD_unknown)
2810 << getOpenMPDirectiveName(ParentRegion);
2811 return true;
2812 }
2813 return false;
2814 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002815 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002816 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002817 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002818 if (ParentRegion == OMPD_unknown &&
2819 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002820 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002821 if (CurrentRegion == OMPD_cancellation_point ||
2822 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002823 // OpenMP [2.16, Nesting of Regions]
2824 // A cancellation point construct for which construct-type-clause is
2825 // taskgroup must be nested inside a task construct. A cancellation
2826 // point construct for which construct-type-clause is not taskgroup must
2827 // be closely nested inside an OpenMP construct that matches the type
2828 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002829 // A cancel construct for which construct-type-clause is taskgroup must be
2830 // nested inside a task construct. A cancel construct for which
2831 // construct-type-clause is not taskgroup must be closely nested inside an
2832 // OpenMP construct that matches the type specified in
2833 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002834 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002835 !((CancelRegion == OMPD_parallel &&
2836 (ParentRegion == OMPD_parallel ||
2837 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002838 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002839 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002840 ParentRegion == OMPD_target_parallel_for ||
2841 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002842 ParentRegion == OMPD_teams_distribute_parallel_for ||
2843 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002844 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2845 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002846 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2847 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002848 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002849 // OpenMP [2.16, Nesting of Regions]
2850 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002851 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002852 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002853 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002854 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2855 // OpenMP [2.16, Nesting of Regions]
2856 // A critical region may not be nested (closely or otherwise) inside a
2857 // critical region with the same name. Note that this restriction is not
2858 // sufficient to prevent deadlock.
2859 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002860 bool DeadLock = Stack->hasDirective(
2861 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2862 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00002863 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00002864 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2865 PreviousCriticalLoc = Loc;
2866 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002867 }
2868 return false;
David Majnemer9d168222016-08-05 17:44:54 +00002869 },
2870 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002871 if (DeadLock) {
2872 SemaRef.Diag(StartLoc,
2873 diag::err_omp_prohibited_region_critical_same_name)
2874 << CurrentName.getName();
2875 if (PreviousCriticalLoc.isValid())
2876 SemaRef.Diag(PreviousCriticalLoc,
2877 diag::note_omp_previous_critical_region);
2878 return true;
2879 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002880 } else if (CurrentRegion == OMPD_barrier) {
2881 // OpenMP [2.16, Nesting of Regions]
2882 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002883 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002884 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2885 isOpenMPTaskingDirective(ParentRegion) ||
2886 ParentRegion == OMPD_master ||
2887 ParentRegion == OMPD_critical ||
2888 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002889 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002890 !isOpenMPParallelDirective(CurrentRegion) &&
2891 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002892 // OpenMP [2.16, Nesting of Regions]
2893 // A worksharing region may not be closely nested inside a worksharing,
2894 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002895 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2896 isOpenMPTaskingDirective(ParentRegion) ||
2897 ParentRegion == OMPD_master ||
2898 ParentRegion == OMPD_critical ||
2899 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002900 Recommend = ShouldBeInParallelRegion;
2901 } else if (CurrentRegion == OMPD_ordered) {
2902 // OpenMP [2.16, Nesting of Regions]
2903 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002904 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002905 // An ordered region must be closely nested inside a loop region (or
2906 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002907 // OpenMP [2.8.1,simd Construct, Restrictions]
2908 // An ordered construct with the simd clause is the only OpenMP construct
2909 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002910 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002911 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002912 !(isOpenMPSimdDirective(ParentRegion) ||
2913 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002914 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002915 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002916 // OpenMP [2.16, Nesting of Regions]
2917 // If specified, a teams construct must be contained within a target
2918 // construct.
2919 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002920 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002921 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002922 }
Kelvin Libf594a52016-12-17 05:48:59 +00002923 if (!NestingProhibited &&
2924 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2925 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2926 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002927 // OpenMP [2.16, Nesting of Regions]
2928 // distribute, parallel, parallel sections, parallel workshare, and the
2929 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2930 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002931 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2932 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002933 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002934 }
David Majnemer9d168222016-08-05 17:44:54 +00002935 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00002936 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002937 // OpenMP 4.5 [2.17 Nesting of Regions]
2938 // The region associated with the distribute construct must be strictly
2939 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00002940 NestingProhibited =
2941 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002942 Recommend = ShouldBeInTeamsRegion;
2943 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002944 if (!NestingProhibited &&
2945 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2946 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2947 // OpenMP 4.5 [2.17 Nesting of Regions]
2948 // If a target, target update, target data, target enter data, or
2949 // target exit data construct is encountered during execution of a
2950 // target region, the behavior is unspecified.
2951 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002952 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00002953 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002954 if (isOpenMPTargetExecutionDirective(K)) {
2955 OffendingRegion = K;
2956 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002957 }
2958 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002959 },
2960 false /* don't skip top directive */);
2961 CloseNesting = false;
2962 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002963 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00002964 if (OrphanSeen) {
2965 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2966 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2967 } else {
2968 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2969 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2970 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2971 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002972 return true;
2973 }
2974 }
2975 return false;
2976}
2977
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002978static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2979 ArrayRef<OMPClause *> Clauses,
2980 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2981 bool ErrorFound = false;
2982 unsigned NamedModifiersNumber = 0;
2983 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2984 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002985 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00002986 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002987 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2988 // At most one if clause without a directive-name-modifier can appear on
2989 // the directive.
2990 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2991 if (FoundNameModifiers[CurNM]) {
2992 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2993 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2994 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2995 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002996 } else if (CurNM != OMPD_unknown) {
2997 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002998 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002999 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003000 FoundNameModifiers[CurNM] = IC;
3001 if (CurNM == OMPD_unknown)
3002 continue;
3003 // Check if the specified name modifier is allowed for the current
3004 // directive.
3005 // At most one if clause with the particular directive-name-modifier can
3006 // appear on the directive.
3007 bool MatchFound = false;
3008 for (auto NM : AllowedNameModifiers) {
3009 if (CurNM == NM) {
3010 MatchFound = true;
3011 break;
3012 }
3013 }
3014 if (!MatchFound) {
3015 S.Diag(IC->getNameModifierLoc(),
3016 diag::err_omp_wrong_if_directive_name_modifier)
3017 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3018 ErrorFound = true;
3019 }
3020 }
3021 }
3022 // If any if clause on the directive includes a directive-name-modifier then
3023 // all if clauses on the directive must include a directive-name-modifier.
3024 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3025 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3026 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3027 diag::err_omp_no_more_if_clause);
3028 } else {
3029 std::string Values;
3030 std::string Sep(", ");
3031 unsigned AllowedCnt = 0;
3032 unsigned TotalAllowedNum =
3033 AllowedNameModifiers.size() - NamedModifiersNumber;
3034 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3035 ++Cnt) {
3036 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3037 if (!FoundNameModifiers[NM]) {
3038 Values += "'";
3039 Values += getOpenMPDirectiveName(NM);
3040 Values += "'";
3041 if (AllowedCnt + 2 == TotalAllowedNum)
3042 Values += " or ";
3043 else if (AllowedCnt + 1 != TotalAllowedNum)
3044 Values += Sep;
3045 ++AllowedCnt;
3046 }
3047 }
3048 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3049 diag::err_omp_unnamed_if_clause)
3050 << (TotalAllowedNum > 1) << Values;
3051 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003052 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003053 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3054 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003055 ErrorFound = true;
3056 }
3057 return ErrorFound;
3058}
3059
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003060StmtResult Sema::ActOnOpenMPExecutableDirective(
3061 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3062 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3063 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003064 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003065 // First check CancelRegion which is then used in checkNestingOfRegions.
3066 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3067 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003068 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003069 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003070
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003071 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003072 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003073 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003074 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003075 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003076 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3077
3078 // Check default data sharing attributes for referenced variables.
3079 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003080 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3081 Stmt *S = AStmt;
3082 while (--ThisCaptureLevel >= 0)
3083 S = cast<CapturedStmt>(S)->getCapturedStmt();
3084 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003085 if (DSAChecker.isErrorFound())
3086 return StmtError();
3087 // Generate list of implicitly defined firstprivate variables.
3088 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003089
Alexey Bataev88202be2017-07-27 13:20:36 +00003090 SmallVector<Expr *, 4> ImplicitFirstprivates(
3091 DSAChecker.getImplicitFirstprivate().begin(),
3092 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003093 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3094 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003095 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003096 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003097 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003098 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003099 if (E)
3100 ImplicitFirstprivates.emplace_back(E);
3101 }
3102 }
3103 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003104 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003105 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3106 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003107 ClausesWithImplicit.push_back(Implicit);
3108 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003109 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003110 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003111 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003112 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003113 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003114 if (!ImplicitMaps.empty()) {
3115 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3116 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3117 SourceLocation(), SourceLocation(), ImplicitMaps,
3118 SourceLocation(), SourceLocation(), SourceLocation())) {
3119 ClausesWithImplicit.emplace_back(Implicit);
3120 ErrorFound |=
3121 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003122 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003123 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003124 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003125 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003126 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003127
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003128 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003129 switch (Kind) {
3130 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003131 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3132 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003133 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003134 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003135 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003136 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3137 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003138 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003139 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003140 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3141 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003142 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003143 case OMPD_for_simd:
3144 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3145 EndLoc, VarsWithInheritedDSA);
3146 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003147 case OMPD_sections:
3148 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3149 EndLoc);
3150 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003151 case OMPD_section:
3152 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003153 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003154 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3155 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003156 case OMPD_single:
3157 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3158 EndLoc);
3159 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003160 case OMPD_master:
3161 assert(ClausesWithImplicit.empty() &&
3162 "No clauses are allowed for 'omp master' directive");
3163 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3164 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003165 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003166 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3167 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003168 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003169 case OMPD_parallel_for:
3170 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3171 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003172 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003173 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003174 case OMPD_parallel_for_simd:
3175 Res = ActOnOpenMPParallelForSimdDirective(
3176 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003177 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003178 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003179 case OMPD_parallel_sections:
3180 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3181 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003182 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003183 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003184 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003185 Res =
3186 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003187 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003188 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003189 case OMPD_taskyield:
3190 assert(ClausesWithImplicit.empty() &&
3191 "No clauses are allowed for 'omp taskyield' directive");
3192 assert(AStmt == nullptr &&
3193 "No associated statement allowed for 'omp taskyield' directive");
3194 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3195 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003196 case OMPD_barrier:
3197 assert(ClausesWithImplicit.empty() &&
3198 "No clauses are allowed for 'omp barrier' directive");
3199 assert(AStmt == nullptr &&
3200 "No associated statement allowed for 'omp barrier' directive");
3201 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3202 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003203 case OMPD_taskwait:
3204 assert(ClausesWithImplicit.empty() &&
3205 "No clauses are allowed for 'omp taskwait' directive");
3206 assert(AStmt == nullptr &&
3207 "No associated statement allowed for 'omp taskwait' directive");
3208 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3209 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003210 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003211 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3212 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003213 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003214 case OMPD_flush:
3215 assert(AStmt == nullptr &&
3216 "No associated statement allowed for 'omp flush' directive");
3217 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3218 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003219 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003220 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3221 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003222 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003223 case OMPD_atomic:
3224 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3225 EndLoc);
3226 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003227 case OMPD_teams:
3228 Res =
3229 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3230 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003231 case OMPD_target:
3232 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3233 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003234 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003235 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003236 case OMPD_target_parallel:
3237 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3238 StartLoc, EndLoc);
3239 AllowedNameModifiers.push_back(OMPD_target);
3240 AllowedNameModifiers.push_back(OMPD_parallel);
3241 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003242 case OMPD_target_parallel_for:
3243 Res = ActOnOpenMPTargetParallelForDirective(
3244 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3245 AllowedNameModifiers.push_back(OMPD_target);
3246 AllowedNameModifiers.push_back(OMPD_parallel);
3247 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003248 case OMPD_cancellation_point:
3249 assert(ClausesWithImplicit.empty() &&
3250 "No clauses are allowed for 'omp cancellation point' directive");
3251 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3252 "cancellation point' directive");
3253 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3254 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003255 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003256 assert(AStmt == nullptr &&
3257 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003258 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3259 CancelRegion);
3260 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003261 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003262 case OMPD_target_data:
3263 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3264 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003265 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003266 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003267 case OMPD_target_enter_data:
3268 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003269 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003270 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3271 break;
Samuel Antao72590762016-01-19 20:04:50 +00003272 case OMPD_target_exit_data:
3273 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003274 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003275 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3276 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003277 case OMPD_taskloop:
3278 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3279 EndLoc, VarsWithInheritedDSA);
3280 AllowedNameModifiers.push_back(OMPD_taskloop);
3281 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003282 case OMPD_taskloop_simd:
3283 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3284 EndLoc, VarsWithInheritedDSA);
3285 AllowedNameModifiers.push_back(OMPD_taskloop);
3286 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003287 case OMPD_distribute:
3288 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3289 EndLoc, VarsWithInheritedDSA);
3290 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003291 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003292 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3293 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003294 AllowedNameModifiers.push_back(OMPD_target_update);
3295 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003296 case OMPD_distribute_parallel_for:
3297 Res = ActOnOpenMPDistributeParallelForDirective(
3298 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3299 AllowedNameModifiers.push_back(OMPD_parallel);
3300 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003301 case OMPD_distribute_parallel_for_simd:
3302 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3303 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3304 AllowedNameModifiers.push_back(OMPD_parallel);
3305 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003306 case OMPD_distribute_simd:
3307 Res = ActOnOpenMPDistributeSimdDirective(
3308 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3309 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003310 case OMPD_target_parallel_for_simd:
3311 Res = ActOnOpenMPTargetParallelForSimdDirective(
3312 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3313 AllowedNameModifiers.push_back(OMPD_target);
3314 AllowedNameModifiers.push_back(OMPD_parallel);
3315 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003316 case OMPD_target_simd:
3317 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3318 EndLoc, VarsWithInheritedDSA);
3319 AllowedNameModifiers.push_back(OMPD_target);
3320 break;
Kelvin Li02532872016-08-05 14:37:37 +00003321 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003322 Res = ActOnOpenMPTeamsDistributeDirective(
3323 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003324 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003325 case OMPD_teams_distribute_simd:
3326 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3327 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3328 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003329 case OMPD_teams_distribute_parallel_for_simd:
3330 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3331 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3332 AllowedNameModifiers.push_back(OMPD_parallel);
3333 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003334 case OMPD_teams_distribute_parallel_for:
3335 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3336 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3337 AllowedNameModifiers.push_back(OMPD_parallel);
3338 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003339 case OMPD_target_teams:
3340 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3341 EndLoc);
3342 AllowedNameModifiers.push_back(OMPD_target);
3343 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003344 case OMPD_target_teams_distribute:
3345 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3346 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3347 AllowedNameModifiers.push_back(OMPD_target);
3348 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003349 case OMPD_target_teams_distribute_parallel_for:
3350 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3351 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3352 AllowedNameModifiers.push_back(OMPD_target);
3353 AllowedNameModifiers.push_back(OMPD_parallel);
3354 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003355 case OMPD_target_teams_distribute_parallel_for_simd:
3356 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3357 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3358 AllowedNameModifiers.push_back(OMPD_target);
3359 AllowedNameModifiers.push_back(OMPD_parallel);
3360 break;
Kelvin Lida681182017-01-10 18:08:18 +00003361 case OMPD_target_teams_distribute_simd:
3362 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3363 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3364 AllowedNameModifiers.push_back(OMPD_target);
3365 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003366 case OMPD_declare_target:
3367 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003368 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003369 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003370 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003371 llvm_unreachable("OpenMP Directive is not allowed");
3372 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003373 llvm_unreachable("Unknown OpenMP directive");
3374 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003375
Alexey Bataeve3727102018-04-18 15:57:46 +00003376 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003377 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3378 << P.first << P.second->getSourceRange();
3379 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003380 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3381
3382 if (!AllowedNameModifiers.empty())
3383 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3384 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003385
Alexey Bataeved09d242014-05-28 05:53:51 +00003386 if (ErrorFound)
3387 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003388 return Res;
3389}
3390
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003391Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3392 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003393 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003394 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3395 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003396 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003397 assert(Linears.size() == LinModifiers.size());
3398 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003399 if (!DG || DG.get().isNull())
3400 return DeclGroupPtrTy();
3401
3402 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003403 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003404 return DG;
3405 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003406 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003407 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3408 ADecl = FTD->getTemplatedDecl();
3409
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003410 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3411 if (!FD) {
3412 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003413 return DeclGroupPtrTy();
3414 }
3415
Alexey Bataev2af33e32016-04-07 12:45:37 +00003416 // OpenMP [2.8.2, declare simd construct, Description]
3417 // The parameter of the simdlen clause must be a constant positive integer
3418 // expression.
3419 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003420 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003421 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003422 // OpenMP [2.8.2, declare simd construct, Description]
3423 // The special this pointer can be used as if was one of the arguments to the
3424 // function in any of the linear, aligned, or uniform clauses.
3425 // The uniform clause declares one or more arguments to have an invariant
3426 // value for all concurrent invocations of the function in the execution of a
3427 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003428 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3429 const Expr *UniformedLinearThis = nullptr;
3430 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003431 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003432 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3433 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003434 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3435 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003436 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003437 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003438 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003439 }
3440 if (isa<CXXThisExpr>(E)) {
3441 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003442 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003443 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003444 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3445 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003446 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003447 // OpenMP [2.8.2, declare simd construct, Description]
3448 // The aligned clause declares that the object to which each list item points
3449 // is aligned to the number of bytes expressed in the optional parameter of
3450 // the aligned clause.
3451 // The special this pointer can be used as if was one of the arguments to the
3452 // function in any of the linear, aligned, or uniform clauses.
3453 // The type of list items appearing in the aligned clause must be array,
3454 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003455 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3456 const Expr *AlignedThis = nullptr;
3457 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003458 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003459 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3460 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3461 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003462 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3463 FD->getParamDecl(PVD->getFunctionScopeIndex())
3464 ->getCanonicalDecl() == CanonPVD) {
3465 // OpenMP [2.8.1, simd construct, Restrictions]
3466 // A list-item cannot appear in more than one aligned clause.
3467 if (AlignedArgs.count(CanonPVD) > 0) {
3468 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3469 << 1 << E->getSourceRange();
3470 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3471 diag::note_omp_explicit_dsa)
3472 << getOpenMPClauseName(OMPC_aligned);
3473 continue;
3474 }
3475 AlignedArgs[CanonPVD] = E;
3476 QualType QTy = PVD->getType()
3477 .getNonReferenceType()
3478 .getUnqualifiedType()
3479 .getCanonicalType();
3480 const Type *Ty = QTy.getTypePtrOrNull();
3481 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3482 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3483 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3484 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3485 }
3486 continue;
3487 }
3488 }
3489 if (isa<CXXThisExpr>(E)) {
3490 if (AlignedThis) {
3491 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3492 << 2 << E->getSourceRange();
3493 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3494 << getOpenMPClauseName(OMPC_aligned);
3495 }
3496 AlignedThis = E;
3497 continue;
3498 }
3499 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3500 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3501 }
3502 // The optional parameter of the aligned clause, alignment, must be a constant
3503 // positive integer expression. If no optional parameter is specified,
3504 // implementation-defined default alignments for SIMD instructions on the
3505 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003506 SmallVector<const Expr *, 4> NewAligns;
3507 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003508 ExprResult Align;
3509 if (E)
3510 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3511 NewAligns.push_back(Align.get());
3512 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003513 // OpenMP [2.8.2, declare simd construct, Description]
3514 // The linear clause declares one or more list items to be private to a SIMD
3515 // lane and to have a linear relationship with respect to the iteration space
3516 // of a loop.
3517 // The special this pointer can be used as if was one of the arguments to the
3518 // function in any of the linear, aligned, or uniform clauses.
3519 // When a linear-step expression is specified in a linear clause it must be
3520 // either a constant integer expression or an integer-typed parameter that is
3521 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003522 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003523 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3524 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003525 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003526 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3527 ++MI;
3528 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003529 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3530 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3531 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003532 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3533 FD->getParamDecl(PVD->getFunctionScopeIndex())
3534 ->getCanonicalDecl() == CanonPVD) {
3535 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3536 // A list-item cannot appear in more than one linear clause.
3537 if (LinearArgs.count(CanonPVD) > 0) {
3538 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3539 << getOpenMPClauseName(OMPC_linear)
3540 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3541 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3542 diag::note_omp_explicit_dsa)
3543 << getOpenMPClauseName(OMPC_linear);
3544 continue;
3545 }
3546 // Each argument can appear in at most one uniform or linear clause.
3547 if (UniformedArgs.count(CanonPVD) > 0) {
3548 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3549 << getOpenMPClauseName(OMPC_linear)
3550 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3551 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3552 diag::note_omp_explicit_dsa)
3553 << getOpenMPClauseName(OMPC_uniform);
3554 continue;
3555 }
3556 LinearArgs[CanonPVD] = E;
3557 if (E->isValueDependent() || E->isTypeDependent() ||
3558 E->isInstantiationDependent() ||
3559 E->containsUnexpandedParameterPack())
3560 continue;
3561 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3562 PVD->getOriginalType());
3563 continue;
3564 }
3565 }
3566 if (isa<CXXThisExpr>(E)) {
3567 if (UniformedLinearThis) {
3568 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3569 << getOpenMPClauseName(OMPC_linear)
3570 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3571 << E->getSourceRange();
3572 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3573 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3574 : OMPC_linear);
3575 continue;
3576 }
3577 UniformedLinearThis = E;
3578 if (E->isValueDependent() || E->isTypeDependent() ||
3579 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3580 continue;
3581 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3582 E->getType());
3583 continue;
3584 }
3585 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3586 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3587 }
3588 Expr *Step = nullptr;
3589 Expr *NewStep = nullptr;
3590 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003591 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003592 // Skip the same step expression, it was checked already.
3593 if (Step == E || !E) {
3594 NewSteps.push_back(E ? NewStep : nullptr);
3595 continue;
3596 }
3597 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003598 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3599 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3600 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003601 if (UniformedArgs.count(CanonPVD) == 0) {
3602 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3603 << Step->getSourceRange();
3604 } else if (E->isValueDependent() || E->isTypeDependent() ||
3605 E->isInstantiationDependent() ||
3606 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003607 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003608 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003609 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003610 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3611 << Step->getSourceRange();
3612 }
3613 continue;
3614 }
3615 NewStep = Step;
3616 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3617 !Step->isInstantiationDependent() &&
3618 !Step->containsUnexpandedParameterPack()) {
3619 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3620 .get();
3621 if (NewStep)
3622 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3623 }
3624 NewSteps.push_back(NewStep);
3625 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003626 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3627 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003628 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003629 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3630 const_cast<Expr **>(Linears.data()), Linears.size(),
3631 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3632 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003633 ADecl->addAttr(NewAttr);
3634 return ConvertDeclToDeclGroup(ADecl);
3635}
3636
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003637StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3638 Stmt *AStmt,
3639 SourceLocation StartLoc,
3640 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003641 if (!AStmt)
3642 return StmtError();
3643
Alexey Bataeve3727102018-04-18 15:57:46 +00003644 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003645 // 1.2.2 OpenMP Language Terminology
3646 // Structured block - An executable statement with a single entry at the
3647 // top and a single exit at the bottom.
3648 // The point of exit cannot be a branch out of the structured block.
3649 // longjmp() and throw() must not violate the entry/exit criteria.
3650 CS->getCapturedDecl()->setNothrow();
3651
Reid Kleckner87a31802018-03-12 21:43:02 +00003652 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003653
Alexey Bataev25e5b442015-09-15 12:52:43 +00003654 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3655 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003656}
3657
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003658namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003659/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660/// extracting iteration space of each loop in the loop nest, that will be used
3661/// for IR generation.
3662class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003663 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003664 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003665 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003666 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003667 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003668 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003669 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003670 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003671 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003672 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003673 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003674 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003675 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003676 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003677 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003678 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003679 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003680 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003681 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003682 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003683 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003684 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003685 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003686 /// Var < UB
3687 /// Var <= UB
3688 /// UB > Var
3689 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003690 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003691 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003692 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003693 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003694 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003695
3696public:
3697 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003698 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003699 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003700 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003701 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003702 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003703 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003704 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003705 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003706 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003707 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003708 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003709 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003710 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003711 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003712 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003713 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003714 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003715 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003716 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003717 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003718 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003719 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003720 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003721 Expr *buildNumIterations(
3722 Scope *S, const bool LimitedType,
3723 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003724 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003725 Expr *
3726 buildPreCond(Scope *S, Expr *Cond,
3727 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003728 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003729 DeclRefExpr *
3730 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3731 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003732 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003733 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003734 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003735 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003736 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003737 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003738 Expr *buildCounterStep() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003739 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003740 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003741
3742private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003743 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003744 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003745 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003746 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003747 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003748 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003749 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003750 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003751 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003752 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003753};
3754
Alexey Bataeve3727102018-04-18 15:57:46 +00003755bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003756 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003757 assert(!LB && !UB && !Step);
3758 return false;
3759 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003760 return LCDecl->getType()->isDependentType() ||
3761 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3762 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003763}
3764
Alexey Bataeve3727102018-04-18 15:57:46 +00003765bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003766 Expr *NewLCRefExpr,
3767 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003769 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003770 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003771 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003772 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003773 LCDecl = getCanonicalDecl(NewLCDecl);
3774 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003775 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3776 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003777 if ((Ctor->isCopyOrMoveConstructor() ||
3778 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3779 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003780 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 LB = NewLB;
3782 return false;
3783}
3784
Alexey Bataeve3727102018-04-18 15:57:46 +00003785bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003786 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003787 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003788 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3789 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003790 if (!NewUB)
3791 return true;
3792 UB = NewUB;
3793 TestIsLessOp = LessOp;
3794 TestIsStrictOp = StrictOp;
3795 ConditionSrcRange = SR;
3796 ConditionLoc = SL;
3797 return false;
3798}
3799
Alexey Bataeve3727102018-04-18 15:57:46 +00003800bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003801 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003802 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003803 if (!NewStep)
3804 return true;
3805 if (!NewStep->isValueDependent()) {
3806 // Check that the step is integer expression.
3807 SourceLocation StepLoc = NewStep->getLocStart();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003808 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3809 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003810 if (Val.isInvalid())
3811 return true;
3812 NewStep = Val.get();
3813
3814 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3815 // If test-expr is of form var relational-op b and relational-op is < or
3816 // <= then incr-expr must cause var to increase on each iteration of the
3817 // loop. If test-expr is of form var relational-op b and relational-op is
3818 // > or >= then incr-expr must cause var to decrease on each iteration of
3819 // the loop.
3820 // If test-expr is of form b relational-op var and relational-op is < or
3821 // <= then incr-expr must cause var to decrease on each iteration of the
3822 // loop. If test-expr is of form b relational-op var and relational-op is
3823 // > or >= then incr-expr must cause var to increase on each iteration of
3824 // the loop.
3825 llvm::APSInt Result;
3826 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3827 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3828 bool IsConstNeg =
3829 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003830 bool IsConstPos =
3831 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003832 bool IsConstZero = IsConstant && !Result.getBoolValue();
3833 if (UB && (IsConstZero ||
3834 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003835 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003836 SemaRef.Diag(NewStep->getExprLoc(),
3837 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003838 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003839 SemaRef.Diag(ConditionLoc,
3840 diag::note_omp_loop_cond_requres_compatible_incr)
3841 << TestIsLessOp << ConditionSrcRange;
3842 return true;
3843 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003844 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003845 NewStep =
3846 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3847 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003848 Subtract = !Subtract;
3849 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003850 }
3851
3852 Step = NewStep;
3853 SubtractStep = Subtract;
3854 return false;
3855}
3856
Alexey Bataeve3727102018-04-18 15:57:46 +00003857bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003858 // Check init-expr for canonical loop form and save loop counter
3859 // variable - #Var and its initialization value - #LB.
3860 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3861 // var = lb
3862 // integer-type var = lb
3863 // random-access-iterator-type var = lb
3864 // pointer-type var = lb
3865 //
3866 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003867 if (EmitDiags) {
3868 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3869 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 return true;
3871 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003872 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3873 if (!ExprTemp->cleanupsHaveSideEffects())
3874 S = ExprTemp->getSubExpr();
3875
Alexander Musmana5f070a2014-10-01 06:03:56 +00003876 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003877 if (Expr *E = dyn_cast<Expr>(S))
3878 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003879 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003880 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003881 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003882 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3883 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3884 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003885 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3886 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003887 }
3888 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3889 if (ME->isArrow() &&
3890 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00003891 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003892 }
3893 }
David Majnemer9d168222016-08-05 17:44:54 +00003894 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003895 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00003896 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003897 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003898 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003899 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003900 SemaRef.Diag(S->getLocStart(),
3901 diag::ext_omp_loop_not_canonical_init)
3902 << S->getSourceRange();
Alexey Bataeve3727102018-04-18 15:57:46 +00003903 return setLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003904 }
3905 }
3906 }
David Majnemer9d168222016-08-05 17:44:54 +00003907 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003908 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003909 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003910 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003911 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3912 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003913 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3914 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003915 }
3916 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3917 if (ME->isArrow() &&
3918 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00003919 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003920 }
3921 }
3922 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003923
Alexey Bataeve3727102018-04-18 15:57:46 +00003924 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003925 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003926 if (EmitDiags) {
3927 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3928 << S->getSourceRange();
3929 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 return true;
3931}
3932
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003933/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00003935static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003937 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003938 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00003939 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003941 if ((Ctor->isCopyOrMoveConstructor() ||
3942 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3943 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003944 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003945 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3946 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003947 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003948 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003949 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003950 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3951 return getCanonicalDecl(ME->getMemberDecl());
3952 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003953}
3954
Alexey Bataeve3727102018-04-18 15:57:46 +00003955bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003956 // Check test-expr for canonical form, save upper-bound UB, flags for
3957 // less/greater and for strict/non-strict comparison.
3958 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3959 // var relational-op b
3960 // b relational-op var
3961 //
3962 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003963 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003964 return true;
3965 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003966 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003967 SourceLocation CondLoc = S->getLocStart();
David Majnemer9d168222016-08-05 17:44:54 +00003968 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003969 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003970 if (getInitLCDecl(BO->getLHS()) == LCDecl)
3971 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003972 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3973 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3974 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00003975 if (getInitLCDecl(BO->getRHS()) == LCDecl)
3976 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3978 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3979 BO->getSourceRange(), BO->getOperatorLoc());
3980 }
David Majnemer9d168222016-08-05 17:44:54 +00003981 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003982 if (CE->getNumArgs() == 2) {
3983 auto Op = CE->getOperator();
3984 switch (Op) {
3985 case OO_Greater:
3986 case OO_GreaterEqual:
3987 case OO_Less:
3988 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00003989 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
3990 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003991 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3992 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00003993 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
3994 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003995 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3996 CE->getOperatorLoc());
3997 break;
3998 default:
3999 break;
4000 }
4001 }
4002 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004003 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004004 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004005 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004006 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004007 return true;
4008}
4009
Alexey Bataeve3727102018-04-18 15:57:46 +00004010bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004011 // RHS of canonical loop form increment can be:
4012 // var + incr
4013 // incr + var
4014 // var - incr
4015 //
4016 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004017 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004018 if (BO->isAdditiveOp()) {
4019 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004020 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4021 return setStep(BO->getRHS(), !IsAdd);
4022 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4023 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004024 }
David Majnemer9d168222016-08-05 17:44:54 +00004025 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004026 bool IsAdd = CE->getOperator() == OO_Plus;
4027 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004028 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4029 return setStep(CE->getArg(1), !IsAdd);
4030 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4031 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004032 }
4033 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004034 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004035 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004036 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004037 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004038 return true;
4039}
4040
Alexey Bataeve3727102018-04-18 15:57:46 +00004041bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004042 // Check incr-expr for canonical loop form and return true if it
4043 // does not conform.
4044 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4045 // ++var
4046 // var++
4047 // --var
4048 // var--
4049 // var += incr
4050 // var -= incr
4051 // var = var + incr
4052 // var = incr + var
4053 // var = var - incr
4054 //
4055 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004056 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 return true;
4058 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004059 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4060 if (!ExprTemp->cleanupsHaveSideEffects())
4061 S = ExprTemp->getSubExpr();
4062
Alexander Musmana5f070a2014-10-01 06:03:56 +00004063 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004064 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004065 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004066 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004067 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4068 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004069 .ActOnIntegerConstant(UO->getLocStart(),
4070 (UO->isDecrementOp() ? -1 : 1))
4071 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004072 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004073 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004074 switch (BO->getOpcode()) {
4075 case BO_AddAssign:
4076 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004077 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4078 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004079 break;
4080 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004081 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4082 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004083 break;
4084 default:
4085 break;
4086 }
David Majnemer9d168222016-08-05 17:44:54 +00004087 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004088 switch (CE->getOperator()) {
4089 case OO_PlusPlus:
4090 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004091 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4092 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004093 .ActOnIntegerConstant(
4094 CE->getLocStart(),
4095 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4096 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004097 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004098 break;
4099 case OO_PlusEqual:
4100 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004101 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4102 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 break;
4104 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004105 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4106 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004107 break;
4108 default:
4109 break;
4110 }
4111 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004112 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004113 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004114 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004115 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 return true;
4117}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004118
Alexey Bataev5a3af132016-03-29 08:58:54 +00004119static ExprResult
4120tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004121 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004122 if (SemaRef.CurContext->isDependentContext())
4123 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004124 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4125 return SemaRef.PerformImplicitConversion(
4126 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4127 /*AllowExplicit=*/true);
4128 auto I = Captures.find(Capture);
4129 if (I != Captures.end())
4130 return buildCapture(SemaRef, Capture, I->second);
4131 DeclRefExpr *Ref = nullptr;
4132 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4133 Captures[Capture] = Ref;
4134 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004135}
4136
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004137/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004138Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004139 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004140 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004141 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004142 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004143 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004144 SemaRef.getLangOpts().CPlusPlus) {
4145 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004146 Expr *UBExpr = TestIsLessOp ? UB : LB;
4147 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004148 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4149 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004150 if (!Upper || !Lower)
4151 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004152
4153 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4154
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004155 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004156 // BuildBinOp already emitted error, this one is to point user to upper
4157 // and lower bound, and to tell what is passed to 'operator-'.
4158 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4159 << Upper->getSourceRange() << Lower->getSourceRange();
4160 return nullptr;
4161 }
4162 }
4163
4164 if (!Diff.isUsable())
4165 return nullptr;
4166
4167 // Upper - Lower [- 1]
4168 if (TestIsStrictOp)
4169 Diff = SemaRef.BuildBinOp(
4170 S, DefaultLoc, BO_Sub, Diff.get(),
4171 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4172 if (!Diff.isUsable())
4173 return nullptr;
4174
4175 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004176 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004177 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004178 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004179 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004180 if (!Diff.isUsable())
4181 return nullptr;
4182
4183 // Parentheses (for dumping/debugging purposes only).
4184 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4185 if (!Diff.isUsable())
4186 return nullptr;
4187
4188 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004189 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004190 if (!Diff.isUsable())
4191 return nullptr;
4192
Alexander Musman174b3ca2014-10-06 11:16:29 +00004193 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004194 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004195 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004196 bool UseVarType = VarType->hasIntegerRepresentation() &&
4197 C.getTypeSize(Type) > C.getTypeSize(VarType);
4198 if (!Type->isIntegerType() || UseVarType) {
4199 unsigned NewSize =
4200 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4201 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4202 : Type->hasSignedIntegerRepresentation();
4203 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004204 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4205 Diff = SemaRef.PerformImplicitConversion(
4206 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4207 if (!Diff.isUsable())
4208 return nullptr;
4209 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004210 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004211 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004212 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4213 if (NewSize != C.getTypeSize(Type)) {
4214 if (NewSize < C.getTypeSize(Type)) {
4215 assert(NewSize == 64 && "incorrect loop var size");
4216 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4217 << InitSrcRange << ConditionSrcRange;
4218 }
4219 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004220 NewSize, Type->hasSignedIntegerRepresentation() ||
4221 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004222 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4223 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4224 Sema::AA_Converting, true);
4225 if (!Diff.isUsable())
4226 return nullptr;
4227 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004228 }
4229 }
4230
Alexander Musmana5f070a2014-10-01 06:03:56 +00004231 return Diff.get();
4232}
4233
Alexey Bataeve3727102018-04-18 15:57:46 +00004234Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004235 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004236 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004237 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4238 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4239 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004240
Alexey Bataeve3727102018-04-18 15:57:46 +00004241 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4242 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004243 if (!NewLB.isUsable() || !NewUB.isUsable())
4244 return nullptr;
4245
Alexey Bataeve3727102018-04-18 15:57:46 +00004246 ExprResult CondExpr =
4247 SemaRef.BuildBinOp(S, DefaultLoc,
4248 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4249 : (TestIsStrictOp ? BO_GT : BO_GE),
4250 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004251 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004252 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4253 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004254 CondExpr = SemaRef.PerformImplicitConversion(
4255 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4256 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004257 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004258 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4259 // Otherwise use original loop conditon and evaluate it in runtime.
4260 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4261}
4262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004263/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004264DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
4265 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004266 auto *VD = dyn_cast<VarDecl>(LCDecl);
4267 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004268 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4269 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004270 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004271 const DSAStackTy::DSAVarData Data =
4272 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004273 // If the loop control decl is explicitly marked as private, do not mark it
4274 // as captured again.
4275 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4276 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004277 return Ref;
4278 }
4279 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004280 DefaultLoc);
4281}
4282
Alexey Bataeve3727102018-04-18 15:57:46 +00004283Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004284 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004285 QualType Type = LCDecl->getType().getNonReferenceType();
4286 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004287 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4288 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4289 isa<VarDecl>(LCDecl)
4290 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4291 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004292 if (PrivateVar->isInvalidDecl())
4293 return nullptr;
4294 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4295 }
4296 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004297}
4298
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004299/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004300Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004302/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004303Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004304
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004305/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004306struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004307 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004308 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004309 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004310 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004311 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004312 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004313 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004314 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004315 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004316 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004317 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004318 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004319 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004320 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004321 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004322 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004323 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004324 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004325 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004326 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004327 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328 SourceRange IncSrcRange;
4329};
4330
Alexey Bataev23b69422014-06-18 07:08:49 +00004331} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004332
Alexey Bataev9c821032015-04-30 04:23:23 +00004333void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4334 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4335 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004336 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4337 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004338 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4339 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004340 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4341 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004342 auto *VD = dyn_cast<VarDecl>(D);
4343 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004344 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004345 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004346 } else {
4347 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4348 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004349 VD = cast<VarDecl>(Ref->getDecl());
4350 }
4351 }
4352 DSAStack->addLoopControlVariable(D, VD);
4353 }
4354 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004355 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004356 }
4357}
4358
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004359/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004361static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004362 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4363 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004364 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004365 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004366 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004367 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004368 // OpenMP [2.6, Canonical Loop Form]
4369 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004370 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004371 if (!For) {
4372 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004373 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4374 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4375 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4376 if (NestedLoopCount > 1) {
4377 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4378 SemaRef.Diag(DSA.getConstructLoc(),
4379 diag::note_omp_collapse_ordered_expr)
4380 << 2 << CollapseLoopCountExpr->getSourceRange()
4381 << OrderedLoopCountExpr->getSourceRange();
4382 else if (CollapseLoopCountExpr)
4383 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4384 diag::note_omp_collapse_ordered_expr)
4385 << 0 << CollapseLoopCountExpr->getSourceRange();
4386 else
4387 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4388 diag::note_omp_collapse_ordered_expr)
4389 << 1 << OrderedLoopCountExpr->getSourceRange();
4390 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004391 return true;
4392 }
4393 assert(For->getBody());
4394
4395 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4396
4397 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004398 Stmt *Init = For->getInit();
4399 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004400 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004401
4402 bool HasErrors = false;
4403
4404 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004405 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4406 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004407
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004408 // OpenMP [2.6, Canonical Loop Form]
4409 // Var is one of the following:
4410 // A variable of signed or unsigned integer type.
4411 // For C++, a variable of a random access iterator type.
4412 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004413 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004414 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4415 !VarType->isPointerType() &&
4416 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4417 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4418 << SemaRef.getLangOpts().CPlusPlus;
4419 HasErrors = true;
4420 }
4421
4422 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4423 // a Construct
4424 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4425 // parallel for construct is (are) private.
4426 // The loop iteration variable in the associated for-loop of a simd
4427 // construct with just one associated for-loop is linear with a
4428 // constant-linear-step that is the increment of the associated for-loop.
4429 // Exclude loop var from the list of variables with implicitly defined data
4430 // sharing attributes.
4431 VarsWithImplicitDSA.erase(LCDecl);
4432
4433 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4434 // in a Construct, C/C++].
4435 // The loop iteration variable in the associated for-loop of a simd
4436 // construct with just one associated for-loop may be listed in a linear
4437 // clause with a constant-linear-step that is the increment of the
4438 // associated for-loop.
4439 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4440 // parallel for construct may be listed in a private or lastprivate clause.
4441 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4442 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4443 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004444 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004445 isOpenMPSimdDirective(DKind)
4446 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4447 : OMPC_private;
4448 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4449 DVar.CKind != PredeterminedCKind) ||
4450 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4451 isOpenMPDistributeDirective(DKind)) &&
4452 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4453 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4454 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4455 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4456 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4457 << getOpenMPClauseName(PredeterminedCKind);
4458 if (DVar.RefExpr == nullptr)
4459 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004460 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004461 HasErrors = true;
4462 } else if (LoopDeclRefExpr != nullptr) {
4463 // Make the loop iteration variable private (for worksharing constructs),
4464 // linear (for simd directives with the only one associated loop) or
4465 // lastprivate (for simd directives with several collapsed or ordered
4466 // loops).
4467 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004468 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4469 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004470 /*FromParent=*/false);
4471 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4472 }
4473
4474 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4475
4476 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004477 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004478
4479 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004480 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004481 }
4482
Alexey Bataeve3727102018-04-18 15:57:46 +00004483 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004484 return HasErrors;
4485
Alexander Musmana5f070a2014-10-01 06:03:56 +00004486 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004487 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004488 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4489 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004490 DSA.getCurScope(),
4491 (isOpenMPWorksharingDirective(DKind) ||
4492 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4493 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004494 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4495 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4496 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4497 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4498 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4499 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4500 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4501 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004502
Alexey Bataev62dbb972015-04-22 11:59:37 +00004503 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4504 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004505 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004506 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004507 ResultIterSpace.CounterInit == nullptr ||
4508 ResultIterSpace.CounterStep == nullptr);
4509
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004510 return HasErrors;
4511}
4512
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004513/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004514static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004515buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004516 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004517 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004518 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004519 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004520 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004521 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004522 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004523 VarRef.get()->getType())) {
4524 NewStart = SemaRef.PerformImplicitConversion(
4525 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4526 /*AllowExplicit=*/true);
4527 if (!NewStart.isUsable())
4528 return ExprError();
4529 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004530
Alexey Bataeve3727102018-04-18 15:57:46 +00004531 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004532 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4533 return Init;
4534}
4535
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004536/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004537static ExprResult buildCounterUpdate(
4538 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4539 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4540 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004541 // Add parentheses (for debugging purposes only).
4542 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4543 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4544 !Step.isUsable())
4545 return ExprError();
4546
Alexey Bataev5a3af132016-03-29 08:58:54 +00004547 ExprResult NewStep = Step;
4548 if (Captures)
4549 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004550 if (NewStep.isInvalid())
4551 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004552 ExprResult Update =
4553 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004554 if (!Update.isUsable())
4555 return ExprError();
4556
Alexey Bataevc0214e02016-02-16 12:13:49 +00004557 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4558 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004559 ExprResult NewStart = Start;
4560 if (Captures)
4561 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004562 if (NewStart.isInvalid())
4563 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004564
Alexey Bataevc0214e02016-02-16 12:13:49 +00004565 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4566 ExprResult SavedUpdate = Update;
4567 ExprResult UpdateVal;
4568 if (VarRef.get()->getType()->isOverloadableType() ||
4569 NewStart.get()->getType()->isOverloadableType() ||
4570 Update.get()->getType()->isOverloadableType()) {
4571 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4572 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4573 Update =
4574 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4575 if (Update.isUsable()) {
4576 UpdateVal =
4577 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4578 VarRef.get(), SavedUpdate.get());
4579 if (UpdateVal.isUsable()) {
4580 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4581 UpdateVal.get());
4582 }
4583 }
4584 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4585 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004586
Alexey Bataevc0214e02016-02-16 12:13:49 +00004587 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4588 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4589 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4590 NewStart.get(), SavedUpdate.get());
4591 if (!Update.isUsable())
4592 return ExprError();
4593
Alexey Bataev11481f52016-02-17 10:29:05 +00004594 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4595 VarRef.get()->getType())) {
4596 Update = SemaRef.PerformImplicitConversion(
4597 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4598 if (!Update.isUsable())
4599 return ExprError();
4600 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004601
4602 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4603 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004604 return Update;
4605}
4606
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004607/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004608/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004609static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004610 if (E == nullptr)
4611 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004612 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004613 QualType OldType = E->getType();
4614 unsigned HasBits = C.getTypeSize(OldType);
4615 if (HasBits >= Bits)
4616 return ExprResult(E);
4617 // OK to convert to signed, because new type has more bits than old.
4618 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4619 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4620 true);
4621}
4622
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004623/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004624/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004625static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004626 if (E == nullptr)
4627 return false;
4628 llvm::APSInt Result;
4629 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4630 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4631 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004632}
4633
Alexey Bataev5a3af132016-03-29 08:58:54 +00004634/// Build preinits statement for the given declarations.
4635static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004636 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004637 if (!PreInits.empty()) {
4638 return new (Context) DeclStmt(
4639 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4640 SourceLocation(), SourceLocation());
4641 }
4642 return nullptr;
4643}
4644
4645/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004646static Stmt *
4647buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004648 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004649 if (!Captures.empty()) {
4650 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004651 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004652 PreInits.push_back(Pair.second->getDecl());
4653 return buildPreInits(Context, PreInits);
4654 }
4655 return nullptr;
4656}
4657
4658/// Build postupdate expression for the given list of postupdates expressions.
4659static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4660 Expr *PostUpdate = nullptr;
4661 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004662 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004663 Expr *ConvE = S.BuildCStyleCastExpr(
4664 E->getExprLoc(),
4665 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4666 E->getExprLoc(), E)
4667 .get();
4668 PostUpdate = PostUpdate
4669 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4670 PostUpdate, ConvE)
4671 .get()
4672 : ConvE;
4673 }
4674 }
4675 return PostUpdate;
4676}
4677
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004678/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004679/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4680/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004681static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00004682checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004683 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4684 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00004685 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004686 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004687 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004688 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004689 // Found 'collapse' clause - calculate collapse number.
4690 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004691 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004692 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004693 }
4694 if (OrderedLoopCountExpr) {
4695 // Found 'ordered' clause - calculate collapse number.
4696 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004697 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4698 if (Result.getLimitedValue() < NestedLoopCount) {
4699 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4700 diag::err_omp_wrong_ordered_loop_count)
4701 << OrderedLoopCountExpr->getSourceRange();
4702 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4703 diag::note_collapse_loop_count)
4704 << CollapseLoopCountExpr->getSourceRange();
4705 }
4706 NestedLoopCount = Result.getLimitedValue();
4707 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004708 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004709 // This is helper routine for loop directives (e.g., 'for', 'simd',
4710 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00004711 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004712 SmallVector<LoopIterationSpace, 4> IterSpaces;
4713 IterSpaces.resize(NestedLoopCount);
4714 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004715 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004716 if (checkOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004717 NestedLoopCount, CollapseLoopCountExpr,
4718 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004719 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004720 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004721 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004722 // OpenMP [2.8.1, simd construct, Restrictions]
4723 // All loops associated with the construct must be perfectly nested; that
4724 // is, there must be no intervening code nor any OpenMP directive between
4725 // any two loops.
4726 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004727 }
4728
Alexander Musmana5f070a2014-10-01 06:03:56 +00004729 Built.clear(/* size */ NestedLoopCount);
4730
4731 if (SemaRef.CurContext->isDependentContext())
4732 return NestedLoopCount;
4733
4734 // An example of what is generated for the following code:
4735 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004736 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004737 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004738 // for (k = 0; k < NK; ++k)
4739 // for (j = J0; j < NJ; j+=2) {
4740 // <loop body>
4741 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004742 //
4743 // We generate the code below.
4744 // Note: the loop body may be outlined in CodeGen.
4745 // Note: some counters may be C++ classes, operator- is used to find number of
4746 // iterations and operator+= to calculate counter value.
4747 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4748 // or i64 is currently supported).
4749 //
4750 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4751 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4752 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4753 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4754 // // similar updates for vars in clauses (e.g. 'linear')
4755 // <loop body (using local i and j)>
4756 // }
4757 // i = NI; // assign final values of counters
4758 // j = NJ;
4759 //
4760
4761 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4762 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004763 // Precondition tests if there is at least one iteration (all conditions are
4764 // true).
4765 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00004766 Expr *N0 = IterSpaces[0].NumIterations;
4767 ExprResult LastIteration32 =
4768 widenIterationCount(/*Bits=*/32,
4769 SemaRef
4770 .PerformImplicitConversion(
4771 N0->IgnoreImpCasts(), N0->getType(),
4772 Sema::AA_Converting, /*AllowExplicit=*/true)
4773 .get(),
4774 SemaRef);
4775 ExprResult LastIteration64 = widenIterationCount(
4776 /*Bits=*/64,
4777 SemaRef
4778 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
4779 Sema::AA_Converting,
4780 /*AllowExplicit=*/true)
4781 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004782 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004783
4784 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4785 return NestedLoopCount;
4786
Alexey Bataeve3727102018-04-18 15:57:46 +00004787 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004788 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4789
4790 Scope *CurScope = DSA.getCurScope();
4791 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004792 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00004793 PreCond =
4794 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4795 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00004796 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004797 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00004798 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004799 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4800 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004801 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004802 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004803 SemaRef
4804 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4805 Sema::AA_Converting,
4806 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004807 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004808 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004809 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004810 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004811 SemaRef
4812 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4813 Sema::AA_Converting,
4814 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004815 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004816 }
4817
4818 // Choose either the 32-bit or 64-bit version.
4819 ExprResult LastIteration = LastIteration64;
4820 if (LastIteration32.isUsable() &&
4821 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4822 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00004823 fitsInto(
4824 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004825 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4826 LastIteration64.get(), SemaRef)))
4827 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004828 QualType VType = LastIteration.get()->getType();
4829 QualType RealVType = VType;
4830 QualType StrideVType = VType;
4831 if (isOpenMPTaskLoopDirective(DKind)) {
4832 VType =
4833 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4834 StrideVType =
4835 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4836 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004837
4838 if (!LastIteration.isUsable())
4839 return 0;
4840
4841 // Save the number of iterations.
4842 ExprResult NumIterations = LastIteration;
4843 {
4844 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004845 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4846 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004847 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4848 if (!LastIteration.isUsable())
4849 return 0;
4850 }
4851
4852 // Calculate the last iteration number beforehand instead of doing this on
4853 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4854 llvm::APSInt Result;
4855 bool IsConstant =
4856 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4857 ExprResult CalcLastIteration;
4858 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004859 ExprResult SaveRef =
4860 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004861 LastIteration = SaveRef;
4862
4863 // Prepare SaveRef + 1.
4864 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004865 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004866 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4867 if (!NumIterations.isUsable())
4868 return 0;
4869 }
4870
4871 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4872
David Majnemer9d168222016-08-05 17:44:54 +00004873 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00004874 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004875 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4876 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004877 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004878 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4879 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004880 SemaRef.AddInitializerToDecl(LBDecl,
4881 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4882 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004883
4884 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004885 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4886 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004887 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00004888 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004889
4890 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4891 // This will be used to implement clause 'lastprivate'.
4892 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004893 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4894 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004895 SemaRef.AddInitializerToDecl(ILDecl,
4896 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4897 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004898
4899 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004900 VarDecl *STDecl =
4901 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4902 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00004903 SemaRef.AddInitializerToDecl(STDecl,
4904 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4905 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00004906
4907 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00004908 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00004909 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4910 UB.get(), LastIteration.get());
4911 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00004912 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
4913 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004914 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4915 CondOp.get());
4916 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00004917
4918 // If we have a combined directive that combines 'distribute', 'for' or
4919 // 'simd' we need to be able to access the bounds of the schedule of the
4920 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4921 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4922 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00004923 // Lower bound variable, initialized with zero.
4924 VarDecl *CombLBDecl =
4925 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4926 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4927 SemaRef.AddInitializerToDecl(
4928 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4929 /*DirectInit*/ false);
4930
4931 // Upper bound variable, initialized with last iteration number.
4932 VarDecl *CombUBDecl =
4933 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4934 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4935 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4936 /*DirectInit*/ false);
4937
4938 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4939 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4940 ExprResult CombCondOp =
4941 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4942 LastIteration.get(), CombUB.get());
4943 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4944 CombCondOp.get());
4945 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4946
Alexey Bataeve3727102018-04-18 15:57:46 +00004947 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00004948 // We expect to have at least 2 more parameters than the 'parallel'
4949 // directive does - the lower and upper bounds of the previous schedule.
4950 assert(CD->getNumParams() >= 4 &&
4951 "Unexpected number of parameters in loop combined directive");
4952
4953 // Set the proper type for the bounds given what we learned from the
4954 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00004955 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4956 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00004957
4958 // Previous lower and upper bounds are obtained from the region
4959 // parameters.
4960 PrevLB =
4961 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4962 PrevUB =
4963 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4964 }
Alexander Musmanc6388682014-12-15 07:07:06 +00004965 }
4966
4967 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004968 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00004969 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004970 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004971 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4972 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00004973 Expr *RHS =
4974 (isOpenMPWorksharingDirective(DKind) ||
4975 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4976 ? LB.get()
4977 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004978 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4979 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00004980
4981 if (isOpenMPLoopBoundSharingDirective(DKind)) {
4982 Expr *CombRHS =
4983 (isOpenMPWorksharingDirective(DKind) ||
4984 isOpenMPTaskLoopDirective(DKind) ||
4985 isOpenMPDistributeDirective(DKind))
4986 ? CombLB.get()
4987 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4988 CombInit =
4989 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4990 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4991 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004992 }
4993
Alexander Musmanc6388682014-12-15 07:07:06 +00004994 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00004995 SourceLocation CondLoc = AStmt->getLocStart();
Alexander Musmanc6388682014-12-15 07:07:06 +00004996 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004997 (isOpenMPWorksharingDirective(DKind) ||
4998 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004999 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5000 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5001 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005002 ExprResult CombCond;
5003 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5004 CombCond =
5005 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5006 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005007 // Loop increment (IV = IV + 1)
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005008 SourceLocation IncLoc = AStmt->getLocStart();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005009 ExprResult Inc =
5010 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5011 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5012 if (!Inc.isUsable())
5013 return 0;
5014 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005015 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5016 if (!Inc.isUsable())
5017 return 0;
5018
5019 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5020 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005021 // In combined construct, add combined version that use CombLB and CombUB
5022 // base variables for the update
5023 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005024 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5025 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005026 // LB + ST
5027 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5028 if (!NextLB.isUsable())
5029 return 0;
5030 // LB = LB + ST
5031 NextLB =
5032 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5033 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5034 if (!NextLB.isUsable())
5035 return 0;
5036 // UB + ST
5037 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5038 if (!NextUB.isUsable())
5039 return 0;
5040 // UB = UB + ST
5041 NextUB =
5042 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5043 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5044 if (!NextUB.isUsable())
5045 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005046 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5047 CombNextLB =
5048 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5049 if (!NextLB.isUsable())
5050 return 0;
5051 // LB = LB + ST
5052 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5053 CombNextLB.get());
5054 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5055 if (!CombNextLB.isUsable())
5056 return 0;
5057 // UB + ST
5058 CombNextUB =
5059 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5060 if (!CombNextUB.isUsable())
5061 return 0;
5062 // UB = UB + ST
5063 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5064 CombNextUB.get());
5065 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5066 if (!CombNextUB.isUsable())
5067 return 0;
5068 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005069 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005070
Carlo Bertolliffafe102017-04-20 00:39:39 +00005071 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005072 // directive with for as IV = IV + ST; ensure upper bound expression based
5073 // on PrevUB instead of NumIterations - used to implement 'for' when found
5074 // in combination with 'distribute', like in 'distribute parallel for'
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005075 SourceLocation DistIncLoc = AStmt->getLocStart();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005076 ExprResult DistCond, DistInc, PrevEUB;
5077 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5078 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5079 assert(DistCond.isUsable() && "distribute cond expr was not built");
5080
5081 DistInc =
5082 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5083 assert(DistInc.isUsable() && "distribute inc expr was not built");
5084 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5085 DistInc.get());
5086 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5087 assert(DistInc.isUsable() && "distribute inc expr was not built");
5088
5089 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5090 // construct
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00005091 SourceLocation DistEUBLoc = AStmt->getLocStart();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005092 ExprResult IsUBGreater =
5093 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5094 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5095 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5096 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5097 CondOp.get());
5098 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5099 }
5100
Alexander Musmana5f070a2014-10-01 06:03:56 +00005101 // Build updates and final values of the loop counters.
5102 bool HasErrors = false;
5103 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005104 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005105 Built.Updates.resize(NestedLoopCount);
5106 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005107 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005108 {
5109 ExprResult Div;
5110 // Go from inner nested loop to outer.
5111 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5112 LoopIterationSpace &IS = IterSpaces[Cnt];
5113 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5114 // Build: Iter = (IV / Div) % IS.NumIters
5115 // where Div is product of previous iterations' IS.NumIters.
5116 ExprResult Iter;
5117 if (Div.isUsable()) {
5118 Iter =
5119 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5120 } else {
5121 Iter = IV;
5122 assert((Cnt == (int)NestedLoopCount - 1) &&
5123 "unusable div expected on first iteration only");
5124 }
5125
5126 if (Cnt != 0 && Iter.isUsable())
5127 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5128 IS.NumIterations);
5129 if (!Iter.isUsable()) {
5130 HasErrors = true;
5131 break;
5132 }
5133
Alexey Bataev39f915b82015-05-08 10:41:21 +00005134 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005135 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005136 DeclRefExpr *CounterVar = buildDeclRefExpr(
5137 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5138 /*RefersToCapture=*/true);
5139 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005140 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005141 if (!Init.isUsable()) {
5142 HasErrors = true;
5143 break;
5144 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005145 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005146 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5147 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005148 if (!Update.isUsable()) {
5149 HasErrors = true;
5150 break;
5151 }
5152
5153 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005154 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005155 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005156 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005157 if (!Final.isUsable()) {
5158 HasErrors = true;
5159 break;
5160 }
5161
5162 // Build Div for the next iteration: Div <- Div * IS.NumIters
5163 if (Cnt != 0) {
5164 if (Div.isUnset())
5165 Div = IS.NumIterations;
5166 else
5167 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5168 IS.NumIterations);
5169
5170 // Add parentheses (for debugging purposes only).
5171 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005172 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005173 if (!Div.isUsable()) {
5174 HasErrors = true;
5175 break;
5176 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005177 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005178 }
5179 if (!Update.isUsable() || !Final.isUsable()) {
5180 HasErrors = true;
5181 break;
5182 }
5183 // Save results
5184 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005185 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005186 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005187 Built.Updates[Cnt] = Update.get();
5188 Built.Finals[Cnt] = Final.get();
5189 }
5190 }
5191
5192 if (HasErrors)
5193 return 0;
5194
5195 // Save results
5196 Built.IterationVarRef = IV.get();
5197 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005198 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005199 Built.CalcLastIteration =
5200 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005201 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005202 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005203 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005204 Built.Init = Init.get();
5205 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005206 Built.LB = LB.get();
5207 Built.UB = UB.get();
5208 Built.IL = IL.get();
5209 Built.ST = ST.get();
5210 Built.EUB = EUB.get();
5211 Built.NLB = NextLB.get();
5212 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005213 Built.PrevLB = PrevLB.get();
5214 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005215 Built.DistInc = DistInc.get();
5216 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005217 Built.DistCombinedFields.LB = CombLB.get();
5218 Built.DistCombinedFields.UB = CombUB.get();
5219 Built.DistCombinedFields.EUB = CombEUB.get();
5220 Built.DistCombinedFields.Init = CombInit.get();
5221 Built.DistCombinedFields.Cond = CombCond.get();
5222 Built.DistCombinedFields.NLB = CombNextLB.get();
5223 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005224
Alexey Bataev8b427062016-05-25 12:36:08 +00005225 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5226 // Fill data for doacross depend clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +00005227 for (const auto &Pair : DSA.getDoacrossDependClauses()) {
5228 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source) {
Alexey Bataev8b427062016-05-25 12:36:08 +00005229 Pair.first->setCounterValue(CounterVal);
Alexey Bataeve3727102018-04-18 15:57:46 +00005230 } else {
Alexey Bataev8b427062016-05-25 12:36:08 +00005231 if (NestedLoopCount != Pair.second.size() ||
5232 NestedLoopCount != LoopMultipliers.size() + 1) {
5233 // Erroneous case - clause has some problems.
5234 Pair.first->setCounterValue(CounterVal);
5235 continue;
5236 }
5237 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5238 auto I = Pair.second.rbegin();
5239 auto IS = IterSpaces.rbegin();
5240 auto ILM = LoopMultipliers.rbegin();
5241 Expr *UpCounterVal = CounterVal;
5242 Expr *Multiplier = nullptr;
5243 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5244 if (I->first) {
5245 assert(IS->CounterStep);
5246 Expr *NormalizedOffset =
5247 SemaRef
5248 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5249 I->first, IS->CounterStep)
5250 .get();
5251 if (Multiplier) {
5252 NormalizedOffset =
5253 SemaRef
5254 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5255 NormalizedOffset, Multiplier)
5256 .get();
5257 }
5258 assert(I->second == OO_Plus || I->second == OO_Minus);
5259 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
David Majnemer9d168222016-08-05 17:44:54 +00005260 UpCounterVal = SemaRef
5261 .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5262 UpCounterVal, NormalizedOffset)
5263 .get();
Alexey Bataev8b427062016-05-25 12:36:08 +00005264 }
5265 Multiplier = *ILM;
5266 ++I;
5267 ++IS;
5268 ++ILM;
5269 }
5270 Pair.first->setCounterValue(UpCounterVal);
5271 }
5272 }
5273
Alexey Bataevabfc0692014-06-25 06:52:00 +00005274 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005275}
5276
Alexey Bataev10e775f2015-07-30 11:36:16 +00005277static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005278 auto CollapseClauses =
5279 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5280 if (CollapseClauses.begin() != CollapseClauses.end())
5281 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005282 return nullptr;
5283}
5284
Alexey Bataev10e775f2015-07-30 11:36:16 +00005285static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005286 auto OrderedClauses =
5287 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5288 if (OrderedClauses.begin() != OrderedClauses.end())
5289 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005290 return nullptr;
5291}
5292
Kelvin Lic5609492016-07-15 04:39:07 +00005293static bool checkSimdlenSafelenSpecified(Sema &S,
5294 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005295 const OMPSafelenClause *Safelen = nullptr;
5296 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005297
Alexey Bataeve3727102018-04-18 15:57:46 +00005298 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005299 if (Clause->getClauseKind() == OMPC_safelen)
5300 Safelen = cast<OMPSafelenClause>(Clause);
5301 else if (Clause->getClauseKind() == OMPC_simdlen)
5302 Simdlen = cast<OMPSimdlenClause>(Clause);
5303 if (Safelen && Simdlen)
5304 break;
5305 }
5306
5307 if (Simdlen && Safelen) {
5308 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005309 const Expr *SimdlenLength = Simdlen->getSimdlen();
5310 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005311 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5312 SimdlenLength->isInstantiationDependent() ||
5313 SimdlenLength->containsUnexpandedParameterPack())
5314 return false;
5315 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5316 SafelenLength->isInstantiationDependent() ||
5317 SafelenLength->containsUnexpandedParameterPack())
5318 return false;
5319 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5320 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5321 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5322 // If both simdlen and safelen clauses are specified, the value of the
5323 // simdlen parameter must be less than or equal to the value of the safelen
5324 // parameter.
5325 if (SimdlenRes > SafelenRes) {
5326 S.Diag(SimdlenLength->getExprLoc(),
5327 diag::err_omp_wrong_simdlen_safelen_values)
5328 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5329 return true;
5330 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005331 }
5332 return false;
5333}
5334
Alexey Bataeve3727102018-04-18 15:57:46 +00005335StmtResult
5336Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5337 SourceLocation StartLoc, SourceLocation EndLoc,
5338 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005339 if (!AStmt)
5340 return StmtError();
5341
5342 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005343 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005344 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5345 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005346 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005347 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5348 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005349 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005350 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005351
Alexander Musmana5f070a2014-10-01 06:03:56 +00005352 assert((CurContext->isDependentContext() || B.builtAll()) &&
5353 "omp simd loop exprs were not built");
5354
Alexander Musman3276a272015-03-21 10:12:56 +00005355 if (!CurContext->isDependentContext()) {
5356 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005357 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005358 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005359 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005360 B.NumIterations, *this, CurScope,
5361 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005362 return StmtError();
5363 }
5364 }
5365
Kelvin Lic5609492016-07-15 04:39:07 +00005366 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005367 return StmtError();
5368
Reid Kleckner87a31802018-03-12 21:43:02 +00005369 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005370 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5371 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005372}
5373
Alexey Bataeve3727102018-04-18 15:57:46 +00005374StmtResult
5375Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5376 SourceLocation StartLoc, SourceLocation EndLoc,
5377 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005378 if (!AStmt)
5379 return StmtError();
5380
5381 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005382 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005383 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5384 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005385 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005386 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5387 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005388 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005389 return StmtError();
5390
Alexander Musmana5f070a2014-10-01 06:03:56 +00005391 assert((CurContext->isDependentContext() || B.builtAll()) &&
5392 "omp for loop exprs were not built");
5393
Alexey Bataev54acd402015-08-04 11:18:19 +00005394 if (!CurContext->isDependentContext()) {
5395 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005396 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005397 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005398 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005399 B.NumIterations, *this, CurScope,
5400 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005401 return StmtError();
5402 }
5403 }
5404
Reid Kleckner87a31802018-03-12 21:43:02 +00005405 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005406 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005407 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005408}
5409
Alexander Musmanf82886e2014-09-18 05:12:34 +00005410StmtResult Sema::ActOnOpenMPForSimdDirective(
5411 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005412 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005413 if (!AStmt)
5414 return StmtError();
5415
5416 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005417 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005418 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5419 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005420 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005421 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005422 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5423 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005424 if (NestedLoopCount == 0)
5425 return StmtError();
5426
Alexander Musmanc6388682014-12-15 07:07:06 +00005427 assert((CurContext->isDependentContext() || B.builtAll()) &&
5428 "omp for simd loop exprs were not built");
5429
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005430 if (!CurContext->isDependentContext()) {
5431 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005432 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005433 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005434 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005435 B.NumIterations, *this, CurScope,
5436 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005437 return StmtError();
5438 }
5439 }
5440
Kelvin Lic5609492016-07-15 04:39:07 +00005441 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005442 return StmtError();
5443
Reid Kleckner87a31802018-03-12 21:43:02 +00005444 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005445 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5446 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005447}
5448
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005449StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5450 Stmt *AStmt,
5451 SourceLocation StartLoc,
5452 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005453 if (!AStmt)
5454 return StmtError();
5455
5456 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005457 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005458 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005459 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005460 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005461 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005462 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005463 return StmtError();
5464 // All associated statements must be '#pragma omp section' except for
5465 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005466 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005467 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5468 if (SectionStmt)
5469 Diag(SectionStmt->getLocStart(),
5470 diag::err_omp_sections_substmt_not_section);
5471 return StmtError();
5472 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005473 cast<OMPSectionDirective>(SectionStmt)
5474 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005475 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005476 } else {
5477 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5478 return StmtError();
5479 }
5480
Reid Kleckner87a31802018-03-12 21:43:02 +00005481 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005482
Alexey Bataev25e5b442015-09-15 12:52:43 +00005483 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5484 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005485}
5486
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005487StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5488 SourceLocation StartLoc,
5489 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005490 if (!AStmt)
5491 return StmtError();
5492
5493 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005494
Reid Kleckner87a31802018-03-12 21:43:02 +00005495 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005496 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005497
Alexey Bataev25e5b442015-09-15 12:52:43 +00005498 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5499 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005500}
5501
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005502StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5503 Stmt *AStmt,
5504 SourceLocation StartLoc,
5505 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005506 if (!AStmt)
5507 return StmtError();
5508
5509 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005510
Reid Kleckner87a31802018-03-12 21:43:02 +00005511 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005512
Alexey Bataev3255bf32015-01-19 05:20:46 +00005513 // OpenMP [2.7.3, single Construct, Restrictions]
5514 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005515 const OMPClause *Nowait = nullptr;
5516 const OMPClause *Copyprivate = nullptr;
5517 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005518 if (Clause->getClauseKind() == OMPC_nowait)
5519 Nowait = Clause;
5520 else if (Clause->getClauseKind() == OMPC_copyprivate)
5521 Copyprivate = Clause;
5522 if (Copyprivate && Nowait) {
5523 Diag(Copyprivate->getLocStart(),
5524 diag::err_omp_single_copyprivate_with_nowait);
5525 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5526 return StmtError();
5527 }
5528 }
5529
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005530 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5531}
5532
Alexander Musman80c22892014-07-17 08:54:58 +00005533StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5534 SourceLocation StartLoc,
5535 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005536 if (!AStmt)
5537 return StmtError();
5538
5539 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005540
Reid Kleckner87a31802018-03-12 21:43:02 +00005541 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005542
5543 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5544}
5545
Alexey Bataev28c75412015-12-15 08:19:24 +00005546StmtResult Sema::ActOnOpenMPCriticalDirective(
5547 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5548 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005549 if (!AStmt)
5550 return StmtError();
5551
5552 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005553
Alexey Bataev28c75412015-12-15 08:19:24 +00005554 bool ErrorFound = false;
5555 llvm::APSInt Hint;
5556 SourceLocation HintLoc;
5557 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005558 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005559 if (C->getClauseKind() == OMPC_hint) {
5560 if (!DirName.getName()) {
5561 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5562 ErrorFound = true;
5563 }
5564 Expr *E = cast<OMPHintClause>(C)->getHint();
5565 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005566 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005567 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005568 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005569 Hint = E->EvaluateKnownConstInt(Context);
5570 HintLoc = C->getLocStart();
5571 }
5572 }
5573 }
5574 if (ErrorFound)
5575 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005576 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005577 if (Pair.first && DirName.getName() && !DependentHint) {
5578 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5579 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005580 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005581 Diag(HintLoc, diag::note_omp_critical_hint_here)
5582 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005583 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005584 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005585 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005586 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5587 << 1
5588 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5589 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005590 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005591 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005592 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005593 }
5594 }
5595
Reid Kleckner87a31802018-03-12 21:43:02 +00005596 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005597
Alexey Bataev28c75412015-12-15 08:19:24 +00005598 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5599 Clauses, AStmt);
5600 if (!Pair.first && DirName.getName() && !DependentHint)
5601 DSAStack->addCriticalWithHint(Dir, Hint);
5602 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005603}
5604
Alexey Bataev4acb8592014-07-07 13:01:15 +00005605StmtResult Sema::ActOnOpenMPParallelForDirective(
5606 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005607 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005608 if (!AStmt)
5609 return StmtError();
5610
Alexey Bataeve3727102018-04-18 15:57:46 +00005611 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005612 // 1.2.2 OpenMP Language Terminology
5613 // Structured block - An executable statement with a single entry at the
5614 // top and a single exit at the bottom.
5615 // The point of exit cannot be a branch out of the structured block.
5616 // longjmp() and throw() must not violate the entry/exit criteria.
5617 CS->getCapturedDecl()->setNothrow();
5618
Alexander Musmanc6388682014-12-15 07:07:06 +00005619 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005620 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5621 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005622 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005623 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005624 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5625 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005626 if (NestedLoopCount == 0)
5627 return StmtError();
5628
Alexander Musmana5f070a2014-10-01 06:03:56 +00005629 assert((CurContext->isDependentContext() || B.builtAll()) &&
5630 "omp parallel for loop exprs were not built");
5631
Alexey Bataev54acd402015-08-04 11:18:19 +00005632 if (!CurContext->isDependentContext()) {
5633 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005634 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005635 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005636 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005637 B.NumIterations, *this, CurScope,
5638 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005639 return StmtError();
5640 }
5641 }
5642
Reid Kleckner87a31802018-03-12 21:43:02 +00005643 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005644 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005645 NestedLoopCount, Clauses, AStmt, B,
5646 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005647}
5648
Alexander Musmane4e893b2014-09-23 09:33:00 +00005649StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5650 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005651 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005652 if (!AStmt)
5653 return StmtError();
5654
Alexey Bataeve3727102018-04-18 15:57:46 +00005655 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005656 // 1.2.2 OpenMP Language Terminology
5657 // Structured block - An executable statement with a single entry at the
5658 // top and a single exit at the bottom.
5659 // The point of exit cannot be a branch out of the structured block.
5660 // longjmp() and throw() must not violate the entry/exit criteria.
5661 CS->getCapturedDecl()->setNothrow();
5662
Alexander Musmanc6388682014-12-15 07:07:06 +00005663 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005664 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5665 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005666 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005667 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005668 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5669 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005670 if (NestedLoopCount == 0)
5671 return StmtError();
5672
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005673 if (!CurContext->isDependentContext()) {
5674 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005675 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005676 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005677 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005678 B.NumIterations, *this, CurScope,
5679 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005680 return StmtError();
5681 }
5682 }
5683
Kelvin Lic5609492016-07-15 04:39:07 +00005684 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005685 return StmtError();
5686
Reid Kleckner87a31802018-03-12 21:43:02 +00005687 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005688 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005689 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005690}
5691
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005692StmtResult
5693Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5694 Stmt *AStmt, SourceLocation StartLoc,
5695 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005696 if (!AStmt)
5697 return StmtError();
5698
5699 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005700 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005701 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005702 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005703 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005704 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005705 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005706 return StmtError();
5707 // All associated statements must be '#pragma omp section' except for
5708 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005709 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005710 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5711 if (SectionStmt)
5712 Diag(SectionStmt->getLocStart(),
5713 diag::err_omp_parallel_sections_substmt_not_section);
5714 return StmtError();
5715 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005716 cast<OMPSectionDirective>(SectionStmt)
5717 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005718 }
5719 } else {
5720 Diag(AStmt->getLocStart(),
5721 diag::err_omp_parallel_sections_not_compound_stmt);
5722 return StmtError();
5723 }
5724
Reid Kleckner87a31802018-03-12 21:43:02 +00005725 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005726
Alexey Bataev25e5b442015-09-15 12:52:43 +00005727 return OMPParallelSectionsDirective::Create(
5728 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005729}
5730
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005731StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5732 Stmt *AStmt, SourceLocation StartLoc,
5733 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005734 if (!AStmt)
5735 return StmtError();
5736
David Majnemer9d168222016-08-05 17:44:54 +00005737 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005738 // 1.2.2 OpenMP Language Terminology
5739 // Structured block - An executable statement with a single entry at the
5740 // top and a single exit at the bottom.
5741 // The point of exit cannot be a branch out of the structured block.
5742 // longjmp() and throw() must not violate the entry/exit criteria.
5743 CS->getCapturedDecl()->setNothrow();
5744
Reid Kleckner87a31802018-03-12 21:43:02 +00005745 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005746
Alexey Bataev25e5b442015-09-15 12:52:43 +00005747 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5748 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005749}
5750
Alexey Bataev68446b72014-07-18 07:47:19 +00005751StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5752 SourceLocation EndLoc) {
5753 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5754}
5755
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005756StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5757 SourceLocation EndLoc) {
5758 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5759}
5760
Alexey Bataev2df347a2014-07-18 10:17:07 +00005761StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5762 SourceLocation EndLoc) {
5763 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5764}
5765
Alexey Bataev169d96a2017-07-18 20:17:46 +00005766StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5767 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005768 SourceLocation StartLoc,
5769 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005770 if (!AStmt)
5771 return StmtError();
5772
5773 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005774
Reid Kleckner87a31802018-03-12 21:43:02 +00005775 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005776
Alexey Bataev169d96a2017-07-18 20:17:46 +00005777 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005778 AStmt,
5779 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005780}
5781
Alexey Bataev6125da92014-07-21 11:26:11 +00005782StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5783 SourceLocation StartLoc,
5784 SourceLocation EndLoc) {
5785 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5786 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5787}
5788
Alexey Bataev346265e2015-09-25 10:37:12 +00005789StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5790 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005791 SourceLocation StartLoc,
5792 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005793 const OMPClause *DependFound = nullptr;
5794 const OMPClause *DependSourceClause = nullptr;
5795 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005796 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005797 const OMPThreadsClause *TC = nullptr;
5798 const OMPSIMDClause *SC = nullptr;
5799 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005800 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5801 DependFound = C;
5802 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5803 if (DependSourceClause) {
5804 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5805 << getOpenMPDirectiveName(OMPD_ordered)
5806 << getOpenMPClauseName(OMPC_depend) << 2;
5807 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005808 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00005809 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00005810 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005811 if (DependSinkClause) {
5812 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5813 << 0;
5814 ErrorFound = true;
5815 }
5816 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5817 if (DependSourceClause) {
5818 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5819 << 1;
5820 ErrorFound = true;
5821 }
5822 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005823 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005824 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00005825 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005826 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005827 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005828 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005829 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005830 if (!ErrorFound && !SC &&
5831 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005832 // OpenMP [2.8.1,simd Construct, Restrictions]
5833 // An ordered construct with the simd clause is the only OpenMP construct
5834 // that can appear in the simd region.
5835 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005836 ErrorFound = true;
5837 } else if (DependFound && (TC || SC)) {
5838 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5839 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5840 ErrorFound = true;
5841 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5842 Diag(DependFound->getLocStart(),
5843 diag::err_omp_ordered_directive_without_param);
5844 ErrorFound = true;
5845 } else if (TC || Clauses.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005846 if (const Expr *Param = DSAStack->getParentOrderedRegionParam()) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005847 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5848 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5849 << (TC != nullptr);
5850 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5851 ErrorFound = true;
5852 }
5853 }
5854 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005855 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005856
5857 if (AStmt) {
5858 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5859
Reid Kleckner87a31802018-03-12 21:43:02 +00005860 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005861 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005862
5863 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005864}
5865
Alexey Bataev1d160b12015-03-13 12:27:31 +00005866namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005867/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00005868/// construct.
5869class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005870 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005871 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005872 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005873 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005874 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005875 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005876 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005877 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005878 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005879 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005880 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005881 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005882 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005883 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005884 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00005885 /// expression.
5886 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005887 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00005888 /// part.
5889 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005890 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005891 NoError
5892 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005893 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005894 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005895 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00005896 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005897 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005898 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005899 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005900 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005901 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00005902 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5903 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5904 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005905 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00005906 /// important for non-associative operations.
5907 bool IsXLHSInRHSPart;
5908 BinaryOperatorKind Op;
5909 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005910 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00005911 /// if it is a prefix unary operation.
5912 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005913
5914public:
5915 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005916 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005917 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005918 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00005919 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005920 /// expression. If DiagId and NoteId == 0, then only check is performed
5921 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005922 /// \param DiagId Diagnostic which should be emitted if error is found.
5923 /// \param NoteId Diagnostic note for the main error message.
5924 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005925 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005926 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005927 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005928 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005929 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005930 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00005931 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5932 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5933 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005934 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00005935 /// false otherwise.
5936 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5937
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005938 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00005939 /// if it is a prefix unary operation.
5940 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5941
Alexey Bataev1d160b12015-03-13 12:27:31 +00005942private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005943 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5944 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005945};
5946} // namespace
5947
5948bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5949 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5950 ExprAnalysisErrorCode ErrorFound = NoError;
5951 SourceLocation ErrorLoc, NoteLoc;
5952 SourceRange ErrorRange, NoteRange;
5953 // Allowed constructs are:
5954 // x = x binop expr;
5955 // x = expr binop x;
5956 if (AtomicBinOp->getOpcode() == BO_Assign) {
5957 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00005958 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005959 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5960 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5961 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5962 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005963 Op = AtomicInnerBinOp->getOpcode();
5964 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00005965 Expr *LHS = AtomicInnerBinOp->getLHS();
5966 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005967 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5968 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5969 /*Canonical=*/true);
5970 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5971 /*Canonical=*/true);
5972 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5973 /*Canonical=*/true);
5974 if (XId == LHSId) {
5975 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005976 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005977 } else if (XId == RHSId) {
5978 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005979 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005980 } else {
5981 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5982 ErrorRange = AtomicInnerBinOp->getSourceRange();
5983 NoteLoc = X->getExprLoc();
5984 NoteRange = X->getSourceRange();
5985 ErrorFound = NotAnUpdateExpression;
5986 }
5987 } else {
5988 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5989 ErrorRange = AtomicInnerBinOp->getSourceRange();
5990 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5991 NoteRange = SourceRange(NoteLoc, NoteLoc);
5992 ErrorFound = NotABinaryOperator;
5993 }
5994 } else {
5995 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5996 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5997 ErrorFound = NotABinaryExpression;
5998 }
5999 } else {
6000 ErrorLoc = AtomicBinOp->getExprLoc();
6001 ErrorRange = AtomicBinOp->getSourceRange();
6002 NoteLoc = AtomicBinOp->getOperatorLoc();
6003 NoteRange = SourceRange(NoteLoc, NoteLoc);
6004 ErrorFound = NotAnAssignmentOp;
6005 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006006 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006007 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6008 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6009 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006010 }
6011 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006012 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006013 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006014}
6015
6016bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6017 unsigned NoteId) {
6018 ExprAnalysisErrorCode ErrorFound = NoError;
6019 SourceLocation ErrorLoc, NoteLoc;
6020 SourceRange ErrorRange, NoteRange;
6021 // Allowed constructs are:
6022 // x++;
6023 // x--;
6024 // ++x;
6025 // --x;
6026 // x binop= expr;
6027 // x = x binop expr;
6028 // x = expr binop x;
6029 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6030 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6031 if (AtomicBody->getType()->isScalarType() ||
6032 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006033 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006034 AtomicBody->IgnoreParenImpCasts())) {
6035 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006036 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006037 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006038 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006039 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006040 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006041 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006042 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6043 AtomicBody->IgnoreParenImpCasts())) {
6044 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006045 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006046 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006047 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006048 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006049 // Check for Unary Operation
6050 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006051 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006052 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6053 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006054 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006055 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6056 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006057 } else {
6058 ErrorFound = NotAnUnaryIncDecExpression;
6059 ErrorLoc = AtomicUnaryOp->getExprLoc();
6060 ErrorRange = AtomicUnaryOp->getSourceRange();
6061 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6062 NoteRange = SourceRange(NoteLoc, NoteLoc);
6063 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006064 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 ErrorFound = NotABinaryOrUnaryExpression;
6066 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6067 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6068 }
6069 } else {
6070 ErrorFound = NotAScalarType;
6071 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6072 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6073 }
6074 } else {
6075 ErrorFound = NotAnExpression;
6076 NoteLoc = ErrorLoc = S->getLocStart();
6077 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6078 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006079 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006080 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6081 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6082 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006083 }
6084 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006085 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006086 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006087 // Build an update expression of form 'OpaqueValueExpr(x) binop
6088 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6089 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6090 auto *OVEX = new (SemaRef.getASTContext())
6091 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6092 auto *OVEExpr = new (SemaRef.getASTContext())
6093 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006094 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006095 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6096 IsXLHSInRHSPart ? OVEExpr : OVEX);
6097 if (Update.isInvalid())
6098 return true;
6099 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6100 Sema::AA_Casting);
6101 if (Update.isInvalid())
6102 return true;
6103 UpdateExpr = Update.get();
6104 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006105 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006106}
6107
Alexey Bataev0162e452014-07-22 10:10:35 +00006108StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6109 Stmt *AStmt,
6110 SourceLocation StartLoc,
6111 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006112 if (!AStmt)
6113 return StmtError();
6114
David Majnemer9d168222016-08-05 17:44:54 +00006115 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006116 // 1.2.2 OpenMP Language Terminology
6117 // Structured block - An executable statement with a single entry at the
6118 // top and a single exit at the bottom.
6119 // The point of exit cannot be a branch out of the structured block.
6120 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006121 OpenMPClauseKind AtomicKind = OMPC_unknown;
6122 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006123 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006124 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006125 C->getClauseKind() == OMPC_update ||
6126 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006127 if (AtomicKind != OMPC_unknown) {
6128 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6129 << SourceRange(C->getLocStart(), C->getLocEnd());
6130 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6131 << getOpenMPClauseName(AtomicKind);
6132 } else {
6133 AtomicKind = C->getClauseKind();
6134 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006135 }
6136 }
6137 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006138
Alexey Bataeve3727102018-04-18 15:57:46 +00006139 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006140 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6141 Body = EWC->getSubExpr();
6142
Alexey Bataev62cec442014-11-18 10:14:22 +00006143 Expr *X = nullptr;
6144 Expr *V = nullptr;
6145 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006146 Expr *UE = nullptr;
6147 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006148 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006149 // OpenMP [2.12.6, atomic Construct]
6150 // In the next expressions:
6151 // * x and v (as applicable) are both l-value expressions with scalar type.
6152 // * During the execution of an atomic region, multiple syntactic
6153 // occurrences of x must designate the same storage location.
6154 // * Neither of v and expr (as applicable) may access the storage location
6155 // designated by x.
6156 // * Neither of x and expr (as applicable) may access the storage location
6157 // designated by v.
6158 // * expr is an expression with scalar type.
6159 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6160 // * binop, binop=, ++, and -- are not overloaded operators.
6161 // * The expression x binop expr must be numerically equivalent to x binop
6162 // (expr). This requirement is satisfied if the operators in expr have
6163 // precedence greater than binop, or by using parentheses around expr or
6164 // subexpressions of expr.
6165 // * The expression expr binop x must be numerically equivalent to (expr)
6166 // binop x. This requirement is satisfied if the operators in expr have
6167 // precedence equal to or greater than binop, or by using parentheses around
6168 // expr or subexpressions of expr.
6169 // * For forms that allow multiple occurrences of x, the number of times
6170 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006171 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006172 enum {
6173 NotAnExpression,
6174 NotAnAssignmentOp,
6175 NotAScalarType,
6176 NotAnLValue,
6177 NoError
6178 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006179 SourceLocation ErrorLoc, NoteLoc;
6180 SourceRange ErrorRange, NoteRange;
6181 // If clause is read:
6182 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006183 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6184 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006185 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6186 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6187 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6188 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6189 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6190 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6191 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006192 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006193 ErrorFound = NotAnLValue;
6194 ErrorLoc = AtomicBinOp->getExprLoc();
6195 ErrorRange = AtomicBinOp->getSourceRange();
6196 NoteLoc = NotLValueExpr->getExprLoc();
6197 NoteRange = NotLValueExpr->getSourceRange();
6198 }
6199 } else if (!X->isInstantiationDependent() ||
6200 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006201 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006202 (X->isInstantiationDependent() || X->getType()->isScalarType())
6203 ? V
6204 : X;
6205 ErrorFound = NotAScalarType;
6206 ErrorLoc = AtomicBinOp->getExprLoc();
6207 ErrorRange = AtomicBinOp->getSourceRange();
6208 NoteLoc = NotScalarExpr->getExprLoc();
6209 NoteRange = NotScalarExpr->getSourceRange();
6210 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006211 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006212 ErrorFound = NotAnAssignmentOp;
6213 ErrorLoc = AtomicBody->getExprLoc();
6214 ErrorRange = AtomicBody->getSourceRange();
6215 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6216 : AtomicBody->getExprLoc();
6217 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6218 : AtomicBody->getSourceRange();
6219 }
6220 } else {
6221 ErrorFound = NotAnExpression;
6222 NoteLoc = ErrorLoc = Body->getLocStart();
6223 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006224 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006225 if (ErrorFound != NoError) {
6226 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6227 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006228 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6229 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006230 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006231 }
6232 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006233 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006234 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006235 enum {
6236 NotAnExpression,
6237 NotAnAssignmentOp,
6238 NotAScalarType,
6239 NotAnLValue,
6240 NoError
6241 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006242 SourceLocation ErrorLoc, NoteLoc;
6243 SourceRange ErrorRange, NoteRange;
6244 // If clause is write:
6245 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006246 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6247 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006248 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6249 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006250 X = AtomicBinOp->getLHS();
6251 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006252 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6253 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6254 if (!X->isLValue()) {
6255 ErrorFound = NotAnLValue;
6256 ErrorLoc = AtomicBinOp->getExprLoc();
6257 ErrorRange = AtomicBinOp->getSourceRange();
6258 NoteLoc = X->getExprLoc();
6259 NoteRange = X->getSourceRange();
6260 }
6261 } else if (!X->isInstantiationDependent() ||
6262 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006263 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006264 (X->isInstantiationDependent() || X->getType()->isScalarType())
6265 ? E
6266 : X;
6267 ErrorFound = NotAScalarType;
6268 ErrorLoc = AtomicBinOp->getExprLoc();
6269 ErrorRange = AtomicBinOp->getSourceRange();
6270 NoteLoc = NotScalarExpr->getExprLoc();
6271 NoteRange = NotScalarExpr->getSourceRange();
6272 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006273 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006274 ErrorFound = NotAnAssignmentOp;
6275 ErrorLoc = AtomicBody->getExprLoc();
6276 ErrorRange = AtomicBody->getSourceRange();
6277 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6278 : AtomicBody->getExprLoc();
6279 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6280 : AtomicBody->getSourceRange();
6281 }
6282 } else {
6283 ErrorFound = NotAnExpression;
6284 NoteLoc = ErrorLoc = Body->getLocStart();
6285 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006286 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006287 if (ErrorFound != NoError) {
6288 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6289 << ErrorRange;
6290 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6291 << NoteRange;
6292 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006293 }
6294 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006295 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006296 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006297 // If clause is update:
6298 // x++;
6299 // x--;
6300 // ++x;
6301 // --x;
6302 // x binop= expr;
6303 // x = x binop expr;
6304 // x = expr binop x;
6305 OpenMPAtomicUpdateChecker Checker(*this);
6306 if (Checker.checkStatement(
6307 Body, (AtomicKind == OMPC_update)
6308 ? diag::err_omp_atomic_update_not_expression_statement
6309 : diag::err_omp_atomic_not_expression_statement,
6310 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006311 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006312 if (!CurContext->isDependentContext()) {
6313 E = Checker.getExpr();
6314 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006315 UE = Checker.getUpdateExpr();
6316 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006317 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006318 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006319 enum {
6320 NotAnAssignmentOp,
6321 NotACompoundStatement,
6322 NotTwoSubstatements,
6323 NotASpecificExpression,
6324 NoError
6325 } ErrorFound = NoError;
6326 SourceLocation ErrorLoc, NoteLoc;
6327 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006328 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006329 // If clause is a capture:
6330 // v = x++;
6331 // v = x--;
6332 // v = ++x;
6333 // v = --x;
6334 // v = x binop= expr;
6335 // v = x = x binop expr;
6336 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006337 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006338 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6339 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6340 V = AtomicBinOp->getLHS();
6341 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6342 OpenMPAtomicUpdateChecker Checker(*this);
6343 if (Checker.checkStatement(
6344 Body, diag::err_omp_atomic_capture_not_expression_statement,
6345 diag::note_omp_atomic_update))
6346 return StmtError();
6347 E = Checker.getExpr();
6348 X = Checker.getX();
6349 UE = Checker.getUpdateExpr();
6350 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6351 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006352 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006353 ErrorLoc = AtomicBody->getExprLoc();
6354 ErrorRange = AtomicBody->getSourceRange();
6355 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6356 : AtomicBody->getExprLoc();
6357 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6358 : AtomicBody->getSourceRange();
6359 ErrorFound = NotAnAssignmentOp;
6360 }
6361 if (ErrorFound != NoError) {
6362 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6363 << ErrorRange;
6364 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6365 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006366 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006367 if (CurContext->isDependentContext())
6368 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006369 } else {
6370 // If clause is a capture:
6371 // { v = x; x = expr; }
6372 // { v = x; x++; }
6373 // { v = x; x--; }
6374 // { v = x; ++x; }
6375 // { v = x; --x; }
6376 // { v = x; x binop= expr; }
6377 // { v = x; x = x binop expr; }
6378 // { v = x; x = expr binop x; }
6379 // { x++; v = x; }
6380 // { x--; v = x; }
6381 // { ++x; v = x; }
6382 // { --x; v = x; }
6383 // { x binop= expr; v = x; }
6384 // { x = x binop expr; v = x; }
6385 // { x = expr binop x; v = x; }
6386 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6387 // Check that this is { expr1; expr2; }
6388 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006389 Stmt *First = CS->body_front();
6390 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006391 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6392 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6393 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6394 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6395 // Need to find what subexpression is 'v' and what is 'x'.
6396 OpenMPAtomicUpdateChecker Checker(*this);
6397 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6398 BinaryOperator *BinOp = nullptr;
6399 if (IsUpdateExprFound) {
6400 BinOp = dyn_cast<BinaryOperator>(First);
6401 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6402 }
6403 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6404 // { v = x; x++; }
6405 // { v = x; x--; }
6406 // { v = x; ++x; }
6407 // { v = x; --x; }
6408 // { v = x; x binop= expr; }
6409 // { v = x; x = x binop expr; }
6410 // { v = x; x = expr binop x; }
6411 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006412 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006413 llvm::FoldingSetNodeID XId, PossibleXId;
6414 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6415 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6416 IsUpdateExprFound = XId == PossibleXId;
6417 if (IsUpdateExprFound) {
6418 V = BinOp->getLHS();
6419 X = Checker.getX();
6420 E = Checker.getExpr();
6421 UE = Checker.getUpdateExpr();
6422 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006423 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006424 }
6425 }
6426 if (!IsUpdateExprFound) {
6427 IsUpdateExprFound = !Checker.checkStatement(First);
6428 BinOp = nullptr;
6429 if (IsUpdateExprFound) {
6430 BinOp = dyn_cast<BinaryOperator>(Second);
6431 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6432 }
6433 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6434 // { x++; v = x; }
6435 // { x--; v = x; }
6436 // { ++x; v = x; }
6437 // { --x; v = x; }
6438 // { x binop= expr; v = x; }
6439 // { x = x binop expr; v = x; }
6440 // { x = expr binop x; v = x; }
6441 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006442 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006443 llvm::FoldingSetNodeID XId, PossibleXId;
6444 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6445 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6446 IsUpdateExprFound = XId == PossibleXId;
6447 if (IsUpdateExprFound) {
6448 V = BinOp->getLHS();
6449 X = Checker.getX();
6450 E = Checker.getExpr();
6451 UE = Checker.getUpdateExpr();
6452 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006453 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006454 }
6455 }
6456 }
6457 if (!IsUpdateExprFound) {
6458 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006459 auto *FirstExpr = dyn_cast<Expr>(First);
6460 auto *SecondExpr = dyn_cast<Expr>(Second);
6461 if (!FirstExpr || !SecondExpr ||
6462 !(FirstExpr->isInstantiationDependent() ||
6463 SecondExpr->isInstantiationDependent())) {
6464 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6465 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006466 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006467 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6468 : First->getLocStart();
6469 NoteRange = ErrorRange = FirstBinOp
6470 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006471 : SourceRange(ErrorLoc, ErrorLoc);
6472 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006473 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6474 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6475 ErrorFound = NotAnAssignmentOp;
6476 NoteLoc = ErrorLoc = SecondBinOp
6477 ? SecondBinOp->getOperatorLoc()
6478 : Second->getLocStart();
6479 NoteRange = ErrorRange =
6480 SecondBinOp ? SecondBinOp->getSourceRange()
6481 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006482 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006483 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006484 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006485 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006486 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6487 llvm::FoldingSetNodeID X1Id, X2Id;
6488 PossibleXRHSInFirst->Profile(X1Id, Context,
6489 /*Canonical=*/true);
6490 PossibleXLHSInSecond->Profile(X2Id, Context,
6491 /*Canonical=*/true);
6492 IsUpdateExprFound = X1Id == X2Id;
6493 if (IsUpdateExprFound) {
6494 V = FirstBinOp->getLHS();
6495 X = SecondBinOp->getLHS();
6496 E = SecondBinOp->getRHS();
6497 UE = nullptr;
6498 IsXLHSInRHSPart = false;
6499 IsPostfixUpdate = true;
6500 } else {
6501 ErrorFound = NotASpecificExpression;
6502 ErrorLoc = FirstBinOp->getExprLoc();
6503 ErrorRange = FirstBinOp->getSourceRange();
6504 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6505 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6506 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006507 }
6508 }
6509 }
6510 }
6511 } else {
6512 NoteLoc = ErrorLoc = Body->getLocStart();
6513 NoteRange = ErrorRange =
6514 SourceRange(Body->getLocStart(), Body->getLocStart());
6515 ErrorFound = NotTwoSubstatements;
6516 }
6517 } else {
6518 NoteLoc = ErrorLoc = Body->getLocStart();
6519 NoteRange = ErrorRange =
6520 SourceRange(Body->getLocStart(), Body->getLocStart());
6521 ErrorFound = NotACompoundStatement;
6522 }
6523 if (ErrorFound != NoError) {
6524 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6525 << ErrorRange;
6526 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6527 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006528 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006529 if (CurContext->isDependentContext())
6530 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006531 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006532 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006533
Reid Kleckner87a31802018-03-12 21:43:02 +00006534 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006535
Alexey Bataev62cec442014-11-18 10:14:22 +00006536 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006537 X, V, E, UE, IsXLHSInRHSPart,
6538 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006539}
6540
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006541StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6542 Stmt *AStmt,
6543 SourceLocation StartLoc,
6544 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006545 if (!AStmt)
6546 return StmtError();
6547
Alexey Bataeve3727102018-04-18 15:57:46 +00006548 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006549 // 1.2.2 OpenMP Language Terminology
6550 // Structured block - An executable statement with a single entry at the
6551 // top and a single exit at the bottom.
6552 // The point of exit cannot be a branch out of the structured block.
6553 // longjmp() and throw() must not violate the entry/exit criteria.
6554 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006555 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6556 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6557 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6558 // 1.2.2 OpenMP Language Terminology
6559 // Structured block - An executable statement with a single entry at the
6560 // top and a single exit at the bottom.
6561 // The point of exit cannot be a branch out of the structured block.
6562 // longjmp() and throw() must not violate the entry/exit criteria.
6563 CS->getCapturedDecl()->setNothrow();
6564 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006565
Alexey Bataev13314bf2014-10-09 04:18:56 +00006566 // OpenMP [2.16, Nesting of Regions]
6567 // If specified, a teams construct must be contained within a target
6568 // construct. That target construct must contain no statements or directives
6569 // outside of the teams construct.
6570 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006571 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006572 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006573 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006574 auto I = CS->body_begin();
6575 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006576 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006577 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6578 OMPTeamsFound = false;
6579 break;
6580 }
6581 ++I;
6582 }
6583 assert(I != CS->body_end() && "Not found statement");
6584 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006585 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006586 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006587 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006588 }
6589 if (!OMPTeamsFound) {
6590 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6591 Diag(DSAStack->getInnerTeamsRegionLoc(),
6592 diag::note_omp_nested_teams_construct_here);
6593 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6594 << isa<OMPExecutableDirective>(S);
6595 return StmtError();
6596 }
6597 }
6598
Reid Kleckner87a31802018-03-12 21:43:02 +00006599 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006600
6601 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6602}
6603
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006604StmtResult
6605Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6606 Stmt *AStmt, SourceLocation StartLoc,
6607 SourceLocation EndLoc) {
6608 if (!AStmt)
6609 return StmtError();
6610
Alexey Bataeve3727102018-04-18 15:57:46 +00006611 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006612 // 1.2.2 OpenMP Language Terminology
6613 // Structured block - An executable statement with a single entry at the
6614 // top and a single exit at the bottom.
6615 // The point of exit cannot be a branch out of the structured block.
6616 // longjmp() and throw() must not violate the entry/exit criteria.
6617 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006618 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6619 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6620 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6621 // 1.2.2 OpenMP Language Terminology
6622 // Structured block - An executable statement with a single entry at the
6623 // top and a single exit at the bottom.
6624 // The point of exit cannot be a branch out of the structured block.
6625 // longjmp() and throw() must not violate the entry/exit criteria.
6626 CS->getCapturedDecl()->setNothrow();
6627 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006628
Reid Kleckner87a31802018-03-12 21:43:02 +00006629 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006630
6631 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6632 AStmt);
6633}
6634
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006635StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6636 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006637 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006638 if (!AStmt)
6639 return StmtError();
6640
Alexey Bataeve3727102018-04-18 15:57:46 +00006641 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006642 // 1.2.2 OpenMP Language Terminology
6643 // Structured block - An executable statement with a single entry at the
6644 // top and a single exit at the bottom.
6645 // The point of exit cannot be a branch out of the structured block.
6646 // longjmp() and throw() must not violate the entry/exit criteria.
6647 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006648 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6649 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6650 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6651 // 1.2.2 OpenMP Language Terminology
6652 // Structured block - An executable statement with a single entry at the
6653 // top and a single exit at the bottom.
6654 // The point of exit cannot be a branch out of the structured block.
6655 // longjmp() and throw() must not violate the entry/exit criteria.
6656 CS->getCapturedDecl()->setNothrow();
6657 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006658
6659 OMPLoopDirective::HelperExprs B;
6660 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6661 // define the nested loops number.
6662 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006663 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006664 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006665 VarsWithImplicitDSA, B);
6666 if (NestedLoopCount == 0)
6667 return StmtError();
6668
6669 assert((CurContext->isDependentContext() || B.builtAll()) &&
6670 "omp target parallel for loop exprs were not built");
6671
6672 if (!CurContext->isDependentContext()) {
6673 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006674 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006675 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006676 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006677 B.NumIterations, *this, CurScope,
6678 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006679 return StmtError();
6680 }
6681 }
6682
Reid Kleckner87a31802018-03-12 21:43:02 +00006683 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006684 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6685 NestedLoopCount, Clauses, AStmt,
6686 B, DSAStack->isCancelRegion());
6687}
6688
Alexey Bataev95b64a92017-05-30 16:00:04 +00006689/// Check for existence of a map clause in the list of clauses.
6690static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6691 const OpenMPClauseKind K) {
6692 return llvm::any_of(
6693 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6694}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006695
Alexey Bataev95b64a92017-05-30 16:00:04 +00006696template <typename... Params>
6697static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6698 const Params... ClauseTypes) {
6699 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006700}
6701
Michael Wong65f367f2015-07-21 13:44:28 +00006702StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6703 Stmt *AStmt,
6704 SourceLocation StartLoc,
6705 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006706 if (!AStmt)
6707 return StmtError();
6708
6709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6710
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006711 // OpenMP [2.10.1, Restrictions, p. 97]
6712 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006713 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6714 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6715 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006716 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006717 return StmtError();
6718 }
6719
Reid Kleckner87a31802018-03-12 21:43:02 +00006720 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00006721
6722 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6723 AStmt);
6724}
6725
Samuel Antaodf67fc42016-01-19 19:15:56 +00006726StmtResult
6727Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6728 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006729 SourceLocation EndLoc, Stmt *AStmt) {
6730 if (!AStmt)
6731 return StmtError();
6732
Alexey Bataeve3727102018-04-18 15:57:46 +00006733 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006734 // 1.2.2 OpenMP Language Terminology
6735 // Structured block - An executable statement with a single entry at the
6736 // top and a single exit at the bottom.
6737 // The point of exit cannot be a branch out of the structured block.
6738 // longjmp() and throw() must not violate the entry/exit criteria.
6739 CS->getCapturedDecl()->setNothrow();
6740 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6741 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6742 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6743 // 1.2.2 OpenMP Language Terminology
6744 // Structured block - An executable statement with a single entry at the
6745 // top and a single exit at the bottom.
6746 // The point of exit cannot be a branch out of the structured block.
6747 // longjmp() and throw() must not violate the entry/exit criteria.
6748 CS->getCapturedDecl()->setNothrow();
6749 }
6750
Samuel Antaodf67fc42016-01-19 19:15:56 +00006751 // OpenMP [2.10.2, Restrictions, p. 99]
6752 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006753 if (!hasClauses(Clauses, OMPC_map)) {
6754 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6755 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006756 return StmtError();
6757 }
6758
Alexey Bataev7828b252017-11-21 17:08:48 +00006759 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6760 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006761}
6762
Samuel Antao72590762016-01-19 20:04:50 +00006763StmtResult
6764Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6765 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006766 SourceLocation EndLoc, Stmt *AStmt) {
6767 if (!AStmt)
6768 return StmtError();
6769
Alexey Bataeve3727102018-04-18 15:57:46 +00006770 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006771 // 1.2.2 OpenMP Language Terminology
6772 // Structured block - An executable statement with a single entry at the
6773 // top and a single exit at the bottom.
6774 // The point of exit cannot be a branch out of the structured block.
6775 // longjmp() and throw() must not violate the entry/exit criteria.
6776 CS->getCapturedDecl()->setNothrow();
6777 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6778 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6779 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6780 // 1.2.2 OpenMP Language Terminology
6781 // Structured block - An executable statement with a single entry at the
6782 // top and a single exit at the bottom.
6783 // The point of exit cannot be a branch out of the structured block.
6784 // longjmp() and throw() must not violate the entry/exit criteria.
6785 CS->getCapturedDecl()->setNothrow();
6786 }
6787
Samuel Antao72590762016-01-19 20:04:50 +00006788 // OpenMP [2.10.3, Restrictions, p. 102]
6789 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006790 if (!hasClauses(Clauses, OMPC_map)) {
6791 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6792 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006793 return StmtError();
6794 }
6795
Alexey Bataev7828b252017-11-21 17:08:48 +00006796 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6797 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006798}
6799
Samuel Antao686c70c2016-05-26 17:30:50 +00006800StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6801 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006802 SourceLocation EndLoc,
6803 Stmt *AStmt) {
6804 if (!AStmt)
6805 return StmtError();
6806
Alexey Bataeve3727102018-04-18 15:57:46 +00006807 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006808 // 1.2.2 OpenMP Language Terminology
6809 // Structured block - An executable statement with a single entry at the
6810 // top and a single exit at the bottom.
6811 // The point of exit cannot be a branch out of the structured block.
6812 // longjmp() and throw() must not violate the entry/exit criteria.
6813 CS->getCapturedDecl()->setNothrow();
6814 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6815 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6816 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6817 // 1.2.2 OpenMP Language Terminology
6818 // Structured block - An executable statement with a single entry at the
6819 // top and a single exit at the bottom.
6820 // The point of exit cannot be a branch out of the structured block.
6821 // longjmp() and throw() must not violate the entry/exit criteria.
6822 CS->getCapturedDecl()->setNothrow();
6823 }
6824
Alexey Bataev95b64a92017-05-30 16:00:04 +00006825 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006826 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6827 return StmtError();
6828 }
Alexey Bataev7828b252017-11-21 17:08:48 +00006829 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6830 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00006831}
6832
Alexey Bataev13314bf2014-10-09 04:18:56 +00006833StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6834 Stmt *AStmt, SourceLocation StartLoc,
6835 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006836 if (!AStmt)
6837 return StmtError();
6838
Alexey Bataeve3727102018-04-18 15:57:46 +00006839 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006840 // 1.2.2 OpenMP Language Terminology
6841 // Structured block - An executable statement with a single entry at the
6842 // top and a single exit at the bottom.
6843 // The point of exit cannot be a branch out of the structured block.
6844 // longjmp() and throw() must not violate the entry/exit criteria.
6845 CS->getCapturedDecl()->setNothrow();
6846
Reid Kleckner87a31802018-03-12 21:43:02 +00006847 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00006848
Alexey Bataevceabd412017-11-30 18:01:54 +00006849 DSAStack->setParentTeamsRegionLoc(StartLoc);
6850
Alexey Bataev13314bf2014-10-09 04:18:56 +00006851 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6852}
6853
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006854StmtResult
6855Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6856 SourceLocation EndLoc,
6857 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006858 if (DSAStack->isParentNowaitRegion()) {
6859 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6860 return StmtError();
6861 }
6862 if (DSAStack->isParentOrderedRegion()) {
6863 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6864 return StmtError();
6865 }
6866 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6867 CancelRegion);
6868}
6869
Alexey Bataev87933c72015-09-18 08:07:34 +00006870StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6871 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006872 SourceLocation EndLoc,
6873 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00006874 if (DSAStack->isParentNowaitRegion()) {
6875 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6876 return StmtError();
6877 }
6878 if (DSAStack->isParentOrderedRegion()) {
6879 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6880 return StmtError();
6881 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006882 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006883 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6884 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006885}
6886
Alexey Bataev382967a2015-12-08 12:06:20 +00006887static bool checkGrainsizeNumTasksClauses(Sema &S,
6888 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006889 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00006890 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006891 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00006892 if (C->getClauseKind() == OMPC_grainsize ||
6893 C->getClauseKind() == OMPC_num_tasks) {
6894 if (!PrevClause)
6895 PrevClause = C;
6896 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6897 S.Diag(C->getLocStart(),
6898 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6899 << getOpenMPClauseName(C->getClauseKind())
6900 << getOpenMPClauseName(PrevClause->getClauseKind());
6901 S.Diag(PrevClause->getLocStart(),
6902 diag::note_omp_previous_grainsize_num_tasks)
6903 << getOpenMPClauseName(PrevClause->getClauseKind());
6904 ErrorFound = true;
6905 }
6906 }
6907 }
6908 return ErrorFound;
6909}
6910
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006911static bool checkReductionClauseWithNogroup(Sema &S,
6912 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006913 const OMPClause *ReductionClause = nullptr;
6914 const OMPClause *NogroupClause = nullptr;
6915 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006916 if (C->getClauseKind() == OMPC_reduction) {
6917 ReductionClause = C;
6918 if (NogroupClause)
6919 break;
6920 continue;
6921 }
6922 if (C->getClauseKind() == OMPC_nogroup) {
6923 NogroupClause = C;
6924 if (ReductionClause)
6925 break;
6926 continue;
6927 }
6928 }
6929 if (ReductionClause && NogroupClause) {
6930 S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6931 << SourceRange(NogroupClause->getLocStart(),
6932 NogroupClause->getLocEnd());
6933 return true;
6934 }
6935 return false;
6936}
6937
Alexey Bataev49f6e782015-12-01 04:18:41 +00006938StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6939 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006940 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006941 if (!AStmt)
6942 return StmtError();
6943
6944 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6945 OMPLoopDirective::HelperExprs B;
6946 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6947 // define the nested loops number.
6948 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006949 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006950 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006951 VarsWithImplicitDSA, B);
6952 if (NestedLoopCount == 0)
6953 return StmtError();
6954
6955 assert((CurContext->isDependentContext() || B.builtAll()) &&
6956 "omp for loop exprs were not built");
6957
Alexey Bataev382967a2015-12-08 12:06:20 +00006958 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6959 // The grainsize clause and num_tasks clause are mutually exclusive and may
6960 // not appear on the same taskloop directive.
6961 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6962 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00006963 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6964 // If a reduction clause is present on the taskloop directive, the nogroup
6965 // clause must not be specified.
6966 if (checkReductionClauseWithNogroup(*this, Clauses))
6967 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00006968
Reid Kleckner87a31802018-03-12 21:43:02 +00006969 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00006970 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6971 NestedLoopCount, Clauses, AStmt, B);
6972}
6973
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006974StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6975 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006976 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006977 if (!AStmt)
6978 return StmtError();
6979
6980 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6981 OMPLoopDirective::HelperExprs B;
6982 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6983 // define the nested loops number.
6984 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006985 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006986 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6987 VarsWithImplicitDSA, B);
6988 if (NestedLoopCount == 0)
6989 return StmtError();
6990
6991 assert((CurContext->isDependentContext() || B.builtAll()) &&
6992 "omp for loop exprs were not built");
6993
Alexey Bataev5a3af132016-03-29 08:58:54 +00006994 if (!CurContext->isDependentContext()) {
6995 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006996 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006997 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006998 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006999 B.NumIterations, *this, CurScope,
7000 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007001 return StmtError();
7002 }
7003 }
7004
Alexey Bataev382967a2015-12-08 12:06:20 +00007005 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7006 // The grainsize clause and num_tasks clause are mutually exclusive and may
7007 // not appear on the same taskloop directive.
7008 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7009 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007010 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7011 // If a reduction clause is present on the taskloop directive, the nogroup
7012 // clause must not be specified.
7013 if (checkReductionClauseWithNogroup(*this, Clauses))
7014 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007015 if (checkSimdlenSafelenSpecified(*this, Clauses))
7016 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007017
Reid Kleckner87a31802018-03-12 21:43:02 +00007018 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007019 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7020 NestedLoopCount, Clauses, AStmt, B);
7021}
7022
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007023StmtResult Sema::ActOnOpenMPDistributeDirective(
7024 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007025 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007026 if (!AStmt)
7027 return StmtError();
7028
7029 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7030 OMPLoopDirective::HelperExprs B;
7031 // In presence of clause 'collapse' with number of loops, it will
7032 // define the nested loops number.
7033 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007034 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007035 nullptr /*ordered not a clause on distribute*/, AStmt,
7036 *this, *DSAStack, VarsWithImplicitDSA, B);
7037 if (NestedLoopCount == 0)
7038 return StmtError();
7039
7040 assert((CurContext->isDependentContext() || B.builtAll()) &&
7041 "omp for loop exprs were not built");
7042
Reid Kleckner87a31802018-03-12 21:43:02 +00007043 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007044 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7045 NestedLoopCount, Clauses, AStmt, B);
7046}
7047
Carlo Bertolli9925f152016-06-27 14:55:37 +00007048StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7049 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007050 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007051 if (!AStmt)
7052 return StmtError();
7053
Alexey Bataeve3727102018-04-18 15:57:46 +00007054 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007055 // 1.2.2 OpenMP Language Terminology
7056 // Structured block - An executable statement with a single entry at the
7057 // top and a single exit at the bottom.
7058 // The point of exit cannot be a branch out of the structured block.
7059 // longjmp() and throw() must not violate the entry/exit criteria.
7060 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007061 for (int ThisCaptureLevel =
7062 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7063 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7064 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7065 // 1.2.2 OpenMP Language Terminology
7066 // Structured block - An executable statement with a single entry at the
7067 // top and a single exit at the bottom.
7068 // The point of exit cannot be a branch out of the structured block.
7069 // longjmp() and throw() must not violate the entry/exit criteria.
7070 CS->getCapturedDecl()->setNothrow();
7071 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007072
7073 OMPLoopDirective::HelperExprs B;
7074 // In presence of clause 'collapse' with number of loops, it will
7075 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007076 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007077 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007078 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007079 VarsWithImplicitDSA, B);
7080 if (NestedLoopCount == 0)
7081 return StmtError();
7082
7083 assert((CurContext->isDependentContext() || B.builtAll()) &&
7084 "omp for loop exprs were not built");
7085
Reid Kleckner87a31802018-03-12 21:43:02 +00007086 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007087 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7089 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007090}
7091
Kelvin Li4a39add2016-07-05 05:00:15 +00007092StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7093 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007094 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007095 if (!AStmt)
7096 return StmtError();
7097
Alexey Bataeve3727102018-04-18 15:57:46 +00007098 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007099 // 1.2.2 OpenMP Language Terminology
7100 // Structured block - An executable statement with a single entry at the
7101 // top and a single exit at the bottom.
7102 // The point of exit cannot be a branch out of the structured block.
7103 // longjmp() and throw() must not violate the entry/exit criteria.
7104 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007105 for (int ThisCaptureLevel =
7106 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7107 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7108 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7109 // 1.2.2 OpenMP Language Terminology
7110 // Structured block - An executable statement with a single entry at the
7111 // top and a single exit at the bottom.
7112 // The point of exit cannot be a branch out of the structured block.
7113 // longjmp() and throw() must not violate the entry/exit criteria.
7114 CS->getCapturedDecl()->setNothrow();
7115 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007116
7117 OMPLoopDirective::HelperExprs B;
7118 // In presence of clause 'collapse' with number of loops, it will
7119 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007120 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007121 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007122 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007123 VarsWithImplicitDSA, B);
7124 if (NestedLoopCount == 0)
7125 return StmtError();
7126
7127 assert((CurContext->isDependentContext() || B.builtAll()) &&
7128 "omp for loop exprs were not built");
7129
Alexey Bataev438388c2017-11-22 18:34:02 +00007130 if (!CurContext->isDependentContext()) {
7131 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007132 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007133 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7134 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7135 B.NumIterations, *this, CurScope,
7136 DSAStack))
7137 return StmtError();
7138 }
7139 }
7140
Kelvin Lic5609492016-07-15 04:39:07 +00007141 if (checkSimdlenSafelenSpecified(*this, Clauses))
7142 return StmtError();
7143
Reid Kleckner87a31802018-03-12 21:43:02 +00007144 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007145 return OMPDistributeParallelForSimdDirective::Create(
7146 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7147}
7148
Kelvin Li787f3fc2016-07-06 04:45:38 +00007149StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7150 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007151 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007152 if (!AStmt)
7153 return StmtError();
7154
Alexey Bataeve3727102018-04-18 15:57:46 +00007155 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007156 // 1.2.2 OpenMP Language Terminology
7157 // Structured block - An executable statement with a single entry at the
7158 // top and a single exit at the bottom.
7159 // The point of exit cannot be a branch out of the structured block.
7160 // longjmp() and throw() must not violate the entry/exit criteria.
7161 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007162 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7163 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7164 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7165 // 1.2.2 OpenMP Language Terminology
7166 // Structured block - An executable statement with a single entry at the
7167 // top and a single exit at the bottom.
7168 // The point of exit cannot be a branch out of the structured block.
7169 // longjmp() and throw() must not violate the entry/exit criteria.
7170 CS->getCapturedDecl()->setNothrow();
7171 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007172
7173 OMPLoopDirective::HelperExprs B;
7174 // In presence of clause 'collapse' with number of loops, it will
7175 // define the nested loops number.
7176 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007177 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007178 nullptr /*ordered not a clause on distribute*/, CS, *this,
7179 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007180 if (NestedLoopCount == 0)
7181 return StmtError();
7182
7183 assert((CurContext->isDependentContext() || B.builtAll()) &&
7184 "omp for loop exprs were not built");
7185
Alexey Bataev438388c2017-11-22 18:34:02 +00007186 if (!CurContext->isDependentContext()) {
7187 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007188 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007189 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7190 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7191 B.NumIterations, *this, CurScope,
7192 DSAStack))
7193 return StmtError();
7194 }
7195 }
7196
Kelvin Lic5609492016-07-15 04:39:07 +00007197 if (checkSimdlenSafelenSpecified(*this, Clauses))
7198 return StmtError();
7199
Reid Kleckner87a31802018-03-12 21:43:02 +00007200 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007201 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7202 NestedLoopCount, Clauses, AStmt, B);
7203}
7204
Kelvin Lia579b912016-07-14 02:54:56 +00007205StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7206 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007207 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007208 if (!AStmt)
7209 return StmtError();
7210
Alexey Bataeve3727102018-04-18 15:57:46 +00007211 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007212 // 1.2.2 OpenMP Language Terminology
7213 // Structured block - An executable statement with a single entry at the
7214 // top and a single exit at the bottom.
7215 // The point of exit cannot be a branch out of the structured block.
7216 // longjmp() and throw() must not violate the entry/exit criteria.
7217 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007218 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7219 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7220 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7221 // 1.2.2 OpenMP Language Terminology
7222 // Structured block - An executable statement with a single entry at the
7223 // top and a single exit at the bottom.
7224 // The point of exit cannot be a branch out of the structured block.
7225 // longjmp() and throw() must not violate the entry/exit criteria.
7226 CS->getCapturedDecl()->setNothrow();
7227 }
Kelvin Lia579b912016-07-14 02:54:56 +00007228
7229 OMPLoopDirective::HelperExprs B;
7230 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7231 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007232 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007233 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007234 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007235 VarsWithImplicitDSA, B);
7236 if (NestedLoopCount == 0)
7237 return StmtError();
7238
7239 assert((CurContext->isDependentContext() || B.builtAll()) &&
7240 "omp target parallel for simd loop exprs were not built");
7241
7242 if (!CurContext->isDependentContext()) {
7243 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007244 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007245 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007246 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7247 B.NumIterations, *this, CurScope,
7248 DSAStack))
7249 return StmtError();
7250 }
7251 }
Kelvin Lic5609492016-07-15 04:39:07 +00007252 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007253 return StmtError();
7254
Reid Kleckner87a31802018-03-12 21:43:02 +00007255 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007256 return OMPTargetParallelForSimdDirective::Create(
7257 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7258}
7259
Kelvin Li986330c2016-07-20 22:57:10 +00007260StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7261 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007262 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007263 if (!AStmt)
7264 return StmtError();
7265
Alexey Bataeve3727102018-04-18 15:57:46 +00007266 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007267 // 1.2.2 OpenMP Language Terminology
7268 // Structured block - An executable statement with a single entry at the
7269 // top and a single exit at the bottom.
7270 // The point of exit cannot be a branch out of the structured block.
7271 // longjmp() and throw() must not violate the entry/exit criteria.
7272 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007273 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7274 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7275 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7276 // 1.2.2 OpenMP Language Terminology
7277 // Structured block - An executable statement with a single entry at the
7278 // top and a single exit at the bottom.
7279 // The point of exit cannot be a branch out of the structured block.
7280 // longjmp() and throw() must not violate the entry/exit criteria.
7281 CS->getCapturedDecl()->setNothrow();
7282 }
7283
Kelvin Li986330c2016-07-20 22:57:10 +00007284 OMPLoopDirective::HelperExprs B;
7285 // In presence of clause 'collapse' with number of loops, it will define the
7286 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007287 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007288 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007289 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007290 VarsWithImplicitDSA, B);
7291 if (NestedLoopCount == 0)
7292 return StmtError();
7293
7294 assert((CurContext->isDependentContext() || B.builtAll()) &&
7295 "omp target simd loop exprs were not built");
7296
7297 if (!CurContext->isDependentContext()) {
7298 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007299 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007300 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007301 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7302 B.NumIterations, *this, CurScope,
7303 DSAStack))
7304 return StmtError();
7305 }
7306 }
7307
7308 if (checkSimdlenSafelenSpecified(*this, Clauses))
7309 return StmtError();
7310
Reid Kleckner87a31802018-03-12 21:43:02 +00007311 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007312 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7313 NestedLoopCount, Clauses, AStmt, B);
7314}
7315
Kelvin Li02532872016-08-05 14:37:37 +00007316StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7317 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007318 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007319 if (!AStmt)
7320 return StmtError();
7321
Alexey Bataeve3727102018-04-18 15:57:46 +00007322 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007323 // 1.2.2 OpenMP Language Terminology
7324 // Structured block - An executable statement with a single entry at the
7325 // top and a single exit at the bottom.
7326 // The point of exit cannot be a branch out of the structured block.
7327 // longjmp() and throw() must not violate the entry/exit criteria.
7328 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007329 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7330 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7331 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7332 // 1.2.2 OpenMP Language Terminology
7333 // Structured block - An executable statement with a single entry at the
7334 // top and a single exit at the bottom.
7335 // The point of exit cannot be a branch out of the structured block.
7336 // longjmp() and throw() must not violate the entry/exit criteria.
7337 CS->getCapturedDecl()->setNothrow();
7338 }
Kelvin Li02532872016-08-05 14:37:37 +00007339
7340 OMPLoopDirective::HelperExprs B;
7341 // In presence of clause 'collapse' with number of loops, it will
7342 // define the nested loops number.
7343 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007344 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007345 nullptr /*ordered not a clause on distribute*/, CS, *this,
7346 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007347 if (NestedLoopCount == 0)
7348 return StmtError();
7349
7350 assert((CurContext->isDependentContext() || B.builtAll()) &&
7351 "omp teams distribute loop exprs were not built");
7352
Reid Kleckner87a31802018-03-12 21:43:02 +00007353 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007354
7355 DSAStack->setParentTeamsRegionLoc(StartLoc);
7356
David Majnemer9d168222016-08-05 17:44:54 +00007357 return OMPTeamsDistributeDirective::Create(
7358 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007359}
7360
Kelvin Li4e325f72016-10-25 12:50:55 +00007361StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7362 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007363 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007364 if (!AStmt)
7365 return StmtError();
7366
Alexey Bataeve3727102018-04-18 15:57:46 +00007367 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007368 // 1.2.2 OpenMP Language Terminology
7369 // Structured block - An executable statement with a single entry at the
7370 // top and a single exit at the bottom.
7371 // The point of exit cannot be a branch out of the structured block.
7372 // longjmp() and throw() must not violate the entry/exit criteria.
7373 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007374 for (int ThisCaptureLevel =
7375 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7376 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7377 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7378 // 1.2.2 OpenMP Language Terminology
7379 // Structured block - An executable statement with a single entry at the
7380 // top and a single exit at the bottom.
7381 // The point of exit cannot be a branch out of the structured block.
7382 // longjmp() and throw() must not violate the entry/exit criteria.
7383 CS->getCapturedDecl()->setNothrow();
7384 }
7385
Kelvin Li4e325f72016-10-25 12:50:55 +00007386
7387 OMPLoopDirective::HelperExprs B;
7388 // In presence of clause 'collapse' with number of loops, it will
7389 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007390 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007391 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007392 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007393 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007394
7395 if (NestedLoopCount == 0)
7396 return StmtError();
7397
7398 assert((CurContext->isDependentContext() || B.builtAll()) &&
7399 "omp teams distribute simd loop exprs were not built");
7400
7401 if (!CurContext->isDependentContext()) {
7402 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007403 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007404 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7405 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7406 B.NumIterations, *this, CurScope,
7407 DSAStack))
7408 return StmtError();
7409 }
7410 }
7411
7412 if (checkSimdlenSafelenSpecified(*this, Clauses))
7413 return StmtError();
7414
Reid Kleckner87a31802018-03-12 21:43:02 +00007415 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007416
7417 DSAStack->setParentTeamsRegionLoc(StartLoc);
7418
Kelvin Li4e325f72016-10-25 12:50:55 +00007419 return OMPTeamsDistributeSimdDirective::Create(
7420 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7421}
7422
Kelvin Li579e41c2016-11-30 23:51:03 +00007423StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7424 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007425 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007426 if (!AStmt)
7427 return StmtError();
7428
Alexey Bataeve3727102018-04-18 15:57:46 +00007429 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007430 // 1.2.2 OpenMP Language Terminology
7431 // Structured block - An executable statement with a single entry at the
7432 // top and a single exit at the bottom.
7433 // The point of exit cannot be a branch out of the structured block.
7434 // longjmp() and throw() must not violate the entry/exit criteria.
7435 CS->getCapturedDecl()->setNothrow();
7436
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007437 for (int ThisCaptureLevel =
7438 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7439 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7440 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7441 // 1.2.2 OpenMP Language Terminology
7442 // Structured block - An executable statement with a single entry at the
7443 // top and a single exit at the bottom.
7444 // The point of exit cannot be a branch out of the structured block.
7445 // longjmp() and throw() must not violate the entry/exit criteria.
7446 CS->getCapturedDecl()->setNothrow();
7447 }
7448
Kelvin Li579e41c2016-11-30 23:51:03 +00007449 OMPLoopDirective::HelperExprs B;
7450 // In presence of clause 'collapse' with number of loops, it will
7451 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007452 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007453 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007454 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007455 VarsWithImplicitDSA, B);
7456
7457 if (NestedLoopCount == 0)
7458 return StmtError();
7459
7460 assert((CurContext->isDependentContext() || B.builtAll()) &&
7461 "omp for loop exprs were not built");
7462
7463 if (!CurContext->isDependentContext()) {
7464 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007465 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007466 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7467 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7468 B.NumIterations, *this, CurScope,
7469 DSAStack))
7470 return StmtError();
7471 }
7472 }
7473
7474 if (checkSimdlenSafelenSpecified(*this, Clauses))
7475 return StmtError();
7476
Reid Kleckner87a31802018-03-12 21:43:02 +00007477 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007478
7479 DSAStack->setParentTeamsRegionLoc(StartLoc);
7480
Kelvin Li579e41c2016-11-30 23:51:03 +00007481 return OMPTeamsDistributeParallelForSimdDirective::Create(
7482 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7483}
7484
Kelvin Li7ade93f2016-12-09 03:24:30 +00007485StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7486 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007487 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007488 if (!AStmt)
7489 return StmtError();
7490
Alexey Bataeve3727102018-04-18 15:57:46 +00007491 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007492 // 1.2.2 OpenMP Language Terminology
7493 // Structured block - An executable statement with a single entry at the
7494 // top and a single exit at the bottom.
7495 // The point of exit cannot be a branch out of the structured block.
7496 // longjmp() and throw() must not violate the entry/exit criteria.
7497 CS->getCapturedDecl()->setNothrow();
7498
Carlo Bertolli62fae152017-11-20 20:46:39 +00007499 for (int ThisCaptureLevel =
7500 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7501 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7502 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7503 // 1.2.2 OpenMP Language Terminology
7504 // Structured block - An executable statement with a single entry at the
7505 // top and a single exit at the bottom.
7506 // The point of exit cannot be a branch out of the structured block.
7507 // longjmp() and throw() must not violate the entry/exit criteria.
7508 CS->getCapturedDecl()->setNothrow();
7509 }
7510
Kelvin Li7ade93f2016-12-09 03:24:30 +00007511 OMPLoopDirective::HelperExprs B;
7512 // In presence of clause 'collapse' with number of loops, it will
7513 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007514 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007515 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007516 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007517 VarsWithImplicitDSA, B);
7518
7519 if (NestedLoopCount == 0)
7520 return StmtError();
7521
7522 assert((CurContext->isDependentContext() || B.builtAll()) &&
7523 "omp for loop exprs were not built");
7524
Reid Kleckner87a31802018-03-12 21:43:02 +00007525 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007526
7527 DSAStack->setParentTeamsRegionLoc(StartLoc);
7528
Kelvin Li7ade93f2016-12-09 03:24:30 +00007529 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007530 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7531 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007532}
7533
Kelvin Libf594a52016-12-17 05:48:59 +00007534StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7535 Stmt *AStmt,
7536 SourceLocation StartLoc,
7537 SourceLocation EndLoc) {
7538 if (!AStmt)
7539 return StmtError();
7540
Alexey Bataeve3727102018-04-18 15:57:46 +00007541 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007542 // 1.2.2 OpenMP Language Terminology
7543 // Structured block - An executable statement with a single entry at the
7544 // top and a single exit at the bottom.
7545 // The point of exit cannot be a branch out of the structured block.
7546 // longjmp() and throw() must not violate the entry/exit criteria.
7547 CS->getCapturedDecl()->setNothrow();
7548
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007549 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7550 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7551 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7552 // 1.2.2 OpenMP Language Terminology
7553 // Structured block - An executable statement with a single entry at the
7554 // top and a single exit at the bottom.
7555 // The point of exit cannot be a branch out of the structured block.
7556 // longjmp() and throw() must not violate the entry/exit criteria.
7557 CS->getCapturedDecl()->setNothrow();
7558 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007559 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007560
7561 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7562 AStmt);
7563}
7564
Kelvin Li83c451e2016-12-25 04:52:54 +00007565StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7566 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007567 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007568 if (!AStmt)
7569 return StmtError();
7570
Alexey Bataeve3727102018-04-18 15:57:46 +00007571 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007572 // 1.2.2 OpenMP Language Terminology
7573 // Structured block - An executable statement with a single entry at the
7574 // top and a single exit at the bottom.
7575 // The point of exit cannot be a branch out of the structured block.
7576 // longjmp() and throw() must not violate the entry/exit criteria.
7577 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007578 for (int ThisCaptureLevel =
7579 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7580 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7581 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7582 // 1.2.2 OpenMP Language Terminology
7583 // Structured block - An executable statement with a single entry at the
7584 // top and a single exit at the bottom.
7585 // The point of exit cannot be a branch out of the structured block.
7586 // longjmp() and throw() must not violate the entry/exit criteria.
7587 CS->getCapturedDecl()->setNothrow();
7588 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007589
7590 OMPLoopDirective::HelperExprs B;
7591 // In presence of clause 'collapse' with number of loops, it will
7592 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007593 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007594 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7595 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007596 VarsWithImplicitDSA, B);
7597 if (NestedLoopCount == 0)
7598 return StmtError();
7599
7600 assert((CurContext->isDependentContext() || B.builtAll()) &&
7601 "omp target teams distribute loop exprs were not built");
7602
Reid Kleckner87a31802018-03-12 21:43:02 +00007603 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007604 return OMPTargetTeamsDistributeDirective::Create(
7605 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7606}
7607
Kelvin Li80e8f562016-12-29 22:16:30 +00007608StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7609 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007610 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007611 if (!AStmt)
7612 return StmtError();
7613
Alexey Bataeve3727102018-04-18 15:57:46 +00007614 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007615 // 1.2.2 OpenMP Language Terminology
7616 // Structured block - An executable statement with a single entry at the
7617 // top and a single exit at the bottom.
7618 // The point of exit cannot be a branch out of the structured block.
7619 // longjmp() and throw() must not violate the entry/exit criteria.
7620 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007621 for (int ThisCaptureLevel =
7622 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7623 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7624 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7625 // 1.2.2 OpenMP Language Terminology
7626 // Structured block - An executable statement with a single entry at the
7627 // top and a single exit at the bottom.
7628 // The point of exit cannot be a branch out of the structured block.
7629 // longjmp() and throw() must not violate the entry/exit criteria.
7630 CS->getCapturedDecl()->setNothrow();
7631 }
7632
Kelvin Li80e8f562016-12-29 22:16:30 +00007633 OMPLoopDirective::HelperExprs B;
7634 // In presence of clause 'collapse' with number of loops, it will
7635 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007636 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007637 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7638 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007639 VarsWithImplicitDSA, B);
7640 if (NestedLoopCount == 0)
7641 return StmtError();
7642
7643 assert((CurContext->isDependentContext() || B.builtAll()) &&
7644 "omp target teams distribute parallel for loop exprs were not built");
7645
Alexey Bataev647dd842018-01-15 20:59:40 +00007646 if (!CurContext->isDependentContext()) {
7647 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007648 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007649 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7650 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7651 B.NumIterations, *this, CurScope,
7652 DSAStack))
7653 return StmtError();
7654 }
7655 }
7656
Reid Kleckner87a31802018-03-12 21:43:02 +00007657 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007658 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007659 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7660 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007661}
7662
Kelvin Li1851df52017-01-03 05:23:48 +00007663StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7664 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007665 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007666 if (!AStmt)
7667 return StmtError();
7668
Alexey Bataeve3727102018-04-18 15:57:46 +00007669 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007670 // 1.2.2 OpenMP Language Terminology
7671 // Structured block - An executable statement with a single entry at the
7672 // top and a single exit at the bottom.
7673 // The point of exit cannot be a branch out of the structured block.
7674 // longjmp() and throw() must not violate the entry/exit criteria.
7675 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007676 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7677 OMPD_target_teams_distribute_parallel_for_simd);
7678 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7679 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7680 // 1.2.2 OpenMP Language Terminology
7681 // Structured block - An executable statement with a single entry at the
7682 // top and a single exit at the bottom.
7683 // The point of exit cannot be a branch out of the structured block.
7684 // longjmp() and throw() must not violate the entry/exit criteria.
7685 CS->getCapturedDecl()->setNothrow();
7686 }
Kelvin Li1851df52017-01-03 05:23:48 +00007687
7688 OMPLoopDirective::HelperExprs B;
7689 // In presence of clause 'collapse' with number of loops, it will
7690 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007691 unsigned NestedLoopCount =
7692 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00007693 getCollapseNumberExpr(Clauses),
7694 nullptr /*ordered not a clause on distribute*/, CS, *this,
7695 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00007696 if (NestedLoopCount == 0)
7697 return StmtError();
7698
7699 assert((CurContext->isDependentContext() || B.builtAll()) &&
7700 "omp target teams distribute parallel for simd loop exprs were not "
7701 "built");
7702
7703 if (!CurContext->isDependentContext()) {
7704 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007705 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00007706 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7707 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7708 B.NumIterations, *this, CurScope,
7709 DSAStack))
7710 return StmtError();
7711 }
7712 }
7713
Alexey Bataev438388c2017-11-22 18:34:02 +00007714 if (checkSimdlenSafelenSpecified(*this, Clauses))
7715 return StmtError();
7716
Reid Kleckner87a31802018-03-12 21:43:02 +00007717 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00007718 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7719 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7720}
7721
Kelvin Lida681182017-01-10 18:08:18 +00007722StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007724 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00007725 if (!AStmt)
7726 return StmtError();
7727
7728 auto *CS = cast<CapturedStmt>(AStmt);
7729 // 1.2.2 OpenMP Language Terminology
7730 // Structured block - An executable statement with a single entry at the
7731 // top and a single exit at the bottom.
7732 // The point of exit cannot be a branch out of the structured block.
7733 // longjmp() and throw() must not violate the entry/exit criteria.
7734 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007735 for (int ThisCaptureLevel =
7736 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7737 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7738 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7739 // 1.2.2 OpenMP Language Terminology
7740 // Structured block - An executable statement with a single entry at the
7741 // top and a single exit at the bottom.
7742 // The point of exit cannot be a branch out of the structured block.
7743 // longjmp() and throw() must not violate the entry/exit criteria.
7744 CS->getCapturedDecl()->setNothrow();
7745 }
Kelvin Lida681182017-01-10 18:08:18 +00007746
7747 OMPLoopDirective::HelperExprs B;
7748 // In presence of clause 'collapse' with number of loops, it will
7749 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007750 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00007751 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007752 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007753 VarsWithImplicitDSA, B);
7754 if (NestedLoopCount == 0)
7755 return StmtError();
7756
7757 assert((CurContext->isDependentContext() || B.builtAll()) &&
7758 "omp target teams distribute simd loop exprs were not built");
7759
Alexey Bataev438388c2017-11-22 18:34:02 +00007760 if (!CurContext->isDependentContext()) {
7761 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007762 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007763 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7764 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7765 B.NumIterations, *this, CurScope,
7766 DSAStack))
7767 return StmtError();
7768 }
7769 }
7770
7771 if (checkSimdlenSafelenSpecified(*this, Clauses))
7772 return StmtError();
7773
Reid Kleckner87a31802018-03-12 21:43:02 +00007774 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00007775 return OMPTargetTeamsDistributeSimdDirective::Create(
7776 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7777}
7778
Alexey Bataeved09d242014-05-28 05:53:51 +00007779OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007780 SourceLocation StartLoc,
7781 SourceLocation LParenLoc,
7782 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007783 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007784 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007785 case OMPC_final:
7786 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7787 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007788 case OMPC_num_threads:
7789 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7790 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007791 case OMPC_safelen:
7792 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7793 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007794 case OMPC_simdlen:
7795 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7796 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007797 case OMPC_collapse:
7798 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7799 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007800 case OMPC_ordered:
7801 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7802 break;
Michael Wonge710d542015-08-07 16:16:36 +00007803 case OMPC_device:
7804 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7805 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007806 case OMPC_num_teams:
7807 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7808 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007809 case OMPC_thread_limit:
7810 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7811 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007812 case OMPC_priority:
7813 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7814 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007815 case OMPC_grainsize:
7816 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7817 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007818 case OMPC_num_tasks:
7819 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7820 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007821 case OMPC_hint:
7822 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7823 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007824 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007825 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007826 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007827 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007828 case OMPC_private:
7829 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007830 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007831 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007832 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00007833 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00007834 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007835 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007836 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007837 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007838 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007839 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007840 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007841 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007842 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007843 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007844 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007845 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007846 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007847 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007848 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007849 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007850 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007851 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007852 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007853 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007854 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007855 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007856 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007857 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007858 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007859 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007860 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007861 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007862 llvm_unreachable("Clause is not allowed.");
7863 }
7864 return Res;
7865}
7866
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007867// An OpenMP directive such as 'target parallel' has two captured regions:
7868// for the 'target' and 'parallel' respectively. This function returns
7869// the region in which to capture expressions associated with a clause.
7870// A return value of OMPD_unknown signifies that the expression should not
7871// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007872static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7873 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7874 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007875 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007876 switch (CKind) {
7877 case OMPC_if:
7878 switch (DKind) {
7879 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007880 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007881 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007882 // If this clause applies to the nested 'parallel' region, capture within
7883 // the 'target' region, otherwise do not capture.
7884 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7885 CaptureRegion = OMPD_target;
7886 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00007887 case OMPD_target_teams_distribute_parallel_for:
7888 case OMPD_target_teams_distribute_parallel_for_simd:
7889 // If this clause applies to the nested 'parallel' region, capture within
7890 // the 'teams' region, otherwise do not capture.
7891 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7892 CaptureRegion = OMPD_teams;
7893 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007894 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007895 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007896 CaptureRegion = OMPD_teams;
7897 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007898 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00007899 case OMPD_target_enter_data:
7900 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007901 CaptureRegion = OMPD_task;
7902 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007903 case OMPD_cancel:
7904 case OMPD_parallel:
7905 case OMPD_parallel_sections:
7906 case OMPD_parallel_for:
7907 case OMPD_parallel_for_simd:
7908 case OMPD_target:
7909 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007910 case OMPD_target_teams:
7911 case OMPD_target_teams_distribute:
7912 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007913 case OMPD_distribute_parallel_for:
7914 case OMPD_distribute_parallel_for_simd:
7915 case OMPD_task:
7916 case OMPD_taskloop:
7917 case OMPD_taskloop_simd:
7918 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007919 // Do not capture if-clause expressions.
7920 break;
7921 case OMPD_threadprivate:
7922 case OMPD_taskyield:
7923 case OMPD_barrier:
7924 case OMPD_taskwait:
7925 case OMPD_cancellation_point:
7926 case OMPD_flush:
7927 case OMPD_declare_reduction:
7928 case OMPD_declare_simd:
7929 case OMPD_declare_target:
7930 case OMPD_end_declare_target:
7931 case OMPD_teams:
7932 case OMPD_simd:
7933 case OMPD_for:
7934 case OMPD_for_simd:
7935 case OMPD_sections:
7936 case OMPD_section:
7937 case OMPD_single:
7938 case OMPD_master:
7939 case OMPD_critical:
7940 case OMPD_taskgroup:
7941 case OMPD_distribute:
7942 case OMPD_ordered:
7943 case OMPD_atomic:
7944 case OMPD_distribute_simd:
7945 case OMPD_teams_distribute:
7946 case OMPD_teams_distribute_simd:
7947 llvm_unreachable("Unexpected OpenMP directive with if-clause");
7948 case OMPD_unknown:
7949 llvm_unreachable("Unknown OpenMP directive");
7950 }
7951 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007952 case OMPC_num_threads:
7953 switch (DKind) {
7954 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007955 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007956 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007957 CaptureRegion = OMPD_target;
7958 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007959 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007960 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00007961 case OMPD_target_teams_distribute_parallel_for:
7962 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007963 CaptureRegion = OMPD_teams;
7964 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007965 case OMPD_parallel:
7966 case OMPD_parallel_sections:
7967 case OMPD_parallel_for:
7968 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007969 case OMPD_distribute_parallel_for:
7970 case OMPD_distribute_parallel_for_simd:
7971 // Do not capture num_threads-clause expressions.
7972 break;
7973 case OMPD_target_data:
7974 case OMPD_target_enter_data:
7975 case OMPD_target_exit_data:
7976 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007977 case OMPD_target:
7978 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007979 case OMPD_target_teams:
7980 case OMPD_target_teams_distribute:
7981 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007982 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007983 case OMPD_task:
7984 case OMPD_taskloop:
7985 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007986 case OMPD_threadprivate:
7987 case OMPD_taskyield:
7988 case OMPD_barrier:
7989 case OMPD_taskwait:
7990 case OMPD_cancellation_point:
7991 case OMPD_flush:
7992 case OMPD_declare_reduction:
7993 case OMPD_declare_simd:
7994 case OMPD_declare_target:
7995 case OMPD_end_declare_target:
7996 case OMPD_teams:
7997 case OMPD_simd:
7998 case OMPD_for:
7999 case OMPD_for_simd:
8000 case OMPD_sections:
8001 case OMPD_section:
8002 case OMPD_single:
8003 case OMPD_master:
8004 case OMPD_critical:
8005 case OMPD_taskgroup:
8006 case OMPD_distribute:
8007 case OMPD_ordered:
8008 case OMPD_atomic:
8009 case OMPD_distribute_simd:
8010 case OMPD_teams_distribute:
8011 case OMPD_teams_distribute_simd:
8012 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8013 case OMPD_unknown:
8014 llvm_unreachable("Unknown OpenMP directive");
8015 }
8016 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008017 case OMPC_num_teams:
8018 switch (DKind) {
8019 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008020 case OMPD_target_teams_distribute:
8021 case OMPD_target_teams_distribute_simd:
8022 case OMPD_target_teams_distribute_parallel_for:
8023 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008024 CaptureRegion = OMPD_target;
8025 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008026 case OMPD_teams_distribute_parallel_for:
8027 case OMPD_teams_distribute_parallel_for_simd:
8028 case OMPD_teams:
8029 case OMPD_teams_distribute:
8030 case OMPD_teams_distribute_simd:
8031 // Do not capture num_teams-clause expressions.
8032 break;
8033 case OMPD_distribute_parallel_for:
8034 case OMPD_distribute_parallel_for_simd:
8035 case OMPD_task:
8036 case OMPD_taskloop:
8037 case OMPD_taskloop_simd:
8038 case OMPD_target_data:
8039 case OMPD_target_enter_data:
8040 case OMPD_target_exit_data:
8041 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008042 case OMPD_cancel:
8043 case OMPD_parallel:
8044 case OMPD_parallel_sections:
8045 case OMPD_parallel_for:
8046 case OMPD_parallel_for_simd:
8047 case OMPD_target:
8048 case OMPD_target_simd:
8049 case OMPD_target_parallel:
8050 case OMPD_target_parallel_for:
8051 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008052 case OMPD_threadprivate:
8053 case OMPD_taskyield:
8054 case OMPD_barrier:
8055 case OMPD_taskwait:
8056 case OMPD_cancellation_point:
8057 case OMPD_flush:
8058 case OMPD_declare_reduction:
8059 case OMPD_declare_simd:
8060 case OMPD_declare_target:
8061 case OMPD_end_declare_target:
8062 case OMPD_simd:
8063 case OMPD_for:
8064 case OMPD_for_simd:
8065 case OMPD_sections:
8066 case OMPD_section:
8067 case OMPD_single:
8068 case OMPD_master:
8069 case OMPD_critical:
8070 case OMPD_taskgroup:
8071 case OMPD_distribute:
8072 case OMPD_ordered:
8073 case OMPD_atomic:
8074 case OMPD_distribute_simd:
8075 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8076 case OMPD_unknown:
8077 llvm_unreachable("Unknown OpenMP directive");
8078 }
8079 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008080 case OMPC_thread_limit:
8081 switch (DKind) {
8082 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008083 case OMPD_target_teams_distribute:
8084 case OMPD_target_teams_distribute_simd:
8085 case OMPD_target_teams_distribute_parallel_for:
8086 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008087 CaptureRegion = OMPD_target;
8088 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008089 case OMPD_teams_distribute_parallel_for:
8090 case OMPD_teams_distribute_parallel_for_simd:
8091 case OMPD_teams:
8092 case OMPD_teams_distribute:
8093 case OMPD_teams_distribute_simd:
8094 // Do not capture thread_limit-clause expressions.
8095 break;
8096 case OMPD_distribute_parallel_for:
8097 case OMPD_distribute_parallel_for_simd:
8098 case OMPD_task:
8099 case OMPD_taskloop:
8100 case OMPD_taskloop_simd:
8101 case OMPD_target_data:
8102 case OMPD_target_enter_data:
8103 case OMPD_target_exit_data:
8104 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008105 case OMPD_cancel:
8106 case OMPD_parallel:
8107 case OMPD_parallel_sections:
8108 case OMPD_parallel_for:
8109 case OMPD_parallel_for_simd:
8110 case OMPD_target:
8111 case OMPD_target_simd:
8112 case OMPD_target_parallel:
8113 case OMPD_target_parallel_for:
8114 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008115 case OMPD_threadprivate:
8116 case OMPD_taskyield:
8117 case OMPD_barrier:
8118 case OMPD_taskwait:
8119 case OMPD_cancellation_point:
8120 case OMPD_flush:
8121 case OMPD_declare_reduction:
8122 case OMPD_declare_simd:
8123 case OMPD_declare_target:
8124 case OMPD_end_declare_target:
8125 case OMPD_simd:
8126 case OMPD_for:
8127 case OMPD_for_simd:
8128 case OMPD_sections:
8129 case OMPD_section:
8130 case OMPD_single:
8131 case OMPD_master:
8132 case OMPD_critical:
8133 case OMPD_taskgroup:
8134 case OMPD_distribute:
8135 case OMPD_ordered:
8136 case OMPD_atomic:
8137 case OMPD_distribute_simd:
8138 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8139 case OMPD_unknown:
8140 llvm_unreachable("Unknown OpenMP directive");
8141 }
8142 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008143 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008144 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008145 case OMPD_parallel_for:
8146 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008147 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008148 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008149 case OMPD_teams_distribute_parallel_for:
8150 case OMPD_teams_distribute_parallel_for_simd:
8151 case OMPD_target_parallel_for:
8152 case OMPD_target_parallel_for_simd:
8153 case OMPD_target_teams_distribute_parallel_for:
8154 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008155 CaptureRegion = OMPD_parallel;
8156 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008157 case OMPD_for:
8158 case OMPD_for_simd:
8159 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008160 break;
8161 case OMPD_task:
8162 case OMPD_taskloop:
8163 case OMPD_taskloop_simd:
8164 case OMPD_target_data:
8165 case OMPD_target_enter_data:
8166 case OMPD_target_exit_data:
8167 case OMPD_target_update:
8168 case OMPD_teams:
8169 case OMPD_teams_distribute:
8170 case OMPD_teams_distribute_simd:
8171 case OMPD_target_teams_distribute:
8172 case OMPD_target_teams_distribute_simd:
8173 case OMPD_target:
8174 case OMPD_target_simd:
8175 case OMPD_target_parallel:
8176 case OMPD_cancel:
8177 case OMPD_parallel:
8178 case OMPD_parallel_sections:
8179 case OMPD_threadprivate:
8180 case OMPD_taskyield:
8181 case OMPD_barrier:
8182 case OMPD_taskwait:
8183 case OMPD_cancellation_point:
8184 case OMPD_flush:
8185 case OMPD_declare_reduction:
8186 case OMPD_declare_simd:
8187 case OMPD_declare_target:
8188 case OMPD_end_declare_target:
8189 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008190 case OMPD_sections:
8191 case OMPD_section:
8192 case OMPD_single:
8193 case OMPD_master:
8194 case OMPD_critical:
8195 case OMPD_taskgroup:
8196 case OMPD_distribute:
8197 case OMPD_ordered:
8198 case OMPD_atomic:
8199 case OMPD_distribute_simd:
8200 case OMPD_target_teams:
8201 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8202 case OMPD_unknown:
8203 llvm_unreachable("Unknown OpenMP directive");
8204 }
8205 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008206 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008207 switch (DKind) {
8208 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008209 case OMPD_teams_distribute_parallel_for_simd:
8210 case OMPD_teams_distribute:
8211 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008212 case OMPD_target_teams_distribute_parallel_for:
8213 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008214 case OMPD_target_teams_distribute:
8215 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008216 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008217 break;
8218 case OMPD_distribute_parallel_for:
8219 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008220 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008221 case OMPD_distribute_simd:
8222 // Do not capture thread_limit-clause expressions.
8223 break;
8224 case OMPD_parallel_for:
8225 case OMPD_parallel_for_simd:
8226 case OMPD_target_parallel_for_simd:
8227 case OMPD_target_parallel_for:
8228 case OMPD_task:
8229 case OMPD_taskloop:
8230 case OMPD_taskloop_simd:
8231 case OMPD_target_data:
8232 case OMPD_target_enter_data:
8233 case OMPD_target_exit_data:
8234 case OMPD_target_update:
8235 case OMPD_teams:
8236 case OMPD_target:
8237 case OMPD_target_simd:
8238 case OMPD_target_parallel:
8239 case OMPD_cancel:
8240 case OMPD_parallel:
8241 case OMPD_parallel_sections:
8242 case OMPD_threadprivate:
8243 case OMPD_taskyield:
8244 case OMPD_barrier:
8245 case OMPD_taskwait:
8246 case OMPD_cancellation_point:
8247 case OMPD_flush:
8248 case OMPD_declare_reduction:
8249 case OMPD_declare_simd:
8250 case OMPD_declare_target:
8251 case OMPD_end_declare_target:
8252 case OMPD_simd:
8253 case OMPD_for:
8254 case OMPD_for_simd:
8255 case OMPD_sections:
8256 case OMPD_section:
8257 case OMPD_single:
8258 case OMPD_master:
8259 case OMPD_critical:
8260 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008261 case OMPD_ordered:
8262 case OMPD_atomic:
8263 case OMPD_target_teams:
8264 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8265 case OMPD_unknown:
8266 llvm_unreachable("Unknown OpenMP directive");
8267 }
8268 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008269 case OMPC_device:
8270 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008271 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008272 case OMPD_target_enter_data:
8273 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008274 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008275 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008276 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008277 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008278 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008279 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008280 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008281 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008282 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008283 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008284 CaptureRegion = OMPD_task;
8285 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008286 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008287 // Do not capture device-clause expressions.
8288 break;
8289 case OMPD_teams_distribute_parallel_for:
8290 case OMPD_teams_distribute_parallel_for_simd:
8291 case OMPD_teams:
8292 case OMPD_teams_distribute:
8293 case OMPD_teams_distribute_simd:
8294 case OMPD_distribute_parallel_for:
8295 case OMPD_distribute_parallel_for_simd:
8296 case OMPD_task:
8297 case OMPD_taskloop:
8298 case OMPD_taskloop_simd:
8299 case OMPD_cancel:
8300 case OMPD_parallel:
8301 case OMPD_parallel_sections:
8302 case OMPD_parallel_for:
8303 case OMPD_parallel_for_simd:
8304 case OMPD_threadprivate:
8305 case OMPD_taskyield:
8306 case OMPD_barrier:
8307 case OMPD_taskwait:
8308 case OMPD_cancellation_point:
8309 case OMPD_flush:
8310 case OMPD_declare_reduction:
8311 case OMPD_declare_simd:
8312 case OMPD_declare_target:
8313 case OMPD_end_declare_target:
8314 case OMPD_simd:
8315 case OMPD_for:
8316 case OMPD_for_simd:
8317 case OMPD_sections:
8318 case OMPD_section:
8319 case OMPD_single:
8320 case OMPD_master:
8321 case OMPD_critical:
8322 case OMPD_taskgroup:
8323 case OMPD_distribute:
8324 case OMPD_ordered:
8325 case OMPD_atomic:
8326 case OMPD_distribute_simd:
8327 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8328 case OMPD_unknown:
8329 llvm_unreachable("Unknown OpenMP directive");
8330 }
8331 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008332 case OMPC_firstprivate:
8333 case OMPC_lastprivate:
8334 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008335 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008336 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008337 case OMPC_linear:
8338 case OMPC_default:
8339 case OMPC_proc_bind:
8340 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008341 case OMPC_safelen:
8342 case OMPC_simdlen:
8343 case OMPC_collapse:
8344 case OMPC_private:
8345 case OMPC_shared:
8346 case OMPC_aligned:
8347 case OMPC_copyin:
8348 case OMPC_copyprivate:
8349 case OMPC_ordered:
8350 case OMPC_nowait:
8351 case OMPC_untied:
8352 case OMPC_mergeable:
8353 case OMPC_threadprivate:
8354 case OMPC_flush:
8355 case OMPC_read:
8356 case OMPC_write:
8357 case OMPC_update:
8358 case OMPC_capture:
8359 case OMPC_seq_cst:
8360 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008361 case OMPC_threads:
8362 case OMPC_simd:
8363 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008364 case OMPC_priority:
8365 case OMPC_grainsize:
8366 case OMPC_nogroup:
8367 case OMPC_num_tasks:
8368 case OMPC_hint:
8369 case OMPC_defaultmap:
8370 case OMPC_unknown:
8371 case OMPC_uniform:
8372 case OMPC_to:
8373 case OMPC_from:
8374 case OMPC_use_device_ptr:
8375 case OMPC_is_device_ptr:
8376 llvm_unreachable("Unexpected OpenMP clause.");
8377 }
8378 return CaptureRegion;
8379}
8380
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008381OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8382 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008383 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008384 SourceLocation NameModifierLoc,
8385 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008386 SourceLocation EndLoc) {
8387 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008388 Stmt *HelperValStmt = nullptr;
8389 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008390 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8391 !Condition->isInstantiationDependent() &&
8392 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008393 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008394 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008395 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008396
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008397 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008398
8399 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8400 CaptureRegion =
8401 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008402 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008403 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008404 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008405 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8406 HelperValStmt = buildPreInits(Context, Captures);
8407 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008408 }
8409
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008410 return new (Context)
8411 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8412 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008413}
8414
Alexey Bataev3778b602014-07-17 07:32:53 +00008415OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8416 SourceLocation StartLoc,
8417 SourceLocation LParenLoc,
8418 SourceLocation EndLoc) {
8419 Expr *ValExpr = Condition;
8420 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8421 !Condition->isInstantiationDependent() &&
8422 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008423 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008424 if (Val.isInvalid())
8425 return nullptr;
8426
Richard Smith03a4aa32016-06-23 19:02:52 +00008427 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008428 }
8429
8430 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8431}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008432ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8433 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008434 if (!Op)
8435 return ExprError();
8436
8437 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8438 public:
8439 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008440 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008441 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8442 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008443 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8444 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008445 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8446 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008447 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8448 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008449 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8450 QualType T,
8451 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008452 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8453 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008454 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8455 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008456 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008457 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008458 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008459 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8460 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008461 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8462 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008463 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8464 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008465 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008466 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008467 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008468 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8469 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008470 llvm_unreachable("conversion functions are permitted");
8471 }
8472 } ConvertDiagnoser;
8473 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8474}
8475
Alexey Bataeve3727102018-04-18 15:57:46 +00008476static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008477 OpenMPClauseKind CKind,
8478 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008479 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8480 !ValExpr->isInstantiationDependent()) {
8481 SourceLocation Loc = ValExpr->getExprLoc();
8482 ExprResult Value =
8483 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8484 if (Value.isInvalid())
8485 return false;
8486
8487 ValExpr = Value.get();
8488 // The expression must evaluate to a non-negative integer value.
8489 llvm::APSInt Result;
8490 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008491 Result.isSigned() &&
8492 !((!StrictlyPositive && Result.isNonNegative()) ||
8493 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008494 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008495 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8496 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008497 return false;
8498 }
8499 }
8500 return true;
8501}
8502
Alexey Bataev568a8332014-03-06 06:15:19 +00008503OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8504 SourceLocation StartLoc,
8505 SourceLocation LParenLoc,
8506 SourceLocation EndLoc) {
8507 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008508 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008509
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008510 // OpenMP [2.5, Restrictions]
8511 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008512 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008513 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008514 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008515
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008516 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008517 OpenMPDirectiveKind CaptureRegion =
8518 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8519 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008520 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008521 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008522 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8523 HelperValStmt = buildPreInits(Context, Captures);
8524 }
8525
8526 return new (Context) OMPNumThreadsClause(
8527 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008528}
8529
Alexey Bataev62c87d22014-03-21 04:51:18 +00008530ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008531 OpenMPClauseKind CKind,
8532 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008533 if (!E)
8534 return ExprError();
8535 if (E->isValueDependent() || E->isTypeDependent() ||
8536 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008537 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008538 llvm::APSInt Result;
8539 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8540 if (ICE.isInvalid())
8541 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008542 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8543 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008544 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008545 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8546 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008547 return ExprError();
8548 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008549 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8550 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8551 << E->getSourceRange();
8552 return ExprError();
8553 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008554 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8555 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008556 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008557 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008558 return ICE;
8559}
8560
8561OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8562 SourceLocation LParenLoc,
8563 SourceLocation EndLoc) {
8564 // OpenMP [2.8.1, simd construct, Description]
8565 // The parameter of the safelen clause must be a constant
8566 // positive integer expression.
8567 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8568 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008569 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008570 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008571 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008572}
8573
Alexey Bataev66b15b52015-08-21 11:14:16 +00008574OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8575 SourceLocation LParenLoc,
8576 SourceLocation EndLoc) {
8577 // OpenMP [2.8.1, simd construct, Description]
8578 // The parameter of the simdlen clause must be a constant
8579 // positive integer expression.
8580 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8581 if (Simdlen.isInvalid())
8582 return nullptr;
8583 return new (Context)
8584 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8585}
8586
Alexander Musman64d33f12014-06-04 07:53:32 +00008587OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8588 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008589 SourceLocation LParenLoc,
8590 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008591 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008592 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008593 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008594 // The parameter of the collapse clause must be a constant
8595 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008596 ExprResult NumForLoopsResult =
8597 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8598 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008599 return nullptr;
8600 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008601 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008602}
8603
Alexey Bataev10e775f2015-07-30 11:36:16 +00008604OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8605 SourceLocation EndLoc,
8606 SourceLocation LParenLoc,
8607 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008608 // OpenMP [2.7.1, loop construct, Description]
8609 // OpenMP [2.8.1, simd construct, Description]
8610 // OpenMP [2.9.6, distribute construct, Description]
8611 // The parameter of the ordered clause must be a constant
8612 // positive integer expression if any.
8613 if (NumForLoops && LParenLoc.isValid()) {
8614 ExprResult NumForLoopsResult =
8615 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8616 if (NumForLoopsResult.isInvalid())
8617 return nullptr;
8618 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008619 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008620 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008621 }
Alexey Bataev346265e2015-09-25 10:37:12 +00008622 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00008623 return new (Context)
8624 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
8625}
8626
Alexey Bataeved09d242014-05-28 05:53:51 +00008627OMPClause *Sema::ActOnOpenMPSimpleClause(
8628 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8629 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008630 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008631 switch (Kind) {
8632 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008633 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008634 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8635 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008636 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008637 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008638 Res = ActOnOpenMPProcBindClause(
8639 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8640 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008641 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008642 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008643 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008644 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008645 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008646 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008647 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008648 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008649 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008650 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008651 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008652 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008653 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008654 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008655 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008656 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008657 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008658 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008659 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008660 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008661 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008662 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008663 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008664 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008665 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008666 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008667 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008668 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008669 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008670 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008671 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008672 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008673 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008674 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008675 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008676 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008677 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008678 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008679 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008680 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008681 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008682 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008683 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008684 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008685 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008686 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008687 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008688 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008689 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008690 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008691 llvm_unreachable("Clause is not allowed.");
8692 }
8693 return Res;
8694}
8695
Alexey Bataev6402bca2015-12-28 07:25:51 +00008696static std::string
8697getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8698 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008699 SmallString<256> Buffer;
8700 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00008701 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8702 unsigned Skipped = Exclude.size();
8703 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00008704 for (unsigned I = First; I < Last; ++I) {
8705 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008706 --Skipped;
8707 continue;
8708 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008709 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
8710 if (I == Bound - Skipped)
8711 Out << " or ";
8712 else if (I != Bound + 1 - Skipped)
8713 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00008714 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008715 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00008716}
8717
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008718OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8719 SourceLocation KindKwLoc,
8720 SourceLocation StartLoc,
8721 SourceLocation LParenLoc,
8722 SourceLocation EndLoc) {
8723 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008724 static_assert(OMPC_DEFAULT_unknown > 0,
8725 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008726 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008727 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8728 /*Last=*/OMPC_DEFAULT_unknown)
8729 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008730 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008731 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008732 switch (Kind) {
8733 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008734 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008735 break;
8736 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008737 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008738 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008739 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008740 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008741 break;
8742 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008743 return new (Context)
8744 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008745}
8746
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008747OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8748 SourceLocation KindKwLoc,
8749 SourceLocation StartLoc,
8750 SourceLocation LParenLoc,
8751 SourceLocation EndLoc) {
8752 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008753 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008754 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8755 /*Last=*/OMPC_PROC_BIND_unknown)
8756 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008757 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008758 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008759 return new (Context)
8760 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008761}
8762
Alexey Bataev56dafe82014-06-20 07:16:17 +00008763OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008764 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008765 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008766 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008767 SourceLocation EndLoc) {
8768 OMPClause *Res = nullptr;
8769 switch (Kind) {
8770 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008771 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8772 assert(Argument.size() == NumberOfElements &&
8773 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008774 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008775 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8776 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8777 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8778 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8779 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008780 break;
8781 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008782 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8783 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8784 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8785 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008786 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008787 case OMPC_dist_schedule:
8788 Res = ActOnOpenMPDistScheduleClause(
8789 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8790 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8791 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008792 case OMPC_defaultmap:
8793 enum { Modifier, DefaultmapKind };
8794 Res = ActOnOpenMPDefaultmapClause(
8795 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8796 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008797 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8798 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008799 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008800 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008801 case OMPC_num_threads:
8802 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008803 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008804 case OMPC_collapse:
8805 case OMPC_default:
8806 case OMPC_proc_bind:
8807 case OMPC_private:
8808 case OMPC_firstprivate:
8809 case OMPC_lastprivate:
8810 case OMPC_shared:
8811 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008812 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008813 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008814 case OMPC_linear:
8815 case OMPC_aligned:
8816 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008817 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008818 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008819 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008820 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008821 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008822 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008823 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008824 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008825 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008826 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008827 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008828 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008829 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008830 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008831 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008832 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008833 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008834 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008835 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008836 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008837 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008838 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008839 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008840 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008841 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008842 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008843 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008844 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008845 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008846 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008847 llvm_unreachable("Clause is not allowed.");
8848 }
8849 return Res;
8850}
8851
Alexey Bataev6402bca2015-12-28 07:25:51 +00008852static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8853 OpenMPScheduleClauseModifier M2,
8854 SourceLocation M1Loc, SourceLocation M2Loc) {
8855 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8856 SmallVector<unsigned, 2> Excluded;
8857 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8858 Excluded.push_back(M2);
8859 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8860 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8861 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8862 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8863 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8864 << getListOfPossibleValues(OMPC_schedule,
8865 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8866 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8867 Excluded)
8868 << getOpenMPClauseName(OMPC_schedule);
8869 return true;
8870 }
8871 return false;
8872}
8873
Alexey Bataev56dafe82014-06-20 07:16:17 +00008874OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008875 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008876 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008877 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8878 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8879 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8880 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8881 return nullptr;
8882 // OpenMP, 2.7.1, Loop Construct, Restrictions
8883 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8884 // but not both.
8885 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8886 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8887 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8888 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8889 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8890 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8891 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8892 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8893 return nullptr;
8894 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008895 if (Kind == OMPC_SCHEDULE_unknown) {
8896 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008897 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8898 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8899 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8900 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8901 Exclude);
8902 } else {
8903 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8904 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008905 }
8906 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8907 << Values << getOpenMPClauseName(OMPC_schedule);
8908 return nullptr;
8909 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00008910 // OpenMP, 2.7.1, Loop Construct, Restrictions
8911 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8912 // schedule(guided).
8913 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8914 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8915 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8916 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8917 diag::err_omp_schedule_nonmonotonic_static);
8918 return nullptr;
8919 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008920 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00008921 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00008922 if (ChunkSize) {
8923 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8924 !ChunkSize->isInstantiationDependent() &&
8925 !ChunkSize->containsUnexpandedParameterPack()) {
8926 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8927 ExprResult Val =
8928 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8929 if (Val.isInvalid())
8930 return nullptr;
8931
8932 ValExpr = Val.get();
8933
8934 // OpenMP [2.7.1, Restrictions]
8935 // chunk_size must be a loop invariant integer expression with a positive
8936 // value.
8937 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00008938 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8939 if (Result.isSigned() && !Result.isStrictlyPositive()) {
8940 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008941 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00008942 return nullptr;
8943 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00008944 } else if (getOpenMPCaptureRegionForClause(
8945 DSAStack->getCurrentDirective(), OMPC_schedule) !=
8946 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00008947 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008948 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008949 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00008950 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8951 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008952 }
8953 }
8954 }
8955
Alexey Bataev6402bca2015-12-28 07:25:51 +00008956 return new (Context)
8957 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00008958 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008959}
8960
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008961OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8962 SourceLocation StartLoc,
8963 SourceLocation EndLoc) {
8964 OMPClause *Res = nullptr;
8965 switch (Kind) {
8966 case OMPC_ordered:
8967 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8968 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00008969 case OMPC_nowait:
8970 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8971 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008972 case OMPC_untied:
8973 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8974 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008975 case OMPC_mergeable:
8976 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8977 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008978 case OMPC_read:
8979 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8980 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00008981 case OMPC_write:
8982 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8983 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00008984 case OMPC_update:
8985 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8986 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00008987 case OMPC_capture:
8988 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8989 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008990 case OMPC_seq_cst:
8991 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8992 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00008993 case OMPC_threads:
8994 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8995 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008996 case OMPC_simd:
8997 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8998 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00008999 case OMPC_nogroup:
9000 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9001 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009002 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009003 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009004 case OMPC_num_threads:
9005 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009006 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009007 case OMPC_collapse:
9008 case OMPC_schedule:
9009 case OMPC_private:
9010 case OMPC_firstprivate:
9011 case OMPC_lastprivate:
9012 case OMPC_shared:
9013 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009014 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009015 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009016 case OMPC_linear:
9017 case OMPC_aligned:
9018 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009019 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009020 case OMPC_default:
9021 case OMPC_proc_bind:
9022 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009023 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009024 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009025 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009026 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009027 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009028 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009029 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009030 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009031 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009032 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009033 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009034 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009035 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009036 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009037 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009038 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009039 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009040 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009041 llvm_unreachable("Clause is not allowed.");
9042 }
9043 return Res;
9044}
9045
Alexey Bataev236070f2014-06-20 11:19:47 +00009046OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9047 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009048 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009049 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9050}
9051
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009052OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9053 SourceLocation EndLoc) {
9054 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9055}
9056
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009057OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9058 SourceLocation EndLoc) {
9059 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9060}
9061
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009062OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9063 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009064 return new (Context) OMPReadClause(StartLoc, EndLoc);
9065}
9066
Alexey Bataevdea47612014-07-23 07:46:59 +00009067OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9068 SourceLocation EndLoc) {
9069 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9070}
9071
Alexey Bataev67a4f222014-07-23 10:25:33 +00009072OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9073 SourceLocation EndLoc) {
9074 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9075}
9076
Alexey Bataev459dec02014-07-24 06:46:57 +00009077OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9078 SourceLocation EndLoc) {
9079 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9080}
9081
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009082OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9083 SourceLocation EndLoc) {
9084 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9085}
9086
Alexey Bataev346265e2015-09-25 10:37:12 +00009087OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9088 SourceLocation EndLoc) {
9089 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9090}
9091
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009092OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9093 SourceLocation EndLoc) {
9094 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9095}
9096
Alexey Bataevb825de12015-12-07 10:51:44 +00009097OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9098 SourceLocation EndLoc) {
9099 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9100}
9101
Alexey Bataevc5e02582014-06-16 07:08:35 +00009102OMPClause *Sema::ActOnOpenMPVarListClause(
9103 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9104 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9105 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009106 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009107 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9108 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9109 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009110 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009111 switch (Kind) {
9112 case OMPC_private:
9113 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9114 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009115 case OMPC_firstprivate:
9116 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9117 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009118 case OMPC_lastprivate:
9119 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9120 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009121 case OMPC_shared:
9122 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9123 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009124 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009125 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9126 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009127 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009128 case OMPC_task_reduction:
9129 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9130 EndLoc, ReductionIdScopeSpec,
9131 ReductionId);
9132 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009133 case OMPC_in_reduction:
9134 Res =
9135 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9136 EndLoc, ReductionIdScopeSpec, ReductionId);
9137 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009138 case OMPC_linear:
9139 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009140 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009141 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009142 case OMPC_aligned:
9143 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9144 ColonLoc, EndLoc);
9145 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009146 case OMPC_copyin:
9147 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9148 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009149 case OMPC_copyprivate:
9150 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9151 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009152 case OMPC_flush:
9153 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9154 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009155 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009156 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009157 StartLoc, LParenLoc, EndLoc);
9158 break;
9159 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009160 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9161 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9162 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009163 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009164 case OMPC_to:
9165 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9166 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009167 case OMPC_from:
9168 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9169 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009170 case OMPC_use_device_ptr:
9171 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9172 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009173 case OMPC_is_device_ptr:
9174 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9175 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009176 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009177 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009178 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009179 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009180 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009181 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009182 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009183 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009184 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009185 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009186 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009187 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009188 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009189 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009190 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009191 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009192 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009193 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009194 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009195 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009196 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009197 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009198 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009199 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009200 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009201 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009202 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009203 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009204 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009205 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009206 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009207 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009208 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009209 llvm_unreachable("Clause is not allowed.");
9210 }
9211 return Res;
9212}
9213
Alexey Bataev90c228f2016-02-08 09:29:13 +00009214ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009215 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009216 ExprResult Res = BuildDeclRefExpr(
9217 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9218 if (!Res.isUsable())
9219 return ExprError();
9220 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9221 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9222 if (!Res.isUsable())
9223 return ExprError();
9224 }
9225 if (VK != VK_LValue && Res.get()->isGLValue()) {
9226 Res = DefaultLvalueConversion(Res.get());
9227 if (!Res.isUsable())
9228 return ExprError();
9229 }
9230 return Res;
9231}
9232
Alexey Bataev60da77e2016-02-29 05:54:20 +00009233static std::pair<ValueDecl *, bool>
9234getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9235 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009236 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9237 RefExpr->containsUnexpandedParameterPack())
9238 return std::make_pair(nullptr, true);
9239
Alexey Bataevd985eda2016-02-10 11:29:16 +00009240 // OpenMP [3.1, C/C++]
9241 // A list item is a variable name.
9242 // OpenMP [2.9.3.3, Restrictions, p.1]
9243 // A variable that is part of another variable (as an array or
9244 // structure element) cannot appear in a private clause.
9245 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009246 enum {
9247 NoArrayExpr = -1,
9248 ArraySubscript = 0,
9249 OMPArraySection = 1
9250 } IsArrayExpr = NoArrayExpr;
9251 if (AllowArraySection) {
9252 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009253 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009254 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9255 Base = TempASE->getBase()->IgnoreParenImpCasts();
9256 RefExpr = Base;
9257 IsArrayExpr = ArraySubscript;
9258 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009259 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009260 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9261 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9262 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9263 Base = TempASE->getBase()->IgnoreParenImpCasts();
9264 RefExpr = Base;
9265 IsArrayExpr = OMPArraySection;
9266 }
9267 }
9268 ELoc = RefExpr->getExprLoc();
9269 ERange = RefExpr->getSourceRange();
9270 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009271 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9272 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9273 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9274 (S.getCurrentThisType().isNull() || !ME ||
9275 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9276 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009277 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009278 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9279 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009280 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009281 S.Diag(ELoc,
9282 AllowArraySection
9283 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9284 : diag::err_omp_expected_var_name_member_expr)
9285 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9286 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009287 return std::make_pair(nullptr, false);
9288 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009289 return std::make_pair(
9290 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009291}
9292
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009293OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9294 SourceLocation StartLoc,
9295 SourceLocation LParenLoc,
9296 SourceLocation EndLoc) {
9297 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009298 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009299 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009300 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009301 SourceLocation ELoc;
9302 SourceRange ERange;
9303 Expr *SimpleRefExpr = RefExpr;
9304 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009305 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009306 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009307 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009308 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009309 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009310 ValueDecl *D = Res.first;
9311 if (!D)
9312 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009313
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009314 QualType Type = D->getType();
9315 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009316
9317 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9318 // A variable that appears in a private clause must not have an incomplete
9319 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009320 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009321 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009322 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009323
Alexey Bataev758e55e2013-09-06 18:03:48 +00009324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9325 // in a Construct]
9326 // Variables with the predetermined data-sharing attributes may not be
9327 // listed in data-sharing attributes clauses, except for the cases
9328 // listed below. For these exceptions only, listing a predetermined
9329 // variable in a data-sharing attribute clause is allowed and overrides
9330 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009331 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009332 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009333 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9334 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009335 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009336 continue;
9337 }
9338
Alexey Bataeve3727102018-04-18 15:57:46 +00009339 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009340 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009341 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009342 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009343 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9344 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009345 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009346 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009347 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009349 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009351 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009352 continue;
9353 }
9354
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009355 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9356 // A list item cannot appear in both a map clause and a data-sharing
9357 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009358 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009359 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009360 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009361 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009362 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9363 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9364 ConflictKind = WhereFoundClauseKind;
9365 return true;
9366 })) {
9367 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009368 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009369 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009370 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009371 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009372 continue;
9373 }
9374 }
9375
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009376 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9377 // A variable of class type (or array thereof) that appears in a private
9378 // clause requires an accessible, unambiguous default constructor for the
9379 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009380 // Generate helper private variable and initialize it with the default
9381 // value. The address of the original variable is replaced by the address of
9382 // the new private variable in CodeGen. This new variable is not added to
9383 // IdResolver, so the code in the OpenMP region uses original variable for
9384 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009385 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009386 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009387 buildVarDecl(*this, ELoc, Type, D->getName(),
9388 D->hasAttrs() ? &D->getAttrs() : nullptr,
9389 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009390 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009391 if (VDPrivate->isInvalidDecl())
9392 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009393 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009394 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009395
Alexey Bataev90c228f2016-02-08 09:29:13 +00009396 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009397 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009398 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009399 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009400 Vars.push_back((VD || CurContext->isDependentContext())
9401 ? RefExpr->IgnoreParens()
9402 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009403 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009404 }
9405
Alexey Bataeved09d242014-05-28 05:53:51 +00009406 if (Vars.empty())
9407 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009408
Alexey Bataev03b340a2014-10-21 03:16:40 +00009409 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9410 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009411}
9412
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009413namespace {
9414class DiagsUninitializedSeveretyRAII {
9415private:
9416 DiagnosticsEngine &Diags;
9417 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009418 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009419
9420public:
9421 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9422 bool IsIgnored)
9423 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9424 if (!IsIgnored) {
9425 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9426 /*Map*/ diag::Severity::Ignored, Loc);
9427 }
9428 }
9429 ~DiagsUninitializedSeveretyRAII() {
9430 if (!IsIgnored)
9431 Diags.popMappings(SavedLoc);
9432 }
9433};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009434}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009435
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009436OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9437 SourceLocation StartLoc,
9438 SourceLocation LParenLoc,
9439 SourceLocation EndLoc) {
9440 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009441 SmallVector<Expr *, 8> PrivateCopies;
9442 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009443 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009444 bool IsImplicitClause =
9445 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009446 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009447
Alexey Bataeve3727102018-04-18 15:57:46 +00009448 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009449 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009450 SourceLocation ELoc;
9451 SourceRange ERange;
9452 Expr *SimpleRefExpr = RefExpr;
9453 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009454 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009455 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009456 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009457 PrivateCopies.push_back(nullptr);
9458 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009459 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009460 ValueDecl *D = Res.first;
9461 if (!D)
9462 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009463
Alexey Bataev60da77e2016-02-29 05:54:20 +00009464 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009465 QualType Type = D->getType();
9466 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009467
9468 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9469 // A variable that appears in a private clause must not have an incomplete
9470 // type or a reference type.
9471 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009472 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009473 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009474 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009475
9476 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9477 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009478 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009479 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009480 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009481
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009482 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009483 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009484 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009485 DSAStackTy::DSAVarData DVar =
9486 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009487 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009488 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009489 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009490 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9491 // A list item that specifies a given variable may not appear in more
9492 // than one clause on the same directive, except that a variable may be
9493 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009494 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9495 // A list item may appear in a firstprivate or lastprivate clause but not
9496 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009497 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009498 (isOpenMPDistributeDirective(CurrDir) ||
9499 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009500 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009501 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009502 << getOpenMPClauseName(DVar.CKind)
9503 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009504 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009505 continue;
9506 }
9507
9508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9509 // in a Construct]
9510 // Variables with the predetermined data-sharing attributes may not be
9511 // listed in data-sharing attributes clauses, except for the cases
9512 // listed below. For these exceptions only, listing a predetermined
9513 // variable in a data-sharing attribute clause is allowed and overrides
9514 // the variable's predetermined data-sharing attributes.
9515 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9516 // in a Construct, C/C++, p.2]
9517 // Variables with const-qualified type having no mutable member may be
9518 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009519 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009520 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9521 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009522 << getOpenMPClauseName(DVar.CKind)
9523 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009524 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009525 continue;
9526 }
9527
9528 // OpenMP [2.9.3.4, Restrictions, p.2]
9529 // A list item that is private within a parallel region must not appear
9530 // in a firstprivate clause on a worksharing construct if any of the
9531 // worksharing regions arising from the worksharing construct ever bind
9532 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009533 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9534 // A list item that is private within a teams region must not appear in a
9535 // firstprivate clause on a distribute construct if any of the distribute
9536 // regions arising from the distribute construct ever bind to any of the
9537 // teams regions arising from the teams construct.
9538 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9539 // A list item that appears in a reduction clause of a teams construct
9540 // must not appear in a firstprivate clause on a distribute construct if
9541 // any of the distribute regions arising from the distribute construct
9542 // ever bind to any of the teams regions arising from the teams construct.
9543 if ((isOpenMPWorksharingDirective(CurrDir) ||
9544 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009545 !isOpenMPParallelDirective(CurrDir) &&
9546 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009547 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009548 if (DVar.CKind != OMPC_shared &&
9549 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009550 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009551 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009552 Diag(ELoc, diag::err_omp_required_access)
9553 << getOpenMPClauseName(OMPC_firstprivate)
9554 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009555 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009556 continue;
9557 }
9558 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009559 // OpenMP [2.9.3.4, Restrictions, p.3]
9560 // A list item that appears in a reduction clause of a parallel construct
9561 // must not appear in a firstprivate clause on a worksharing or task
9562 // construct if any of the worksharing or task regions arising from the
9563 // worksharing or task construct ever bind to any of the parallel regions
9564 // arising from the parallel construct.
9565 // OpenMP [2.9.3.4, Restrictions, p.4]
9566 // A list item that appears in a reduction clause in worksharing
9567 // construct must not appear in a firstprivate clause in a task construct
9568 // encountered during execution of any of the worksharing regions arising
9569 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009570 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009571 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009572 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9573 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009574 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009575 isOpenMPWorksharingDirective(K) ||
9576 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009577 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009578 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009579 if (DVar.CKind == OMPC_reduction &&
9580 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009581 isOpenMPWorksharingDirective(DVar.DKind) ||
9582 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009583 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9584 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009585 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009586 continue;
9587 }
9588 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009589
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009590 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9591 // A list item cannot appear in both a map clause and a data-sharing
9592 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009593 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009594 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009595 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009596 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009597 [&ConflictKind](
9598 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9599 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009600 ConflictKind = WhereFoundClauseKind;
9601 return true;
9602 })) {
9603 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009604 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009605 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009606 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009607 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009608 continue;
9609 }
9610 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009611 }
9612
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009613 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009614 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009615 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009616 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9617 << getOpenMPClauseName(OMPC_firstprivate) << Type
9618 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9619 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009620 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009621 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009622 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009623 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009624 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009625 continue;
9626 }
9627
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009628 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009629 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009630 buildVarDecl(*this, ELoc, Type, D->getName(),
9631 D->hasAttrs() ? &D->getAttrs() : nullptr,
9632 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009633 // Generate helper private variable and initialize it with the value of the
9634 // original variable. The address of the original variable is replaced by
9635 // the address of the new private variable in the CodeGen. This new variable
9636 // is not added to IdResolver, so the code in the OpenMP region uses
9637 // original variable for proper diagnostics and variable capturing.
9638 Expr *VDInitRefExpr = nullptr;
9639 // For arrays generate initializer for single element and replace it by the
9640 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009641 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009642 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009643 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009644 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009645 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009646 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009647 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9648 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009649 InitializedEntity Entity =
9650 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009651 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9652
9653 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9654 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9655 if (Result.isInvalid())
9656 VDPrivate->setInvalidDecl();
9657 else
9658 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009659 // Remove temp variable declaration.
9660 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009661 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00009662 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9663 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +00009664 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9665 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009666 AddInitializerToDecl(VDPrivate,
9667 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009668 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009669 }
9670 if (VDPrivate->isInvalidDecl()) {
9671 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009672 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009673 diag::note_omp_task_predetermined_firstprivate_here);
9674 }
9675 continue;
9676 }
9677 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009678 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009679 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9680 RefExpr->getExprLoc());
9681 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009682 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009683 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009684 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009685 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009686 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +00009687 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009688 ExprCaptures.push_back(Ref->getDecl());
9689 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009690 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009691 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009692 Vars.push_back((VD || CurContext->isDependentContext())
9693 ? RefExpr->IgnoreParens()
9694 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009695 PrivateCopies.push_back(VDPrivateRefExpr);
9696 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009697 }
9698
Alexey Bataeved09d242014-05-28 05:53:51 +00009699 if (Vars.empty())
9700 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009701
9702 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009703 Vars, PrivateCopies, Inits,
9704 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009705}
9706
Alexander Musman1bb328c2014-06-04 13:06:39 +00009707OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9708 SourceLocation StartLoc,
9709 SourceLocation LParenLoc,
9710 SourceLocation EndLoc) {
9711 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009712 SmallVector<Expr *, 8> SrcExprs;
9713 SmallVector<Expr *, 8> DstExprs;
9714 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009715 SmallVector<Decl *, 4> ExprCaptures;
9716 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +00009717 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009718 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009719 SourceLocation ELoc;
9720 SourceRange ERange;
9721 Expr *SimpleRefExpr = RefExpr;
9722 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009723 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009724 // It will be analyzed later.
9725 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009726 SrcExprs.push_back(nullptr);
9727 DstExprs.push_back(nullptr);
9728 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009729 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009730 ValueDecl *D = Res.first;
9731 if (!D)
9732 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009733
Alexey Bataev74caaf22016-02-20 04:09:36 +00009734 QualType Type = D->getType();
9735 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009736
9737 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9738 // A variable that appears in a lastprivate clause must not have an
9739 // incomplete type or a reference type.
9740 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009741 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009742 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009743 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009744
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009745 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009746 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9747 // in a Construct]
9748 // Variables with the predetermined data-sharing attributes may not be
9749 // listed in data-sharing attributes clauses, except for the cases
9750 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009751 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9752 // A list item may appear in a firstprivate or lastprivate clause but not
9753 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +00009754 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009755 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009756 (isOpenMPDistributeDirective(CurrDir) ||
9757 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009758 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9759 Diag(ELoc, diag::err_omp_wrong_dsa)
9760 << getOpenMPClauseName(DVar.CKind)
9761 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009762 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009763 continue;
9764 }
9765
Alexey Bataevf29276e2014-06-18 04:14:57 +00009766 // OpenMP [2.14.3.5, Restrictions, p.2]
9767 // A list item that is private within a parallel region, or that appears in
9768 // the reduction clause of a parallel construct, must not appear in a
9769 // lastprivate clause on a worksharing construct if any of the corresponding
9770 // worksharing regions ever binds to any of the corresponding parallel
9771 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009772 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009773 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009774 !isOpenMPParallelDirective(CurrDir) &&
9775 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009776 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009777 if (DVar.CKind != OMPC_shared) {
9778 Diag(ELoc, diag::err_omp_required_access)
9779 << getOpenMPClauseName(OMPC_lastprivate)
9780 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009781 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009782 continue;
9783 }
9784 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009785
Alexander Musman1bb328c2014-06-04 13:06:39 +00009786 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009787 // A variable of class type (or array thereof) that appears in a
9788 // lastprivate clause requires an accessible, unambiguous default
9789 // constructor for the class type, unless the list item is also specified
9790 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009791 // A variable of class type (or array thereof) that appears in a
9792 // lastprivate clause requires an accessible, unambiguous copy assignment
9793 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009794 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009795 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
9796 Type.getUnqualifiedType(), ".lastprivate.src",
9797 D->hasAttrs() ? &D->getAttrs() : nullptr);
9798 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +00009799 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009800 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009801 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009802 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +00009803 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009804 // For arrays generate assignment operation for single element and replace
9805 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009806 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
9807 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009808 if (AssignmentOp.isInvalid())
9809 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009810 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009811 /*DiscardedValue=*/true);
9812 if (AssignmentOp.isInvalid())
9813 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009814
Alexey Bataev74caaf22016-02-20 04:09:36 +00009815 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009816 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009817 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009818 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009819 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009820 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00009821 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009822 ExprCaptures.push_back(Ref->getDecl());
9823 }
9824 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +00009825 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009826 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009827 ExprResult RefRes = DefaultLvalueConversion(Ref);
9828 if (!RefRes.isUsable())
9829 continue;
9830 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009831 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9832 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009833 if (!PostUpdateRes.isUsable())
9834 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009835 ExprPostUpdates.push_back(
9836 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009837 }
9838 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009839 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009840 Vars.push_back((VD || CurContext->isDependentContext())
9841 ? RefExpr->IgnoreParens()
9842 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00009843 SrcExprs.push_back(PseudoSrcExpr);
9844 DstExprs.push_back(PseudoDstExpr);
9845 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00009846 }
9847
9848 if (Vars.empty())
9849 return nullptr;
9850
9851 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009852 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009853 buildPreInits(Context, ExprCaptures),
9854 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009855}
9856
Alexey Bataev758e55e2013-09-06 18:03:48 +00009857OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9858 SourceLocation StartLoc,
9859 SourceLocation LParenLoc,
9860 SourceLocation EndLoc) {
9861 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00009862 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009863 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009864 SourceLocation ELoc;
9865 SourceRange ERange;
9866 Expr *SimpleRefExpr = RefExpr;
9867 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009868 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009869 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009870 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009871 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009872 ValueDecl *D = Res.first;
9873 if (!D)
9874 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009875
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009876 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009877 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9878 // in a Construct]
9879 // Variables with the predetermined data-sharing attributes may not be
9880 // listed in data-sharing attributes clauses, except for the cases
9881 // listed below. For these exceptions only, listing a predetermined
9882 // variable in a data-sharing attribute clause is allowed and overrides
9883 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009884 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009885 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9886 DVar.RefExpr) {
9887 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9888 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009889 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009890 continue;
9891 }
9892
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009893 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009894 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009895 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009896 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009897 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9898 ? RefExpr->IgnoreParens()
9899 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009900 }
9901
Alexey Bataeved09d242014-05-28 05:53:51 +00009902 if (Vars.empty())
9903 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009904
9905 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9906}
9907
Alexey Bataevc5e02582014-06-16 07:08:35 +00009908namespace {
9909class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9910 DSAStackTy *Stack;
9911
9912public:
9913 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009914 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
9915 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009916 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9917 return false;
9918 if (DVar.CKind != OMPC_unknown)
9919 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009920 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009921 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009922 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +00009923 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009924 }
9925 return false;
9926 }
9927 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009928 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00009929 if (Child && Visit(Child))
9930 return true;
9931 }
9932 return false;
9933 }
Alexey Bataev23b69422014-06-18 07:08:49 +00009934 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00009935};
Alexey Bataev23b69422014-06-18 07:08:49 +00009936} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00009937
Alexey Bataev60da77e2016-02-29 05:54:20 +00009938namespace {
9939// Transform MemberExpression for specified FieldDecl of current class to
9940// DeclRefExpr to specified OMPCapturedExprDecl.
9941class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9942 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +00009943 ValueDecl *Field = nullptr;
9944 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009945
9946public:
9947 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9948 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9949
9950 ExprResult TransformMemberExpr(MemberExpr *E) {
9951 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9952 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00009953 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009954 return CapturedExpr;
9955 }
9956 return BaseTransform::TransformMemberExpr(E);
9957 }
9958 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9959};
9960} // namespace
9961
Alexey Bataev97d18bf2018-04-11 19:21:00 +00009962template <typename T, typename U>
9963static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
9964 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009965 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009966 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009967 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009968 return Res;
9969 }
9970 }
9971 return T();
9972}
9973
9974static ExprResult
9975buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9976 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9977 const DeclarationNameInfo &ReductionId, QualType Ty,
9978 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9979 if (ReductionIdScopeSpec.isInvalid())
9980 return ExprError();
9981 SmallVector<UnresolvedSet<8>, 4> Lookups;
9982 if (S) {
9983 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9984 Lookup.suppressDiagnostics();
9985 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009986 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009987 do {
9988 S = S->getParent();
9989 } while (S && !S->isDeclScope(D));
9990 if (S)
9991 S = S->getParent();
9992 Lookups.push_back(UnresolvedSet<8>());
9993 Lookups.back().append(Lookup.begin(), Lookup.end());
9994 Lookup.clear();
9995 }
9996 } else if (auto *ULE =
9997 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9998 Lookups.push_back(UnresolvedSet<8>());
9999 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010000 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010001 if (D == PrevD)
10002 Lookups.push_back(UnresolvedSet<8>());
10003 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10004 Lookups.back().addDecl(DRD);
10005 PrevD = D;
10006 }
10007 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010008 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10009 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010010 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010011 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010012 return !D->isInvalidDecl() &&
10013 (D->getType()->isDependentType() ||
10014 D->getType()->isInstantiationDependentType() ||
10015 D->getType()->containsUnexpandedParameterPack());
10016 })) {
10017 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010018 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010019 ResSet.append(Set.begin(), Set.end());
10020 // The last item marks the end of all declarations at the specified scope.
10021 ResSet.addDecl(Set[Set.size() - 1]);
10022 }
10023 return UnresolvedLookupExpr::Create(
10024 SemaRef.Context, /*NamingClass=*/nullptr,
10025 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10026 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10027 }
10028 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10029 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10030 if (!D->isInvalidDecl() &&
10031 SemaRef.Context.hasSameType(D->getType(), Ty))
10032 return D;
10033 return nullptr;
10034 }))
10035 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10036 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10037 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10038 if (!D->isInvalidDecl() &&
10039 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10040 !Ty.isMoreQualifiedThan(D->getType()))
10041 return D;
10042 return nullptr;
10043 })) {
10044 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10045 /*DetectVirtual=*/false);
10046 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10047 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10048 VD->getType().getUnqualifiedType()))) {
10049 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10050 /*DiagID=*/0) !=
10051 Sema::AR_inaccessible) {
10052 SemaRef.BuildBasePathArray(Paths, BasePath);
10053 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10054 }
10055 }
10056 }
10057 }
10058 if (ReductionIdScopeSpec.isSet()) {
10059 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10060 return ExprError();
10061 }
10062 return ExprEmpty();
10063}
10064
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010065namespace {
10066/// Data for the reduction-based clauses.
10067struct ReductionData {
10068 /// List of original reduction items.
10069 SmallVector<Expr *, 8> Vars;
10070 /// List of private copies of the reduction items.
10071 SmallVector<Expr *, 8> Privates;
10072 /// LHS expressions for the reduction_op expressions.
10073 SmallVector<Expr *, 8> LHSs;
10074 /// RHS expressions for the reduction_op expressions.
10075 SmallVector<Expr *, 8> RHSs;
10076 /// Reduction operation expression.
10077 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010078 /// Taskgroup descriptors for the corresponding reduction items in
10079 /// in_reduction clauses.
10080 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010081 /// List of captures for clause.
10082 SmallVector<Decl *, 4> ExprCaptures;
10083 /// List of postupdate expressions.
10084 SmallVector<Expr *, 4> ExprPostUpdates;
10085 ReductionData() = delete;
10086 /// Reserves required memory for the reduction data.
10087 ReductionData(unsigned Size) {
10088 Vars.reserve(Size);
10089 Privates.reserve(Size);
10090 LHSs.reserve(Size);
10091 RHSs.reserve(Size);
10092 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010093 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010094 ExprCaptures.reserve(Size);
10095 ExprPostUpdates.reserve(Size);
10096 }
10097 /// Stores reduction item and reduction operation only (required for dependent
10098 /// reduction item).
10099 void push(Expr *Item, Expr *ReductionOp) {
10100 Vars.emplace_back(Item);
10101 Privates.emplace_back(nullptr);
10102 LHSs.emplace_back(nullptr);
10103 RHSs.emplace_back(nullptr);
10104 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010105 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010106 }
10107 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010108 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10109 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010110 Vars.emplace_back(Item);
10111 Privates.emplace_back(Private);
10112 LHSs.emplace_back(LHS);
10113 RHSs.emplace_back(RHS);
10114 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010115 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010116 }
10117};
10118} // namespace
10119
Alexey Bataeve3727102018-04-18 15:57:46 +000010120static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010121 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10122 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10123 const Expr *Length = OASE->getLength();
10124 if (Length == nullptr) {
10125 // For array sections of the form [1:] or [:], we would need to analyze
10126 // the lower bound...
10127 if (OASE->getColonLoc().isValid())
10128 return false;
10129
10130 // This is an array subscript which has implicit length 1!
10131 SingleElement = true;
10132 ArraySizes.push_back(llvm::APSInt::get(1));
10133 } else {
10134 llvm::APSInt ConstantLengthValue;
10135 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10136 return false;
10137
10138 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10139 ArraySizes.push_back(ConstantLengthValue);
10140 }
10141
10142 // Get the base of this array section and walk up from there.
10143 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10144
10145 // We require length = 1 for all array sections except the right-most to
10146 // guarantee that the memory region is contiguous and has no holes in it.
10147 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10148 Length = TempOASE->getLength();
10149 if (Length == nullptr) {
10150 // For array sections of the form [1:] or [:], we would need to analyze
10151 // the lower bound...
10152 if (OASE->getColonLoc().isValid())
10153 return false;
10154
10155 // This is an array subscript which has implicit length 1!
10156 ArraySizes.push_back(llvm::APSInt::get(1));
10157 } else {
10158 llvm::APSInt ConstantLengthValue;
10159 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10160 ConstantLengthValue.getSExtValue() != 1)
10161 return false;
10162
10163 ArraySizes.push_back(ConstantLengthValue);
10164 }
10165 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10166 }
10167
10168 // If we have a single element, we don't need to add the implicit lengths.
10169 if (!SingleElement) {
10170 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10171 // Has implicit length 1!
10172 ArraySizes.push_back(llvm::APSInt::get(1));
10173 Base = TempASE->getBase()->IgnoreParenImpCasts();
10174 }
10175 }
10176
10177 // This array section can be privatized as a single value or as a constant
10178 // sized array.
10179 return true;
10180}
10181
Alexey Bataeve3727102018-04-18 15:57:46 +000010182static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010183 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10184 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10185 SourceLocation ColonLoc, SourceLocation EndLoc,
10186 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010187 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010188 DeclarationName DN = ReductionId.getName();
10189 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010190 BinaryOperatorKind BOK = BO_Comma;
10191
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010192 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010193 // OpenMP [2.14.3.6, reduction clause]
10194 // C
10195 // reduction-identifier is either an identifier or one of the following
10196 // operators: +, -, *, &, |, ^, && and ||
10197 // C++
10198 // reduction-identifier is either an id-expression or one of the following
10199 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010200 switch (OOK) {
10201 case OO_Plus:
10202 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010203 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010204 break;
10205 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010206 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010207 break;
10208 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010209 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010210 break;
10211 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010212 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010213 break;
10214 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010215 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010216 break;
10217 case OO_AmpAmp:
10218 BOK = BO_LAnd;
10219 break;
10220 case OO_PipePipe:
10221 BOK = BO_LOr;
10222 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010223 case OO_New:
10224 case OO_Delete:
10225 case OO_Array_New:
10226 case OO_Array_Delete:
10227 case OO_Slash:
10228 case OO_Percent:
10229 case OO_Tilde:
10230 case OO_Exclaim:
10231 case OO_Equal:
10232 case OO_Less:
10233 case OO_Greater:
10234 case OO_LessEqual:
10235 case OO_GreaterEqual:
10236 case OO_PlusEqual:
10237 case OO_MinusEqual:
10238 case OO_StarEqual:
10239 case OO_SlashEqual:
10240 case OO_PercentEqual:
10241 case OO_CaretEqual:
10242 case OO_AmpEqual:
10243 case OO_PipeEqual:
10244 case OO_LessLess:
10245 case OO_GreaterGreater:
10246 case OO_LessLessEqual:
10247 case OO_GreaterGreaterEqual:
10248 case OO_EqualEqual:
10249 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010250 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010251 case OO_PlusPlus:
10252 case OO_MinusMinus:
10253 case OO_Comma:
10254 case OO_ArrowStar:
10255 case OO_Arrow:
10256 case OO_Call:
10257 case OO_Subscript:
10258 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010259 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010260 case NUM_OVERLOADED_OPERATORS:
10261 llvm_unreachable("Unexpected reduction identifier");
10262 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010263 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010264 if (II->isStr("max"))
10265 BOK = BO_GT;
10266 else if (II->isStr("min"))
10267 BOK = BO_LT;
10268 }
10269 break;
10270 }
10271 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010272 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010273 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010274 else
10275 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010276 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010277
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010278 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10279 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010280 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010281 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010282 // OpenMP [2.1, C/C++]
10283 // A list item is a variable or array section, subject to the restrictions
10284 // specified in Section 2.4 on page 42 and in each of the sections
10285 // describing clauses and directives for which a list appears.
10286 // OpenMP [2.14.3.3, Restrictions, p.1]
10287 // A variable that is part of another variable (as an array or
10288 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010289 if (!FirstIter && IR != ER)
10290 ++IR;
10291 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010292 SourceLocation ELoc;
10293 SourceRange ERange;
10294 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010295 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010296 /*AllowArraySection=*/true);
10297 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010298 // Try to find 'declare reduction' corresponding construct before using
10299 // builtin/overloaded operators.
10300 QualType Type = Context.DependentTy;
10301 CXXCastPath BasePath;
10302 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010303 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010304 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010305 Expr *ReductionOp = nullptr;
10306 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010307 (DeclareReductionRef.isUnset() ||
10308 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010309 ReductionOp = DeclareReductionRef.get();
10310 // It will be analyzed later.
10311 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010312 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010313 ValueDecl *D = Res.first;
10314 if (!D)
10315 continue;
10316
Alexey Bataev88202be2017-07-27 13:20:36 +000010317 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010318 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010319 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10320 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010321 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010322 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010323 } else if (OASE) {
10324 QualType BaseType =
10325 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10326 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010327 Type = ATy->getElementType();
10328 else
10329 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010330 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010331 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010332 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010333 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010334 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010335
Alexey Bataevc5e02582014-06-16 07:08:35 +000010336 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10337 // A variable that appears in a private clause must not have an incomplete
10338 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010339 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010340 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010341 continue;
10342 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010343 // A list item that appears in a reduction clause must not be
10344 // const-qualified.
10345 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010346 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010347 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010348 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10349 VarDecl::DeclarationOnly;
10350 S.Diag(D->getLocation(),
10351 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010352 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010353 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010354 continue;
10355 }
10356 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10357 // If a list-item is a reference type then it must bind to the same object
10358 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +000010359 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010360 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +000010361 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010362 DSARefChecker Check(Stack);
Alexey Bataeva1764212015-09-30 09:22:36 +000010363 if (Check.Visit(VDDef->getInit())) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010364 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10365 << getOpenMPClauseName(ClauseKind) << ERange;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010366 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
Alexey Bataeva1764212015-09-30 09:22:36 +000010367 continue;
10368 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010369 }
10370 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010371
Alexey Bataevc5e02582014-06-16 07:08:35 +000010372 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10373 // in a Construct]
10374 // Variables with the predetermined data-sharing attributes may not be
10375 // listed in data-sharing attributes clauses, except for the cases
10376 // listed below. For these exceptions only, listing a predetermined
10377 // variable in a data-sharing attribute clause is allowed and overrides
10378 // the variable's predetermined data-sharing attributes.
10379 // OpenMP [2.14.3.6, Restrictions, p.3]
10380 // Any number of reduction clauses can be specified on the directive,
10381 // but a list item can appear only once in the reduction clauses for that
10382 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +000010383 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010384 if (DVar.CKind == OMPC_reduction) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010385 S.Diag(ELoc, diag::err_omp_once_referenced)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010386 << getOpenMPClauseName(ClauseKind);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010387 if (DVar.RefExpr)
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010388 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010389 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000010390 }
10391 if (DVar.CKind != OMPC_unknown) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010392 S.Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010393 << getOpenMPClauseName(DVar.CKind)
10394 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010395 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010396 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010397 }
10398
10399 // OpenMP [2.14.3.6, Restrictions, p.1]
10400 // A list item that appears in a reduction clause of a worksharing
10401 // construct must be shared in the parallel regions to which any of the
10402 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010403 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010404 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010405 !isOpenMPParallelDirective(CurrDir) &&
10406 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010407 DVar = Stack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010408 if (DVar.CKind != OMPC_shared) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010409 S.Diag(ELoc, diag::err_omp_required_access)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010410 << getOpenMPClauseName(OMPC_reduction)
10411 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010412 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010413 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010414 }
10415 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010416
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010417 // Try to find 'declare reduction' corresponding construct before using
10418 // builtin/overloaded operators.
10419 CXXCastPath BasePath;
10420 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010421 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010422 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10423 if (DeclareReductionRef.isInvalid())
10424 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010425 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010426 (DeclareReductionRef.isUnset() ||
10427 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010428 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010429 continue;
10430 }
10431 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10432 // Not allowed reduction identifier is found.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010433 S.Diag(ReductionId.getLocStart(),
10434 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010435 << Type << ReductionIdRange;
10436 continue;
10437 }
10438
10439 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10440 // The type of a list item that appears in a reduction clause must be valid
10441 // for the reduction-identifier. For a max or min reduction in C, the type
10442 // of the list item must be an allowed arithmetic data type: char, int,
10443 // float, double, or _Bool, possibly modified with long, short, signed, or
10444 // unsigned. For a max or min reduction in C++, the type of the list item
10445 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10446 // double, or bool, possibly modified with long, short, signed, or unsigned.
10447 if (DeclareReductionRef.isUnset()) {
10448 if ((BOK == BO_GT || BOK == BO_LT) &&
10449 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010450 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10451 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010452 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010453 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010454 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10455 VarDecl::DeclarationOnly;
10456 S.Diag(D->getLocation(),
10457 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010458 << D;
10459 }
10460 continue;
10461 }
10462 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010463 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010464 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10465 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010466 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010467 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10468 VarDecl::DeclarationOnly;
10469 S.Diag(D->getLocation(),
10470 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010471 << D;
10472 }
10473 continue;
10474 }
10475 }
10476
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010477 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010478 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10479 D->hasAttrs() ? &D->getAttrs() : nullptr);
10480 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10481 D->hasAttrs() ? &D->getAttrs() : nullptr);
10482 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010483
10484 // Try if we can determine constant lengths for all array sections and avoid
10485 // the VLA.
10486 bool ConstantLengthOASE = false;
10487 if (OASE) {
10488 bool SingleElement;
10489 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010490 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010491 Context, OASE, SingleElement, ArraySizes);
10492
10493 // If we don't have a single element, we must emit a constant array type.
10494 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010495 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010496 PrivateTy = Context.getConstantArrayType(
10497 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010498 }
10499 }
10500
10501 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010502 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010503 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010504 if (!Context.getTargetInfo().isVLASupported() &&
10505 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10506 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10507 S.Diag(ELoc, diag::note_vla_unsupported);
10508 continue;
10509 }
David Majnemer9d168222016-08-05 17:44:54 +000010510 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010511 // Create pseudo array type for private copy. The size for this array will
10512 // be generated during codegen.
10513 // For array subscripts or single variables Private Ty is the same as Type
10514 // (type of the variable or single array element).
10515 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010516 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010517 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010518 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010519 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000010520 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010521 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010522 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010523 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000010524 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010525 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10526 D->hasAttrs() ? &D->getAttrs() : nullptr,
10527 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010528 // Add initializer for private variable.
10529 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010530 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10531 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010532 if (DeclareReductionRef.isUsable()) {
10533 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10534 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10535 if (DRD->getInitializer()) {
10536 Init = DRDRef;
10537 RHSVD->setInit(DRDRef);
10538 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010539 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010540 } else {
10541 switch (BOK) {
10542 case BO_Add:
10543 case BO_Xor:
10544 case BO_Or:
10545 case BO_LOr:
10546 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10547 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010548 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010549 break;
10550 case BO_Mul:
10551 case BO_LAnd:
10552 if (Type->isScalarType() || Type->isAnyComplexType()) {
10553 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010554 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010555 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010556 break;
10557 case BO_And: {
10558 // '&' reduction op - initializer is '~0'.
10559 QualType OrigType = Type;
10560 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10561 Type = ComplexTy->getElementType();
10562 if (Type->isRealFloatingType()) {
10563 llvm::APFloat InitValue =
10564 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10565 /*isIEEE=*/true);
10566 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10567 Type, ELoc);
10568 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010569 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010570 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10571 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10572 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10573 }
10574 if (Init && OrigType->isAnyComplexType()) {
10575 // Init = 0xFFFF + 0xFFFFi;
10576 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010577 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010578 }
10579 Type = OrigType;
10580 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010581 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010582 case BO_LT:
10583 case BO_GT: {
10584 // 'min' reduction op - initializer is 'Largest representable number in
10585 // the reduction list item type'.
10586 // 'max' reduction op - initializer is 'Least representable number in
10587 // the reduction list item type'.
10588 if (Type->isIntegerType() || Type->isPointerType()) {
10589 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000010590 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010591 QualType IntTy =
10592 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10593 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010594 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10595 : llvm::APInt::getMinValue(Size)
10596 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10597 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010598 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10599 if (Type->isPointerType()) {
10600 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010601 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010602 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010603 if (CastExpr.isInvalid())
10604 continue;
10605 Init = CastExpr.get();
10606 }
10607 } else if (Type->isRealFloatingType()) {
10608 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10609 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10610 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10611 Type, ELoc);
10612 }
10613 break;
10614 }
10615 case BO_PtrMemD:
10616 case BO_PtrMemI:
10617 case BO_MulAssign:
10618 case BO_Div:
10619 case BO_Rem:
10620 case BO_Sub:
10621 case BO_Shl:
10622 case BO_Shr:
10623 case BO_LE:
10624 case BO_GE:
10625 case BO_EQ:
10626 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010627 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010628 case BO_AndAssign:
10629 case BO_XorAssign:
10630 case BO_OrAssign:
10631 case BO_Assign:
10632 case BO_AddAssign:
10633 case BO_SubAssign:
10634 case BO_DivAssign:
10635 case BO_RemAssign:
10636 case BO_ShlAssign:
10637 case BO_ShrAssign:
10638 case BO_Comma:
10639 llvm_unreachable("Unexpected reduction operation");
10640 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010641 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010642 if (Init && DeclareReductionRef.isUnset())
10643 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10644 else if (!Init)
10645 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010646 if (RHSVD->isInvalidDecl())
10647 continue;
10648 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010649 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10650 << Type << ReductionIdRange;
10651 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10652 VarDecl::DeclarationOnly;
10653 S.Diag(D->getLocation(),
10654 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010655 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010656 continue;
10657 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010658 // Store initializer for single element in private copy. Will be used during
10659 // codegen.
10660 PrivateVD->setInit(RHSVD->getInit());
10661 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000010662 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010663 ExprResult ReductionOp;
10664 if (DeclareReductionRef.isUsable()) {
10665 QualType RedTy = DeclareReductionRef.get()->getType();
10666 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010667 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10668 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010669 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010670 LHS = S.DefaultLvalueConversion(LHS.get());
10671 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010672 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10673 CK_UncheckedDerivedToBase, LHS.get(),
10674 &BasePath, LHS.get()->getValueKind());
10675 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10676 CK_UncheckedDerivedToBase, RHS.get(),
10677 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010678 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010679 FunctionProtoType::ExtProtoInfo EPI;
10680 QualType Params[] = {PtrRedTy, PtrRedTy};
10681 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10682 auto *OVE = new (Context) OpaqueValueExpr(
10683 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010684 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010685 Expr *Args[] = {LHS.get(), RHS.get()};
10686 ReductionOp = new (Context)
10687 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10688 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010689 ReductionOp = S.BuildBinOp(
10690 Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010691 if (ReductionOp.isUsable()) {
10692 if (BOK != BO_LT && BOK != BO_GT) {
10693 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010694 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10695 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010696 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000010697 auto *ConditionalOp = new (Context)
10698 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10699 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010700 ReductionOp =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010701 S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10702 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010703 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010704 if (ReductionOp.isUsable())
10705 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010706 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010707 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010708 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010709 }
10710
Alexey Bataevfa312f32017-07-21 18:48:21 +000010711 // OpenMP [2.15.4.6, Restrictions, p.2]
10712 // A list item that appears in an in_reduction clause of a task construct
10713 // must appear in a task_reduction clause of a construct associated with a
10714 // taskgroup region that includes the participating task in its taskgroup
10715 // set. The construct associated with the innermost region that meets this
10716 // condition must specify the same reduction-identifier as the in_reduction
10717 // clause.
10718 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000010719 SourceRange ParentSR;
10720 BinaryOperatorKind ParentBOK;
10721 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000010722 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000010723 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010724 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10725 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010726 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010727 Stack->getTopMostTaskgroupReductionData(
10728 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010729 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10730 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10731 if (!IsParentBOK && !IsParentReductionOp) {
10732 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10733 continue;
10734 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000010735 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10736 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10737 IsParentReductionOp) {
10738 bool EmitError = true;
10739 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10740 llvm::FoldingSetNodeID RedId, ParentRedId;
10741 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10742 DeclareReductionRef.get()->Profile(RedId, Context,
10743 /*Canonical=*/true);
10744 EmitError = RedId != ParentRedId;
10745 }
10746 if (EmitError) {
10747 S.Diag(ReductionId.getLocStart(),
10748 diag::err_omp_reduction_identifier_mismatch)
10749 << ReductionIdRange << RefExpr->getSourceRange();
10750 S.Diag(ParentSR.getBegin(),
10751 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000010752 << ParentSR
10753 << (IsParentBOK ? ParentBOKDSA.RefExpr
10754 : ParentReductionOpDSA.RefExpr)
10755 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000010756 continue;
10757 }
10758 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010759 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10760 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000010761 }
10762
Alexey Bataev60da77e2016-02-29 05:54:20 +000010763 DeclRefExpr *Ref = nullptr;
10764 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010765 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010766 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010767 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010768 VarsExpr =
10769 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10770 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000010771 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010772 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010773 }
Alexey Bataeve3727102018-04-18 15:57:46 +000010774 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010775 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010776 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010777 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010778 if (!RefRes.isUsable())
10779 continue;
10780 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010781 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10782 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010783 if (!PostUpdateRes.isUsable())
10784 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010785 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10786 Stack->getCurrentDirective() == OMPD_taskgroup) {
10787 S.Diag(RefExpr->getExprLoc(),
10788 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000010789 << RefExpr->getSourceRange();
10790 continue;
10791 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010792 RD.ExprPostUpdates.emplace_back(
10793 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000010794 }
10795 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010796 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000010797 // All reduction items are still marked as reduction (to do not increase
10798 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010799 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010800 if (CurrDir == OMPD_taskgroup) {
10801 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010802 Stack->addTaskgroupReductionData(D, ReductionIdRange,
10803 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000010804 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010805 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010806 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010807 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10808 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010809 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010810 return RD.Vars.empty();
10811}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010812
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010813OMPClause *Sema::ActOnOpenMPReductionClause(
10814 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10815 SourceLocation ColonLoc, SourceLocation EndLoc,
10816 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10817 ArrayRef<Expr *> UnresolvedReductions) {
10818 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010819 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000010820 StartLoc, LParenLoc, ColonLoc, EndLoc,
10821 ReductionIdScopeSpec, ReductionId,
10822 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010823 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000010824
Alexey Bataevc5e02582014-06-16 07:08:35 +000010825 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010826 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10827 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10828 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10829 buildPreInits(Context, RD.ExprCaptures),
10830 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000010831}
10832
Alexey Bataev169d96a2017-07-18 20:17:46 +000010833OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10834 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10835 SourceLocation ColonLoc, SourceLocation EndLoc,
10836 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10837 ArrayRef<Expr *> UnresolvedReductions) {
10838 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010839 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
10840 StartLoc, LParenLoc, ColonLoc, EndLoc,
10841 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000010842 UnresolvedReductions, RD))
10843 return nullptr;
10844
10845 return OMPTaskReductionClause::Create(
10846 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10847 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10848 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10849 buildPreInits(Context, RD.ExprCaptures),
10850 buildPostUpdate(*this, RD.ExprPostUpdates));
10851}
10852
Alexey Bataevfa312f32017-07-21 18:48:21 +000010853OMPClause *Sema::ActOnOpenMPInReductionClause(
10854 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10855 SourceLocation ColonLoc, SourceLocation EndLoc,
10856 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10857 ArrayRef<Expr *> UnresolvedReductions) {
10858 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010859 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010860 StartLoc, LParenLoc, ColonLoc, EndLoc,
10861 ReductionIdScopeSpec, ReductionId,
10862 UnresolvedReductions, RD))
10863 return nullptr;
10864
10865 return OMPInReductionClause::Create(
10866 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10867 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000010868 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010869 buildPreInits(Context, RD.ExprCaptures),
10870 buildPostUpdate(*this, RD.ExprPostUpdates));
10871}
10872
Alexey Bataevecba70f2016-04-12 11:02:11 +000010873bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10874 SourceLocation LinLoc) {
10875 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10876 LinKind == OMPC_LINEAR_unknown) {
10877 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10878 return true;
10879 }
10880 return false;
10881}
10882
Alexey Bataeve3727102018-04-18 15:57:46 +000010883bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000010884 OpenMPLinearClauseKind LinKind,
10885 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010886 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000010887 // A variable must not have an incomplete type or a reference type.
10888 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10889 return true;
10890 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10891 !Type->isReferenceType()) {
10892 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10893 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10894 return true;
10895 }
10896 Type = Type.getNonReferenceType();
10897
10898 // A list item must not be const-qualified.
10899 if (Type.isConstant(Context)) {
10900 Diag(ELoc, diag::err_omp_const_variable)
10901 << getOpenMPClauseName(OMPC_linear);
10902 if (D) {
10903 bool IsDecl =
10904 !VD ||
10905 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10906 Diag(D->getLocation(),
10907 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10908 << D;
10909 }
10910 return true;
10911 }
10912
10913 // A list item must be of integral or pointer type.
10914 Type = Type.getUnqualifiedType().getCanonicalType();
10915 const auto *Ty = Type.getTypePtrOrNull();
10916 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10917 !Ty->isPointerType())) {
10918 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10919 if (D) {
10920 bool IsDecl =
10921 !VD ||
10922 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10923 Diag(D->getLocation(),
10924 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10925 << D;
10926 }
10927 return true;
10928 }
10929 return false;
10930}
10931
Alexey Bataev182227b2015-08-20 10:54:39 +000010932OMPClause *Sema::ActOnOpenMPLinearClause(
10933 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10934 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10935 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010936 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010937 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000010938 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010939 SmallVector<Decl *, 4> ExprCaptures;
10940 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010941 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000010942 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000010943 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010944 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010945 SourceLocation ELoc;
10946 SourceRange ERange;
10947 Expr *SimpleRefExpr = RefExpr;
10948 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10949 /*AllowArraySection=*/false);
10950 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000010951 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010952 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010953 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000010954 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000010955 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010956 ValueDecl *D = Res.first;
10957 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000010958 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000010959
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010960 QualType Type = D->getType();
10961 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000010962
10963 // OpenMP [2.14.3.7, linear clause]
10964 // A list-item cannot appear in more than one linear clause.
10965 // A list-item that appears in a linear clause cannot appear in any
10966 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000010967 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000010968 if (DVar.RefExpr) {
10969 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10970 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000010971 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000010972 continue;
10973 }
10974
Alexey Bataevecba70f2016-04-12 11:02:11 +000010975 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000010976 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000010977 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000010978
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010979 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000010980 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010981 buildVarDecl(*this, ELoc, Type, D->getName(),
10982 D->hasAttrs() ? &D->getAttrs() : nullptr,
10983 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010984 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000010985 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010986 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000010987 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010988 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010989 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000010990 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010991 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000010992 ExprCaptures.push_back(Ref->getDecl());
10993 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10994 ExprResult RefRes = DefaultLvalueConversion(Ref);
10995 if (!RefRes.isUsable())
10996 continue;
10997 ExprResult PostUpdateRes =
10998 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10999 SimpleRefExpr, RefRes.get());
11000 if (!PostUpdateRes.isUsable())
11001 continue;
11002 ExprPostUpdates.push_back(
11003 IgnoredValueConversions(PostUpdateRes.get()).get());
11004 }
11005 }
11006 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011007 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011008 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011009 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011010 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011011 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011012 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011013 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011014
11015 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011016 Vars.push_back((VD || CurContext->isDependentContext())
11017 ? RefExpr->IgnoreParens()
11018 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011019 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011020 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011021 }
11022
11023 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011024 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011025
11026 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011027 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011028 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11029 !Step->isInstantiationDependent() &&
11030 !Step->containsUnexpandedParameterPack()) {
11031 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011032 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011033 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011034 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011035 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011036
Alexander Musman3276a272015-03-21 10:12:56 +000011037 // Build var to save the step value.
11038 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011039 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011040 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011041 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011042 ExprResult CalcStep =
11043 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011044 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011045
Alexander Musman8dba6642014-04-22 13:09:42 +000011046 // Warn about zero linear step (it would be probably better specified as
11047 // making corresponding variables 'const').
11048 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011049 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11050 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011051 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11052 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011053 if (!IsConstant && CalcStep.isUsable()) {
11054 // Calculate the step beforehand instead of doing this on each iteration.
11055 // (This is not used if the number of iterations may be kfold-ed).
11056 CalcStepExpr = CalcStep.get();
11057 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011058 }
11059
Alexey Bataev182227b2015-08-20 10:54:39 +000011060 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11061 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011062 StepExpr, CalcStepExpr,
11063 buildPreInits(Context, ExprCaptures),
11064 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011065}
11066
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011067static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11068 Expr *NumIterations, Sema &SemaRef,
11069 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011070 // Walk the vars and build update/final expressions for the CodeGen.
11071 SmallVector<Expr *, 8> Updates;
11072 SmallVector<Expr *, 8> Finals;
11073 Expr *Step = Clause.getStep();
11074 Expr *CalcStep = Clause.getCalcStep();
11075 // OpenMP [2.14.3.7, linear clause]
11076 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011077 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011078 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011079 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011080 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11081 bool HasErrors = false;
11082 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011083 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011084 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11085 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011086 SourceLocation ELoc;
11087 SourceRange ERange;
11088 Expr *SimpleRefExpr = RefExpr;
11089 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
11090 /*AllowArraySection=*/false);
11091 ValueDecl *D = Res.first;
11092 if (Res.second || !D) {
11093 Updates.push_back(nullptr);
11094 Finals.push_back(nullptr);
11095 HasErrors = true;
11096 continue;
11097 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011098 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011099 // OpenMP [2.15.11, distribute simd Construct]
11100 // A list item may not appear in a linear clause, unless it is the loop
11101 // iteration variable.
11102 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11103 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11104 SemaRef.Diag(ELoc,
11105 diag::err_omp_linear_distribute_var_non_loop_iteration);
11106 Updates.push_back(nullptr);
11107 Finals.push_back(nullptr);
11108 HasErrors = true;
11109 continue;
11110 }
Alexander Musman3276a272015-03-21 10:12:56 +000011111 Expr *InitExpr = *CurInit;
11112
11113 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011114 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011115 Expr *CapturedRef;
11116 if (LinKind == OMPC_LINEAR_uval)
11117 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11118 else
11119 CapturedRef =
11120 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11121 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11122 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011123
11124 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011125 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011126 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011127 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011128 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011129 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011130 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011131 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011132 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
11133 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011134
11135 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011136 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011137 if (!Info.first)
11138 Final =
11139 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11140 InitExpr, NumIterations, Step, /*Subtract=*/false);
11141 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011142 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011143 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
11144 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011145
Alexander Musman3276a272015-03-21 10:12:56 +000011146 if (!Update.isUsable() || !Final.isUsable()) {
11147 Updates.push_back(nullptr);
11148 Finals.push_back(nullptr);
11149 HasErrors = true;
11150 } else {
11151 Updates.push_back(Update.get());
11152 Finals.push_back(Final.get());
11153 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011154 ++CurInit;
11155 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011156 }
11157 Clause.setUpdates(Updates);
11158 Clause.setFinals(Finals);
11159 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011160}
11161
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011162OMPClause *Sema::ActOnOpenMPAlignedClause(
11163 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11164 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011165 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011166 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011167 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11168 SourceLocation ELoc;
11169 SourceRange ERange;
11170 Expr *SimpleRefExpr = RefExpr;
11171 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11172 /*AllowArraySection=*/false);
11173 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011174 // It will be analyzed later.
11175 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011176 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011177 ValueDecl *D = Res.first;
11178 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011179 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011180
Alexey Bataev1efd1662016-03-29 10:59:56 +000011181 QualType QType = D->getType();
11182 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011183
11184 // OpenMP [2.8.1, simd construct, Restrictions]
11185 // The type of list items appearing in the aligned clause must be
11186 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011187 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011188 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011189 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011190 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011191 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011192 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011193 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011194 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011195 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011196 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011197 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011198 continue;
11199 }
11200
11201 // OpenMP [2.8.1, simd construct, Restrictions]
11202 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011203 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011204 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011205 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11206 << getOpenMPClauseName(OMPC_aligned);
11207 continue;
11208 }
11209
Alexey Bataev1efd1662016-03-29 10:59:56 +000011210 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011211 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011212 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11213 Vars.push_back(DefaultFunctionArrayConversion(
11214 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11215 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011216 }
11217
11218 // OpenMP [2.8.1, simd construct, Description]
11219 // The parameter of the aligned clause, alignment, must be a constant
11220 // positive integer expression.
11221 // If no optional parameter is specified, implementation-defined default
11222 // alignments for SIMD instructions on the target platforms are assumed.
11223 if (Alignment != nullptr) {
11224 ExprResult AlignResult =
11225 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11226 if (AlignResult.isInvalid())
11227 return nullptr;
11228 Alignment = AlignResult.get();
11229 }
11230 if (Vars.empty())
11231 return nullptr;
11232
11233 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11234 EndLoc, Vars, Alignment);
11235}
11236
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011237OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11238 SourceLocation StartLoc,
11239 SourceLocation LParenLoc,
11240 SourceLocation EndLoc) {
11241 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011242 SmallVector<Expr *, 8> SrcExprs;
11243 SmallVector<Expr *, 8> DstExprs;
11244 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011245 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011246 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11247 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011248 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011249 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011250 SrcExprs.push_back(nullptr);
11251 DstExprs.push_back(nullptr);
11252 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011253 continue;
11254 }
11255
Alexey Bataeved09d242014-05-28 05:53:51 +000011256 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011257 // OpenMP [2.1, C/C++]
11258 // A list item is a variable name.
11259 // OpenMP [2.14.4.1, Restrictions, p.1]
11260 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011261 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011262 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011263 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11264 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011265 continue;
11266 }
11267
11268 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011269 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011270
11271 QualType Type = VD->getType();
11272 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11273 // It will be analyzed later.
11274 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011275 SrcExprs.push_back(nullptr);
11276 DstExprs.push_back(nullptr);
11277 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011278 continue;
11279 }
11280
11281 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11282 // A list item that appears in a copyin clause must be threadprivate.
11283 if (!DSAStack->isThreadPrivate(VD)) {
11284 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011285 << getOpenMPClauseName(OMPC_copyin)
11286 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011287 continue;
11288 }
11289
11290 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11291 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011292 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011293 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011294 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11295 VarDecl *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011296 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
11297 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011298 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011299 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011300 VarDecl *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011301 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
11302 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011303 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011304 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011305 // For arrays generate assignment operation for single element and replace
11306 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011307 ExprResult AssignmentOp =
11308 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11309 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011310 if (AssignmentOp.isInvalid())
11311 continue;
11312 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11313 /*DiscardedValue=*/true);
11314 if (AssignmentOp.isInvalid())
11315 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011316
11317 DSAStack->addDSA(VD, DE, OMPC_copyin);
11318 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011319 SrcExprs.push_back(PseudoSrcExpr);
11320 DstExprs.push_back(PseudoDstExpr);
11321 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011322 }
11323
Alexey Bataeved09d242014-05-28 05:53:51 +000011324 if (Vars.empty())
11325 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011326
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011327 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11328 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011329}
11330
Alexey Bataevbae9a792014-06-27 10:37:06 +000011331OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11332 SourceLocation StartLoc,
11333 SourceLocation LParenLoc,
11334 SourceLocation EndLoc) {
11335 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011336 SmallVector<Expr *, 8> SrcExprs;
11337 SmallVector<Expr *, 8> DstExprs;
11338 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011339 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011340 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11341 SourceLocation ELoc;
11342 SourceRange ERange;
11343 Expr *SimpleRefExpr = RefExpr;
11344 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11345 /*AllowArraySection=*/false);
11346 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011347 // It will be analyzed later.
11348 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011349 SrcExprs.push_back(nullptr);
11350 DstExprs.push_back(nullptr);
11351 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011352 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011353 ValueDecl *D = Res.first;
11354 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011355 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011356
Alexey Bataeve122da12016-03-17 10:50:17 +000011357 QualType Type = D->getType();
11358 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011359
11360 // OpenMP [2.14.4.2, Restrictions, p.2]
11361 // A list item that appears in a copyprivate clause may not appear in a
11362 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011363 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011364 DSAStackTy::DSAVarData DVar =
11365 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011366 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11367 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011368 Diag(ELoc, diag::err_omp_wrong_dsa)
11369 << getOpenMPClauseName(DVar.CKind)
11370 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011371 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011372 continue;
11373 }
11374
11375 // OpenMP [2.11.4.2, Restrictions, p.1]
11376 // All list items that appear in a copyprivate clause must be either
11377 // threadprivate or private in the enclosing context.
11378 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011379 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011380 if (DVar.CKind == OMPC_shared) {
11381 Diag(ELoc, diag::err_omp_required_access)
11382 << getOpenMPClauseName(OMPC_copyprivate)
11383 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011384 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011385 continue;
11386 }
11387 }
11388 }
11389
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011390 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011391 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011392 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011393 << getOpenMPClauseName(OMPC_copyprivate) << Type
11394 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011395 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011396 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011397 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011398 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011399 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011400 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011401 continue;
11402 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011403
Alexey Bataevbae9a792014-06-27 10:37:06 +000011404 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11405 // A variable of class type (or array thereof) that appears in a
11406 // copyin clause requires an accessible, unambiguous copy assignment
11407 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011408 Type = Context.getBaseElementType(Type.getNonReferenceType())
11409 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011410 VarDecl *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011411 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
11412 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011413 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11414 VarDecl *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000011415 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
11416 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011417 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11418 ExprResult AssignmentOp = BuildBinOp(
11419 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011420 if (AssignmentOp.isInvalid())
11421 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011422 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011423 /*DiscardedValue=*/true);
11424 if (AssignmentOp.isInvalid())
11425 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011426
11427 // No need to mark vars as copyprivate, they are already threadprivate or
11428 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011429 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011430 Vars.push_back(
11431 VD ? RefExpr->IgnoreParens()
11432 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011433 SrcExprs.push_back(PseudoSrcExpr);
11434 DstExprs.push_back(PseudoDstExpr);
11435 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011436 }
11437
11438 if (Vars.empty())
11439 return nullptr;
11440
Alexey Bataeva63048e2015-03-23 06:18:07 +000011441 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11442 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011443}
11444
Alexey Bataev6125da92014-07-21 11:26:11 +000011445OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11446 SourceLocation StartLoc,
11447 SourceLocation LParenLoc,
11448 SourceLocation EndLoc) {
11449 if (VarList.empty())
11450 return nullptr;
11451
11452 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11453}
Alexey Bataevdea47612014-07-23 07:46:59 +000011454
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011455OMPClause *
11456Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11457 SourceLocation DepLoc, SourceLocation ColonLoc,
11458 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11459 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011460 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011461 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011462 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011463 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011464 return nullptr;
11465 }
11466 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011467 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11468 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011469 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011470 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011471 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11472 /*Last=*/OMPC_DEPEND_unknown, Except)
11473 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011474 return nullptr;
11475 }
11476 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011477 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011478 llvm::APSInt DepCounter(/*BitWidth=*/32);
11479 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
11480 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011481 if (const Expr *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011482 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11483 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011484 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011485 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011486 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011487 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11488 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11489 // It will be analyzed later.
11490 Vars.push_back(RefExpr);
11491 continue;
11492 }
11493
11494 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011495 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011496 if (DepKind == OMPC_DEPEND_sink) {
11497 if (DSAStack->getParentOrderedRegionParam() &&
11498 DepCounter >= TotalDepCount) {
11499 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11500 continue;
11501 }
11502 ++DepCounter;
11503 // OpenMP [2.13.9, Summary]
11504 // depend(dependence-type : vec), where dependence-type is:
11505 // 'sink' and where vec is the iteration vector, which has the form:
11506 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11507 // where n is the value specified by the ordered clause in the loop
11508 // directive, xi denotes the loop iteration variable of the i-th nested
11509 // loop associated with the loop directive, and di is a constant
11510 // non-negative integer.
11511 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011512 // It will be analyzed later.
11513 Vars.push_back(RefExpr);
11514 continue;
11515 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011516 SimpleExpr = SimpleExpr->IgnoreImplicit();
11517 OverloadedOperatorKind OOK = OO_None;
11518 SourceLocation OOLoc;
11519 Expr *LHS = SimpleExpr;
11520 Expr *RHS = nullptr;
11521 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11522 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11523 OOLoc = BO->getOperatorLoc();
11524 LHS = BO->getLHS()->IgnoreParenImpCasts();
11525 RHS = BO->getRHS()->IgnoreParenImpCasts();
11526 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11527 OOK = OCE->getOperator();
11528 OOLoc = OCE->getOperatorLoc();
11529 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11530 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11531 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11532 OOK = MCE->getMethodDecl()
11533 ->getNameInfo()
11534 .getName()
11535 .getCXXOverloadedOperator();
11536 OOLoc = MCE->getCallee()->getExprLoc();
11537 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11538 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011539 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011540 SourceLocation ELoc;
11541 SourceRange ERange;
11542 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11543 /*AllowArraySection=*/false);
11544 if (Res.second) {
11545 // It will be analyzed later.
11546 Vars.push_back(RefExpr);
11547 }
11548 ValueDecl *D = Res.first;
11549 if (!D)
11550 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011551
Alexey Bataev17daedf2018-02-15 22:42:57 +000011552 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11553 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11554 continue;
11555 }
11556 if (RHS) {
11557 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11558 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11559 if (RHSRes.isInvalid())
11560 continue;
11561 }
11562 if (!CurContext->isDependentContext() &&
11563 DSAStack->getParentOrderedRegionParam() &&
11564 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011565 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000011566 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000011567 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000011568 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11569 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000011570 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000011571 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000011572 continue;
11573 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011574 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011575 } else {
11576 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11577 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11578 (ASE &&
11579 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11580 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11581 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11582 << RefExpr->getSourceRange();
11583 continue;
11584 }
11585 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11586 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11587 ExprResult Res =
11588 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11589 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11590 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11591 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11592 << RefExpr->getSourceRange();
11593 continue;
11594 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011595 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011596 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011597 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011598
11599 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11600 TotalDepCount > VarList.size() &&
11601 DSAStack->getParentOrderedRegionParam() &&
11602 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11603 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11604 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11605 }
11606 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11607 Vars.empty())
11608 return nullptr;
11609
Alexey Bataev8b427062016-05-25 12:36:08 +000011610 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11611 DepKind, DepLoc, ColonLoc, Vars);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011612 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11613 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000011614 DSAStack->addDoacrossDependClause(C, OpsOffs);
11615 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011616}
Michael Wonge710d542015-08-07 16:16:36 +000011617
11618OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11619 SourceLocation LParenLoc,
11620 SourceLocation EndLoc) {
11621 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011622 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011623
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011624 // OpenMP [2.9.1, Restrictions]
11625 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000011626 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000011627 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011628 return nullptr;
11629
Alexey Bataev931e19b2017-10-02 16:32:39 +000011630 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011631 OpenMPDirectiveKind CaptureRegion =
11632 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11633 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011634 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011635 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011636 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11637 HelperValStmt = buildPreInits(Context, Captures);
11638 }
11639
Alexey Bataev8451efa2018-01-15 19:06:12 +000011640 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11641 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011642}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011643
Alexey Bataeve3727102018-04-18 15:57:46 +000011644static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000011645 DSAStackTy *Stack, QualType QTy,
11646 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000011647 NamedDecl *ND;
11648 if (QTy->isIncompleteType(&ND)) {
11649 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11650 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011651 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000011652 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11653 !QTy.isTrivialType(SemaRef.Context))
11654 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011655 return true;
11656}
11657
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011658/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011659/// (array section or array subscript) does NOT specify the whole size of the
11660/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011661static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011662 const Expr *E,
11663 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011664 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011665
11666 // If this is an array subscript, it refers to the whole size if the size of
11667 // the dimension is constant and equals 1. Also, an array section assumes the
11668 // format of an array subscript if no colon is used.
11669 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011670 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011671 return ATy->getSize().getSExtValue() != 1;
11672 // Size can't be evaluated statically.
11673 return false;
11674 }
11675
11676 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000011677 const Expr *LowerBound = OASE->getLowerBound();
11678 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011679
11680 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000011681 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011682 if (LowerBound) {
11683 llvm::APSInt ConstLowerBound;
11684 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11685 return false; // Can't get the integer value as a constant.
11686 if (ConstLowerBound.getSExtValue())
11687 return true;
11688 }
11689
11690 // If we don't have a length we covering the whole dimension.
11691 if (!Length)
11692 return false;
11693
11694 // If the base is a pointer, we don't have a way to get the size of the
11695 // pointee.
11696 if (BaseQTy->isPointerType())
11697 return false;
11698
11699 // We can only check if the length is the same as the size of the dimension
11700 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000011701 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011702 if (!CATy)
11703 return false;
11704
11705 llvm::APSInt ConstLength;
11706 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11707 return false; // Can't get the integer value as a constant.
11708
11709 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11710}
11711
11712// Return true if it can be proven that the provided array expression (array
11713// section or array subscript) does NOT specify a single element of the array
11714// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011715static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000011716 const Expr *E,
11717 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011718 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011719
11720 // An array subscript always refer to a single element. Also, an array section
11721 // assumes the format of an array subscript if no colon is used.
11722 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11723 return false;
11724
11725 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000011726 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011727
11728 // If we don't have a length we have to check if the array has unitary size
11729 // for this dimension. Also, we should always expect a length if the base type
11730 // is pointer.
11731 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011732 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011733 return ATy->getSize().getSExtValue() != 1;
11734 // We cannot assume anything.
11735 return false;
11736 }
11737
11738 // Check if the length evaluates to 1.
11739 llvm::APSInt ConstLength;
11740 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11741 return false; // Can't get the integer value as a constant.
11742
11743 return ConstLength.getSExtValue() != 1;
11744}
11745
Samuel Antao661c0902016-05-26 17:39:58 +000011746// Return the expression of the base of the mappable expression or null if it
11747// cannot be determined and do all the necessary checks to see if the expression
11748// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000011749// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000011750static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000011751 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000011752 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011753 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011754 SourceLocation ELoc = E->getExprLoc();
11755 SourceRange ERange = E->getSourceRange();
11756
11757 // The base of elements of list in a map clause have to be either:
11758 // - a reference to variable or field.
11759 // - a member expression.
11760 // - an array expression.
11761 //
11762 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11763 // reference to 'r'.
11764 //
11765 // If we have:
11766 //
11767 // struct SS {
11768 // Bla S;
11769 // foo() {
11770 // #pragma omp target map (S.Arr[:12]);
11771 // }
11772 // }
11773 //
11774 // We want to retrieve the member expression 'this->S';
11775
Alexey Bataeve3727102018-04-18 15:57:46 +000011776 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011777
Samuel Antao5de996e2016-01-22 20:21:36 +000011778 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11779 // If a list item is an array section, it must specify contiguous storage.
11780 //
11781 // For this restriction it is sufficient that we make sure only references
11782 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011783 // exist except in the rightmost expression (unless they cover the whole
11784 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000011785 //
11786 // r.ArrS[3:5].Arr[6:7]
11787 //
11788 // r.ArrS[3:5].x
11789 //
11790 // but these would be valid:
11791 // r.ArrS[3].Arr[6:7]
11792 //
11793 // r.ArrS[3].x
11794
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011795 bool AllowUnitySizeArraySection = true;
11796 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000011797
Dmitry Polukhin644a9252016-03-11 07:58:34 +000011798 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011799 E = E->IgnoreParenImpCasts();
11800
11801 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11802 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000011803 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011804
11805 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011806
11807 // If we got a reference to a declaration, we should not expect any array
11808 // section before that.
11809 AllowUnitySizeArraySection = false;
11810 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011811
11812 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011813 CurComponents.emplace_back(CurE, CurE->getDecl());
11814 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011815 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000011816
11817 if (isa<CXXThisExpr>(BaseE))
11818 // We found a base expression: this->Val.
11819 RelevantExpr = CurE;
11820 else
11821 E = BaseE;
11822
11823 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011824 if (!NoDiagnose) {
11825 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11826 << CurE->getSourceRange();
11827 return nullptr;
11828 }
11829 if (RelevantExpr)
11830 return nullptr;
11831 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011832 }
11833
11834 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11835
11836 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11837 // A bit-field cannot appear in a map clause.
11838 //
11839 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011840 if (!NoDiagnose) {
11841 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11842 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11843 return nullptr;
11844 }
11845 if (RelevantExpr)
11846 return nullptr;
11847 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011848 }
11849
11850 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11851 // If the type of a list item is a reference to a type T then the type
11852 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011853 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011854
11855 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11856 // A list item cannot be a variable that is a member of a structure with
11857 // a union type.
11858 //
Alexey Bataeve3727102018-04-18 15:57:46 +000011859 if (CurType->isUnionType()) {
11860 if (!NoDiagnose) {
11861 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11862 << CurE->getSourceRange();
11863 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011864 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011865 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011866 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011867
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011868 // If we got a member expression, we should not expect any array section
11869 // before that:
11870 //
11871 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11872 // If a list item is an element of a structure, only the rightmost symbol
11873 // of the variable reference can be an array section.
11874 //
11875 AllowUnitySizeArraySection = false;
11876 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011877
11878 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011879 CurComponents.emplace_back(CurE, FD);
11880 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011881 E = CurE->getBase()->IgnoreParenImpCasts();
11882
11883 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011884 if (!NoDiagnose) {
11885 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11886 << 0 << CurE->getSourceRange();
11887 return nullptr;
11888 }
11889 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011890 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011891
11892 // If we got an array subscript that express the whole dimension we
11893 // can have any array expressions before. If it only expressing part of
11894 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000011895 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011896 E->getType()))
11897 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011898
11899 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011900 CurComponents.emplace_back(CurE, nullptr);
11901 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011902 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000011903 E = CurE->getBase()->IgnoreParenImpCasts();
11904
Alexey Bataev27041fa2017-12-05 15:22:49 +000011905 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011906 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11907
Samuel Antao5de996e2016-01-22 20:21:36 +000011908 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11909 // If the type of a list item is a reference to a type T then the type
11910 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000011911 if (CurType->isReferenceType())
11912 CurType = CurType->getPointeeType();
11913
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011914 bool IsPointer = CurType->isAnyPointerType();
11915
11916 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011917 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11918 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011919 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011920 }
11921
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011922 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000011923 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011924 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000011925 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011926
Samuel Antaodab51bb2016-07-18 23:22:11 +000011927 if (AllowWholeSizeArraySection) {
11928 // Any array section is currently allowed. Allowing a whole size array
11929 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011930 //
11931 // If this array section refers to the whole dimension we can still
11932 // accept other array sections before this one, except if the base is a
11933 // pointer. Otherwise, only unitary sections are accepted.
11934 if (NotWhole || IsPointer)
11935 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000011936 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011937 // A unity or whole array section is not allowed and that is not
11938 // compatible with the properties of the current array section.
11939 SemaRef.Diag(
11940 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11941 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000011942 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011943 }
Samuel Antao90927002016-04-26 14:54:23 +000011944
11945 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011946 CurComponents.emplace_back(CurE, nullptr);
11947 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011948 if (!NoDiagnose) {
11949 // If nothing else worked, this is not a valid map clause expression.
11950 SemaRef.Diag(
11951 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
11952 << ERange;
11953 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000011954 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011955 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011956 }
11957
11958 return RelevantExpr;
11959}
11960
11961// Return true if expression E associated with value VD has conflicts with other
11962// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000011963static bool checkMapConflicts(
11964 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000011965 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000011966 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11967 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011968 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000011969 SourceLocation ELoc = E->getExprLoc();
11970 SourceRange ERange = E->getSourceRange();
11971
11972 // In order to easily check the conflicts we need to match each component of
11973 // the expression under test with the components of the expressions that are
11974 // already in the stack.
11975
Samuel Antao5de996e2016-01-22 20:21:36 +000011976 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011977 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011978 "Map clause expression with unexpected base!");
11979
11980 // Variables to help detecting enclosing problems in data environment nests.
11981 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000011982 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011983
Samuel Antao90927002016-04-26 14:54:23 +000011984 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11985 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000011986 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
11987 ERange, CKind, &EnclosingExpr,
11988 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
11989 StackComponents,
11990 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011991 assert(!StackComponents.empty() &&
11992 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000011993 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000011994 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000011995 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000011996
Samuel Antao90927002016-04-26 14:54:23 +000011997 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000011998 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000011999
Samuel Antao5de996e2016-01-22 20:21:36 +000012000 // Expressions must start from the same base. Here we detect at which
12001 // point both expressions diverge from each other and see if we can
12002 // detect if the memory referred to both expressions is contiguous and
12003 // do not overlap.
12004 auto CI = CurComponents.rbegin();
12005 auto CE = CurComponents.rend();
12006 auto SI = StackComponents.rbegin();
12007 auto SE = StackComponents.rend();
12008 for (; CI != CE && SI != SE; ++CI, ++SI) {
12009
12010 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12011 // At most one list item can be an array item derived from a given
12012 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012013 if (CurrentRegionOnly &&
12014 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12015 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12016 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12017 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12018 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012019 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012020 << CI->getAssociatedExpression()->getSourceRange();
12021 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12022 diag::note_used_here)
12023 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012024 return true;
12025 }
12026
12027 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012028 if (CI->getAssociatedExpression()->getStmtClass() !=
12029 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012030 break;
12031
12032 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012033 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012034 break;
12035 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012036 // Check if the extra components of the expressions in the enclosing
12037 // data environment are redundant for the current base declaration.
12038 // If they are, the maps completely overlap, which is legal.
12039 for (; SI != SE; ++SI) {
12040 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012041 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012042 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012043 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012044 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012045 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012046 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012047 Type =
12048 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12049 }
12050 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012051 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012052 SemaRef, SI->getAssociatedExpression(), Type))
12053 break;
12054 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012055
12056 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12057 // List items of map clauses in the same construct must not share
12058 // original storage.
12059 //
12060 // If the expressions are exactly the same or one is a subset of the
12061 // other, it means they are sharing storage.
12062 if (CI == CE && SI == SE) {
12063 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012064 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012065 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012066 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012067 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012068 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12069 << ERange;
12070 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012071 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12072 << RE->getSourceRange();
12073 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012074 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012075 // If we find the same expression in the enclosing data environment,
12076 // that is legal.
12077 IsEnclosedByDataEnvironmentExpr = true;
12078 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012079 }
12080
Samuel Antao90927002016-04-26 14:54:23 +000012081 QualType DerivedType =
12082 std::prev(CI)->getAssociatedDeclaration()->getType();
12083 SourceLocation DerivedLoc =
12084 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012085
12086 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12087 // If the type of a list item is a reference to a type T then the type
12088 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012089 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012090
12091 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12092 // A variable for which the type is pointer and an array section
12093 // derived from that variable must not appear as list items of map
12094 // clauses of the same construct.
12095 //
12096 // Also, cover one of the cases in:
12097 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12098 // If any part of the original storage of a list item has corresponding
12099 // storage in the device data environment, all of the original storage
12100 // must have corresponding storage in the device data environment.
12101 //
12102 if (DerivedType->isAnyPointerType()) {
12103 if (CI == CE || SI == SE) {
12104 SemaRef.Diag(
12105 DerivedLoc,
12106 diag::err_omp_pointer_mapped_along_with_derived_section)
12107 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012108 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12109 << RE->getSourceRange();
12110 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012111 }
12112 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012113 SI->getAssociatedExpression()->getStmtClass() ||
12114 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12115 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012116 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012117 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012118 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012119 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12120 << RE->getSourceRange();
12121 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012122 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012123 }
12124
12125 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12126 // List items of map clauses in the same construct must not share
12127 // original storage.
12128 //
12129 // An expression is a subset of the other.
12130 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012131 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012132 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012133 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012134 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012135 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12136 << ERange;
12137 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012138 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12139 << RE->getSourceRange();
12140 return true;
12141 }
12142
12143 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012144 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012145 if (!CurrentRegionOnly && SI != SE)
12146 EnclosingExpr = RE;
12147
12148 // The current expression is a subset of the expression in the data
12149 // environment.
12150 IsEnclosedByDataEnvironmentExpr |=
12151 (!CurrentRegionOnly && CI != CE && SI == SE);
12152
12153 return false;
12154 });
12155
12156 if (CurrentRegionOnly)
12157 return FoundError;
12158
12159 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12160 // If any part of the original storage of a list item has corresponding
12161 // storage in the device data environment, all of the original storage must
12162 // have corresponding storage in the device data environment.
12163 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12164 // If a list item is an element of a structure, and a different element of
12165 // the structure has a corresponding list item in the device data environment
12166 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012167 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012168 // data environment prior to the task encountering the construct.
12169 //
12170 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12171 SemaRef.Diag(ELoc,
12172 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12173 << ERange;
12174 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12175 << EnclosingExpr->getSourceRange();
12176 return true;
12177 }
12178
12179 return FoundError;
12180}
12181
Samuel Antao661c0902016-05-26 17:39:58 +000012182namespace {
12183// Utility struct that gathers all the related lists associated with a mappable
12184// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012185struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012186 // The list of expressions.
12187 ArrayRef<Expr *> VarList;
12188 // The list of processed expressions.
12189 SmallVector<Expr *, 16> ProcessedVarList;
12190 // The mappble components for each expression.
12191 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12192 // The base declaration of the variable.
12193 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12194
12195 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12196 // We have a list of components and base declarations for each entry in the
12197 // variable list.
12198 VarComponents.reserve(VarList.size());
12199 VarBaseDeclarations.reserve(VarList.size());
12200 }
12201};
12202}
12203
12204// Check the validity of the provided variable list for the provided clause kind
12205// \a CKind. In the check process the valid expressions, and mappable expression
12206// components and variables are extracted and used to fill \a Vars,
12207// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12208// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12209static void
12210checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12211 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12212 SourceLocation StartLoc,
12213 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12214 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012215 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12216 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012217 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012218
Samuel Antao90927002016-04-26 14:54:23 +000012219 // Keep track of the mappable components and base declarations in this clause.
12220 // Each entry in the list is going to have a list of components associated. We
12221 // record each set of the components so that we can build the clause later on.
12222 // In the end we should have the same amount of declarations and component
12223 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012224
Alexey Bataeve3727102018-04-18 15:57:46 +000012225 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012226 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012227 SourceLocation ELoc = RE->getExprLoc();
12228
Alexey Bataeve3727102018-04-18 15:57:46 +000012229 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012230
12231 if (VE->isValueDependent() || VE->isTypeDependent() ||
12232 VE->isInstantiationDependent() ||
12233 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012234 // We can only analyze this information once the missing information is
12235 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012236 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012237 continue;
12238 }
12239
Alexey Bataeve3727102018-04-18 15:57:46 +000012240 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012241
Samuel Antao5de996e2016-01-22 20:21:36 +000012242 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012243 SemaRef.Diag(ELoc,
12244 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012245 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012246 continue;
12247 }
12248
Samuel Antao90927002016-04-26 14:54:23 +000012249 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12250 ValueDecl *CurDeclaration = nullptr;
12251
12252 // Obtain the array or member expression bases if required. Also, fill the
12253 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012254 const Expr *BE = checkMapClauseExpressionBase(
12255 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012256 if (!BE)
12257 continue;
12258
Samuel Antao90927002016-04-26 14:54:23 +000012259 assert(!CurComponents.empty() &&
12260 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012261
Samuel Antao90927002016-04-26 14:54:23 +000012262 // For the following checks, we rely on the base declaration which is
12263 // expected to be associated with the last component. The declaration is
12264 // expected to be a variable or a field (if 'this' is being mapped).
12265 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12266 assert(CurDeclaration && "Null decl on map clause.");
12267 assert(
12268 CurDeclaration->isCanonicalDecl() &&
12269 "Expecting components to have associated only canonical declarations.");
12270
12271 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012272 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012273
12274 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012275 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012276
12277 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012278 // threadprivate variables cannot appear in a map clause.
12279 // OpenMP 4.5 [2.10.5, target update Construct]
12280 // threadprivate variables cannot appear in a from clause.
12281 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012282 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012283 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12284 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012285 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012286 continue;
12287 }
12288
Samuel Antao5de996e2016-01-22 20:21:36 +000012289 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12290 // A list item cannot appear in both a map clause and a data-sharing
12291 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012292
Samuel Antao5de996e2016-01-22 20:21:36 +000012293 // Check conflicts with other map clause expressions. We check the conflicts
12294 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012295 // environment, because the restrictions are different. We only have to
12296 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012297 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012298 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012299 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012300 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012301 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012302 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012303 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012304
Samuel Antao661c0902016-05-26 17:39:58 +000012305 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012306 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12307 // If the type of a list item is a reference to a type T then the type will
12308 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012309 auto I = llvm::find_if(
12310 CurComponents,
12311 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12312 return MC.getAssociatedDeclaration();
12313 });
12314 assert(I != CurComponents.end() && "Null decl on map clause.");
12315 QualType Type =
12316 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012317
Samuel Antao661c0902016-05-26 17:39:58 +000012318 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12319 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012320 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012321 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012322 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012323 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012324 continue;
12325
Samuel Antao661c0902016-05-26 17:39:58 +000012326 if (CKind == OMPC_map) {
12327 // target enter data
12328 // OpenMP [2.10.2, Restrictions, p. 99]
12329 // A map-type must be specified in all map clauses and must be either
12330 // to or alloc.
12331 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12332 if (DKind == OMPD_target_enter_data &&
12333 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12334 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12335 << (IsMapTypeImplicit ? 1 : 0)
12336 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12337 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012338 continue;
12339 }
Samuel Antao661c0902016-05-26 17:39:58 +000012340
12341 // target exit_data
12342 // OpenMP [2.10.3, Restrictions, p. 102]
12343 // A map-type must be specified in all map clauses and must be either
12344 // from, release, or delete.
12345 if (DKind == OMPD_target_exit_data &&
12346 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12347 MapType == OMPC_MAP_delete)) {
12348 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12349 << (IsMapTypeImplicit ? 1 : 0)
12350 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12351 << getOpenMPDirectiveName(DKind);
12352 continue;
12353 }
12354
12355 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12356 // A list item cannot appear in both a map clause and a data-sharing
12357 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012358 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12359 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012360 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012361 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012362 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012363 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012364 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012365 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012366 continue;
12367 }
12368 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012369 }
12370
Samuel Antao90927002016-04-26 14:54:23 +000012371 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012372 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012373
12374 // Store the components in the stack so that they can be used to check
12375 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012376 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12377 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012378
12379 // Save the components and declaration to create the clause. For purposes of
12380 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012381 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012382 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12383 MVLI.VarComponents.back().append(CurComponents.begin(),
12384 CurComponents.end());
12385 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12386 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012387 }
Samuel Antao661c0902016-05-26 17:39:58 +000012388}
12389
12390OMPClause *
12391Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12392 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12393 SourceLocation MapLoc, SourceLocation ColonLoc,
12394 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12395 SourceLocation LParenLoc, SourceLocation EndLoc) {
12396 MappableVarListInfo MVLI(VarList);
12397 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12398 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012399
Samuel Antao5de996e2016-01-22 20:21:36 +000012400 // We need to produce a map clause even if we don't have variables so that
12401 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012402 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12403 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12404 MVLI.VarComponents, MapTypeModifier, MapType,
12405 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012406}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012407
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012408QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12409 TypeResult ParsedType) {
12410 assert(ParsedType.isUsable());
12411
12412 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12413 if (ReductionType.isNull())
12414 return QualType();
12415
12416 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12417 // A type name in a declare reduction directive cannot be a function type, an
12418 // array type, a reference type, or a type qualified with const, volatile or
12419 // restrict.
12420 if (ReductionType.hasQualifiers()) {
12421 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12422 return QualType();
12423 }
12424
12425 if (ReductionType->isFunctionType()) {
12426 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12427 return QualType();
12428 }
12429 if (ReductionType->isReferenceType()) {
12430 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12431 return QualType();
12432 }
12433 if (ReductionType->isArrayType()) {
12434 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12435 return QualType();
12436 }
12437 return ReductionType;
12438}
12439
12440Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12441 Scope *S, DeclContext *DC, DeclarationName Name,
12442 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12443 AccessSpecifier AS, Decl *PrevDeclInScope) {
12444 SmallVector<Decl *, 8> Decls;
12445 Decls.reserve(ReductionTypes.size());
12446
12447 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012448 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012449 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12450 // A reduction-identifier may not be re-declared in the current scope for the
12451 // same type or for a type that is compatible according to the base language
12452 // rules.
12453 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12454 OMPDeclareReductionDecl *PrevDRD = nullptr;
12455 bool InCompoundScope = true;
12456 if (S != nullptr) {
12457 // Find previous declaration with the same name not referenced in other
12458 // declarations.
12459 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12460 InCompoundScope =
12461 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12462 LookupName(Lookup, S);
12463 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12464 /*AllowInlineNamespace=*/false);
12465 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012466 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012467 while (Filter.hasNext()) {
12468 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12469 if (InCompoundScope) {
12470 auto I = UsedAsPrevious.find(PrevDecl);
12471 if (I == UsedAsPrevious.end())
12472 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012473 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012474 UsedAsPrevious[D] = true;
12475 }
12476 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12477 PrevDecl->getLocation();
12478 }
12479 Filter.done();
12480 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012481 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012482 if (!PrevData.second) {
12483 PrevDRD = PrevData.first;
12484 break;
12485 }
12486 }
12487 }
12488 } else if (PrevDeclInScope != nullptr) {
12489 auto *PrevDRDInScope = PrevDRD =
12490 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12491 do {
12492 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12493 PrevDRDInScope->getLocation();
12494 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12495 } while (PrevDRDInScope != nullptr);
12496 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012497 for (const auto &TyData : ReductionTypes) {
12498 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012499 bool Invalid = false;
12500 if (I != PreviousRedeclTypes.end()) {
12501 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12502 << TyData.first;
12503 Diag(I->second, diag::note_previous_definition);
12504 Invalid = true;
12505 }
12506 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12507 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12508 Name, TyData.first, PrevDRD);
12509 DC->addDecl(DRD);
12510 DRD->setAccess(AS);
12511 Decls.push_back(DRD);
12512 if (Invalid)
12513 DRD->setInvalidDecl();
12514 else
12515 PrevDRD = DRD;
12516 }
12517
12518 return DeclGroupPtrTy::make(
12519 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12520}
12521
12522void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12523 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12524
12525 // Enter new function scope.
12526 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012527 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012528 getCurFunction()->setHasOMPDeclareReductionCombiner();
12529
12530 if (S != nullptr)
12531 PushDeclContext(S, DRD);
12532 else
12533 CurContext = DRD;
12534
Faisal Valid143a0c2017-04-01 21:30:49 +000012535 PushExpressionEvaluationContext(
12536 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012537
12538 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012539 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12540 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12541 // uses semantics of argument handles by value, but it should be passed by
12542 // reference. C lang does not support references, so pass all parameters as
12543 // pointers.
12544 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012545 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012546 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012547 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12548 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12549 // uses semantics of argument handles by value, but it should be passed by
12550 // reference. C lang does not support references, so pass all parameters as
12551 // pointers.
12552 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012553 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012554 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12555 if (S != nullptr) {
12556 PushOnScopeChains(OmpInParm, S);
12557 PushOnScopeChains(OmpOutParm, S);
12558 } else {
12559 DRD->addDecl(OmpInParm);
12560 DRD->addDecl(OmpOutParm);
12561 }
12562}
12563
12564void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12565 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12566 DiscardCleanupsInEvaluationContext();
12567 PopExpressionEvaluationContext();
12568
12569 PopDeclContext();
12570 PopFunctionScopeInfo();
12571
12572 if (Combiner != nullptr)
12573 DRD->setCombiner(Combiner);
12574 else
12575 DRD->setInvalidDecl();
12576}
12577
Alexey Bataev070f43a2017-09-06 14:49:58 +000012578VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012579 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12580
12581 // Enter new function scope.
12582 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012583 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012584
12585 if (S != nullptr)
12586 PushDeclContext(S, DRD);
12587 else
12588 CurContext = DRD;
12589
Faisal Valid143a0c2017-04-01 21:30:49 +000012590 PushExpressionEvaluationContext(
12591 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012592
12593 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012594 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12595 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12596 // uses semantics of argument handles by value, but it should be passed by
12597 // reference. C lang does not support references, so pass all parameters as
12598 // pointers.
12599 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012600 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012601 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012602 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12603 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12604 // uses semantics of argument handles by value, but it should be passed by
12605 // reference. C lang does not support references, so pass all parameters as
12606 // pointers.
12607 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012608 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012609 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012610 if (S != nullptr) {
12611 PushOnScopeChains(OmpPrivParm, S);
12612 PushOnScopeChains(OmpOrigParm, S);
12613 } else {
12614 DRD->addDecl(OmpPrivParm);
12615 DRD->addDecl(OmpOrigParm);
12616 }
Alexey Bataev070f43a2017-09-06 14:49:58 +000012617 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012618}
12619
Alexey Bataev070f43a2017-09-06 14:49:58 +000012620void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12621 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012622 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12623 DiscardCleanupsInEvaluationContext();
12624 PopExpressionEvaluationContext();
12625
12626 PopDeclContext();
12627 PopFunctionScopeInfo();
12628
Alexey Bataev070f43a2017-09-06 14:49:58 +000012629 if (Initializer != nullptr) {
12630 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12631 } else if (OmpPrivParm->hasInit()) {
12632 DRD->setInitializer(OmpPrivParm->getInit(),
12633 OmpPrivParm->isDirectInit()
12634 ? OMPDeclareReductionDecl::DirectInit
12635 : OMPDeclareReductionDecl::CopyInit);
12636 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012637 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000012638 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012639}
12640
12641Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12642 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012643 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012644 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012645 if (S)
12646 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
12647 /*AddToContext=*/false);
12648 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012649 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000012650 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012651 }
12652 return DeclReductions;
12653}
12654
David Majnemer9d168222016-08-05 17:44:54 +000012655OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000012656 SourceLocation StartLoc,
12657 SourceLocation LParenLoc,
12658 SourceLocation EndLoc) {
12659 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012660 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012661
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012662 // OpenMP [teams Constrcut, Restrictions]
12663 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012664 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000012665 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012666 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012667
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012668 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012669 OpenMPDirectiveKind CaptureRegion =
12670 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12671 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012672 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012673 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012674 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12675 HelperValStmt = buildPreInits(Context, Captures);
12676 }
12677
12678 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12679 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000012680}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012681
12682OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12683 SourceLocation StartLoc,
12684 SourceLocation LParenLoc,
12685 SourceLocation EndLoc) {
12686 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012687 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012688
12689 // OpenMP [teams Constrcut, Restrictions]
12690 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012691 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000012692 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012693 return nullptr;
12694
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012695 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012696 OpenMPDirectiveKind CaptureRegion =
12697 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12698 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012699 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012700 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012701 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12702 HelperValStmt = buildPreInits(Context, Captures);
12703 }
12704
12705 return new (Context) OMPThreadLimitClause(
12706 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012707}
Alexey Bataeva0569352015-12-01 10:17:31 +000012708
12709OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12710 SourceLocation StartLoc,
12711 SourceLocation LParenLoc,
12712 SourceLocation EndLoc) {
12713 Expr *ValExpr = Priority;
12714
12715 // OpenMP [2.9.1, task Constrcut]
12716 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012717 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000012718 /*StrictlyPositive=*/false))
12719 return nullptr;
12720
12721 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12722}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012723
12724OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12725 SourceLocation StartLoc,
12726 SourceLocation LParenLoc,
12727 SourceLocation EndLoc) {
12728 Expr *ValExpr = Grainsize;
12729
12730 // OpenMP [2.9.2, taskloop Constrcut]
12731 // The parameter of the grainsize clause must be a positive integer
12732 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012733 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012734 /*StrictlyPositive=*/true))
12735 return nullptr;
12736
12737 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12738}
Alexey Bataev382967a2015-12-08 12:06:20 +000012739
12740OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12741 SourceLocation StartLoc,
12742 SourceLocation LParenLoc,
12743 SourceLocation EndLoc) {
12744 Expr *ValExpr = NumTasks;
12745
12746 // OpenMP [2.9.2, taskloop Constrcut]
12747 // The parameter of the num_tasks clause must be a positive integer
12748 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012749 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000012750 /*StrictlyPositive=*/true))
12751 return nullptr;
12752
12753 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12754}
12755
Alexey Bataev28c75412015-12-15 08:19:24 +000012756OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12757 SourceLocation LParenLoc,
12758 SourceLocation EndLoc) {
12759 // OpenMP [2.13.2, critical construct, Description]
12760 // ... where hint-expression is an integer constant expression that evaluates
12761 // to a valid lock hint.
12762 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12763 if (HintExpr.isInvalid())
12764 return nullptr;
12765 return new (Context)
12766 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12767}
12768
Carlo Bertollib4adf552016-01-15 18:50:31 +000012769OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12770 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12771 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12772 SourceLocation EndLoc) {
12773 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12774 std::string Values;
12775 Values += "'";
12776 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12777 Values += "'";
12778 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12779 << Values << getOpenMPClauseName(OMPC_dist_schedule);
12780 return nullptr;
12781 }
12782 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000012783 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000012784 if (ChunkSize) {
12785 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12786 !ChunkSize->isInstantiationDependent() &&
12787 !ChunkSize->containsUnexpandedParameterPack()) {
12788 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
12789 ExprResult Val =
12790 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12791 if (Val.isInvalid())
12792 return nullptr;
12793
12794 ValExpr = Val.get();
12795
12796 // OpenMP [2.7.1, Restrictions]
12797 // chunk_size must be a loop invariant integer expression with a positive
12798 // value.
12799 llvm::APSInt Result;
12800 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12801 if (Result.isSigned() && !Result.isStrictlyPositive()) {
12802 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12803 << "dist_schedule" << ChunkSize->getSourceRange();
12804 return nullptr;
12805 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000012806 } else if (getOpenMPCaptureRegionForClause(
12807 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12808 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000012809 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012810 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012811 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000012812 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12813 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012814 }
12815 }
12816 }
12817
12818 return new (Context)
12819 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000012820 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012821}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012822
12823OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12824 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12825 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12826 SourceLocation KindLoc, SourceLocation EndLoc) {
12827 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000012828 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012829 std::string Value;
12830 SourceLocation Loc;
12831 Value += "'";
12832 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12833 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012834 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012835 Loc = MLoc;
12836 } else {
12837 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012838 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012839 Loc = KindLoc;
12840 }
12841 Value += "'";
12842 Diag(Loc, diag::err_omp_unexpected_clause_value)
12843 << Value << getOpenMPClauseName(OMPC_defaultmap);
12844 return nullptr;
12845 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000012846 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012847
12848 return new (Context)
12849 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12850}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012851
12852bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12853 DeclContext *CurLexicalContext = getCurLexicalContext();
12854 if (!CurLexicalContext->isFileContext() &&
12855 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000012856 !CurLexicalContext->isExternCXXContext() &&
12857 !isa<CXXRecordDecl>(CurLexicalContext) &&
12858 !isa<ClassTemplateDecl>(CurLexicalContext) &&
12859 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12860 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012861 Diag(Loc, diag::err_omp_region_not_file_context);
12862 return false;
12863 }
12864 if (IsInOpenMPDeclareTargetContext) {
12865 Diag(Loc, diag::err_omp_enclosed_declare_target);
12866 return false;
12867 }
12868
12869 IsInOpenMPDeclareTargetContext = true;
12870 return true;
12871}
12872
12873void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12874 assert(IsInOpenMPDeclareTargetContext &&
12875 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012876 IsInOpenMPDeclareTargetContext = false;
12877}
12878
David Majnemer9d168222016-08-05 17:44:54 +000012879void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12880 CXXScopeSpec &ScopeSpec,
12881 const DeclarationNameInfo &Id,
12882 OMPDeclareTargetDeclAttr::MapTypeTy MT,
12883 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012884 LookupResult Lookup(*this, Id, LookupOrdinaryName);
12885 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12886
12887 if (Lookup.isAmbiguous())
12888 return;
12889 Lookup.suppressDiagnostics();
12890
12891 if (!Lookup.isSingleResult()) {
12892 if (TypoCorrection Corrected =
12893 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12894 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12895 CTK_ErrorRecovery)) {
12896 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12897 << Id.getName());
12898 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12899 return;
12900 }
12901
12902 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
12903 return;
12904 }
12905
12906 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
12907 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
12908 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
12909 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012910 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012911 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012912 ND->addAttr(A);
12913 if (ASTMutationListener *ML = Context.getASTMutationListener())
12914 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000012915 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012916 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
12917 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
12918 << Id.getName();
12919 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012920 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012921 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000012922 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012923}
12924
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012925static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
12926 Sema &SemaRef, Decl *D) {
12927 if (!D)
12928 return;
Alexey Bataev8e39c342018-02-16 21:23:23 +000012929 const Decl *LD = nullptr;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012930 if (isa<TagDecl>(D)) {
12931 LD = cast<TagDecl>(D)->getDefinition();
12932 } else if (isa<VarDecl>(D)) {
12933 LD = cast<VarDecl>(D)->getDefinition();
12934
12935 // If this is an implicit variable that is legal and we do not need to do
12936 // anything.
12937 if (cast<VarDecl>(D)->isImplicit()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012938 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012939 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12940 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012941 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012942 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012943 return;
12944 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012945 } else if (const auto *F = dyn_cast<FunctionDecl>(D)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012946 const FunctionDecl *FD = nullptr;
Alexey Bataev8e39c342018-02-16 21:23:23 +000012947 if (cast<FunctionDecl>(D)->hasBody(FD)) {
12948 LD = FD;
12949 // If the definition is associated with the current declaration in the
12950 // target region (it can be e.g. a lambda) that is legal and we do not
12951 // need to do anything else.
12952 if (LD == D) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012953 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Alexey Bataev8e39c342018-02-16 21:23:23 +000012954 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12955 D->addAttr(A);
12956 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12957 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12958 return;
12959 }
12960 } else if (F->isFunctionTemplateSpecialization() &&
12961 F->getTemplateSpecializationKind() ==
12962 TSK_ImplicitInstantiation) {
12963 // Check if the function is implicitly instantiated from the template
12964 // defined in the declare target region.
12965 const FunctionTemplateDecl *FTD = F->getPrimaryTemplate();
12966 if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>())
12967 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012968 }
12969 }
12970 if (!LD)
12971 LD = D;
12972 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
Alexey Bataevd0df36a2018-04-30 18:28:08 +000012973 ((isa<VarDecl>(LD) && !isa<ParmVarDecl>(LD)) || isa<FunctionDecl>(LD))) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012974 // Outlined declaration is not declared target.
Alexey Bataevdcc815d2018-05-02 17:39:00 +000012975 if (!isa<FunctionDecl>(LD)) {
12976 if (LD->isOutOfLine()) {
12977 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12978 SemaRef.Diag(SL, diag::note_used_here) << SR;
12979 } else {
12980 const DeclContext *DC = LD->getDeclContext();
12981 while (DC &&
12982 (!isa<FunctionDecl>(DC) ||
12983 !cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()))
12984 DC = DC->getParent();
12985 if (DC)
12986 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012987
Alexey Bataevdcc815d2018-05-02 17:39:00 +000012988 // Is not declared in target context.
12989 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12990 SemaRef.Diag(SL, diag::note_used_here) << SR;
12991 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012992 }
12993 // Mark decl as declared target to prevent further diagnostic.
Alexey Bataeve3727102018-04-18 15:57:46 +000012994 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012995 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12996 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012997 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012998 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012999 }
13000}
13001
13002static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13003 Sema &SemaRef, DSAStackTy *Stack,
13004 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013005 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13006 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13007 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013008}
13009
Kelvin Li1ce87c72017-12-12 20:08:12 +000013010void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13011 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013012 if (!D || D->isInvalidDecl())
13013 return;
13014 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
13015 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013016 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013017 // Only global variables can be marked as declare target.
13018 if (VD->isLocalVarDeclOrParm())
13019 return;
13020 // 2.10.6: threadprivate variable cannot appear in a declare target
13021 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013022 if (DSAStack->isThreadPrivate(VD)) {
13023 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013024 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013025 return;
13026 }
13027 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013028 if (auto *VD = dyn_cast<ValueDecl>(D)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013029 // Problem if any with var declared with incomplete type will be reported
13030 // as normal, so no need to check it here.
13031 if ((E || !VD->getType()->isIncompleteType()) &&
13032 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
13033 // Mark decl as declared target to prevent further diagnostic.
Alexey Bataev8e39c342018-02-16 21:23:23 +000013034 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) ||
13035 isa<FunctionTemplateDecl>(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013036 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013037 Context, OMPDeclareTargetDeclAttr::MT_To);
13038 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013039 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013040 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013041 }
13042 return;
13043 }
13044 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013045 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013046 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13047 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13048 OMPDeclareTargetDeclAttr::MT_Link)) {
13049 assert(IdLoc.isValid() && "Source location is expected");
13050 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13051 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13052 return;
13053 }
13054 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013055 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) {
Alexey Bataev8e39c342018-02-16 21:23:23 +000013056 if (FTD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13057 (FTD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13058 OMPDeclareTargetDeclAttr::MT_Link)) {
13059 assert(IdLoc.isValid() && "Source location is expected");
13060 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13061 Diag(FTD->getLocation(), diag::note_defined_here) << FTD;
13062 return;
13063 }
13064 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013065 if (!E) {
13066 // Checking declaration inside declare target region.
13067 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
Alexey Bataev8e39c342018-02-16 21:23:23 +000013068 (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13069 isa<FunctionTemplateDecl>(D))) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013070 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013071 Context, OMPDeclareTargetDeclAttr::MT_To);
13072 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013073 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013074 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013075 }
13076 return;
13077 }
13078 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13079}
Samuel Antao661c0902016-05-26 17:39:58 +000013080
13081OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13082 SourceLocation StartLoc,
13083 SourceLocation LParenLoc,
13084 SourceLocation EndLoc) {
13085 MappableVarListInfo MVLI(VarList);
13086 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13087 if (MVLI.ProcessedVarList.empty())
13088 return nullptr;
13089
13090 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13091 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13092 MVLI.VarComponents);
13093}
Samuel Antaoec172c62016-05-26 17:49:04 +000013094
13095OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13096 SourceLocation StartLoc,
13097 SourceLocation LParenLoc,
13098 SourceLocation EndLoc) {
13099 MappableVarListInfo MVLI(VarList);
13100 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13101 if (MVLI.ProcessedVarList.empty())
13102 return nullptr;
13103
13104 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13105 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13106 MVLI.VarComponents);
13107}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013108
13109OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13110 SourceLocation StartLoc,
13111 SourceLocation LParenLoc,
13112 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013113 MappableVarListInfo MVLI(VarList);
13114 SmallVector<Expr *, 8> PrivateCopies;
13115 SmallVector<Expr *, 8> Inits;
13116
Alexey Bataeve3727102018-04-18 15:57:46 +000013117 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013118 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13119 SourceLocation ELoc;
13120 SourceRange ERange;
13121 Expr *SimpleRefExpr = RefExpr;
13122 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13123 if (Res.second) {
13124 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013125 MVLI.ProcessedVarList.push_back(RefExpr);
13126 PrivateCopies.push_back(nullptr);
13127 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013128 }
13129 ValueDecl *D = Res.first;
13130 if (!D)
13131 continue;
13132
13133 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013134 Type = Type.getNonReferenceType().getUnqualifiedType();
13135
13136 auto *VD = dyn_cast<VarDecl>(D);
13137
13138 // Item should be a pointer or reference to pointer.
13139 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013140 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13141 << 0 << RefExpr->getSourceRange();
13142 continue;
13143 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013144
13145 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013146 auto VDPrivate =
13147 buildVarDecl(*this, ELoc, Type, D->getName(),
13148 D->hasAttrs() ? &D->getAttrs() : nullptr,
13149 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013150 if (VDPrivate->isInvalidDecl())
13151 continue;
13152
13153 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013154 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013155 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13156
13157 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013158 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013159 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013160 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13161 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013162 AddInitializerToDecl(VDPrivate,
13163 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013164 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013165
13166 // If required, build a capture to implement the privatization initialized
13167 // with the current list item value.
13168 DeclRefExpr *Ref = nullptr;
13169 if (!VD)
13170 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13171 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13172 PrivateCopies.push_back(VDPrivateRefExpr);
13173 Inits.push_back(VDInitRefExpr);
13174
13175 // We need to add a data sharing attribute for this variable to make sure it
13176 // is correctly captured. A variable that shows up in a use_device_ptr has
13177 // similar properties of a first private variable.
13178 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13179
13180 // Create a mappable component for the list item. List items in this clause
13181 // only need a component.
13182 MVLI.VarBaseDeclarations.push_back(D);
13183 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13184 MVLI.VarComponents.back().push_back(
13185 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013186 }
13187
Samuel Antaocc10b852016-07-28 14:23:26 +000013188 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013189 return nullptr;
13190
Samuel Antaocc10b852016-07-28 14:23:26 +000013191 return OMPUseDevicePtrClause::Create(
13192 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13193 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013194}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013195
13196OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13197 SourceLocation StartLoc,
13198 SourceLocation LParenLoc,
13199 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013200 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013201 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013202 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013203 SourceLocation ELoc;
13204 SourceRange ERange;
13205 Expr *SimpleRefExpr = RefExpr;
13206 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13207 if (Res.second) {
13208 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013209 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013210 }
13211 ValueDecl *D = Res.first;
13212 if (!D)
13213 continue;
13214
13215 QualType Type = D->getType();
13216 // item should be a pointer or array or reference to pointer or array
13217 if (!Type.getNonReferenceType()->isPointerType() &&
13218 !Type.getNonReferenceType()->isArrayType()) {
13219 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13220 << 0 << RefExpr->getSourceRange();
13221 continue;
13222 }
Samuel Antao6890b092016-07-28 14:25:09 +000013223
13224 // Check if the declaration in the clause does not show up in any data
13225 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013226 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013227 if (isOpenMPPrivate(DVar.CKind)) {
13228 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13229 << getOpenMPClauseName(DVar.CKind)
13230 << getOpenMPClauseName(OMPC_is_device_ptr)
13231 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013232 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013233 continue;
13234 }
13235
Alexey Bataeve3727102018-04-18 15:57:46 +000013236 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013237 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013238 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013239 [&ConflictExpr](
13240 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13241 OpenMPClauseKind) -> bool {
13242 ConflictExpr = R.front().getAssociatedExpression();
13243 return true;
13244 })) {
13245 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13246 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13247 << ConflictExpr->getSourceRange();
13248 continue;
13249 }
13250
13251 // Store the components in the stack so that they can be used to check
13252 // against other clauses later on.
13253 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13254 DSAStack->addMappableExpressionComponents(
13255 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13256
13257 // Record the expression we've just processed.
13258 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13259
13260 // Create a mappable component for the list item. List items in this clause
13261 // only need a component. We use a null declaration to signal fields in
13262 // 'this'.
13263 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13264 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13265 "Unexpected device pointer expression!");
13266 MVLI.VarBaseDeclarations.push_back(
13267 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13268 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13269 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013270 }
13271
Samuel Antao6890b092016-07-28 14:25:09 +000013272 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013273 return nullptr;
13274
Samuel Antao6890b092016-07-28 14:25:09 +000013275 return OMPIsDevicePtrClause::Create(
13276 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13277 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013278}