blob: 215b4bf10902ddd02fe19404a8e8aea88f8ca26f [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000031#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
Alexey Bataeve3727102018-04-18 15:57:46 +000038static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000039 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000041 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000042
Alexey Bataev758e55e2013-09-06 18:03:48 +000043namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000049};
50
51/// Attributes of the defaultmap clause.
52enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000055};
Alexey Bataev7ff55242014-06-19 09:13:45 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000058/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000059class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataeve3727102018-04-18 15:57:46 +000061 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000064 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000065 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000071 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
Alexey Bataeve3727102018-04-18 15:57:46 +000074 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000076 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
Alexey Bataeve3727102018-04-18 15:57:46 +000080 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000081 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000084 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000085 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataeve3727102018-04-18 15:57:46 +000087 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000092 /// Struct that associates a component with the clause kind where they are
93 /// found.
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
97 };
Alexey Bataeve3727102018-04-18 15:57:46 +000098 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
108 ReductionRange = RR;
109 ReductionOp = BO;
110 }
111 void set(const Expr *RefExpr, SourceRange RR) {
112 ReductionRange = RR;
113 ReductionOp = RefExpr;
114 }
115 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
Alexey Bataeve3727102018-04-18 15:57:46 +0000119 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000121 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000122 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000123 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000129 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000131 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000132 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// first argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000141 bool NowaitRegion = false;
142 bool CancelRegion = false;
143 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000144 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000145 /// Reference to the taskgroup task_reduction reference expression.
146 Expr *TaskgroupReductionRef = nullptr;
Alexey Bataeved09d242014-05-28 05:53:51 +0000147 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000148 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000149 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
150 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000151 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000152 };
153
Alexey Bataeve3727102018-04-18 15:57:46 +0000154 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000157 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000158 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
159 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000160 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000161 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000162 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000163 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000164 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000165 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166
Alexey Bataeve3727102018-04-18 15:57:46 +0000167 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168
Alexey Bataeve3727102018-04-18 15:57:46 +0000169 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000171 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000172 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000173
Alexey Bataev4b465392017-04-26 15:06:24 +0000174 bool isStackEmpty() const {
175 return Stack.empty() ||
176 Stack.back().second != CurrentNonCapturingFunctionScope ||
177 Stack.back().first.empty();
178 }
179
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 Bataevf138fda2018-08-13 19:04:24 +0000401 void setOrderedRegion(bool IsOrdered, const Expr *Param,
402 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000403 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000404 if (IsOrdered)
405 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
406 else
407 Stack.back().first.back().OrderedRegion.reset();
408 }
409 /// Returns true, if region is ordered (has associated 'ordered' clause),
410 /// false - otherwise.
411 bool isOrderedRegion() const {
412 if (isStackEmpty())
413 return false;
414 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
415 }
416 /// Returns optional parameter for the ordered region.
417 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
418 if (isStackEmpty() ||
419 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
420 return std::make_pair(nullptr, nullptr);
421 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000422 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000423 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000424 /// 'ordered' clause), false - otherwise.
425 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000426 if (isStackEmpty() || Stack.back().first.size() == 1)
427 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000428 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000429 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000430 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000431 std::pair<const Expr *, OMPOrderedClause *>
432 getParentOrderedRegionParam() const {
433 if (isStackEmpty() || Stack.back().first.size() == 1 ||
434 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
435 return std::make_pair(nullptr, nullptr);
436 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000437 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000438 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000439 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000440 assert(!isStackEmpty());
441 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000442 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000443 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000444 /// 'nowait' clause), false - otherwise.
445 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000446 if (isStackEmpty() || Stack.back().first.size() == 1)
447 return false;
448 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000449 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000450 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000451 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000452 if (!isStackEmpty() && Stack.back().first.size() > 1) {
453 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
454 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
455 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000456 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000457 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000458 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000459 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000460 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000461
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000462 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000463 void setAssociatedLoops(unsigned Val) {
464 assert(!isStackEmpty());
465 Stack.back().first.back().AssociatedLoops = Val;
466 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000467 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000468 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000469 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000470 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000471
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000472 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000473 /// region.
474 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000475 if (!isStackEmpty() && Stack.back().first.size() > 1) {
476 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
477 TeamsRegionLoc;
478 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000479 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000480 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000481 bool hasInnerTeamsRegion() const {
482 return getInnerTeamsRegionLoc().isValid();
483 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000484 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000485 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000486 return isStackEmpty() ? SourceLocation()
487 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000488 }
489
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000490 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000491 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000492 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000493 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000494 return isStackEmpty() ? SourceLocation()
495 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000496 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000497
Samuel Antao4c8035b2016-12-12 18:00:20 +0000498 /// Do the check specified in \a Check to all component lists and return true
499 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000500 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000501 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000502 const llvm::function_ref<
503 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000504 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000505 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000506 if (isStackEmpty())
507 return false;
508 auto SI = Stack.back().first.rbegin();
509 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000510
511 if (SI == SE)
512 return false;
513
Alexey Bataeve3727102018-04-18 15:57:46 +0000514 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000515 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000516 else
517 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000518
519 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000520 auto MI = SI->MappedExprComponents.find(VD);
521 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000522 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
523 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000524 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000525 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000526 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000527 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000528 }
529
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000530 /// Do the check specified in \a Check to all component lists at a given level
531 /// and return true if any issue is found.
532 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000533 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000534 const llvm::function_ref<
535 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000536 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000537 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000538 if (isStackEmpty())
539 return false;
540
541 auto StartI = Stack.back().first.begin();
542 auto EndI = Stack.back().first.end();
543 if (std::distance(StartI, EndI) <= (int)Level)
544 return false;
545 std::advance(StartI, Level);
546
547 auto MI = StartI->MappedExprComponents.find(VD);
548 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000549 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
550 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000551 if (Check(L, MI->second.Kind))
552 return true;
553 return false;
554 }
555
Samuel Antao4c8035b2016-12-12 18:00:20 +0000556 /// Create a new mappable expression component list associated with a given
557 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000558 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000559 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000560 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
561 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000562 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000563 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000564 MappedExprComponentTy &MEC =
565 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000566 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000567 MEC.Components.resize(MEC.Components.size() + 1);
568 MEC.Components.back().append(Components.begin(), Components.end());
569 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000570 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000571
572 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000573 assert(!isStackEmpty());
574 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000575 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000576 void addDoacrossDependClause(OMPDependClause *C,
577 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000578 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000579 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000580 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000581 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000582 }
583 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
584 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000585 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000586 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000587 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000588 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000589 return llvm::make_range(Ref.begin(), Ref.end());
590 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000591 return llvm::make_range(StackElem.DoacrossDepends.end(),
592 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000595bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000596 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
597 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000598}
Alexey Bataeve3727102018-04-18 15:57:46 +0000599
Alexey Bataeved09d242014-05-28 05:53:51 +0000600} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000601
Alexey Bataeve3727102018-04-18 15:57:46 +0000602static const Expr *getExprAsWritten(const Expr *E) {
603 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000604 E = ExprTemp->getSubExpr();
605
Alexey Bataeve3727102018-04-18 15:57:46 +0000606 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000607 E = MTE->GetTemporaryExpr();
608
Alexey Bataeve3727102018-04-18 15:57:46 +0000609 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000610 E = Binder->getSubExpr();
611
Alexey Bataeve3727102018-04-18 15:57:46 +0000612 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000613 E = ICE->getSubExprAsWritten();
614 return E->IgnoreParens();
615}
616
Alexey Bataeve3727102018-04-18 15:57:46 +0000617static Expr *getExprAsWritten(Expr *E) {
618 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
619}
620
621static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
622 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
623 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000624 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000625 const auto *VD = dyn_cast<VarDecl>(D);
626 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000627 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000628 VD = VD->getCanonicalDecl();
629 D = VD;
630 } else {
631 assert(FD);
632 FD = FD->getCanonicalDecl();
633 D = FD;
634 }
635 return D;
636}
637
Alexey Bataeve3727102018-04-18 15:57:46 +0000638static ValueDecl *getCanonicalDecl(ValueDecl *D) {
639 return const_cast<ValueDecl *>(
640 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
641}
642
643DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
644 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000645 D = getCanonicalDecl(D);
646 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000647 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000649 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000650 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
651 // in a region but not in construct]
652 // File-scope or namespace-scope variables referenced in called routines
653 // in the region are shared unless they appear in a threadprivate
654 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000655 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000656 DVar.CKind = OMPC_shared;
657
658 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
659 // in a region but not in construct]
660 // Variables with static storage duration that are declared in called
661 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000662 if (VD && VD->hasGlobalStorage())
663 DVar.CKind = OMPC_shared;
664
665 // Non-static data members are shared by default.
666 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000667 DVar.CKind = OMPC_shared;
668
Alexey Bataev758e55e2013-09-06 18:03:48 +0000669 return DVar;
670 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000671
Alexey Bataevec3da872014-01-31 05:15:34 +0000672 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
673 // in a Construct, C/C++, predetermined, p.1]
674 // Variables with automatic storage duration that are declared in a scope
675 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000676 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
677 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000678 DVar.CKind = OMPC_private;
679 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000680 }
681
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000682 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 // Explicitly specified attributes and local variables with predetermined
684 // attributes.
685 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000686 const DSAInfo &Data = Iter->SharingMap.lookup(D);
687 DVar.RefExpr = Data.RefExpr.getPointer();
688 DVar.PrivateCopy = Data.PrivateCopy;
689 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000690 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 return DVar;
692 }
693
694 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
695 // in a Construct, C/C++, implicitly determined, p.1]
696 // In a parallel or task construct, the data-sharing attributes of these
697 // variables are determined by the default clause, if present.
698 switch (Iter->DefaultAttr) {
699 case DSA_shared:
700 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000701 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 return DVar;
703 case DSA_none:
704 return DVar;
705 case DSA_unspecified:
706 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
707 // in a Construct, implicitly determined, p.2]
708 // In a parallel construct, if no default clause is present, these
709 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000710 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000711 if (isOpenMPParallelDirective(DVar.DKind) ||
712 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
717 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
718 // in a Construct, implicitly determined, p.4]
719 // In a task construct, if no default clause is present, a variable that in
720 // the enclosing context is determined to be shared by all implicit tasks
721 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000722 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000723 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000724 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000725 do {
726 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000727 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000728 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000729 // In a task construct, if no default clause is present, a variable
730 // whose data-sharing attribute is not determined by the rules above is
731 // firstprivate.
732 DVarTemp = getDSA(I, D);
733 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000734 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000735 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000736 return DVar;
737 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000738 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000739 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000740 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741 return DVar;
742 }
743 }
744 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
745 // in a Construct, implicitly determined, p.3]
746 // For constructs other than task, if no default clause is present, these
747 // variables inherit their data-sharing attributes from the enclosing
748 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000749 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000750}
751
Alexey Bataeve3727102018-04-18 15:57:46 +0000752const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
753 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000754 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000755 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000756 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000757 auto It = StackElem.AlignedMap.find(D);
758 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000759 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000760 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000761 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000762 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000763 assert(It->second && "Unexpected nullptr expr in the aligned map");
764 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000765}
766
Alexey Bataeve3727102018-04-18 15:57:46 +0000767void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000768 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000769 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000770 SharingMapTy &StackElem = Stack.back().first.back();
771 StackElem.LCVMap.try_emplace(
772 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000773}
774
Alexey Bataeve3727102018-04-18 15:57:46 +0000775const DSAStackTy::LCDeclInfo
776DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000777 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000778 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000779 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000780 auto It = StackElem.LCVMap.find(D);
781 if (It != StackElem.LCVMap.end())
782 return It->second;
783 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000784}
785
Alexey Bataeve3727102018-04-18 15:57:46 +0000786const DSAStackTy::LCDeclInfo
787DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000788 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
789 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000790 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000791 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000792 auto It = StackElem.LCVMap.find(D);
793 if (It != StackElem.LCVMap.end())
794 return It->second;
795 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000796}
797
Alexey Bataeve3727102018-04-18 15:57:46 +0000798const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000799 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
800 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000801 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000802 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000803 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000804 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000805 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000806 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000807 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000808}
809
Alexey Bataeve3727102018-04-18 15:57:46 +0000810void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000811 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000812 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000813 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000814 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000815 Data.Attributes = A;
816 Data.RefExpr.setPointer(E);
817 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000818 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000819 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000820 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000821 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
822 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
823 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
824 (isLoopControlVariable(D).first && A == OMPC_private));
825 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
826 Data.RefExpr.setInt(/*IntVal=*/true);
827 return;
828 }
829 const bool IsLastprivate =
830 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
831 Data.Attributes = A;
832 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
833 Data.PrivateCopy = PrivateCopy;
834 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000835 DSAInfo &Data =
836 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000837 Data.Attributes = A;
838 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
839 Data.PrivateCopy = nullptr;
840 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000841 }
842}
843
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000844/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000845static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000846 StringRef Name, const AttrVec *Attrs = nullptr,
847 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000848 DeclContext *DC = SemaRef.CurContext;
849 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
850 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000851 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000852 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
853 if (Attrs) {
854 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
855 I != E; ++I)
856 Decl->addAttr(*I);
857 }
858 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000859 if (OrigRef) {
860 Decl->addAttr(
861 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
862 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000863 return Decl;
864}
865
866static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
867 SourceLocation Loc,
868 bool RefersToCapture = false) {
869 D->setReferenced();
870 D->markUsed(S.Context);
871 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
872 SourceLocation(), D, RefersToCapture, Loc, Ty,
873 VK_LValue);
874}
875
Alexey Bataeve3727102018-04-18 15:57:46 +0000876void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000877 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000878 D = getCanonicalDecl(D);
879 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000880 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000881 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000882 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000883 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000884 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000885 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000886 "Additional reduction info may be specified only once for reduction "
887 "items.");
888 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000889 Expr *&TaskgroupReductionRef =
890 Stack.back().first.back().TaskgroupReductionRef;
891 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000892 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
893 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000894 TaskgroupReductionRef =
895 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000896 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000897}
898
Alexey Bataeve3727102018-04-18 15:57:46 +0000899void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000900 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000901 D = getCanonicalDecl(D);
902 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000903 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000904 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000905 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000906 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000907 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000908 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000909 "Additional reduction info may be specified only once for reduction "
910 "items.");
911 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000912 Expr *&TaskgroupReductionRef =
913 Stack.back().first.back().TaskgroupReductionRef;
914 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000915 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
916 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000917 TaskgroupReductionRef =
918 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000919 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000920}
921
Alexey Bataeve3727102018-04-18 15:57:46 +0000922const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
923 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
924 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000925 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000926 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
927 if (Stack.back().first.empty())
928 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000929 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
930 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000931 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000932 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000933 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000934 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000935 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000936 if (!ReductionData.ReductionOp ||
937 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000938 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000939 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000940 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000941 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
942 "expression for the descriptor is not "
943 "set.");
944 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000945 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
946 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000947 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000948 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000949}
950
Alexey Bataeve3727102018-04-18 15:57:46 +0000951const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
952 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
953 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000954 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000955 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
956 if (Stack.back().first.empty())
957 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000958 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
959 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000960 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000961 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000962 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000963 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000964 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000965 if (!ReductionData.ReductionOp ||
966 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000967 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000968 SR = ReductionData.ReductionRange;
969 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000970 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
971 "expression for the descriptor is not "
972 "set.");
973 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000974 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
975 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000976 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000977 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000978}
979
Alexey Bataeve3727102018-04-18 15:57:46 +0000980bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000981 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +0000982 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000983 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000984 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +0000985 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
986 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +0000987 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000988 if (I == E)
989 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000990 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000991 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000992 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +0000993 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000994 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000995 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000996 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000997}
998
Alexey Bataeve3727102018-04-18 15:57:46 +0000999const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1000 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001001 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001002 DSAVarData DVar;
1003
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001005 auto TI = Threadprivates.find(D);
1006 if (TI != Threadprivates.end()) {
1007 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001008 DVar.CKind = OMPC_threadprivate;
1009 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001010 }
1011 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001012 DVar.RefExpr = buildDeclRefExpr(
1013 SemaRef, VD, D->getType().getNonReferenceType(),
1014 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1015 DVar.CKind = OMPC_threadprivate;
1016 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001017 return DVar;
1018 }
1019 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1020 // in a Construct, C/C++, predetermined, p.1]
1021 // Variables appearing in threadprivate directives are threadprivate.
1022 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1023 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1024 SemaRef.getLangOpts().OpenMPUseTLS &&
1025 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1026 (VD && VD->getStorageClass() == SC_Register &&
1027 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1028 DVar.RefExpr = buildDeclRefExpr(
1029 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1030 DVar.CKind = OMPC_threadprivate;
1031 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1032 return DVar;
1033 }
1034 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1035 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1036 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001037 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001038 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1039 [](const SharingMapTy &Data) {
1040 return isOpenMPTargetExecutionDirective(Data.Directive);
1041 });
1042 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001043 iterator ParentIterTarget = std::next(IterTarget, 1);
1044 for (iterator Iter = Stack.back().first.rbegin();
1045 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001046 if (isOpenMPLocal(VD, Iter)) {
1047 DVar.RefExpr =
1048 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1049 D->getLocation());
1050 DVar.CKind = OMPC_threadprivate;
1051 return DVar;
1052 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001053 }
1054 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1055 auto DSAIter = IterTarget->SharingMap.find(D);
1056 if (DSAIter != IterTarget->SharingMap.end() &&
1057 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1058 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1059 DVar.CKind = OMPC_threadprivate;
1060 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001061 }
1062 iterator End = Stack.back().first.rend();
1063 if (!SemaRef.isOpenMPCapturedByRef(
1064 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001065 DVar.RefExpr =
1066 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1067 IterTarget->ConstructLoc);
1068 DVar.CKind = OMPC_threadprivate;
1069 return DVar;
1070 }
1071 }
1072 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001073 }
1074
Alexey Bataev4b465392017-04-26 15:06:24 +00001075 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001076 // Not in OpenMP execution region and top scope was already checked.
1077 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001078
Alexey Bataev758e55e2013-09-06 18:03:48 +00001079 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001080 // in a Construct, C/C++, predetermined, p.4]
1081 // Static data members are shared.
1082 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1083 // in a Construct, C/C++, predetermined, p.7]
1084 // Variables with static storage duration that are declared in a scope
1085 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001086 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001087 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001088 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001089 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001090 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001091
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001092 DVar.CKind = OMPC_shared;
1093 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001094 }
1095
1096 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001097 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1098 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001099 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1100 // in a Construct, C/C++, predetermined, p.6]
1101 // Variables with const qualified type having no mutable member are
1102 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001103 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001104 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001105 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1106 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001107 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001108 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001109 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1110 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001111 // Variables with const-qualified type having no mutable member may be
1112 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001113 DSAVarData DVarTemp =
1114 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1115 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001116 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001117 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001118
Alexey Bataev758e55e2013-09-06 18:03:48 +00001119 DVar.CKind = OMPC_shared;
1120 return DVar;
1121 }
1122
Alexey Bataev758e55e2013-09-06 18:03:48 +00001123 // Explicitly specified attributes and local variables with predetermined
1124 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001125 iterator I = Stack.back().first.rbegin();
1126 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001127 if (FromParent && I != EndI)
1128 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001129 auto It = I->SharingMap.find(D);
1130 if (It != I->SharingMap.end()) {
1131 const DSAInfo &Data = It->getSecond();
1132 DVar.RefExpr = Data.RefExpr.getPointer();
1133 DVar.PrivateCopy = Data.PrivateCopy;
1134 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001135 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001136 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001137 }
1138
1139 return DVar;
1140}
1141
Alexey Bataeve3727102018-04-18 15:57:46 +00001142const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1143 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001144 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001145 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001146 return getDSA(I, D);
1147 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001148 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001149 iterator StartI = Stack.back().first.rbegin();
1150 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001151 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001152 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001153 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001154}
1155
Alexey Bataeve3727102018-04-18 15:57:46 +00001156const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001157DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001158 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1159 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001160 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001161 if (isStackEmpty())
1162 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001163 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001164 iterator I = Stack.back().first.rbegin();
1165 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001166 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001167 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001168 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001169 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001170 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001171 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001172 DSAVarData DVar = getDSA(NewI, D);
1173 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001174 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001175 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001176 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001177}
1178
Alexey Bataeve3727102018-04-18 15:57:46 +00001179const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001180 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1181 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001182 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001183 if (isStackEmpty())
1184 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001185 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001186 iterator StartI = Stack.back().first.rbegin();
1187 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001188 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001189 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001190 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001191 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001192 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001193 DSAVarData DVar = getDSA(NewI, D);
1194 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001195}
1196
Alexey Bataevaac108a2015-06-23 04:51:00 +00001197bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001198 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1199 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001200 if (isStackEmpty())
1201 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001202 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001203 auto StartI = Stack.back().first.begin();
1204 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001205 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001206 return false;
1207 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001208 auto I = StartI->SharingMap.find(D);
1209 return (I != StartI->SharingMap.end()) &&
1210 I->getSecond().RefExpr.getPointer() &&
1211 CPred(I->getSecond().Attributes) &&
1212 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001213}
1214
Samuel Antao4be30e92015-10-02 17:14:03 +00001215bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001216 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1217 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001218 if (isStackEmpty())
1219 return false;
1220 auto StartI = Stack.back().first.begin();
1221 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001222 if (std::distance(StartI, EndI) <= (int)Level)
1223 return false;
1224 std::advance(StartI, Level);
1225 return DPred(StartI->Directive);
1226}
1227
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001228bool DSAStackTy::hasDirective(
1229 const llvm::function_ref<bool(OpenMPDirectiveKind,
1230 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001231 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001232 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001233 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001234 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001235 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001236 auto StartI = std::next(Stack.back().first.rbegin());
1237 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001238 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001239 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001240 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1241 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1242 return true;
1243 }
1244 return false;
1245}
1246
Alexey Bataev758e55e2013-09-06 18:03:48 +00001247void Sema::InitDataSharingAttributesStack() {
1248 VarDataSharingAttributesStack = new DSAStackTy(*this);
1249}
1250
1251#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1252
Alexey Bataev4b465392017-04-26 15:06:24 +00001253void Sema::pushOpenMPFunctionRegion() {
1254 DSAStack->pushFunction();
1255}
1256
1257void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1258 DSAStack->popFunction(OldFSI);
1259}
1260
Alexey Bataeve3727102018-04-18 15:57:46 +00001261bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001262 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1263
Alexey Bataeve3727102018-04-18 15:57:46 +00001264 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001265 bool IsByRef = true;
1266
1267 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001268 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001269 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001270
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001271 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001272 // This table summarizes how a given variable should be passed to the device
1273 // given its type and the clauses where it appears. This table is based on
1274 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1275 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1276 //
1277 // =========================================================================
1278 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1279 // | |(tofrom:scalar)| | pvt | | | |
1280 // =========================================================================
1281 // | scl | | | | - | | bycopy|
1282 // | scl | | - | x | - | - | bycopy|
1283 // | scl | | x | - | - | - | null |
1284 // | scl | x | | | - | | byref |
1285 // | scl | x | - | x | - | - | bycopy|
1286 // | scl | x | x | - | - | - | null |
1287 // | scl | | - | - | - | x | byref |
1288 // | scl | x | - | - | - | x | byref |
1289 //
1290 // | agg | n.a. | | | - | | byref |
1291 // | agg | n.a. | - | x | - | - | byref |
1292 // | agg | n.a. | x | - | - | - | null |
1293 // | agg | n.a. | - | - | - | x | byref |
1294 // | agg | n.a. | - | - | - | x[] | byref |
1295 //
1296 // | ptr | n.a. | | | - | | bycopy|
1297 // | ptr | n.a. | - | x | - | - | bycopy|
1298 // | ptr | n.a. | x | - | - | - | null |
1299 // | ptr | n.a. | - | - | - | x | byref |
1300 // | ptr | n.a. | - | - | - | x[] | bycopy|
1301 // | ptr | n.a. | - | - | x | | bycopy|
1302 // | ptr | n.a. | - | - | x | x | bycopy|
1303 // | ptr | n.a. | - | - | x | x[] | bycopy|
1304 // =========================================================================
1305 // Legend:
1306 // scl - scalar
1307 // ptr - pointer
1308 // agg - aggregate
1309 // x - applies
1310 // - - invalid in this combination
1311 // [] - mapped with an array section
1312 // byref - should be mapped by reference
1313 // byval - should be mapped by value
1314 // null - initialize a local variable to null on the device
1315 //
1316 // Observations:
1317 // - All scalar declarations that show up in a map clause have to be passed
1318 // by reference, because they may have been mapped in the enclosing data
1319 // environment.
1320 // - If the scalar value does not fit the size of uintptr, it has to be
1321 // passed by reference, regardless the result in the table above.
1322 // - For pointers mapped by value that have either an implicit map or an
1323 // array section, the runtime library may pass the NULL value to the
1324 // device instead of the value passed to it by the compiler.
1325
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001326 if (Ty->isReferenceType())
1327 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001328
1329 // Locate map clauses and see if the variable being captured is referred to
1330 // in any of those clauses. Here we only care about variables, not fields,
1331 // because fields are part of aggregates.
1332 bool IsVariableUsedInMapClause = false;
1333 bool IsVariableAssociatedWithSection = false;
1334
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001335 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001336 D, Level,
1337 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1338 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001339 MapExprComponents,
1340 OpenMPClauseKind WhereFoundClauseKind) {
1341 // Only the map clause information influences how a variable is
1342 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001343 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001344 if (WhereFoundClauseKind != OMPC_map)
1345 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001346
1347 auto EI = MapExprComponents.rbegin();
1348 auto EE = MapExprComponents.rend();
1349
1350 assert(EI != EE && "Invalid map expression!");
1351
1352 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1353 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1354
1355 ++EI;
1356 if (EI == EE)
1357 return false;
1358
1359 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1360 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1361 isa<MemberExpr>(EI->getAssociatedExpression())) {
1362 IsVariableAssociatedWithSection = true;
1363 // There is nothing more we need to know about this variable.
1364 return true;
1365 }
1366
1367 // Keep looking for more map info.
1368 return false;
1369 });
1370
1371 if (IsVariableUsedInMapClause) {
1372 // If variable is identified in a map clause it is always captured by
1373 // reference except if it is a pointer that is dereferenced somehow.
1374 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1375 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001376 // By default, all the data that has a scalar type is mapped by copy
1377 // (except for reduction variables).
1378 IsByRef =
1379 !Ty->isScalarType() ||
1380 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1381 DSAStack->hasExplicitDSA(
1382 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001383 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001384 }
1385
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001386 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001387 IsByRef =
1388 !DSAStack->hasExplicitDSA(
1389 D,
1390 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1391 Level, /*NotLastprivate=*/true) &&
1392 // If the variable is artificial and must be captured by value - try to
1393 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001394 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1395 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001396 }
1397
Samuel Antao86ace552016-04-27 22:40:57 +00001398 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001399 // and alignment, because the runtime library only deals with uintptr types.
1400 // If it does not fit the uintptr size, we need to pass the data by reference
1401 // instead.
1402 if (!IsByRef &&
1403 (Ctx.getTypeSizeInChars(Ty) >
1404 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001405 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001406 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001407 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001408
1409 return IsByRef;
1410}
1411
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001412unsigned Sema::getOpenMPNestingLevel() const {
1413 assert(getLangOpts().OpenMP);
1414 return DSAStack->getNestingLevel();
1415}
1416
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001417bool Sema::isInOpenMPTargetExecutionDirective() const {
1418 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1419 !DSAStack->isClauseParsingMode()) ||
1420 DSAStack->hasDirective(
1421 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1422 SourceLocation) -> bool {
1423 return isOpenMPTargetExecutionDirective(K);
1424 },
1425 false);
1426}
1427
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001428VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001429 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001430 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001431
1432 // If we are attempting to capture a global variable in a directive with
1433 // 'target' we return true so that this global is also mapped to the device.
1434 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001436 if (VD && !VD->hasLocalStorage()) {
1437 if (isInOpenMPDeclareTargetContext() &&
1438 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1439 // Try to mark variable as declare target if it is used in capturing
1440 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001441 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001442 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001443 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001444 } else if (isInOpenMPTargetExecutionDirective()) {
1445 // If the declaration is enclosed in a 'declare target' directive,
1446 // then it should not be captured.
1447 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001448 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001449 return nullptr;
1450 return VD;
1451 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001452 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001453
Alexey Bataev48977c32015-08-04 08:10:48 +00001454 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1455 (!DSAStack->isClauseParsingMode() ||
1456 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001457 auto &&Info = DSAStack->isLoopControlVariable(D);
1458 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001459 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001460 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001461 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001462 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001463 DSAStackTy::DSAVarData DVarPrivate =
1464 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001465 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001466 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001467 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1468 [](OpenMPDirectiveKind) { return true; },
1469 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001470 if (DVarPrivate.CKind != OMPC_unknown)
1471 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001472 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001473 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001474}
1475
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001476void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1477 unsigned Level) const {
1478 SmallVector<OpenMPDirectiveKind, 4> Regions;
1479 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1480 FunctionScopesIndex -= Regions.size();
1481}
1482
Alexey Bataeve3727102018-04-18 15:57:46 +00001483bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001484 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1485 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001486 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001487 (DSAStack->isClauseParsingMode() &&
1488 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001489 // Consider taskgroup reduction descriptor variable a private to avoid
1490 // possible capture in the region.
1491 (DSAStack->hasExplicitDirective(
1492 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1493 Level) &&
1494 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001495}
1496
Alexey Bataeve3727102018-04-18 15:57:46 +00001497void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1498 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001499 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1500 D = getCanonicalDecl(D);
1501 OpenMPClauseKind OMPC = OMPC_unknown;
1502 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1503 const unsigned NewLevel = I - 1;
1504 if (DSAStack->hasExplicitDSA(D,
1505 [&OMPC](const OpenMPClauseKind K) {
1506 if (isOpenMPPrivate(K)) {
1507 OMPC = K;
1508 return true;
1509 }
1510 return false;
1511 },
1512 NewLevel))
1513 break;
1514 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1515 D, NewLevel,
1516 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1517 OpenMPClauseKind) { return true; })) {
1518 OMPC = OMPC_map;
1519 break;
1520 }
1521 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1522 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001523 OMPC = OMPC_map;
1524 if (D->getType()->isScalarType() &&
1525 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1526 DefaultMapAttributes::DMA_tofrom_scalar)
1527 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001528 break;
1529 }
1530 }
1531 if (OMPC != OMPC_unknown)
1532 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1533}
1534
Alexey Bataeve3727102018-04-18 15:57:46 +00001535bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1536 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001537 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1538 // Return true if the current level is no longer enclosed in a target region.
1539
Alexey Bataeve3727102018-04-18 15:57:46 +00001540 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001541 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001542 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1543 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001544}
1545
Alexey Bataeved09d242014-05-28 05:53:51 +00001546void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001547
1548void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1549 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001550 Scope *CurScope, SourceLocation Loc) {
1551 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001552 PushExpressionEvaluationContext(
1553 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001554}
1555
Alexey Bataevaac108a2015-06-23 04:51:00 +00001556void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1557 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001558}
1559
Alexey Bataevaac108a2015-06-23 04:51:00 +00001560void Sema::EndOpenMPClause() {
1561 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001562}
1563
Alexey Bataev758e55e2013-09-06 18:03:48 +00001564void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001565 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1566 // A variable of class type (or array thereof) that appears in a lastprivate
1567 // clause requires an accessible, unambiguous default constructor for the
1568 // class type, unless the list item is also specified in a firstprivate
1569 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001570 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1571 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001572 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1573 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001574 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001575 if (DE->isValueDependent() || DE->isTypeDependent()) {
1576 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001577 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001578 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001579 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001580 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001581 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001582 const DSAStackTy::DSAVarData DVar =
1583 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001584 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001585 // Generate helper private variable and initialize it with the
1586 // default value. The address of the original variable is replaced
1587 // by the address of the new private variable in CodeGen. This new
1588 // variable is not added to IdResolver, so the code in the OpenMP
1589 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001590 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001591 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001592 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001593 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001594 if (VDPrivate->isInvalidDecl())
1595 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001596 PrivateCopies.push_back(buildDeclRefExpr(
1597 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001598 } else {
1599 // The variable is also a firstprivate, so initialization sequence
1600 // for private copy is generated already.
1601 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001602 }
1603 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001604 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001605 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001606 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001607 }
1608 }
1609 }
1610
Alexey Bataev758e55e2013-09-06 18:03:48 +00001611 DSAStack->pop();
1612 DiscardCleanupsInEvaluationContext();
1613 PopExpressionEvaluationContext();
1614}
1615
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001616static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1617 Expr *NumIterations, Sema &SemaRef,
1618 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001619
Alexey Bataeva769e072013-03-22 06:34:35 +00001620namespace {
1621
Alexey Bataeve3727102018-04-18 15:57:46 +00001622class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001623private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001624 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001625
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001626public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001627 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001628 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001629 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001630 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001631 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001632 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1633 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001634 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001635 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001636 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001637};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001638
Alexey Bataeve3727102018-04-18 15:57:46 +00001639class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001640private:
1641 Sema &SemaRef;
1642
1643public:
1644 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1645 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1646 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001647 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001648 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1649 SemaRef.getCurScope());
1650 }
1651 return false;
1652 }
1653};
1654
Alexey Bataeved09d242014-05-28 05:53:51 +00001655} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001656
1657ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1658 CXXScopeSpec &ScopeSpec,
1659 const DeclarationNameInfo &Id) {
1660 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1661 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1662
1663 if (Lookup.isAmbiguous())
1664 return ExprError();
1665
1666 VarDecl *VD;
1667 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001668 if (TypoCorrection Corrected = CorrectTypo(
1669 Id, LookupOrdinaryName, CurScope, nullptr,
1670 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001671 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001672 PDiag(Lookup.empty()
1673 ? diag::err_undeclared_var_use_suggest
1674 : diag::err_omp_expected_var_arg_suggest)
1675 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001676 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001677 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001678 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1679 : diag::err_omp_expected_var_arg)
1680 << Id.getName();
1681 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001682 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001683 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1684 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1685 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1686 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001687 }
1688 Lookup.suppressDiagnostics();
1689
1690 // OpenMP [2.9.2, Syntax, C/C++]
1691 // Variables must be file-scope, namespace-scope, or static block-scope.
1692 if (!VD->hasGlobalStorage()) {
1693 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001694 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1695 bool IsDecl =
1696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001697 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1699 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001700 return ExprError();
1701 }
1702
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001703 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001704 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001705 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1706 // A threadprivate directive for file-scope variables must appear outside
1707 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001708 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1709 !getCurLexicalContext()->isTranslationUnit()) {
1710 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001711 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1712 bool IsDecl =
1713 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1714 Diag(VD->getLocation(),
1715 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1716 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001717 return ExprError();
1718 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001719 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1720 // A threadprivate directive for static class member variables must appear
1721 // in the class definition, in the same scope in which the member
1722 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001723 if (CanonicalVD->isStaticDataMember() &&
1724 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1725 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001726 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1727 bool IsDecl =
1728 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1729 Diag(VD->getLocation(),
1730 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1731 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001732 return ExprError();
1733 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001734 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1735 // A threadprivate directive for namespace-scope variables must appear
1736 // outside any definition or declaration other than the namespace
1737 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001738 if (CanonicalVD->getDeclContext()->isNamespace() &&
1739 (!getCurLexicalContext()->isFileContext() ||
1740 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1741 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001742 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1743 bool IsDecl =
1744 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1745 Diag(VD->getLocation(),
1746 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1747 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001748 return ExprError();
1749 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001750 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1751 // A threadprivate directive for static block-scope variables must appear
1752 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001753 if (CanonicalVD->isStaticLocal() && CurScope &&
1754 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001755 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001756 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1757 bool IsDecl =
1758 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1759 Diag(VD->getLocation(),
1760 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1761 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001762 return ExprError();
1763 }
1764
1765 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1766 // A threadprivate directive must lexically precede all references to any
1767 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001768 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001769 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001770 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001771 return ExprError();
1772 }
1773
1774 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001775 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1776 SourceLocation(), VD,
1777 /*RefersToEnclosingVariableOrCapture=*/false,
1778 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001779}
1780
Alexey Bataeved09d242014-05-28 05:53:51 +00001781Sema::DeclGroupPtrTy
1782Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1783 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001784 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001785 CurContext->addDecl(D);
1786 return DeclGroupPtrTy::make(DeclGroupRef(D));
1787 }
David Blaikie0403cb12016-01-15 23:43:25 +00001788 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001789}
1790
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001791namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001792class LocalVarRefChecker final
1793 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001794 Sema &SemaRef;
1795
1796public:
1797 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001798 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001799 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001800 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001801 diag::err_omp_local_var_in_threadprivate_init)
1802 << E->getSourceRange();
1803 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1804 << VD << VD->getSourceRange();
1805 return true;
1806 }
1807 }
1808 return false;
1809 }
1810 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001811 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001812 if (Child && Visit(Child))
1813 return true;
1814 }
1815 return false;
1816 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001817 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001818};
1819} // namespace
1820
Alexey Bataeved09d242014-05-28 05:53:51 +00001821OMPThreadPrivateDecl *
1822Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001823 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001824 for (Expr *RefExpr : VarList) {
1825 auto *DE = cast<DeclRefExpr>(RefExpr);
1826 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001827 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001828
Alexey Bataev376b4a42016-02-09 09:41:09 +00001829 // Mark variable as used.
1830 VD->setReferenced();
1831 VD->markUsed(Context);
1832
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001833 QualType QType = VD->getType();
1834 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1835 // It will be analyzed later.
1836 Vars.push_back(DE);
1837 continue;
1838 }
1839
Alexey Bataeva769e072013-03-22 06:34:35 +00001840 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1841 // A threadprivate variable must not have an incomplete type.
1842 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001843 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001844 continue;
1845 }
1846
1847 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1848 // A threadprivate variable must not have a reference type.
1849 if (VD->getType()->isReferenceType()) {
1850 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001851 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1852 bool IsDecl =
1853 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1854 Diag(VD->getLocation(),
1855 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1856 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001857 continue;
1858 }
1859
Samuel Antaof8b50122015-07-13 22:54:53 +00001860 // Check if this is a TLS variable. If TLS is not being supported, produce
1861 // the corresponding diagnostic.
1862 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1863 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1864 getLangOpts().OpenMPUseTLS &&
1865 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001866 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1867 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001868 Diag(ILoc, diag::err_omp_var_thread_local)
1869 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001870 bool IsDecl =
1871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1872 Diag(VD->getLocation(),
1873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1874 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001875 continue;
1876 }
1877
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001878 // Check if initial value of threadprivate variable reference variable with
1879 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00001880 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001881 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001882 if (Checker.Visit(Init))
1883 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001884 }
1885
Alexey Bataeved09d242014-05-28 05:53:51 +00001886 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001887 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001888 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1889 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00001890 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00001891 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001892 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001893 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001894 if (!Vars.empty()) {
1895 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1896 Vars);
1897 D->setAccess(AS_public);
1898 }
1899 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001900}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001901
Alexey Bataeve3727102018-04-18 15:57:46 +00001902static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
1903 const ValueDecl *D,
1904 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001905 bool IsLoopIterVar = false) {
1906 if (DVar.RefExpr) {
1907 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1908 << getOpenMPClauseName(DVar.CKind);
1909 return;
1910 }
1911 enum {
1912 PDSA_StaticMemberShared,
1913 PDSA_StaticLocalVarShared,
1914 PDSA_LoopIterVarPrivate,
1915 PDSA_LoopIterVarLinear,
1916 PDSA_LoopIterVarLastprivate,
1917 PDSA_ConstVarShared,
1918 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001919 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001920 PDSA_LocalVarPrivate,
1921 PDSA_Implicit
1922 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001923 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001924 auto ReportLoc = D->getLocation();
1925 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001926 if (IsLoopIterVar) {
1927 if (DVar.CKind == OMPC_private)
1928 Reason = PDSA_LoopIterVarPrivate;
1929 else if (DVar.CKind == OMPC_lastprivate)
1930 Reason = PDSA_LoopIterVarLastprivate;
1931 else
1932 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001933 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1934 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001935 Reason = PDSA_TaskVarFirstprivate;
1936 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001937 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001938 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001939 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001940 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001941 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001942 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001943 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001944 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001945 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001946 ReportHint = true;
1947 Reason = PDSA_LocalVarPrivate;
1948 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001949 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001950 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001951 << Reason << ReportHint
1952 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1953 } else if (DVar.ImplicitDSALoc.isValid()) {
1954 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1955 << getOpenMPClauseName(DVar.CKind);
1956 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001957}
1958
Alexey Bataev758e55e2013-09-06 18:03:48 +00001959namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001960class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001961 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001962 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00001963 bool ErrorFound = false;
1964 CapturedStmt *CS = nullptr;
1965 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
1966 llvm::SmallVector<Expr *, 4> ImplicitMap;
1967 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
1968 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00001969
Alexey Bataev758e55e2013-09-06 18:03:48 +00001970public:
1971 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001972 if (E->isTypeDependent() || E->isValueDependent() ||
1973 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1974 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001975 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001976 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001977 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00001978 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00001979 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001980
Alexey Bataeve3727102018-04-18 15:57:46 +00001981 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001982 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001983 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00001984 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001985
Alexey Bataevafe50572017-10-06 17:00:28 +00001986 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00001987 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00001988 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001989 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
1990 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00001991 return;
1992
Alexey Bataeve3727102018-04-18 15:57:46 +00001993 SourceLocation ELoc = E->getExprLoc();
1994 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001995 // The default(none) clause requires that each variable that is referenced
1996 // in the construct, and does not have a predetermined data-sharing
1997 // attribute, must have its data-sharing attribute explicitly determined
1998 // by being listed in a data-sharing attribute clause.
1999 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002000 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002001 VarsWithInheritedDSA.count(VD) == 0) {
2002 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002003 return;
2004 }
2005
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002006 if (isOpenMPTargetExecutionDirective(DKind) &&
2007 !Stack->isLoopControlVariable(VD).first) {
2008 if (!Stack->checkMappableExprComponentListsForDecl(
2009 VD, /*CurrentRegionOnly=*/true,
2010 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2011 StackComponents,
2012 OpenMPClauseKind) {
2013 // Variable is used if it has been marked as an array, array
2014 // section or the variable iself.
2015 return StackComponents.size() == 1 ||
2016 std::all_of(
2017 std::next(StackComponents.rbegin()),
2018 StackComponents.rend(),
2019 [](const OMPClauseMappableExprCommon::
2020 MappableComponent &MC) {
2021 return MC.getAssociatedDeclaration() ==
2022 nullptr &&
2023 (isa<OMPArraySectionExpr>(
2024 MC.getAssociatedExpression()) ||
2025 isa<ArraySubscriptExpr>(
2026 MC.getAssociatedExpression()));
2027 });
2028 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002029 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002030 // By default lambdas are captured as firstprivates.
2031 if (const auto *RD =
2032 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002033 IsFirstprivate = RD->isLambda();
2034 IsFirstprivate =
2035 IsFirstprivate ||
2036 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002037 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002038 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002039 ImplicitFirstprivate.emplace_back(E);
2040 else
2041 ImplicitMap.emplace_back(E);
2042 return;
2043 }
2044 }
2045
Alexey Bataev758e55e2013-09-06 18:03:48 +00002046 // OpenMP [2.9.3.6, Restrictions, p.2]
2047 // A list item that appears in a reduction clause of the innermost
2048 // enclosing worksharing or parallel construct may not be accessed in an
2049 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002050 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002051 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2052 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002053 return isOpenMPParallelDirective(K) ||
2054 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2055 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002056 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002057 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002058 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002059 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002060 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002061 return;
2062 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002063
2064 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002065 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002066 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2067 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002068 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002069 }
2070 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002071 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002072 if (E->isTypeDependent() || E->isValueDependent() ||
2073 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2074 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002075 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002076 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002077 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002078 if (!FD)
2079 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002080 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002081 // Check if the variable has explicit DSA set and stop analysis if it
2082 // so.
2083 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2084 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002085
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002086 if (isOpenMPTargetExecutionDirective(DKind) &&
2087 !Stack->isLoopControlVariable(FD).first &&
2088 !Stack->checkMappableExprComponentListsForDecl(
2089 FD, /*CurrentRegionOnly=*/true,
2090 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2091 StackComponents,
2092 OpenMPClauseKind) {
2093 return isa<CXXThisExpr>(
2094 cast<MemberExpr>(
2095 StackComponents.back().getAssociatedExpression())
2096 ->getBase()
2097 ->IgnoreParens());
2098 })) {
2099 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2100 // A bit-field cannot appear in a map clause.
2101 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002102 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002103 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002104 ImplicitMap.emplace_back(E);
2105 return;
2106 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002107
Alexey Bataeve3727102018-04-18 15:57:46 +00002108 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002109 // OpenMP [2.9.3.6, Restrictions, p.2]
2110 // A list item that appears in a reduction clause of the innermost
2111 // enclosing worksharing or parallel construct may not be accessed in
2112 // an explicit task.
2113 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002114 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2115 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002116 return isOpenMPParallelDirective(K) ||
2117 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2118 },
2119 /*FromParent=*/true);
2120 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2121 ErrorFound = true;
2122 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002123 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002124 return;
2125 }
2126
2127 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002128 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002129 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2130 !Stack->isLoopControlVariable(FD).first)
2131 ImplicitFirstprivate.push_back(E);
2132 return;
2133 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002134 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002135 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002136 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002137 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002138 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002139 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002140 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2141 if (!Stack->checkMappableExprComponentListsForDecl(
2142 VD, /*CurrentRegionOnly=*/true,
2143 [&CurComponents](
2144 OMPClauseMappableExprCommon::MappableExprComponentListRef
2145 StackComponents,
2146 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002147 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002148 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002149 for (const auto &SC : llvm::reverse(StackComponents)) {
2150 // Do both expressions have the same kind?
2151 if (CCI->getAssociatedExpression()->getStmtClass() !=
2152 SC.getAssociatedExpression()->getStmtClass())
2153 if (!(isa<OMPArraySectionExpr>(
2154 SC.getAssociatedExpression()) &&
2155 isa<ArraySubscriptExpr>(
2156 CCI->getAssociatedExpression())))
2157 return false;
2158
Alexey Bataeve3727102018-04-18 15:57:46 +00002159 const Decl *CCD = CCI->getAssociatedDeclaration();
2160 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002161 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2162 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2163 if (SCD != CCD)
2164 return false;
2165 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002166 if (CCI == CCE)
2167 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002168 }
2169 return true;
2170 })) {
2171 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002172 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002173 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002174 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002175 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002176 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002177 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002178 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002179 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002180 // for task|target directives.
2181 // Skip analysis of arguments of implicitly defined map clause for target
2182 // directives.
2183 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2184 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002185 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002186 if (CC)
2187 Visit(CC);
2188 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002189 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002190 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002191 }
2192 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002193 for (Stmt *C : S->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002194 if (C && !isa<OMPExecutableDirective>(C))
2195 Visit(C);
2196 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002197 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002198
Alexey Bataeve3727102018-04-18 15:57:46 +00002199 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002200 ArrayRef<Expr *> getImplicitFirstprivate() const {
2201 return ImplicitFirstprivate;
2202 }
2203 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002204 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002205 return VarsWithInheritedDSA;
2206 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002207
Alexey Bataev7ff55242014-06-19 09:13:45 +00002208 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2209 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002210};
Alexey Bataeved09d242014-05-28 05:53:51 +00002211} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002212
Alexey Bataevbae9a792014-06-27 10:37:06 +00002213void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002214 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002215 case OMPD_parallel:
2216 case OMPD_parallel_for:
2217 case OMPD_parallel_for_simd:
2218 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002219 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002220 case OMPD_teams_distribute:
2221 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002222 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002223 QualType KmpInt32PtrTy =
2224 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002225 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002226 std::make_pair(".global_tid.", KmpInt32PtrTy),
2227 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2228 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002229 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002230 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2231 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002232 break;
2233 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002234 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002235 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002236 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002237 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002238 case OMPD_target_teams_distribute:
2239 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002240 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2241 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2242 QualType KmpInt32PtrTy =
2243 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2244 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002245 FunctionProtoType::ExtProtoInfo EPI;
2246 EPI.Variadic = true;
2247 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2248 Sema::CapturedParamNameType Params[] = {
2249 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002250 std::make_pair(".part_id.", KmpInt32PtrTy),
2251 std::make_pair(".privates.", VoidPtrTy),
2252 std::make_pair(
2253 ".copy_fn.",
2254 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002255 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2256 std::make_pair(StringRef(), QualType()) // __context with shared vars
2257 };
2258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2259 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002260 // Mark this captured region as inlined, because we don't use outlined
2261 // function directly.
2262 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2263 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002264 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002265 Sema::CapturedParamNameType ParamsTarget[] = {
2266 std::make_pair(StringRef(), QualType()) // __context with shared vars
2267 };
2268 // Start a captured region for 'target' with no implicit parameters.
2269 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2270 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002271 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002272 std::make_pair(".global_tid.", KmpInt32PtrTy),
2273 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2274 std::make_pair(StringRef(), QualType()) // __context with shared vars
2275 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002276 // Start a captured region for 'teams' or 'parallel'. Both regions have
2277 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002278 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002279 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002280 break;
2281 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002282 case OMPD_target:
2283 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002284 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2285 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2286 QualType KmpInt32PtrTy =
2287 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2288 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002289 FunctionProtoType::ExtProtoInfo EPI;
2290 EPI.Variadic = true;
2291 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2292 Sema::CapturedParamNameType Params[] = {
2293 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002294 std::make_pair(".part_id.", KmpInt32PtrTy),
2295 std::make_pair(".privates.", VoidPtrTy),
2296 std::make_pair(
2297 ".copy_fn.",
2298 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002299 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2300 std::make_pair(StringRef(), QualType()) // __context with shared vars
2301 };
2302 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2303 Params);
2304 // Mark this captured region as inlined, because we don't use outlined
2305 // function directly.
2306 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2307 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002308 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002309 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2310 std::make_pair(StringRef(), QualType()));
2311 break;
2312 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002313 case OMPD_simd:
2314 case OMPD_for:
2315 case OMPD_for_simd:
2316 case OMPD_sections:
2317 case OMPD_section:
2318 case OMPD_single:
2319 case OMPD_master:
2320 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002321 case OMPD_taskgroup:
2322 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002323 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002324 case OMPD_ordered:
2325 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002326 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002327 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002328 std::make_pair(StringRef(), QualType()) // __context with shared vars
2329 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002330 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2331 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002332 break;
2333 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002334 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002335 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2336 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2337 QualType KmpInt32PtrTy =
2338 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2339 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002340 FunctionProtoType::ExtProtoInfo EPI;
2341 EPI.Variadic = true;
2342 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002343 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002344 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002345 std::make_pair(".part_id.", KmpInt32PtrTy),
2346 std::make_pair(".privates.", VoidPtrTy),
2347 std::make_pair(
2348 ".copy_fn.",
2349 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002350 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002351 std::make_pair(StringRef(), QualType()) // __context with shared vars
2352 };
2353 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2354 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002355 // Mark this captured region as inlined, because we don't use outlined
2356 // function directly.
2357 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2358 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002359 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002360 break;
2361 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002362 case OMPD_taskloop:
2363 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002364 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002365 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2366 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002367 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002368 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2369 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002370 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002371 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2372 .withConst();
2373 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2374 QualType KmpInt32PtrTy =
2375 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2376 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002377 FunctionProtoType::ExtProtoInfo EPI;
2378 EPI.Variadic = true;
2379 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002380 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002381 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002382 std::make_pair(".part_id.", KmpInt32PtrTy),
2383 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002384 std::make_pair(
2385 ".copy_fn.",
2386 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2387 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2388 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002389 std::make_pair(".ub.", KmpUInt64Ty),
2390 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002391 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002392 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002393 std::make_pair(StringRef(), QualType()) // __context with shared vars
2394 };
2395 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2396 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002397 // Mark this captured region as inlined, because we don't use outlined
2398 // function directly.
2399 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2400 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002401 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002402 break;
2403 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002404 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002405 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002406 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002407 QualType KmpInt32PtrTy =
2408 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2409 Sema::CapturedParamNameType Params[] = {
2410 std::make_pair(".global_tid.", KmpInt32PtrTy),
2411 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002412 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2413 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002414 std::make_pair(StringRef(), QualType()) // __context with shared vars
2415 };
2416 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2417 Params);
2418 break;
2419 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002420 case OMPD_target_teams_distribute_parallel_for:
2421 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002422 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002423 QualType KmpInt32PtrTy =
2424 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002425 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002426
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002427 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002428 FunctionProtoType::ExtProtoInfo EPI;
2429 EPI.Variadic = true;
2430 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2431 Sema::CapturedParamNameType Params[] = {
2432 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002433 std::make_pair(".part_id.", KmpInt32PtrTy),
2434 std::make_pair(".privates.", VoidPtrTy),
2435 std::make_pair(
2436 ".copy_fn.",
2437 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002438 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2439 std::make_pair(StringRef(), QualType()) // __context with shared vars
2440 };
2441 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2442 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002443 // Mark this captured region as inlined, because we don't use outlined
2444 // function directly.
2445 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2446 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002447 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002448 Sema::CapturedParamNameType ParamsTarget[] = {
2449 std::make_pair(StringRef(), QualType()) // __context with shared vars
2450 };
2451 // Start a captured region for 'target' with no implicit parameters.
2452 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2453 ParamsTarget);
2454
2455 Sema::CapturedParamNameType ParamsTeams[] = {
2456 std::make_pair(".global_tid.", KmpInt32PtrTy),
2457 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2458 std::make_pair(StringRef(), QualType()) // __context with shared vars
2459 };
2460 // Start a captured region for 'target' with no implicit parameters.
2461 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2462 ParamsTeams);
2463
2464 Sema::CapturedParamNameType ParamsParallel[] = {
2465 std::make_pair(".global_tid.", KmpInt32PtrTy),
2466 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002467 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2468 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002469 std::make_pair(StringRef(), QualType()) // __context with shared vars
2470 };
2471 // Start a captured region for 'teams' or 'parallel'. Both regions have
2472 // the same implicit parameters.
2473 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2474 ParamsParallel);
2475 break;
2476 }
2477
Alexey Bataev46506272017-12-05 17:41:34 +00002478 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002479 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002480 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002481 QualType KmpInt32PtrTy =
2482 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2483
2484 Sema::CapturedParamNameType ParamsTeams[] = {
2485 std::make_pair(".global_tid.", KmpInt32PtrTy),
2486 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2487 std::make_pair(StringRef(), QualType()) // __context with shared vars
2488 };
2489 // Start a captured region for 'target' with no implicit parameters.
2490 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2491 ParamsTeams);
2492
2493 Sema::CapturedParamNameType ParamsParallel[] = {
2494 std::make_pair(".global_tid.", KmpInt32PtrTy),
2495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002496 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2497 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002498 std::make_pair(StringRef(), QualType()) // __context with shared vars
2499 };
2500 // Start a captured region for 'teams' or 'parallel'. Both regions have
2501 // the same implicit parameters.
2502 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2503 ParamsParallel);
2504 break;
2505 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002506 case OMPD_target_update:
2507 case OMPD_target_enter_data:
2508 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002509 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2510 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2511 QualType KmpInt32PtrTy =
2512 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2513 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002514 FunctionProtoType::ExtProtoInfo EPI;
2515 EPI.Variadic = true;
2516 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2517 Sema::CapturedParamNameType Params[] = {
2518 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002519 std::make_pair(".part_id.", KmpInt32PtrTy),
2520 std::make_pair(".privates.", VoidPtrTy),
2521 std::make_pair(
2522 ".copy_fn.",
2523 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002524 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2525 std::make_pair(StringRef(), QualType()) // __context with shared vars
2526 };
2527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2528 Params);
2529 // Mark this captured region as inlined, because we don't use outlined
2530 // function directly.
2531 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2532 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002533 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002534 break;
2535 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002536 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002537 case OMPD_taskyield:
2538 case OMPD_barrier:
2539 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002540 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002541 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002542 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002543 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002544 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002545 case OMPD_declare_target:
2546 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00002547 llvm_unreachable("OpenMP Directive is not allowed");
2548 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002549 llvm_unreachable("Unknown OpenMP directive");
2550 }
2551}
2552
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002553int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2554 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2555 getOpenMPCaptureRegions(CaptureRegions, DKind);
2556 return CaptureRegions.size();
2557}
2558
Alexey Bataev3392d762016-02-16 11:18:12 +00002559static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002560 Expr *CaptureExpr, bool WithInit,
2561 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002562 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002563 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002564 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002565 QualType Ty = Init->getType();
2566 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002567 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002568 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002569 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002570 Ty = C.getPointerType(Ty);
2571 ExprResult Res =
2572 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2573 if (!Res.isUsable())
2574 return nullptr;
2575 Init = Res.get();
2576 }
Alexey Bataev61205072016-03-02 04:57:40 +00002577 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002578 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002579 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002580 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002581 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002582 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002583 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002584 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002585 return CED;
2586}
2587
Alexey Bataev61205072016-03-02 04:57:40 +00002588static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2589 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002590 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002591 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002592 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002593 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002594 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2595 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002596 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002597 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002598}
2599
Alexey Bataev5a3af132016-03-29 08:58:54 +00002600static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002601 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002602 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002603 OMPCapturedExprDecl *CD = buildCaptureDecl(
2604 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2605 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002606 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2607 CaptureExpr->getExprLoc());
2608 }
2609 ExprResult Res = Ref;
2610 if (!S.getLangOpts().CPlusPlus &&
2611 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002612 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002613 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002614 if (!Res.isUsable())
2615 return ExprError();
2616 }
2617 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002618}
2619
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002620namespace {
2621// OpenMP directives parsed in this section are represented as a
2622// CapturedStatement with an associated statement. If a syntax error
2623// is detected during the parsing of the associated statement, the
2624// compiler must abort processing and close the CapturedStatement.
2625//
2626// Combined directives such as 'target parallel' have more than one
2627// nested CapturedStatements. This RAII ensures that we unwind out
2628// of all the nested CapturedStatements when an error is found.
2629class CaptureRegionUnwinderRAII {
2630private:
2631 Sema &S;
2632 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002633 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002634
2635public:
2636 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2637 OpenMPDirectiveKind DKind)
2638 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2639 ~CaptureRegionUnwinderRAII() {
2640 if (ErrorFound) {
2641 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2642 while (--ThisCaptureLevel >= 0)
2643 S.ActOnCapturedRegionError();
2644 }
2645 }
2646};
2647} // namespace
2648
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002649StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2650 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002651 bool ErrorFound = false;
2652 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2653 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002654 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002655 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002656 return StmtError();
2657 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002658
Alexey Bataev2ba67042017-11-28 21:11:44 +00002659 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2660 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002661 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002662 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002663 SmallVector<const OMPLinearClause *, 4> LCs;
2664 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002665 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002666 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002667 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2668 Clause->getClauseKind() == OMPC_in_reduction) {
2669 // Capture taskgroup task_reduction descriptors inside the tasking regions
2670 // with the corresponding in_reduction items.
2671 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002672 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002673 if (E)
2674 MarkDeclarationsReferencedInExpr(E);
2675 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002676 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002677 Clause->getClauseKind() == OMPC_copyprivate ||
2678 (getLangOpts().OpenMPUseTLS &&
2679 getASTContext().getTargetInfo().isTLSSupported() &&
2680 Clause->getClauseKind() == OMPC_copyin)) {
2681 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002682 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002683 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002684 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002685 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002686 }
2687 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002688 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002689 } else if (CaptureRegions.size() > 1 ||
2690 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002691 if (auto *C = OMPClauseWithPreInit::get(Clause))
2692 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002693 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002694 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002695 MarkDeclarationsReferencedInExpr(E);
2696 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002697 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002698 if (Clause->getClauseKind() == OMPC_schedule)
2699 SC = cast<OMPScheduleClause>(Clause);
2700 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002701 OC = cast<OMPOrderedClause>(Clause);
2702 else if (Clause->getClauseKind() == OMPC_linear)
2703 LCs.push_back(cast<OMPLinearClause>(Clause));
2704 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002705 // OpenMP, 2.7.1 Loop Construct, Restrictions
2706 // The nonmonotonic modifier cannot be specified if an ordered clause is
2707 // specified.
2708 if (SC &&
2709 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2710 SC->getSecondScheduleModifier() ==
2711 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2712 OC) {
2713 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2714 ? SC->getFirstScheduleModifierLoc()
2715 : SC->getSecondScheduleModifierLoc(),
2716 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002717 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002718 ErrorFound = true;
2719 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002720 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002721 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002722 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002723 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002724 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002725 ErrorFound = true;
2726 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002727 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2728 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2729 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002730 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002731 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2732 ErrorFound = true;
2733 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002734 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002735 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002736 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002737 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002738 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002739 // Mark all variables in private list clauses as used in inner region.
2740 // Required for proper codegen of combined directives.
2741 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002742 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002743 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002744 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2745 // Find the particular capture region for the clause if the
2746 // directive is a combined one with multiple capture regions.
2747 // If the directive is not a combined one, the capture region
2748 // associated with the clause is OMPD_unknown and is generated
2749 // only once.
2750 if (CaptureRegion == ThisCaptureRegion ||
2751 CaptureRegion == OMPD_unknown) {
2752 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002753 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002754 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2755 }
2756 }
2757 }
2758 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002759 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002760 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002761 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002762}
2763
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002764static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2765 OpenMPDirectiveKind CancelRegion,
2766 SourceLocation StartLoc) {
2767 // CancelRegion is only needed for cancel and cancellation_point.
2768 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2769 return false;
2770
2771 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2772 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2773 return false;
2774
2775 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2776 << getOpenMPDirectiveName(CancelRegion);
2777 return true;
2778}
2779
Alexey Bataeve3727102018-04-18 15:57:46 +00002780static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002781 OpenMPDirectiveKind CurrentRegion,
2782 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002783 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002784 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002785 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002786 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2787 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002788 bool NestingProhibited = false;
2789 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002790 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002791 enum {
2792 NoRecommend,
2793 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002794 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002795 ShouldBeInTargetRegion,
2796 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002797 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002798 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002799 // OpenMP [2.16, Nesting of Regions]
2800 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002801 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002802 // An ordered construct with the simd clause is the only OpenMP
2803 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002804 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002805 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2806 // message.
2807 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2808 ? diag::err_omp_prohibited_region_simd
2809 : diag::warn_omp_nesting_simd);
2810 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002811 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002812 if (ParentRegion == OMPD_atomic) {
2813 // OpenMP [2.16, Nesting of Regions]
2814 // OpenMP constructs may not be nested inside an atomic region.
2815 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2816 return true;
2817 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002818 if (CurrentRegion == OMPD_section) {
2819 // OpenMP [2.7.2, sections Construct, Restrictions]
2820 // Orphaned section directives are prohibited. That is, the section
2821 // directives must appear within the sections construct and must not be
2822 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002823 if (ParentRegion != OMPD_sections &&
2824 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002825 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2826 << (ParentRegion != OMPD_unknown)
2827 << getOpenMPDirectiveName(ParentRegion);
2828 return true;
2829 }
2830 return false;
2831 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002832 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002833 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002834 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002835 if (ParentRegion == OMPD_unknown &&
2836 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002837 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002838 if (CurrentRegion == OMPD_cancellation_point ||
2839 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002840 // OpenMP [2.16, Nesting of Regions]
2841 // A cancellation point construct for which construct-type-clause is
2842 // taskgroup must be nested inside a task construct. A cancellation
2843 // point construct for which construct-type-clause is not taskgroup must
2844 // be closely nested inside an OpenMP construct that matches the type
2845 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002846 // A cancel construct for which construct-type-clause is taskgroup must be
2847 // nested inside a task construct. A cancel construct for which
2848 // construct-type-clause is not taskgroup must be closely nested inside an
2849 // OpenMP construct that matches the type specified in
2850 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002851 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002852 !((CancelRegion == OMPD_parallel &&
2853 (ParentRegion == OMPD_parallel ||
2854 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002855 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002856 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002857 ParentRegion == OMPD_target_parallel_for ||
2858 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002859 ParentRegion == OMPD_teams_distribute_parallel_for ||
2860 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002861 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2862 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002863 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2864 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002865 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002866 // OpenMP [2.16, Nesting of Regions]
2867 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002868 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002869 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002870 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002871 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2872 // OpenMP [2.16, Nesting of Regions]
2873 // A critical region may not be nested (closely or otherwise) inside a
2874 // critical region with the same name. Note that this restriction is not
2875 // sufficient to prevent deadlock.
2876 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002877 bool DeadLock = Stack->hasDirective(
2878 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2879 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00002880 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00002881 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2882 PreviousCriticalLoc = Loc;
2883 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002884 }
2885 return false;
David Majnemer9d168222016-08-05 17:44:54 +00002886 },
2887 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002888 if (DeadLock) {
2889 SemaRef.Diag(StartLoc,
2890 diag::err_omp_prohibited_region_critical_same_name)
2891 << CurrentName.getName();
2892 if (PreviousCriticalLoc.isValid())
2893 SemaRef.Diag(PreviousCriticalLoc,
2894 diag::note_omp_previous_critical_region);
2895 return true;
2896 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002897 } else if (CurrentRegion == OMPD_barrier) {
2898 // OpenMP [2.16, Nesting of Regions]
2899 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002900 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002901 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2902 isOpenMPTaskingDirective(ParentRegion) ||
2903 ParentRegion == OMPD_master ||
2904 ParentRegion == OMPD_critical ||
2905 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002906 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002907 !isOpenMPParallelDirective(CurrentRegion) &&
2908 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002909 // OpenMP [2.16, Nesting of Regions]
2910 // A worksharing region may not be closely nested inside a worksharing,
2911 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002912 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2913 isOpenMPTaskingDirective(ParentRegion) ||
2914 ParentRegion == OMPD_master ||
2915 ParentRegion == OMPD_critical ||
2916 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002917 Recommend = ShouldBeInParallelRegion;
2918 } else if (CurrentRegion == OMPD_ordered) {
2919 // OpenMP [2.16, Nesting of Regions]
2920 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002921 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002922 // An ordered region must be closely nested inside a loop region (or
2923 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002924 // OpenMP [2.8.1,simd Construct, Restrictions]
2925 // An ordered construct with the simd clause is the only OpenMP construct
2926 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002927 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002928 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002929 !(isOpenMPSimdDirective(ParentRegion) ||
2930 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002931 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00002932 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002933 // OpenMP [2.16, Nesting of Regions]
2934 // If specified, a teams construct must be contained within a target
2935 // construct.
2936 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00002937 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002938 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002939 }
Kelvin Libf594a52016-12-17 05:48:59 +00002940 if (!NestingProhibited &&
2941 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2942 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2943 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00002944 // OpenMP [2.16, Nesting of Regions]
2945 // distribute, parallel, parallel sections, parallel workshare, and the
2946 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2947 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002948 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2949 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002950 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002951 }
David Majnemer9d168222016-08-05 17:44:54 +00002952 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00002953 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002954 // OpenMP 4.5 [2.17 Nesting of Regions]
2955 // The region associated with the distribute construct must be strictly
2956 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00002957 NestingProhibited =
2958 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002959 Recommend = ShouldBeInTeamsRegion;
2960 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002961 if (!NestingProhibited &&
2962 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2963 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2964 // OpenMP 4.5 [2.17 Nesting of Regions]
2965 // If a target, target update, target data, target enter data, or
2966 // target exit data construct is encountered during execution of a
2967 // target region, the behavior is unspecified.
2968 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002969 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00002970 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002971 if (isOpenMPTargetExecutionDirective(K)) {
2972 OffendingRegion = K;
2973 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002974 }
2975 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002976 },
2977 false /* don't skip top directive */);
2978 CloseNesting = false;
2979 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002980 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00002981 if (OrphanSeen) {
2982 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2983 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2984 } else {
2985 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2986 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2987 << Recommend << getOpenMPDirectiveName(CurrentRegion);
2988 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002989 return true;
2990 }
2991 }
2992 return false;
2993}
2994
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002995static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2996 ArrayRef<OMPClause *> Clauses,
2997 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2998 bool ErrorFound = false;
2999 unsigned NamedModifiersNumber = 0;
3000 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3001 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003002 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003003 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003004 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3005 // At most one if clause without a directive-name-modifier can appear on
3006 // the directive.
3007 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3008 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003009 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003010 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3011 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3012 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003013 } else if (CurNM != OMPD_unknown) {
3014 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003015 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003016 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003017 FoundNameModifiers[CurNM] = IC;
3018 if (CurNM == OMPD_unknown)
3019 continue;
3020 // Check if the specified name modifier is allowed for the current
3021 // directive.
3022 // At most one if clause with the particular directive-name-modifier can
3023 // appear on the directive.
3024 bool MatchFound = false;
3025 for (auto NM : AllowedNameModifiers) {
3026 if (CurNM == NM) {
3027 MatchFound = true;
3028 break;
3029 }
3030 }
3031 if (!MatchFound) {
3032 S.Diag(IC->getNameModifierLoc(),
3033 diag::err_omp_wrong_if_directive_name_modifier)
3034 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3035 ErrorFound = true;
3036 }
3037 }
3038 }
3039 // If any if clause on the directive includes a directive-name-modifier then
3040 // all if clauses on the directive must include a directive-name-modifier.
3041 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3042 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003043 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003044 diag::err_omp_no_more_if_clause);
3045 } else {
3046 std::string Values;
3047 std::string Sep(", ");
3048 unsigned AllowedCnt = 0;
3049 unsigned TotalAllowedNum =
3050 AllowedNameModifiers.size() - NamedModifiersNumber;
3051 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3052 ++Cnt) {
3053 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3054 if (!FoundNameModifiers[NM]) {
3055 Values += "'";
3056 Values += getOpenMPDirectiveName(NM);
3057 Values += "'";
3058 if (AllowedCnt + 2 == TotalAllowedNum)
3059 Values += " or ";
3060 else if (AllowedCnt + 1 != TotalAllowedNum)
3061 Values += Sep;
3062 ++AllowedCnt;
3063 }
3064 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003065 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003066 diag::err_omp_unnamed_if_clause)
3067 << (TotalAllowedNum > 1) << Values;
3068 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003069 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003070 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3071 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003072 ErrorFound = true;
3073 }
3074 return ErrorFound;
3075}
3076
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003077StmtResult Sema::ActOnOpenMPExecutableDirective(
3078 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3079 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3080 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003081 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003082 // First check CancelRegion which is then used in checkNestingOfRegions.
3083 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3084 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003085 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003086 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003087
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003088 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003089 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003090 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003091 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003092 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003093 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3094
3095 // Check default data sharing attributes for referenced variables.
3096 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003097 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3098 Stmt *S = AStmt;
3099 while (--ThisCaptureLevel >= 0)
3100 S = cast<CapturedStmt>(S)->getCapturedStmt();
3101 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003102 if (DSAChecker.isErrorFound())
3103 return StmtError();
3104 // Generate list of implicitly defined firstprivate variables.
3105 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003106
Alexey Bataev88202be2017-07-27 13:20:36 +00003107 SmallVector<Expr *, 4> ImplicitFirstprivates(
3108 DSAChecker.getImplicitFirstprivate().begin(),
3109 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003110 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3111 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003112 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003113 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003114 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003115 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003116 if (E)
3117 ImplicitFirstprivates.emplace_back(E);
3118 }
3119 }
3120 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003121 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003122 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3123 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003124 ClausesWithImplicit.push_back(Implicit);
3125 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003126 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003127 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003128 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003129 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003130 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003131 if (!ImplicitMaps.empty()) {
3132 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3133 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3134 SourceLocation(), SourceLocation(), ImplicitMaps,
3135 SourceLocation(), SourceLocation(), SourceLocation())) {
3136 ClausesWithImplicit.emplace_back(Implicit);
3137 ErrorFound |=
3138 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003139 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003140 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003141 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003142 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003143 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003144
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003145 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003146 switch (Kind) {
3147 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003148 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3149 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003150 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003151 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003152 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003153 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3154 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003155 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003156 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003157 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3158 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003159 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003160 case OMPD_for_simd:
3161 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3162 EndLoc, VarsWithInheritedDSA);
3163 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003164 case OMPD_sections:
3165 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3166 EndLoc);
3167 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003168 case OMPD_section:
3169 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003170 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003171 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3172 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003173 case OMPD_single:
3174 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3175 EndLoc);
3176 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003177 case OMPD_master:
3178 assert(ClausesWithImplicit.empty() &&
3179 "No clauses are allowed for 'omp master' directive");
3180 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3181 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003182 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003183 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3184 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003185 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003186 case OMPD_parallel_for:
3187 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3188 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003189 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003190 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003191 case OMPD_parallel_for_simd:
3192 Res = ActOnOpenMPParallelForSimdDirective(
3193 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003194 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003195 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003196 case OMPD_parallel_sections:
3197 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3198 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003199 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003200 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003201 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003202 Res =
3203 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003204 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003205 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003206 case OMPD_taskyield:
3207 assert(ClausesWithImplicit.empty() &&
3208 "No clauses are allowed for 'omp taskyield' directive");
3209 assert(AStmt == nullptr &&
3210 "No associated statement allowed for 'omp taskyield' directive");
3211 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3212 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003213 case OMPD_barrier:
3214 assert(ClausesWithImplicit.empty() &&
3215 "No clauses are allowed for 'omp barrier' directive");
3216 assert(AStmt == nullptr &&
3217 "No associated statement allowed for 'omp barrier' directive");
3218 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3219 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003220 case OMPD_taskwait:
3221 assert(ClausesWithImplicit.empty() &&
3222 "No clauses are allowed for 'omp taskwait' directive");
3223 assert(AStmt == nullptr &&
3224 "No associated statement allowed for 'omp taskwait' directive");
3225 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3226 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003227 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003228 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3229 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003230 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003231 case OMPD_flush:
3232 assert(AStmt == nullptr &&
3233 "No associated statement allowed for 'omp flush' directive");
3234 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3235 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003236 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003237 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3238 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003239 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003240 case OMPD_atomic:
3241 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3242 EndLoc);
3243 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003244 case OMPD_teams:
3245 Res =
3246 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3247 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003248 case OMPD_target:
3249 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3250 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003251 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003252 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003253 case OMPD_target_parallel:
3254 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3255 StartLoc, EndLoc);
3256 AllowedNameModifiers.push_back(OMPD_target);
3257 AllowedNameModifiers.push_back(OMPD_parallel);
3258 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003259 case OMPD_target_parallel_for:
3260 Res = ActOnOpenMPTargetParallelForDirective(
3261 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3262 AllowedNameModifiers.push_back(OMPD_target);
3263 AllowedNameModifiers.push_back(OMPD_parallel);
3264 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003265 case OMPD_cancellation_point:
3266 assert(ClausesWithImplicit.empty() &&
3267 "No clauses are allowed for 'omp cancellation point' directive");
3268 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3269 "cancellation point' directive");
3270 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3271 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003272 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003273 assert(AStmt == nullptr &&
3274 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003275 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3276 CancelRegion);
3277 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003278 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003279 case OMPD_target_data:
3280 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3281 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003282 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003283 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003284 case OMPD_target_enter_data:
3285 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003286 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003287 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3288 break;
Samuel Antao72590762016-01-19 20:04:50 +00003289 case OMPD_target_exit_data:
3290 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003291 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003292 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3293 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003294 case OMPD_taskloop:
3295 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3296 EndLoc, VarsWithInheritedDSA);
3297 AllowedNameModifiers.push_back(OMPD_taskloop);
3298 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003299 case OMPD_taskloop_simd:
3300 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3301 EndLoc, VarsWithInheritedDSA);
3302 AllowedNameModifiers.push_back(OMPD_taskloop);
3303 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003304 case OMPD_distribute:
3305 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3306 EndLoc, VarsWithInheritedDSA);
3307 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003308 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003309 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3310 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003311 AllowedNameModifiers.push_back(OMPD_target_update);
3312 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003313 case OMPD_distribute_parallel_for:
3314 Res = ActOnOpenMPDistributeParallelForDirective(
3315 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3316 AllowedNameModifiers.push_back(OMPD_parallel);
3317 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003318 case OMPD_distribute_parallel_for_simd:
3319 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3320 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3321 AllowedNameModifiers.push_back(OMPD_parallel);
3322 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003323 case OMPD_distribute_simd:
3324 Res = ActOnOpenMPDistributeSimdDirective(
3325 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3326 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003327 case OMPD_target_parallel_for_simd:
3328 Res = ActOnOpenMPTargetParallelForSimdDirective(
3329 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3330 AllowedNameModifiers.push_back(OMPD_target);
3331 AllowedNameModifiers.push_back(OMPD_parallel);
3332 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003333 case OMPD_target_simd:
3334 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3335 EndLoc, VarsWithInheritedDSA);
3336 AllowedNameModifiers.push_back(OMPD_target);
3337 break;
Kelvin Li02532872016-08-05 14:37:37 +00003338 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003339 Res = ActOnOpenMPTeamsDistributeDirective(
3340 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003341 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003342 case OMPD_teams_distribute_simd:
3343 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3344 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3345 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003346 case OMPD_teams_distribute_parallel_for_simd:
3347 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3348 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3349 AllowedNameModifiers.push_back(OMPD_parallel);
3350 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003351 case OMPD_teams_distribute_parallel_for:
3352 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3353 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3354 AllowedNameModifiers.push_back(OMPD_parallel);
3355 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003356 case OMPD_target_teams:
3357 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3358 EndLoc);
3359 AllowedNameModifiers.push_back(OMPD_target);
3360 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003361 case OMPD_target_teams_distribute:
3362 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3363 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3364 AllowedNameModifiers.push_back(OMPD_target);
3365 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003366 case OMPD_target_teams_distribute_parallel_for:
3367 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3368 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3369 AllowedNameModifiers.push_back(OMPD_target);
3370 AllowedNameModifiers.push_back(OMPD_parallel);
3371 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003372 case OMPD_target_teams_distribute_parallel_for_simd:
3373 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3374 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3375 AllowedNameModifiers.push_back(OMPD_target);
3376 AllowedNameModifiers.push_back(OMPD_parallel);
3377 break;
Kelvin Lida681182017-01-10 18:08:18 +00003378 case OMPD_target_teams_distribute_simd:
3379 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3380 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3381 AllowedNameModifiers.push_back(OMPD_target);
3382 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003383 case OMPD_declare_target:
3384 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003385 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003386 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003387 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003388 llvm_unreachable("OpenMP Directive is not allowed");
3389 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003390 llvm_unreachable("Unknown OpenMP directive");
3391 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003392
Alexey Bataeve3727102018-04-18 15:57:46 +00003393 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003394 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3395 << P.first << P.second->getSourceRange();
3396 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003397 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3398
3399 if (!AllowedNameModifiers.empty())
3400 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3401 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003402
Alexey Bataeved09d242014-05-28 05:53:51 +00003403 if (ErrorFound)
3404 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003405 return Res;
3406}
3407
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003408Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3409 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003410 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003411 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3412 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003413 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003414 assert(Linears.size() == LinModifiers.size());
3415 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003416 if (!DG || DG.get().isNull())
3417 return DeclGroupPtrTy();
3418
3419 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003420 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003421 return DG;
3422 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003423 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003424 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3425 ADecl = FTD->getTemplatedDecl();
3426
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003427 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3428 if (!FD) {
3429 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003430 return DeclGroupPtrTy();
3431 }
3432
Alexey Bataev2af33e32016-04-07 12:45:37 +00003433 // OpenMP [2.8.2, declare simd construct, Description]
3434 // The parameter of the simdlen clause must be a constant positive integer
3435 // expression.
3436 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003437 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003438 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003439 // OpenMP [2.8.2, declare simd construct, Description]
3440 // The special this pointer can be used as if was one of the arguments to the
3441 // function in any of the linear, aligned, or uniform clauses.
3442 // The uniform clause declares one or more arguments to have an invariant
3443 // value for all concurrent invocations of the function in the execution of a
3444 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003445 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3446 const Expr *UniformedLinearThis = nullptr;
3447 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003448 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003449 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3450 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003451 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3452 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003453 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003454 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003455 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003456 }
3457 if (isa<CXXThisExpr>(E)) {
3458 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003459 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003460 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003461 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3462 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003463 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003464 // OpenMP [2.8.2, declare simd construct, Description]
3465 // The aligned clause declares that the object to which each list item points
3466 // is aligned to the number of bytes expressed in the optional parameter of
3467 // the aligned clause.
3468 // The special this pointer can be used as if was one of the arguments to the
3469 // function in any of the linear, aligned, or uniform clauses.
3470 // The type of list items appearing in the aligned clause must be array,
3471 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003472 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3473 const Expr *AlignedThis = nullptr;
3474 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003475 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003476 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3477 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3478 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003479 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3480 FD->getParamDecl(PVD->getFunctionScopeIndex())
3481 ->getCanonicalDecl() == CanonPVD) {
3482 // OpenMP [2.8.1, simd construct, Restrictions]
3483 // A list-item cannot appear in more than one aligned clause.
3484 if (AlignedArgs.count(CanonPVD) > 0) {
3485 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3486 << 1 << E->getSourceRange();
3487 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3488 diag::note_omp_explicit_dsa)
3489 << getOpenMPClauseName(OMPC_aligned);
3490 continue;
3491 }
3492 AlignedArgs[CanonPVD] = E;
3493 QualType QTy = PVD->getType()
3494 .getNonReferenceType()
3495 .getUnqualifiedType()
3496 .getCanonicalType();
3497 const Type *Ty = QTy.getTypePtrOrNull();
3498 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3499 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3500 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3501 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3502 }
3503 continue;
3504 }
3505 }
3506 if (isa<CXXThisExpr>(E)) {
3507 if (AlignedThis) {
3508 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3509 << 2 << E->getSourceRange();
3510 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3511 << getOpenMPClauseName(OMPC_aligned);
3512 }
3513 AlignedThis = E;
3514 continue;
3515 }
3516 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3517 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3518 }
3519 // The optional parameter of the aligned clause, alignment, must be a constant
3520 // positive integer expression. If no optional parameter is specified,
3521 // implementation-defined default alignments for SIMD instructions on the
3522 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003523 SmallVector<const Expr *, 4> NewAligns;
3524 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003525 ExprResult Align;
3526 if (E)
3527 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3528 NewAligns.push_back(Align.get());
3529 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003530 // OpenMP [2.8.2, declare simd construct, Description]
3531 // The linear clause declares one or more list items to be private to a SIMD
3532 // lane and to have a linear relationship with respect to the iteration space
3533 // of a loop.
3534 // The special this pointer can be used as if was one of the arguments to the
3535 // function in any of the linear, aligned, or uniform clauses.
3536 // When a linear-step expression is specified in a linear clause it must be
3537 // either a constant integer expression or an integer-typed parameter that is
3538 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003539 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003540 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3541 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003542 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003543 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3544 ++MI;
3545 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003546 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3547 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3548 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003549 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3550 FD->getParamDecl(PVD->getFunctionScopeIndex())
3551 ->getCanonicalDecl() == CanonPVD) {
3552 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3553 // A list-item cannot appear in more than one linear clause.
3554 if (LinearArgs.count(CanonPVD) > 0) {
3555 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3556 << getOpenMPClauseName(OMPC_linear)
3557 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3558 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3559 diag::note_omp_explicit_dsa)
3560 << getOpenMPClauseName(OMPC_linear);
3561 continue;
3562 }
3563 // Each argument can appear in at most one uniform or linear clause.
3564 if (UniformedArgs.count(CanonPVD) > 0) {
3565 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3566 << getOpenMPClauseName(OMPC_linear)
3567 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3568 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3569 diag::note_omp_explicit_dsa)
3570 << getOpenMPClauseName(OMPC_uniform);
3571 continue;
3572 }
3573 LinearArgs[CanonPVD] = E;
3574 if (E->isValueDependent() || E->isTypeDependent() ||
3575 E->isInstantiationDependent() ||
3576 E->containsUnexpandedParameterPack())
3577 continue;
3578 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3579 PVD->getOriginalType());
3580 continue;
3581 }
3582 }
3583 if (isa<CXXThisExpr>(E)) {
3584 if (UniformedLinearThis) {
3585 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3586 << getOpenMPClauseName(OMPC_linear)
3587 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3588 << E->getSourceRange();
3589 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3590 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3591 : OMPC_linear);
3592 continue;
3593 }
3594 UniformedLinearThis = E;
3595 if (E->isValueDependent() || E->isTypeDependent() ||
3596 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3597 continue;
3598 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3599 E->getType());
3600 continue;
3601 }
3602 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3603 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3604 }
3605 Expr *Step = nullptr;
3606 Expr *NewStep = nullptr;
3607 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003608 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003609 // Skip the same step expression, it was checked already.
3610 if (Step == E || !E) {
3611 NewSteps.push_back(E ? NewStep : nullptr);
3612 continue;
3613 }
3614 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003615 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3616 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3617 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003618 if (UniformedArgs.count(CanonPVD) == 0) {
3619 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3620 << Step->getSourceRange();
3621 } else if (E->isValueDependent() || E->isTypeDependent() ||
3622 E->isInstantiationDependent() ||
3623 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003624 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003625 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003626 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003627 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3628 << Step->getSourceRange();
3629 }
3630 continue;
3631 }
3632 NewStep = Step;
3633 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3634 !Step->isInstantiationDependent() &&
3635 !Step->containsUnexpandedParameterPack()) {
3636 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3637 .get();
3638 if (NewStep)
3639 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3640 }
3641 NewSteps.push_back(NewStep);
3642 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003643 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3644 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003645 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003646 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3647 const_cast<Expr **>(Linears.data()), Linears.size(),
3648 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3649 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003650 ADecl->addAttr(NewAttr);
3651 return ConvertDeclToDeclGroup(ADecl);
3652}
3653
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003654StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3655 Stmt *AStmt,
3656 SourceLocation StartLoc,
3657 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003658 if (!AStmt)
3659 return StmtError();
3660
Alexey Bataeve3727102018-04-18 15:57:46 +00003661 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003662 // 1.2.2 OpenMP Language Terminology
3663 // Structured block - An executable statement with a single entry at the
3664 // top and a single exit at the bottom.
3665 // The point of exit cannot be a branch out of the structured block.
3666 // longjmp() and throw() must not violate the entry/exit criteria.
3667 CS->getCapturedDecl()->setNothrow();
3668
Reid Kleckner87a31802018-03-12 21:43:02 +00003669 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003670
Alexey Bataev25e5b442015-09-15 12:52:43 +00003671 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3672 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003673}
3674
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003675namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003676/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003677/// extracting iteration space of each loop in the loop nest, that will be used
3678/// for IR generation.
3679class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003680 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003681 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003682 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003683 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003684 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003685 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003686 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003687 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003688 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003689 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003690 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003691 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003692 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003693 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003694 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003695 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003696 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003697 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003698 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003699 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003700 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003701 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003702 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003703 /// Var < UB
3704 /// Var <= UB
3705 /// UB > Var
3706 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003707 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003708 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003709 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003710 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003711 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712
3713public:
3714 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003715 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003716 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003717 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003718 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003719 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003720 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003721 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003722 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003723 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003724 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003725 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003726 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003727 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003728 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003729 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003730 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003731 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003732 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003733 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003734 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003735 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003736 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003737 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003738 Expr *buildNumIterations(
3739 Scope *S, const bool LimitedType,
3740 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003741 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003742 Expr *
3743 buildPreCond(Scope *S, Expr *Cond,
3744 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003745 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003746 DeclRefExpr *
3747 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3748 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003749 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003750 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003751 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003752 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003753 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003754 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003755 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003756 /// Build loop data with counter value for depend clauses in ordered
3757 /// directives.
3758 Expr *
3759 buildOrderedLoopData(Scope *S, Expr *Counter,
3760 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3761 SourceLocation Loc, Expr *Inc = nullptr,
3762 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003763 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003764 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003765
3766private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003767 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003769 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003770 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003771 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003772 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003773 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003774 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003775 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003776 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003777};
3778
Alexey Bataeve3727102018-04-18 15:57:46 +00003779bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003780 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 assert(!LB && !UB && !Step);
3782 return false;
3783 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003784 return LCDecl->getType()->isDependentType() ||
3785 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3786 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003787}
3788
Alexey Bataeve3727102018-04-18 15:57:46 +00003789bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003790 Expr *NewLCRefExpr,
3791 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003792 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003793 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003794 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003795 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003796 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003797 LCDecl = getCanonicalDecl(NewLCDecl);
3798 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003799 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3800 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003801 if ((Ctor->isCopyOrMoveConstructor() ||
3802 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3803 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003804 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003805 LB = NewLB;
3806 return false;
3807}
3808
Alexey Bataeve3727102018-04-18 15:57:46 +00003809bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003810 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003811 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003812 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3813 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003814 if (!NewUB)
3815 return true;
3816 UB = NewUB;
3817 TestIsLessOp = LessOp;
3818 TestIsStrictOp = StrictOp;
3819 ConditionSrcRange = SR;
3820 ConditionLoc = SL;
3821 return false;
3822}
3823
Alexey Bataeve3727102018-04-18 15:57:46 +00003824bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003825 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003826 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003827 if (!NewStep)
3828 return true;
3829 if (!NewStep->isValueDependent()) {
3830 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003831 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003832 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3833 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003834 if (Val.isInvalid())
3835 return true;
3836 NewStep = Val.get();
3837
3838 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3839 // If test-expr is of form var relational-op b and relational-op is < or
3840 // <= then incr-expr must cause var to increase on each iteration of the
3841 // loop. If test-expr is of form var relational-op b and relational-op is
3842 // > or >= then incr-expr must cause var to decrease on each iteration of
3843 // the loop.
3844 // If test-expr is of form b relational-op var and relational-op is < or
3845 // <= then incr-expr must cause var to decrease on each iteration of the
3846 // loop. If test-expr is of form b relational-op var and relational-op is
3847 // > or >= then incr-expr must cause var to increase on each iteration of
3848 // the loop.
3849 llvm::APSInt Result;
3850 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3851 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3852 bool IsConstNeg =
3853 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003854 bool IsConstPos =
3855 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003856 bool IsConstZero = IsConstant && !Result.getBoolValue();
3857 if (UB && (IsConstZero ||
3858 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003859 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003860 SemaRef.Diag(NewStep->getExprLoc(),
3861 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003862 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003863 SemaRef.Diag(ConditionLoc,
3864 diag::note_omp_loop_cond_requres_compatible_incr)
3865 << TestIsLessOp << ConditionSrcRange;
3866 return true;
3867 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003868 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003869 NewStep =
3870 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3871 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003872 Subtract = !Subtract;
3873 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003874 }
3875
3876 Step = NewStep;
3877 SubtractStep = Subtract;
3878 return false;
3879}
3880
Alexey Bataeve3727102018-04-18 15:57:46 +00003881bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003882 // Check init-expr for canonical loop form and save loop counter
3883 // variable - #Var and its initialization value - #LB.
3884 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3885 // var = lb
3886 // integer-type var = lb
3887 // random-access-iterator-type var = lb
3888 // pointer-type var = lb
3889 //
3890 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003891 if (EmitDiags) {
3892 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3893 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 return true;
3895 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003896 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3897 if (!ExprTemp->cleanupsHaveSideEffects())
3898 S = ExprTemp->getSubExpr();
3899
Alexander Musmana5f070a2014-10-01 06:03:56 +00003900 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003901 if (Expr *E = dyn_cast<Expr>(S))
3902 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003903 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003904 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003905 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003906 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3907 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3908 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003909 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3910 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003911 }
3912 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3913 if (ME->isArrow() &&
3914 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00003915 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003916 }
3917 }
David Majnemer9d168222016-08-05 17:44:54 +00003918 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00003920 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003921 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003923 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003924 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003925 diag::ext_omp_loop_not_canonical_init)
3926 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00003927 return setLCDeclAndLB(
3928 Var,
3929 buildDeclRefExpr(SemaRef, Var,
3930 Var->getType().getNonReferenceType(),
3931 DS->getBeginLoc()),
3932 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003933 }
3934 }
3935 }
David Majnemer9d168222016-08-05 17:44:54 +00003936 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003938 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00003939 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003940 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3941 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003942 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3943 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944 }
3945 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3946 if (ME->isArrow() &&
3947 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00003948 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003949 }
3950 }
3951 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003952
Alexey Bataeve3727102018-04-18 15:57:46 +00003953 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003954 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003955 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003956 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00003957 << S->getSourceRange();
3958 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 return true;
3960}
3961
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003962/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003963/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00003964static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003965 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003966 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003967 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00003968 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003969 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003970 if ((Ctor->isCopyOrMoveConstructor() ||
3971 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3972 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003973 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003974 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3975 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003976 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003977 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003978 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003979 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3980 return getCanonicalDecl(ME->getMemberDecl());
3981 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003982}
3983
Alexey Bataeve3727102018-04-18 15:57:46 +00003984bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003985 // Check test-expr for canonical form, save upper-bound UB, flags for
3986 // less/greater and for strict/non-strict comparison.
3987 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3988 // var relational-op b
3989 // b relational-op var
3990 //
3991 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003993 return true;
3994 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003995 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003996 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00003997 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003998 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003999 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4000 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004001 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4002 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4003 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004004 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4005 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4007 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4008 BO->getSourceRange(), BO->getOperatorLoc());
4009 }
David Majnemer9d168222016-08-05 17:44:54 +00004010 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004011 if (CE->getNumArgs() == 2) {
4012 auto Op = CE->getOperator();
4013 switch (Op) {
4014 case OO_Greater:
4015 case OO_GreaterEqual:
4016 case OO_Less:
4017 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004018 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4019 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4021 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004022 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4023 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004024 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4025 CE->getOperatorLoc());
4026 break;
4027 default:
4028 break;
4029 }
4030 }
4031 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004032 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004034 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004035 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004036 return true;
4037}
4038
Alexey Bataeve3727102018-04-18 15:57:46 +00004039bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004040 // RHS of canonical loop form increment can be:
4041 // var + incr
4042 // incr + var
4043 // var - incr
4044 //
4045 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004046 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047 if (BO->isAdditiveOp()) {
4048 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004049 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4050 return setStep(BO->getRHS(), !IsAdd);
4051 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4052 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004053 }
David Majnemer9d168222016-08-05 17:44:54 +00004054 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004055 bool IsAdd = CE->getOperator() == OO_Plus;
4056 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004057 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4058 return setStep(CE->getArg(1), !IsAdd);
4059 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4060 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004061 }
4062 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004063 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004064 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004065 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004066 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067 return true;
4068}
4069
Alexey Bataeve3727102018-04-18 15:57:46 +00004070bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004071 // Check incr-expr for canonical loop form and return true if it
4072 // does not conform.
4073 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4074 // ++var
4075 // var++
4076 // --var
4077 // var--
4078 // var += incr
4079 // var -= incr
4080 // var = var + incr
4081 // var = incr + var
4082 // var = var - incr
4083 //
4084 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004085 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004086 return true;
4087 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004088 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4089 if (!ExprTemp->cleanupsHaveSideEffects())
4090 S = ExprTemp->getSubExpr();
4091
Alexander Musmana5f070a2014-10-01 06:03:56 +00004092 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004093 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004094 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004095 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004096 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4097 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004098 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004099 (UO->isDecrementOp() ? -1 : 1))
4100 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004101 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004102 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004103 switch (BO->getOpcode()) {
4104 case BO_AddAssign:
4105 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004106 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4107 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004108 break;
4109 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004110 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4111 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004112 break;
4113 default:
4114 break;
4115 }
David Majnemer9d168222016-08-05 17:44:54 +00004116 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 switch (CE->getOperator()) {
4118 case OO_PlusPlus:
4119 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004120 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4121 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004122 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004123 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004124 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4125 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004126 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 break;
4128 case OO_PlusEqual:
4129 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004130 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4131 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004132 break;
4133 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004134 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4135 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 break;
4137 default:
4138 break;
4139 }
4140 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004141 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004142 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004143 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004144 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004145 return true;
4146}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004147
Alexey Bataev5a3af132016-03-29 08:58:54 +00004148static ExprResult
4149tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004150 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004151 if (SemaRef.CurContext->isDependentContext())
4152 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004153 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4154 return SemaRef.PerformImplicitConversion(
4155 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4156 /*AllowExplicit=*/true);
4157 auto I = Captures.find(Capture);
4158 if (I != Captures.end())
4159 return buildCapture(SemaRef, Capture, I->second);
4160 DeclRefExpr *Ref = nullptr;
4161 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4162 Captures[Capture] = Ref;
4163 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004164}
4165
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004166/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004167Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004168 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004169 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004170 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004171 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004172 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004173 SemaRef.getLangOpts().CPlusPlus) {
4174 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004175 Expr *UBExpr = TestIsLessOp ? UB : LB;
4176 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004177 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4178 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004179 if (!Upper || !Lower)
4180 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004181
4182 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4183
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004184 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004185 // BuildBinOp already emitted error, this one is to point user to upper
4186 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004187 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004188 << Upper->getSourceRange() << Lower->getSourceRange();
4189 return nullptr;
4190 }
4191 }
4192
4193 if (!Diff.isUsable())
4194 return nullptr;
4195
4196 // Upper - Lower [- 1]
4197 if (TestIsStrictOp)
4198 Diff = SemaRef.BuildBinOp(
4199 S, DefaultLoc, BO_Sub, Diff.get(),
4200 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4201 if (!Diff.isUsable())
4202 return nullptr;
4203
4204 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004205 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004206 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004207 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004208 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004209 if (!Diff.isUsable())
4210 return nullptr;
4211
4212 // Parentheses (for dumping/debugging purposes only).
4213 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4214 if (!Diff.isUsable())
4215 return nullptr;
4216
4217 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004218 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004219 if (!Diff.isUsable())
4220 return nullptr;
4221
Alexander Musman174b3ca2014-10-06 11:16:29 +00004222 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004223 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004224 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004225 bool UseVarType = VarType->hasIntegerRepresentation() &&
4226 C.getTypeSize(Type) > C.getTypeSize(VarType);
4227 if (!Type->isIntegerType() || UseVarType) {
4228 unsigned NewSize =
4229 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4230 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4231 : Type->hasSignedIntegerRepresentation();
4232 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004233 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4234 Diff = SemaRef.PerformImplicitConversion(
4235 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4236 if (!Diff.isUsable())
4237 return nullptr;
4238 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004239 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004240 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004241 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4242 if (NewSize != C.getTypeSize(Type)) {
4243 if (NewSize < C.getTypeSize(Type)) {
4244 assert(NewSize == 64 && "incorrect loop var size");
4245 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4246 << InitSrcRange << ConditionSrcRange;
4247 }
4248 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004249 NewSize, Type->hasSignedIntegerRepresentation() ||
4250 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004251 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4252 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4253 Sema::AA_Converting, true);
4254 if (!Diff.isUsable())
4255 return nullptr;
4256 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004257 }
4258 }
4259
Alexander Musmana5f070a2014-10-01 06:03:56 +00004260 return Diff.get();
4261}
4262
Alexey Bataeve3727102018-04-18 15:57:46 +00004263Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004264 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004265 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004266 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4267 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4268 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004269
Alexey Bataeve3727102018-04-18 15:57:46 +00004270 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4271 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004272 if (!NewLB.isUsable() || !NewUB.isUsable())
4273 return nullptr;
4274
Alexey Bataeve3727102018-04-18 15:57:46 +00004275 ExprResult CondExpr =
4276 SemaRef.BuildBinOp(S, DefaultLoc,
4277 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4278 : (TestIsStrictOp ? BO_GT : BO_GE),
4279 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004280 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004281 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4282 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004283 CondExpr = SemaRef.PerformImplicitConversion(
4284 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4285 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004286 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004287 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4288 // Otherwise use original loop conditon and evaluate it in runtime.
4289 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4290}
4291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004292/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004293DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004294 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4295 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004296 auto *VD = dyn_cast<VarDecl>(LCDecl);
4297 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004298 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4299 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004301 const DSAStackTy::DSAVarData Data =
4302 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004303 // If the loop control decl is explicitly marked as private, do not mark it
4304 // as captured again.
4305 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4306 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004307 return Ref;
4308 }
4309 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004310 DefaultLoc);
4311}
4312
Alexey Bataeve3727102018-04-18 15:57:46 +00004313Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004314 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004315 QualType Type = LCDecl->getType().getNonReferenceType();
4316 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004317 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4318 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4319 isa<VarDecl>(LCDecl)
4320 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4321 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004322 if (PrivateVar->isInvalidDecl())
4323 return nullptr;
4324 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4325 }
4326 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004327}
4328
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004329/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004330Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004331
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004332/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004333Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004334
Alexey Bataevf138fda2018-08-13 19:04:24 +00004335Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4336 Scope *S, Expr *Counter,
4337 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4338 Expr *Inc, OverloadedOperatorKind OOK) {
4339 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4340 if (!Cnt)
4341 return nullptr;
4342 if (Inc) {
4343 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4344 "Expected only + or - operations for depend clauses.");
4345 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4346 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4347 if (!Cnt)
4348 return nullptr;
4349 }
4350 ExprResult Diff;
4351 QualType VarType = LCDecl->getType().getNonReferenceType();
4352 if (VarType->isIntegerType() || VarType->isPointerType() ||
4353 SemaRef.getLangOpts().CPlusPlus) {
4354 // Upper - Lower
4355 Expr *Upper =
4356 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4357 Expr *Lower =
4358 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4359 if (!Upper || !Lower)
4360 return nullptr;
4361
4362 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4363
4364 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4365 // BuildBinOp already emitted error, this one is to point user to upper
4366 // and lower bound, and to tell what is passed to 'operator-'.
4367 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4368 << Upper->getSourceRange() << Lower->getSourceRange();
4369 return nullptr;
4370 }
4371 }
4372
4373 if (!Diff.isUsable())
4374 return nullptr;
4375
4376 // Parentheses (for dumping/debugging purposes only).
4377 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4378 if (!Diff.isUsable())
4379 return nullptr;
4380
4381 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4382 if (!NewStep.isUsable())
4383 return nullptr;
4384 // (Upper - Lower) / Step
4385 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4386 if (!Diff.isUsable())
4387 return nullptr;
4388
4389 return Diff.get();
4390}
4391
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004392/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004393struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004394 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004395 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004396 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004397 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004398 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004399 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004400 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004401 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004402 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004403 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004404 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004405 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004406 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004407 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004408 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004409 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004410 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004411 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004412 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004414 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004415 SourceRange IncSrcRange;
4416};
4417
Alexey Bataev23b69422014-06-18 07:08:49 +00004418} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004419
Alexey Bataev9c821032015-04-30 04:23:23 +00004420void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4421 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4422 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004423 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4424 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004425 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4426 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004427 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4428 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004429 auto *VD = dyn_cast<VarDecl>(D);
4430 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004431 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004432 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004433 } else {
4434 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4435 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004436 VD = cast<VarDecl>(Ref->getDecl());
4437 }
4438 }
4439 DSAStack->addLoopControlVariable(D, VD);
4440 }
4441 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004442 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004443 }
4444}
4445
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004446/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004447/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004448static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004449 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4450 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004451 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4452 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004453 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004454 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004455 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004456 // OpenMP [2.6, Canonical Loop Form]
4457 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004458 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004459 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004460 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004461 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004462 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004463 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004464 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004465 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4466 SemaRef.Diag(DSA.getConstructLoc(),
4467 diag::note_omp_collapse_ordered_expr)
4468 << 2 << CollapseLoopCountExpr->getSourceRange()
4469 << OrderedLoopCountExpr->getSourceRange();
4470 else if (CollapseLoopCountExpr)
4471 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4472 diag::note_omp_collapse_ordered_expr)
4473 << 0 << CollapseLoopCountExpr->getSourceRange();
4474 else
4475 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4476 diag::note_omp_collapse_ordered_expr)
4477 << 1 << OrderedLoopCountExpr->getSourceRange();
4478 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004479 return true;
4480 }
4481 assert(For->getBody());
4482
4483 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4484
4485 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004486 Stmt *Init = For->getInit();
4487 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004488 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004489
4490 bool HasErrors = false;
4491
4492 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004493 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4494 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004495
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004496 // OpenMP [2.6, Canonical Loop Form]
4497 // Var is one of the following:
4498 // A variable of signed or unsigned integer type.
4499 // For C++, a variable of a random access iterator type.
4500 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004501 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004502 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4503 !VarType->isPointerType() &&
4504 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004505 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004506 << SemaRef.getLangOpts().CPlusPlus;
4507 HasErrors = true;
4508 }
4509
4510 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4511 // a Construct
4512 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4513 // parallel for construct is (are) private.
4514 // The loop iteration variable in the associated for-loop of a simd
4515 // construct with just one associated for-loop is linear with a
4516 // constant-linear-step that is the increment of the associated for-loop.
4517 // Exclude loop var from the list of variables with implicitly defined data
4518 // sharing attributes.
4519 VarsWithImplicitDSA.erase(LCDecl);
4520
4521 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4522 // in a Construct, C/C++].
4523 // The loop iteration variable in the associated for-loop of a simd
4524 // construct with just one associated for-loop may be listed in a linear
4525 // clause with a constant-linear-step that is the increment of the
4526 // associated for-loop.
4527 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4528 // parallel for construct may be listed in a private or lastprivate clause.
4529 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4530 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4531 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004532 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004533 isOpenMPSimdDirective(DKind)
4534 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4535 : OMPC_private;
4536 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4537 DVar.CKind != PredeterminedCKind) ||
4538 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4539 isOpenMPDistributeDirective(DKind)) &&
4540 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4541 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4542 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004543 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004544 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4545 << getOpenMPClauseName(PredeterminedCKind);
4546 if (DVar.RefExpr == nullptr)
4547 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004548 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004549 HasErrors = true;
4550 } else if (LoopDeclRefExpr != nullptr) {
4551 // Make the loop iteration variable private (for worksharing constructs),
4552 // linear (for simd directives with the only one associated loop) or
4553 // lastprivate (for simd directives with several collapsed or ordered
4554 // loops).
4555 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004556 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4557 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004558 /*FromParent=*/false);
4559 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4560 }
4561
4562 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4563
4564 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004565 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004566
4567 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004568 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004569 }
4570
Alexey Bataeve3727102018-04-18 15:57:46 +00004571 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004572 return HasErrors;
4573
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004575 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004576 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4577 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004578 DSA.getCurScope(),
4579 (isOpenMPWorksharingDirective(DKind) ||
4580 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4581 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004582 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4583 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4584 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4585 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4586 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4587 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4588 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4589 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004590
Alexey Bataev62dbb972015-04-22 11:59:37 +00004591 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4592 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004593 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004594 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004595 ResultIterSpace.CounterInit == nullptr ||
4596 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004597 if (!HasErrors && DSA.isOrderedRegion()) {
4598 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4599 if (CurrentNestedLoopCount <
4600 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4601 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4602 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4603 DSA.getOrderedRegionParam().second->setLoopCounter(
4604 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4605 }
4606 }
4607 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4608 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4609 // Erroneous case - clause has some problems.
4610 continue;
4611 }
4612 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4613 Pair.second.size() <= CurrentNestedLoopCount) {
4614 // Erroneous case - clause has some problems.
4615 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4616 continue;
4617 }
4618 Expr *CntValue;
4619 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4620 CntValue = ISC.buildOrderedLoopData(
4621 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4622 Pair.first->getDependencyLoc());
4623 else
4624 CntValue = ISC.buildOrderedLoopData(
4625 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4626 Pair.first->getDependencyLoc(),
4627 Pair.second[CurrentNestedLoopCount].first,
4628 Pair.second[CurrentNestedLoopCount].second);
4629 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4630 }
4631 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004632
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004633 return HasErrors;
4634}
4635
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004636/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004637static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004638buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004639 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004640 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004641 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004642 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004643 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004644 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004645 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004646 VarRef.get()->getType())) {
4647 NewStart = SemaRef.PerformImplicitConversion(
4648 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4649 /*AllowExplicit=*/true);
4650 if (!NewStart.isUsable())
4651 return ExprError();
4652 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004653
Alexey Bataeve3727102018-04-18 15:57:46 +00004654 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004655 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4656 return Init;
4657}
4658
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004659/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004660static ExprResult buildCounterUpdate(
4661 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4662 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4663 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004664 // Add parentheses (for debugging purposes only).
4665 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4666 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4667 !Step.isUsable())
4668 return ExprError();
4669
Alexey Bataev5a3af132016-03-29 08:58:54 +00004670 ExprResult NewStep = Step;
4671 if (Captures)
4672 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004673 if (NewStep.isInvalid())
4674 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004675 ExprResult Update =
4676 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004677 if (!Update.isUsable())
4678 return ExprError();
4679
Alexey Bataevc0214e02016-02-16 12:13:49 +00004680 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4681 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004682 ExprResult NewStart = Start;
4683 if (Captures)
4684 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004685 if (NewStart.isInvalid())
4686 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004687
Alexey Bataevc0214e02016-02-16 12:13:49 +00004688 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4689 ExprResult SavedUpdate = Update;
4690 ExprResult UpdateVal;
4691 if (VarRef.get()->getType()->isOverloadableType() ||
4692 NewStart.get()->getType()->isOverloadableType() ||
4693 Update.get()->getType()->isOverloadableType()) {
4694 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4695 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4696 Update =
4697 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4698 if (Update.isUsable()) {
4699 UpdateVal =
4700 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4701 VarRef.get(), SavedUpdate.get());
4702 if (UpdateVal.isUsable()) {
4703 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4704 UpdateVal.get());
4705 }
4706 }
4707 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4708 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004709
Alexey Bataevc0214e02016-02-16 12:13:49 +00004710 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4711 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4712 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4713 NewStart.get(), SavedUpdate.get());
4714 if (!Update.isUsable())
4715 return ExprError();
4716
Alexey Bataev11481f52016-02-17 10:29:05 +00004717 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4718 VarRef.get()->getType())) {
4719 Update = SemaRef.PerformImplicitConversion(
4720 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4721 if (!Update.isUsable())
4722 return ExprError();
4723 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004724
4725 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4726 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004727 return Update;
4728}
4729
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004730/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004731/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004732static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004733 if (E == nullptr)
4734 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004735 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004736 QualType OldType = E->getType();
4737 unsigned HasBits = C.getTypeSize(OldType);
4738 if (HasBits >= Bits)
4739 return ExprResult(E);
4740 // OK to convert to signed, because new type has more bits than old.
4741 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4742 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4743 true);
4744}
4745
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004746/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004747/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004748static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004749 if (E == nullptr)
4750 return false;
4751 llvm::APSInt Result;
4752 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4753 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4754 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004755}
4756
Alexey Bataev5a3af132016-03-29 08:58:54 +00004757/// Build preinits statement for the given declarations.
4758static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004759 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004760 if (!PreInits.empty()) {
4761 return new (Context) DeclStmt(
4762 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4763 SourceLocation(), SourceLocation());
4764 }
4765 return nullptr;
4766}
4767
4768/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004769static Stmt *
4770buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004771 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004772 if (!Captures.empty()) {
4773 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004774 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004775 PreInits.push_back(Pair.second->getDecl());
4776 return buildPreInits(Context, PreInits);
4777 }
4778 return nullptr;
4779}
4780
4781/// Build postupdate expression for the given list of postupdates expressions.
4782static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4783 Expr *PostUpdate = nullptr;
4784 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004785 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004786 Expr *ConvE = S.BuildCStyleCastExpr(
4787 E->getExprLoc(),
4788 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4789 E->getExprLoc(), E)
4790 .get();
4791 PostUpdate = PostUpdate
4792 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4793 PostUpdate, ConvE)
4794 .get()
4795 : ConvE;
4796 }
4797 }
4798 return PostUpdate;
4799}
4800
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004801/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004802/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4803/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004804static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00004805checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004806 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4807 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00004808 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004809 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004810 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004811 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004812 // Found 'collapse' clause - calculate collapse number.
4813 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004814 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004815 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004816 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004817 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004818 if (OrderedLoopCountExpr) {
4819 // Found 'ordered' clause - calculate collapse number.
4820 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004821 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4822 if (Result.getLimitedValue() < NestedLoopCount) {
4823 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4824 diag::err_omp_wrong_ordered_loop_count)
4825 << OrderedLoopCountExpr->getSourceRange();
4826 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4827 diag::note_collapse_loop_count)
4828 << CollapseLoopCountExpr->getSourceRange();
4829 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004830 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004831 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004832 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004833 // This is helper routine for loop directives (e.g., 'for', 'simd',
4834 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00004835 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004836 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004837 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004838 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004839 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00004840 if (checkOpenMPIterationSpace(
4841 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4842 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4843 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4844 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004845 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004846 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004847 // OpenMP [2.8.1, simd construct, Restrictions]
4848 // All loops associated with the construct must be perfectly nested; that
4849 // is, there must be no intervening code nor any OpenMP directive between
4850 // any two loops.
4851 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004852 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004853 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
4854 if (checkOpenMPIterationSpace(
4855 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4856 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4857 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4858 Captures))
4859 return 0;
4860 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
4861 // Handle initialization of captured loop iterator variables.
4862 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
4863 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
4864 Captures[DRE] = DRE;
4865 }
4866 }
4867 // Move on to the next nested for loop, or to the loop body.
4868 // OpenMP [2.8.1, simd construct, Restrictions]
4869 // All loops associated with the construct must be perfectly nested; that
4870 // is, there must be no intervening code nor any OpenMP directive between
4871 // any two loops.
4872 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4873 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004874
Alexander Musmana5f070a2014-10-01 06:03:56 +00004875 Built.clear(/* size */ NestedLoopCount);
4876
4877 if (SemaRef.CurContext->isDependentContext())
4878 return NestedLoopCount;
4879
4880 // An example of what is generated for the following code:
4881 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004882 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004883 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004884 // for (k = 0; k < NK; ++k)
4885 // for (j = J0; j < NJ; j+=2) {
4886 // <loop body>
4887 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004888 //
4889 // We generate the code below.
4890 // Note: the loop body may be outlined in CodeGen.
4891 // Note: some counters may be C++ classes, operator- is used to find number of
4892 // iterations and operator+= to calculate counter value.
4893 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4894 // or i64 is currently supported).
4895 //
4896 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4897 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4898 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4899 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4900 // // similar updates for vars in clauses (e.g. 'linear')
4901 // <loop body (using local i and j)>
4902 // }
4903 // i = NI; // assign final values of counters
4904 // j = NJ;
4905 //
4906
4907 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4908 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004909 // Precondition tests if there is at least one iteration (all conditions are
4910 // true).
4911 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00004912 Expr *N0 = IterSpaces[0].NumIterations;
4913 ExprResult LastIteration32 =
4914 widenIterationCount(/*Bits=*/32,
4915 SemaRef
4916 .PerformImplicitConversion(
4917 N0->IgnoreImpCasts(), N0->getType(),
4918 Sema::AA_Converting, /*AllowExplicit=*/true)
4919 .get(),
4920 SemaRef);
4921 ExprResult LastIteration64 = widenIterationCount(
4922 /*Bits=*/64,
4923 SemaRef
4924 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
4925 Sema::AA_Converting,
4926 /*AllowExplicit=*/true)
4927 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004928 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004929
4930 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4931 return NestedLoopCount;
4932
Alexey Bataeve3727102018-04-18 15:57:46 +00004933 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004934 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4935
4936 Scope *CurScope = DSA.getCurScope();
4937 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004938 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00004939 PreCond =
4940 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4941 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00004942 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004943 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00004944 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004945 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4946 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004947 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004948 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004949 SemaRef
4950 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4951 Sema::AA_Converting,
4952 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004953 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004954 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004955 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004956 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00004957 SemaRef
4958 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4959 Sema::AA_Converting,
4960 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004961 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004962 }
4963
4964 // Choose either the 32-bit or 64-bit version.
4965 ExprResult LastIteration = LastIteration64;
4966 if (LastIteration32.isUsable() &&
4967 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4968 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00004969 fitsInto(
4970 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004971 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4972 LastIteration64.get(), SemaRef)))
4973 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004974 QualType VType = LastIteration.get()->getType();
4975 QualType RealVType = VType;
4976 QualType StrideVType = VType;
4977 if (isOpenMPTaskLoopDirective(DKind)) {
4978 VType =
4979 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4980 StrideVType =
4981 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4982 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004983
4984 if (!LastIteration.isUsable())
4985 return 0;
4986
4987 // Save the number of iterations.
4988 ExprResult NumIterations = LastIteration;
4989 {
4990 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00004991 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4992 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004993 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4994 if (!LastIteration.isUsable())
4995 return 0;
4996 }
4997
4998 // Calculate the last iteration number beforehand instead of doing this on
4999 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5000 llvm::APSInt Result;
5001 bool IsConstant =
5002 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5003 ExprResult CalcLastIteration;
5004 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005005 ExprResult SaveRef =
5006 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005007 LastIteration = SaveRef;
5008
5009 // Prepare SaveRef + 1.
5010 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005011 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005012 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5013 if (!NumIterations.isUsable())
5014 return 0;
5015 }
5016
5017 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5018
David Majnemer9d168222016-08-05 17:44:54 +00005019 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005020 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005021 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5022 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005023 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005024 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5025 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005026 SemaRef.AddInitializerToDecl(LBDecl,
5027 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5028 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005029
5030 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005031 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5032 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005033 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005034 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005035
5036 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5037 // This will be used to implement clause 'lastprivate'.
5038 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005039 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5040 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005041 SemaRef.AddInitializerToDecl(ILDecl,
5042 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5043 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005044
5045 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005046 VarDecl *STDecl =
5047 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5048 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005049 SemaRef.AddInitializerToDecl(STDecl,
5050 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5051 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005052
5053 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005054 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005055 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5056 UB.get(), LastIteration.get());
5057 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005058 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5059 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005060 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5061 CondOp.get());
5062 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005063
5064 // If we have a combined directive that combines 'distribute', 'for' or
5065 // 'simd' we need to be able to access the bounds of the schedule of the
5066 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5067 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5068 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005069 // Lower bound variable, initialized with zero.
5070 VarDecl *CombLBDecl =
5071 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5072 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5073 SemaRef.AddInitializerToDecl(
5074 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5075 /*DirectInit*/ false);
5076
5077 // Upper bound variable, initialized with last iteration number.
5078 VarDecl *CombUBDecl =
5079 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5080 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5081 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5082 /*DirectInit*/ false);
5083
5084 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5085 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5086 ExprResult CombCondOp =
5087 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5088 LastIteration.get(), CombUB.get());
5089 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5090 CombCondOp.get());
5091 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5092
Alexey Bataeve3727102018-04-18 15:57:46 +00005093 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005094 // We expect to have at least 2 more parameters than the 'parallel'
5095 // directive does - the lower and upper bounds of the previous schedule.
5096 assert(CD->getNumParams() >= 4 &&
5097 "Unexpected number of parameters in loop combined directive");
5098
5099 // Set the proper type for the bounds given what we learned from the
5100 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005101 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5102 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005103
5104 // Previous lower and upper bounds are obtained from the region
5105 // parameters.
5106 PrevLB =
5107 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5108 PrevUB =
5109 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5110 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005111 }
5112
5113 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005114 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005115 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005116 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005117 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5118 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005119 Expr *RHS =
5120 (isOpenMPWorksharingDirective(DKind) ||
5121 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5122 ? LB.get()
5123 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005124 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5125 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005126
5127 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5128 Expr *CombRHS =
5129 (isOpenMPWorksharingDirective(DKind) ||
5130 isOpenMPTaskLoopDirective(DKind) ||
5131 isOpenMPDistributeDirective(DKind))
5132 ? CombLB.get()
5133 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5134 CombInit =
5135 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5136 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5137 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005138 }
5139
Alexander Musmanc6388682014-12-15 07:07:06 +00005140 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005141 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005142 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005143 (isOpenMPWorksharingDirective(DKind) ||
5144 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005145 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5146 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5147 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005148 ExprResult CombCond;
5149 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5150 CombCond =
5151 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5152 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005153 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005154 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005155 ExprResult Inc =
5156 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5157 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5158 if (!Inc.isUsable())
5159 return 0;
5160 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005161 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5162 if (!Inc.isUsable())
5163 return 0;
5164
5165 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5166 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005167 // In combined construct, add combined version that use CombLB and CombUB
5168 // base variables for the update
5169 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005170 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5171 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005172 // LB + ST
5173 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5174 if (!NextLB.isUsable())
5175 return 0;
5176 // LB = LB + ST
5177 NextLB =
5178 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5179 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5180 if (!NextLB.isUsable())
5181 return 0;
5182 // UB + ST
5183 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5184 if (!NextUB.isUsable())
5185 return 0;
5186 // UB = UB + ST
5187 NextUB =
5188 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5189 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5190 if (!NextUB.isUsable())
5191 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005192 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5193 CombNextLB =
5194 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5195 if (!NextLB.isUsable())
5196 return 0;
5197 // LB = LB + ST
5198 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5199 CombNextLB.get());
5200 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5201 if (!CombNextLB.isUsable())
5202 return 0;
5203 // UB + ST
5204 CombNextUB =
5205 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5206 if (!CombNextUB.isUsable())
5207 return 0;
5208 // UB = UB + ST
5209 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5210 CombNextUB.get());
5211 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5212 if (!CombNextUB.isUsable())
5213 return 0;
5214 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005215 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005216
Carlo Bertolliffafe102017-04-20 00:39:39 +00005217 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005218 // directive with for as IV = IV + ST; ensure upper bound expression based
5219 // on PrevUB instead of NumIterations - used to implement 'for' when found
5220 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005221 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005222 ExprResult DistCond, DistInc, PrevEUB;
5223 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5224 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5225 assert(DistCond.isUsable() && "distribute cond expr was not built");
5226
5227 DistInc =
5228 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5229 assert(DistInc.isUsable() && "distribute inc expr was not built");
5230 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5231 DistInc.get());
5232 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5233 assert(DistInc.isUsable() && "distribute inc expr was not built");
5234
5235 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5236 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005237 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005238 ExprResult IsUBGreater =
5239 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5240 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5241 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5242 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5243 CondOp.get());
5244 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5245 }
5246
Alexander Musmana5f070a2014-10-01 06:03:56 +00005247 // Build updates and final values of the loop counters.
5248 bool HasErrors = false;
5249 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005250 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005251 Built.Updates.resize(NestedLoopCount);
5252 Built.Finals.resize(NestedLoopCount);
5253 {
5254 ExprResult Div;
5255 // Go from inner nested loop to outer.
5256 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5257 LoopIterationSpace &IS = IterSpaces[Cnt];
5258 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5259 // Build: Iter = (IV / Div) % IS.NumIters
5260 // where Div is product of previous iterations' IS.NumIters.
5261 ExprResult Iter;
5262 if (Div.isUsable()) {
5263 Iter =
5264 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5265 } else {
5266 Iter = IV;
5267 assert((Cnt == (int)NestedLoopCount - 1) &&
5268 "unusable div expected on first iteration only");
5269 }
5270
5271 if (Cnt != 0 && Iter.isUsable())
5272 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5273 IS.NumIterations);
5274 if (!Iter.isUsable()) {
5275 HasErrors = true;
5276 break;
5277 }
5278
Alexey Bataev39f915b82015-05-08 10:41:21 +00005279 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005280 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005281 DeclRefExpr *CounterVar = buildDeclRefExpr(
5282 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5283 /*RefersToCapture=*/true);
5284 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005285 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005286 if (!Init.isUsable()) {
5287 HasErrors = true;
5288 break;
5289 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005290 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005291 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5292 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005293 if (!Update.isUsable()) {
5294 HasErrors = true;
5295 break;
5296 }
5297
5298 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005299 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005300 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005301 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005302 if (!Final.isUsable()) {
5303 HasErrors = true;
5304 break;
5305 }
5306
5307 // Build Div for the next iteration: Div <- Div * IS.NumIters
5308 if (Cnt != 0) {
5309 if (Div.isUnset())
5310 Div = IS.NumIterations;
5311 else
5312 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5313 IS.NumIterations);
5314
5315 // Add parentheses (for debugging purposes only).
5316 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005317 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005318 if (!Div.isUsable()) {
5319 HasErrors = true;
5320 break;
5321 }
5322 }
5323 if (!Update.isUsable() || !Final.isUsable()) {
5324 HasErrors = true;
5325 break;
5326 }
5327 // Save results
5328 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005329 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005330 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005331 Built.Updates[Cnt] = Update.get();
5332 Built.Finals[Cnt] = Final.get();
5333 }
5334 }
5335
5336 if (HasErrors)
5337 return 0;
5338
5339 // Save results
5340 Built.IterationVarRef = IV.get();
5341 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005342 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005343 Built.CalcLastIteration =
5344 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005345 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005346 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005347 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005348 Built.Init = Init.get();
5349 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005350 Built.LB = LB.get();
5351 Built.UB = UB.get();
5352 Built.IL = IL.get();
5353 Built.ST = ST.get();
5354 Built.EUB = EUB.get();
5355 Built.NLB = NextLB.get();
5356 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005357 Built.PrevLB = PrevLB.get();
5358 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005359 Built.DistInc = DistInc.get();
5360 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005361 Built.DistCombinedFields.LB = CombLB.get();
5362 Built.DistCombinedFields.UB = CombUB.get();
5363 Built.DistCombinedFields.EUB = CombEUB.get();
5364 Built.DistCombinedFields.Init = CombInit.get();
5365 Built.DistCombinedFields.Cond = CombCond.get();
5366 Built.DistCombinedFields.NLB = CombNextLB.get();
5367 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005368
Alexey Bataevabfc0692014-06-25 06:52:00 +00005369 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005370}
5371
Alexey Bataev10e775f2015-07-30 11:36:16 +00005372static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005373 auto CollapseClauses =
5374 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5375 if (CollapseClauses.begin() != CollapseClauses.end())
5376 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005377 return nullptr;
5378}
5379
Alexey Bataev10e775f2015-07-30 11:36:16 +00005380static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005381 auto OrderedClauses =
5382 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5383 if (OrderedClauses.begin() != OrderedClauses.end())
5384 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005385 return nullptr;
5386}
5387
Kelvin Lic5609492016-07-15 04:39:07 +00005388static bool checkSimdlenSafelenSpecified(Sema &S,
5389 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005390 const OMPSafelenClause *Safelen = nullptr;
5391 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005392
Alexey Bataeve3727102018-04-18 15:57:46 +00005393 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005394 if (Clause->getClauseKind() == OMPC_safelen)
5395 Safelen = cast<OMPSafelenClause>(Clause);
5396 else if (Clause->getClauseKind() == OMPC_simdlen)
5397 Simdlen = cast<OMPSimdlenClause>(Clause);
5398 if (Safelen && Simdlen)
5399 break;
5400 }
5401
5402 if (Simdlen && Safelen) {
5403 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005404 const Expr *SimdlenLength = Simdlen->getSimdlen();
5405 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005406 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5407 SimdlenLength->isInstantiationDependent() ||
5408 SimdlenLength->containsUnexpandedParameterPack())
5409 return false;
5410 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5411 SafelenLength->isInstantiationDependent() ||
5412 SafelenLength->containsUnexpandedParameterPack())
5413 return false;
5414 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5415 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5416 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5417 // If both simdlen and safelen clauses are specified, the value of the
5418 // simdlen parameter must be less than or equal to the value of the safelen
5419 // parameter.
5420 if (SimdlenRes > SafelenRes) {
5421 S.Diag(SimdlenLength->getExprLoc(),
5422 diag::err_omp_wrong_simdlen_safelen_values)
5423 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5424 return true;
5425 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005426 }
5427 return false;
5428}
5429
Alexey Bataeve3727102018-04-18 15:57:46 +00005430StmtResult
5431Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5432 SourceLocation StartLoc, SourceLocation EndLoc,
5433 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005434 if (!AStmt)
5435 return StmtError();
5436
5437 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005438 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005439 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5440 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005441 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005442 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5443 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005444 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005445 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005446
Alexander Musmana5f070a2014-10-01 06:03:56 +00005447 assert((CurContext->isDependentContext() || B.builtAll()) &&
5448 "omp simd loop exprs were not built");
5449
Alexander Musman3276a272015-03-21 10:12:56 +00005450 if (!CurContext->isDependentContext()) {
5451 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005452 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005453 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005454 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005455 B.NumIterations, *this, CurScope,
5456 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005457 return StmtError();
5458 }
5459 }
5460
Kelvin Lic5609492016-07-15 04:39:07 +00005461 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005462 return StmtError();
5463
Reid Kleckner87a31802018-03-12 21:43:02 +00005464 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005465 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5466 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005467}
5468
Alexey Bataeve3727102018-04-18 15:57:46 +00005469StmtResult
5470Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5471 SourceLocation StartLoc, SourceLocation EndLoc,
5472 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005473 if (!AStmt)
5474 return StmtError();
5475
5476 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005477 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005478 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5479 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005480 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005481 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5482 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005483 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005484 return StmtError();
5485
Alexander Musmana5f070a2014-10-01 06:03:56 +00005486 assert((CurContext->isDependentContext() || B.builtAll()) &&
5487 "omp for loop exprs were not built");
5488
Alexey Bataev54acd402015-08-04 11:18:19 +00005489 if (!CurContext->isDependentContext()) {
5490 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005491 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005492 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005493 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005494 B.NumIterations, *this, CurScope,
5495 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005496 return StmtError();
5497 }
5498 }
5499
Reid Kleckner87a31802018-03-12 21:43:02 +00005500 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005501 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005502 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005503}
5504
Alexander Musmanf82886e2014-09-18 05:12:34 +00005505StmtResult Sema::ActOnOpenMPForSimdDirective(
5506 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005507 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005508 if (!AStmt)
5509 return StmtError();
5510
5511 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005512 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005513 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5514 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005515 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005516 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005517 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5518 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005519 if (NestedLoopCount == 0)
5520 return StmtError();
5521
Alexander Musmanc6388682014-12-15 07:07:06 +00005522 assert((CurContext->isDependentContext() || B.builtAll()) &&
5523 "omp for simd loop exprs were not built");
5524
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005525 if (!CurContext->isDependentContext()) {
5526 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005527 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005528 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005529 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005530 B.NumIterations, *this, CurScope,
5531 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005532 return StmtError();
5533 }
5534 }
5535
Kelvin Lic5609492016-07-15 04:39:07 +00005536 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005537 return StmtError();
5538
Reid Kleckner87a31802018-03-12 21:43:02 +00005539 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005540 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5541 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005542}
5543
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005544StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5545 Stmt *AStmt,
5546 SourceLocation StartLoc,
5547 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005548 if (!AStmt)
5549 return StmtError();
5550
5551 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005552 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005553 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005554 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005555 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005556 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005557 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005558 return StmtError();
5559 // All associated statements must be '#pragma omp section' except for
5560 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005561 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005562 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5563 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005564 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005565 diag::err_omp_sections_substmt_not_section);
5566 return StmtError();
5567 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005568 cast<OMPSectionDirective>(SectionStmt)
5569 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005570 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005571 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005572 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005573 return StmtError();
5574 }
5575
Reid Kleckner87a31802018-03-12 21:43:02 +00005576 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005577
Alexey Bataev25e5b442015-09-15 12:52:43 +00005578 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5579 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005580}
5581
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005582StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5583 SourceLocation StartLoc,
5584 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005585 if (!AStmt)
5586 return StmtError();
5587
5588 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005589
Reid Kleckner87a31802018-03-12 21:43:02 +00005590 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005591 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005592
Alexey Bataev25e5b442015-09-15 12:52:43 +00005593 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5594 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005595}
5596
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005597StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5598 Stmt *AStmt,
5599 SourceLocation StartLoc,
5600 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005601 if (!AStmt)
5602 return StmtError();
5603
5604 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005605
Reid Kleckner87a31802018-03-12 21:43:02 +00005606 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005607
Alexey Bataev3255bf32015-01-19 05:20:46 +00005608 // OpenMP [2.7.3, single Construct, Restrictions]
5609 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005610 const OMPClause *Nowait = nullptr;
5611 const OMPClause *Copyprivate = nullptr;
5612 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005613 if (Clause->getClauseKind() == OMPC_nowait)
5614 Nowait = Clause;
5615 else if (Clause->getClauseKind() == OMPC_copyprivate)
5616 Copyprivate = Clause;
5617 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005618 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005619 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005620 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005621 return StmtError();
5622 }
5623 }
5624
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005625 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5626}
5627
Alexander Musman80c22892014-07-17 08:54:58 +00005628StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5629 SourceLocation StartLoc,
5630 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005631 if (!AStmt)
5632 return StmtError();
5633
5634 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005635
Reid Kleckner87a31802018-03-12 21:43:02 +00005636 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005637
5638 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5639}
5640
Alexey Bataev28c75412015-12-15 08:19:24 +00005641StmtResult Sema::ActOnOpenMPCriticalDirective(
5642 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5643 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005644 if (!AStmt)
5645 return StmtError();
5646
5647 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005648
Alexey Bataev28c75412015-12-15 08:19:24 +00005649 bool ErrorFound = false;
5650 llvm::APSInt Hint;
5651 SourceLocation HintLoc;
5652 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005653 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005654 if (C->getClauseKind() == OMPC_hint) {
5655 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005656 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005657 ErrorFound = true;
5658 }
5659 Expr *E = cast<OMPHintClause>(C)->getHint();
5660 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005661 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005662 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005663 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005664 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005665 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005666 }
5667 }
5668 }
5669 if (ErrorFound)
5670 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005671 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005672 if (Pair.first && DirName.getName() && !DependentHint) {
5673 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5674 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005675 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005676 Diag(HintLoc, diag::note_omp_critical_hint_here)
5677 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005678 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005679 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005680 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005681 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005682 << 1
5683 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5684 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005685 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005686 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005687 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005688 }
5689 }
5690
Reid Kleckner87a31802018-03-12 21:43:02 +00005691 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005692
Alexey Bataev28c75412015-12-15 08:19:24 +00005693 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5694 Clauses, AStmt);
5695 if (!Pair.first && DirName.getName() && !DependentHint)
5696 DSAStack->addCriticalWithHint(Dir, Hint);
5697 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005698}
5699
Alexey Bataev4acb8592014-07-07 13:01:15 +00005700StmtResult Sema::ActOnOpenMPParallelForDirective(
5701 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005702 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005703 if (!AStmt)
5704 return StmtError();
5705
Alexey Bataeve3727102018-04-18 15:57:46 +00005706 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005707 // 1.2.2 OpenMP Language Terminology
5708 // Structured block - An executable statement with a single entry at the
5709 // top and a single exit at the bottom.
5710 // The point of exit cannot be a branch out of the structured block.
5711 // longjmp() and throw() must not violate the entry/exit criteria.
5712 CS->getCapturedDecl()->setNothrow();
5713
Alexander Musmanc6388682014-12-15 07:07:06 +00005714 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005715 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5716 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005717 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005718 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005719 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5720 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005721 if (NestedLoopCount == 0)
5722 return StmtError();
5723
Alexander Musmana5f070a2014-10-01 06:03:56 +00005724 assert((CurContext->isDependentContext() || B.builtAll()) &&
5725 "omp parallel for loop exprs were not built");
5726
Alexey Bataev54acd402015-08-04 11:18:19 +00005727 if (!CurContext->isDependentContext()) {
5728 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005729 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005730 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005731 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005732 B.NumIterations, *this, CurScope,
5733 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005734 return StmtError();
5735 }
5736 }
5737
Reid Kleckner87a31802018-03-12 21:43:02 +00005738 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005739 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005740 NestedLoopCount, Clauses, AStmt, B,
5741 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005742}
5743
Alexander Musmane4e893b2014-09-23 09:33:00 +00005744StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5745 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005746 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005747 if (!AStmt)
5748 return StmtError();
5749
Alexey Bataeve3727102018-04-18 15:57:46 +00005750 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005751 // 1.2.2 OpenMP Language Terminology
5752 // Structured block - An executable statement with a single entry at the
5753 // top and a single exit at the bottom.
5754 // The point of exit cannot be a branch out of the structured block.
5755 // longjmp() and throw() must not violate the entry/exit criteria.
5756 CS->getCapturedDecl()->setNothrow();
5757
Alexander Musmanc6388682014-12-15 07:07:06 +00005758 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005759 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5760 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005761 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005762 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005763 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5764 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005765 if (NestedLoopCount == 0)
5766 return StmtError();
5767
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005768 if (!CurContext->isDependentContext()) {
5769 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005770 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005771 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005772 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005773 B.NumIterations, *this, CurScope,
5774 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005775 return StmtError();
5776 }
5777 }
5778
Kelvin Lic5609492016-07-15 04:39:07 +00005779 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005780 return StmtError();
5781
Reid Kleckner87a31802018-03-12 21:43:02 +00005782 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005783 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005784 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005785}
5786
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005787StmtResult
5788Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5789 Stmt *AStmt, SourceLocation StartLoc,
5790 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005791 if (!AStmt)
5792 return StmtError();
5793
5794 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005795 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005796 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005797 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005798 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005799 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005800 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005801 return StmtError();
5802 // All associated statements must be '#pragma omp section' except for
5803 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005804 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005805 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5806 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005807 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005808 diag::err_omp_parallel_sections_substmt_not_section);
5809 return StmtError();
5810 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005811 cast<OMPSectionDirective>(SectionStmt)
5812 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005813 }
5814 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005815 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005816 diag::err_omp_parallel_sections_not_compound_stmt);
5817 return StmtError();
5818 }
5819
Reid Kleckner87a31802018-03-12 21:43:02 +00005820 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005821
Alexey Bataev25e5b442015-09-15 12:52:43 +00005822 return OMPParallelSectionsDirective::Create(
5823 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005824}
5825
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005826StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5827 Stmt *AStmt, SourceLocation StartLoc,
5828 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005829 if (!AStmt)
5830 return StmtError();
5831
David Majnemer9d168222016-08-05 17:44:54 +00005832 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005833 // 1.2.2 OpenMP Language Terminology
5834 // Structured block - An executable statement with a single entry at the
5835 // top and a single exit at the bottom.
5836 // The point of exit cannot be a branch out of the structured block.
5837 // longjmp() and throw() must not violate the entry/exit criteria.
5838 CS->getCapturedDecl()->setNothrow();
5839
Reid Kleckner87a31802018-03-12 21:43:02 +00005840 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005841
Alexey Bataev25e5b442015-09-15 12:52:43 +00005842 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5843 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005844}
5845
Alexey Bataev68446b72014-07-18 07:47:19 +00005846StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5847 SourceLocation EndLoc) {
5848 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5849}
5850
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005851StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5852 SourceLocation EndLoc) {
5853 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5854}
5855
Alexey Bataev2df347a2014-07-18 10:17:07 +00005856StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5857 SourceLocation EndLoc) {
5858 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5859}
5860
Alexey Bataev169d96a2017-07-18 20:17:46 +00005861StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5862 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005863 SourceLocation StartLoc,
5864 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005865 if (!AStmt)
5866 return StmtError();
5867
5868 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005869
Reid Kleckner87a31802018-03-12 21:43:02 +00005870 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005871
Alexey Bataev169d96a2017-07-18 20:17:46 +00005872 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005873 AStmt,
5874 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005875}
5876
Alexey Bataev6125da92014-07-21 11:26:11 +00005877StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5878 SourceLocation StartLoc,
5879 SourceLocation EndLoc) {
5880 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5881 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5882}
5883
Alexey Bataev346265e2015-09-25 10:37:12 +00005884StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5885 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005886 SourceLocation StartLoc,
5887 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005888 const OMPClause *DependFound = nullptr;
5889 const OMPClause *DependSourceClause = nullptr;
5890 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005891 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005892 const OMPThreadsClause *TC = nullptr;
5893 const OMPSIMDClause *SC = nullptr;
5894 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005895 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5896 DependFound = C;
5897 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5898 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005899 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00005900 << getOpenMPDirectiveName(OMPD_ordered)
5901 << getOpenMPClauseName(OMPC_depend) << 2;
5902 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005903 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00005904 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00005905 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005906 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005907 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005908 << 0;
5909 ErrorFound = true;
5910 }
5911 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5912 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005913 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005914 << 1;
5915 ErrorFound = true;
5916 }
5917 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005918 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005919 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00005920 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005921 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005922 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00005923 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005924 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005925 if (!ErrorFound && !SC &&
5926 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005927 // OpenMP [2.8.1,simd Construct, Restrictions]
5928 // An ordered construct with the simd clause is the only OpenMP construct
5929 // that can appear in the simd region.
5930 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005931 ErrorFound = true;
5932 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005933 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00005934 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5935 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005936 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005937 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00005938 diag::err_omp_ordered_directive_without_param);
5939 ErrorFound = true;
5940 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005941 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005942 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00005943 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5944 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005945 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00005946 ErrorFound = true;
5947 }
5948 }
5949 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005950 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005951
5952 if (AStmt) {
5953 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5954
Reid Kleckner87a31802018-03-12 21:43:02 +00005955 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005956 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005957
5958 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005959}
5960
Alexey Bataev1d160b12015-03-13 12:27:31 +00005961namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005962/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00005963/// construct.
5964class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005965 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005966 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005967 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005968 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005969 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005970 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005971 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005972 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005973 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005974 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005975 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005976 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005977 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005978 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005979 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00005980 /// expression.
5981 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005982 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00005983 /// part.
5984 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005985 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005986 NoError
5987 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005988 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005989 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005990 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00005991 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005992 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005993 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005994 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005995 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005996 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00005997 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5998 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5999 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006000 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006001 /// important for non-associative operations.
6002 bool IsXLHSInRHSPart;
6003 BinaryOperatorKind Op;
6004 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006005 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006006 /// if it is a prefix unary operation.
6007 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006008
6009public:
6010 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006011 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006012 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006013 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006014 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006015 /// expression. If DiagId and NoteId == 0, then only check is performed
6016 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006017 /// \param DiagId Diagnostic which should be emitted if error is found.
6018 /// \param NoteId Diagnostic note for the main error message.
6019 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006020 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006021 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006022 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006023 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006024 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006025 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006026 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6027 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6028 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006029 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006030 /// false otherwise.
6031 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6032
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006033 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006034 /// if it is a prefix unary operation.
6035 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6036
Alexey Bataev1d160b12015-03-13 12:27:31 +00006037private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006038 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6039 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006040};
6041} // namespace
6042
6043bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6044 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6045 ExprAnalysisErrorCode ErrorFound = NoError;
6046 SourceLocation ErrorLoc, NoteLoc;
6047 SourceRange ErrorRange, NoteRange;
6048 // Allowed constructs are:
6049 // x = x binop expr;
6050 // x = expr binop x;
6051 if (AtomicBinOp->getOpcode() == BO_Assign) {
6052 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006053 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006054 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6055 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6056 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6057 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006058 Op = AtomicInnerBinOp->getOpcode();
6059 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006060 Expr *LHS = AtomicInnerBinOp->getLHS();
6061 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006062 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6063 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6064 /*Canonical=*/true);
6065 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6066 /*Canonical=*/true);
6067 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6068 /*Canonical=*/true);
6069 if (XId == LHSId) {
6070 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006071 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006072 } else if (XId == RHSId) {
6073 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006074 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006075 } else {
6076 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6077 ErrorRange = AtomicInnerBinOp->getSourceRange();
6078 NoteLoc = X->getExprLoc();
6079 NoteRange = X->getSourceRange();
6080 ErrorFound = NotAnUpdateExpression;
6081 }
6082 } else {
6083 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6084 ErrorRange = AtomicInnerBinOp->getSourceRange();
6085 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6086 NoteRange = SourceRange(NoteLoc, NoteLoc);
6087 ErrorFound = NotABinaryOperator;
6088 }
6089 } else {
6090 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6091 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6092 ErrorFound = NotABinaryExpression;
6093 }
6094 } else {
6095 ErrorLoc = AtomicBinOp->getExprLoc();
6096 ErrorRange = AtomicBinOp->getSourceRange();
6097 NoteLoc = AtomicBinOp->getOperatorLoc();
6098 NoteRange = SourceRange(NoteLoc, NoteLoc);
6099 ErrorFound = NotAnAssignmentOp;
6100 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006101 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006102 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6103 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6104 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006105 }
6106 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006107 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006108 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006109}
6110
6111bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6112 unsigned NoteId) {
6113 ExprAnalysisErrorCode ErrorFound = NoError;
6114 SourceLocation ErrorLoc, NoteLoc;
6115 SourceRange ErrorRange, NoteRange;
6116 // Allowed constructs are:
6117 // x++;
6118 // x--;
6119 // ++x;
6120 // --x;
6121 // x binop= expr;
6122 // x = x binop expr;
6123 // x = expr binop x;
6124 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6125 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6126 if (AtomicBody->getType()->isScalarType() ||
6127 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006128 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006129 AtomicBody->IgnoreParenImpCasts())) {
6130 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006131 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006132 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006133 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006134 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006135 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006136 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006137 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6138 AtomicBody->IgnoreParenImpCasts())) {
6139 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006140 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006141 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006142 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006143 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006144 // Check for Unary Operation
6145 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006146 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006147 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6148 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006149 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006150 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6151 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006152 } else {
6153 ErrorFound = NotAnUnaryIncDecExpression;
6154 ErrorLoc = AtomicUnaryOp->getExprLoc();
6155 ErrorRange = AtomicUnaryOp->getSourceRange();
6156 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6157 NoteRange = SourceRange(NoteLoc, NoteLoc);
6158 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006159 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006160 ErrorFound = NotABinaryOrUnaryExpression;
6161 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6162 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6163 }
6164 } else {
6165 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006166 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006167 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6168 }
6169 } else {
6170 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006171 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006172 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6173 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006174 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006175 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6176 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6177 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006178 }
6179 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006180 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006181 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006182 // Build an update expression of form 'OpaqueValueExpr(x) binop
6183 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6184 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6185 auto *OVEX = new (SemaRef.getASTContext())
6186 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6187 auto *OVEExpr = new (SemaRef.getASTContext())
6188 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006189 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006190 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6191 IsXLHSInRHSPart ? OVEExpr : OVEX);
6192 if (Update.isInvalid())
6193 return true;
6194 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6195 Sema::AA_Casting);
6196 if (Update.isInvalid())
6197 return true;
6198 UpdateExpr = Update.get();
6199 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006200 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006201}
6202
Alexey Bataev0162e452014-07-22 10:10:35 +00006203StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6204 Stmt *AStmt,
6205 SourceLocation StartLoc,
6206 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006207 if (!AStmt)
6208 return StmtError();
6209
David Majnemer9d168222016-08-05 17:44:54 +00006210 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006211 // 1.2.2 OpenMP Language Terminology
6212 // Structured block - An executable statement with a single entry at the
6213 // top and a single exit at the bottom.
6214 // The point of exit cannot be a branch out of the structured block.
6215 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006216 OpenMPClauseKind AtomicKind = OMPC_unknown;
6217 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006218 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006219 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006220 C->getClauseKind() == OMPC_update ||
6221 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006222 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006223 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006224 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006225 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6226 << getOpenMPClauseName(AtomicKind);
6227 } else {
6228 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006229 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006230 }
6231 }
6232 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006233
Alexey Bataeve3727102018-04-18 15:57:46 +00006234 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006235 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6236 Body = EWC->getSubExpr();
6237
Alexey Bataev62cec442014-11-18 10:14:22 +00006238 Expr *X = nullptr;
6239 Expr *V = nullptr;
6240 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006241 Expr *UE = nullptr;
6242 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006243 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006244 // OpenMP [2.12.6, atomic Construct]
6245 // In the next expressions:
6246 // * x and v (as applicable) are both l-value expressions with scalar type.
6247 // * During the execution of an atomic region, multiple syntactic
6248 // occurrences of x must designate the same storage location.
6249 // * Neither of v and expr (as applicable) may access the storage location
6250 // designated by x.
6251 // * Neither of x and expr (as applicable) may access the storage location
6252 // designated by v.
6253 // * expr is an expression with scalar type.
6254 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6255 // * binop, binop=, ++, and -- are not overloaded operators.
6256 // * The expression x binop expr must be numerically equivalent to x binop
6257 // (expr). This requirement is satisfied if the operators in expr have
6258 // precedence greater than binop, or by using parentheses around expr or
6259 // subexpressions of expr.
6260 // * The expression expr binop x must be numerically equivalent to (expr)
6261 // binop x. This requirement is satisfied if the operators in expr have
6262 // precedence equal to or greater than binop, or by using parentheses around
6263 // expr or subexpressions of expr.
6264 // * For forms that allow multiple occurrences of x, the number of times
6265 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006266 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006267 enum {
6268 NotAnExpression,
6269 NotAnAssignmentOp,
6270 NotAScalarType,
6271 NotAnLValue,
6272 NoError
6273 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006274 SourceLocation ErrorLoc, NoteLoc;
6275 SourceRange ErrorRange, NoteRange;
6276 // If clause is read:
6277 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006278 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6279 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006280 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6281 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6282 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6283 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6284 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6285 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6286 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006287 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006288 ErrorFound = NotAnLValue;
6289 ErrorLoc = AtomicBinOp->getExprLoc();
6290 ErrorRange = AtomicBinOp->getSourceRange();
6291 NoteLoc = NotLValueExpr->getExprLoc();
6292 NoteRange = NotLValueExpr->getSourceRange();
6293 }
6294 } else if (!X->isInstantiationDependent() ||
6295 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006296 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006297 (X->isInstantiationDependent() || X->getType()->isScalarType())
6298 ? V
6299 : X;
6300 ErrorFound = NotAScalarType;
6301 ErrorLoc = AtomicBinOp->getExprLoc();
6302 ErrorRange = AtomicBinOp->getSourceRange();
6303 NoteLoc = NotScalarExpr->getExprLoc();
6304 NoteRange = NotScalarExpr->getSourceRange();
6305 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006306 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006307 ErrorFound = NotAnAssignmentOp;
6308 ErrorLoc = AtomicBody->getExprLoc();
6309 ErrorRange = AtomicBody->getSourceRange();
6310 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6311 : AtomicBody->getExprLoc();
6312 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6313 : AtomicBody->getSourceRange();
6314 }
6315 } else {
6316 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006317 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006318 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006319 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006320 if (ErrorFound != NoError) {
6321 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6322 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006323 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6324 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006325 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006326 }
6327 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006328 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006329 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006330 enum {
6331 NotAnExpression,
6332 NotAnAssignmentOp,
6333 NotAScalarType,
6334 NotAnLValue,
6335 NoError
6336 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006337 SourceLocation ErrorLoc, NoteLoc;
6338 SourceRange ErrorRange, NoteRange;
6339 // If clause is write:
6340 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006341 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6342 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006343 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6344 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006345 X = AtomicBinOp->getLHS();
6346 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006347 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6348 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6349 if (!X->isLValue()) {
6350 ErrorFound = NotAnLValue;
6351 ErrorLoc = AtomicBinOp->getExprLoc();
6352 ErrorRange = AtomicBinOp->getSourceRange();
6353 NoteLoc = X->getExprLoc();
6354 NoteRange = X->getSourceRange();
6355 }
6356 } else if (!X->isInstantiationDependent() ||
6357 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006358 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006359 (X->isInstantiationDependent() || X->getType()->isScalarType())
6360 ? E
6361 : X;
6362 ErrorFound = NotAScalarType;
6363 ErrorLoc = AtomicBinOp->getExprLoc();
6364 ErrorRange = AtomicBinOp->getSourceRange();
6365 NoteLoc = NotScalarExpr->getExprLoc();
6366 NoteRange = NotScalarExpr->getSourceRange();
6367 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006368 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006369 ErrorFound = NotAnAssignmentOp;
6370 ErrorLoc = AtomicBody->getExprLoc();
6371 ErrorRange = AtomicBody->getSourceRange();
6372 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6373 : AtomicBody->getExprLoc();
6374 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6375 : AtomicBody->getSourceRange();
6376 }
6377 } else {
6378 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006379 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006380 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006381 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006382 if (ErrorFound != NoError) {
6383 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6384 << ErrorRange;
6385 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6386 << NoteRange;
6387 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006388 }
6389 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006390 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006391 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006392 // If clause is update:
6393 // x++;
6394 // x--;
6395 // ++x;
6396 // --x;
6397 // x binop= expr;
6398 // x = x binop expr;
6399 // x = expr binop x;
6400 OpenMPAtomicUpdateChecker Checker(*this);
6401 if (Checker.checkStatement(
6402 Body, (AtomicKind == OMPC_update)
6403 ? diag::err_omp_atomic_update_not_expression_statement
6404 : diag::err_omp_atomic_not_expression_statement,
6405 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006406 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006407 if (!CurContext->isDependentContext()) {
6408 E = Checker.getExpr();
6409 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006410 UE = Checker.getUpdateExpr();
6411 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006412 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006413 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006414 enum {
6415 NotAnAssignmentOp,
6416 NotACompoundStatement,
6417 NotTwoSubstatements,
6418 NotASpecificExpression,
6419 NoError
6420 } ErrorFound = NoError;
6421 SourceLocation ErrorLoc, NoteLoc;
6422 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006423 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006424 // If clause is a capture:
6425 // v = x++;
6426 // v = x--;
6427 // v = ++x;
6428 // v = --x;
6429 // v = x binop= expr;
6430 // v = x = x binop expr;
6431 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006432 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006433 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6434 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6435 V = AtomicBinOp->getLHS();
6436 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6437 OpenMPAtomicUpdateChecker Checker(*this);
6438 if (Checker.checkStatement(
6439 Body, diag::err_omp_atomic_capture_not_expression_statement,
6440 diag::note_omp_atomic_update))
6441 return StmtError();
6442 E = Checker.getExpr();
6443 X = Checker.getX();
6444 UE = Checker.getUpdateExpr();
6445 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6446 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006447 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006448 ErrorLoc = AtomicBody->getExprLoc();
6449 ErrorRange = AtomicBody->getSourceRange();
6450 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6451 : AtomicBody->getExprLoc();
6452 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6453 : AtomicBody->getSourceRange();
6454 ErrorFound = NotAnAssignmentOp;
6455 }
6456 if (ErrorFound != NoError) {
6457 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6458 << ErrorRange;
6459 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6460 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006461 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006462 if (CurContext->isDependentContext())
6463 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006464 } else {
6465 // If clause is a capture:
6466 // { v = x; x = expr; }
6467 // { v = x; x++; }
6468 // { v = x; x--; }
6469 // { v = x; ++x; }
6470 // { v = x; --x; }
6471 // { v = x; x binop= expr; }
6472 // { v = x; x = x binop expr; }
6473 // { v = x; x = expr binop x; }
6474 // { x++; v = x; }
6475 // { x--; v = x; }
6476 // { ++x; v = x; }
6477 // { --x; v = x; }
6478 // { x binop= expr; v = x; }
6479 // { x = x binop expr; v = x; }
6480 // { x = expr binop x; v = x; }
6481 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6482 // Check that this is { expr1; expr2; }
6483 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006484 Stmt *First = CS->body_front();
6485 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006486 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6487 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6488 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6489 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6490 // Need to find what subexpression is 'v' and what is 'x'.
6491 OpenMPAtomicUpdateChecker Checker(*this);
6492 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6493 BinaryOperator *BinOp = nullptr;
6494 if (IsUpdateExprFound) {
6495 BinOp = dyn_cast<BinaryOperator>(First);
6496 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6497 }
6498 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6499 // { v = x; x++; }
6500 // { v = x; x--; }
6501 // { v = x; ++x; }
6502 // { v = x; --x; }
6503 // { v = x; x binop= expr; }
6504 // { v = x; x = x binop expr; }
6505 // { v = x; x = expr binop x; }
6506 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006507 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006508 llvm::FoldingSetNodeID XId, PossibleXId;
6509 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6510 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6511 IsUpdateExprFound = XId == PossibleXId;
6512 if (IsUpdateExprFound) {
6513 V = BinOp->getLHS();
6514 X = Checker.getX();
6515 E = Checker.getExpr();
6516 UE = Checker.getUpdateExpr();
6517 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006518 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006519 }
6520 }
6521 if (!IsUpdateExprFound) {
6522 IsUpdateExprFound = !Checker.checkStatement(First);
6523 BinOp = nullptr;
6524 if (IsUpdateExprFound) {
6525 BinOp = dyn_cast<BinaryOperator>(Second);
6526 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6527 }
6528 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6529 // { x++; v = x; }
6530 // { x--; v = x; }
6531 // { ++x; v = x; }
6532 // { --x; v = x; }
6533 // { x binop= expr; v = x; }
6534 // { x = x binop expr; v = x; }
6535 // { x = expr binop x; v = x; }
6536 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006537 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006538 llvm::FoldingSetNodeID XId, PossibleXId;
6539 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6540 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6541 IsUpdateExprFound = XId == PossibleXId;
6542 if (IsUpdateExprFound) {
6543 V = BinOp->getLHS();
6544 X = Checker.getX();
6545 E = Checker.getExpr();
6546 UE = Checker.getUpdateExpr();
6547 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006548 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006549 }
6550 }
6551 }
6552 if (!IsUpdateExprFound) {
6553 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006554 auto *FirstExpr = dyn_cast<Expr>(First);
6555 auto *SecondExpr = dyn_cast<Expr>(Second);
6556 if (!FirstExpr || !SecondExpr ||
6557 !(FirstExpr->isInstantiationDependent() ||
6558 SecondExpr->isInstantiationDependent())) {
6559 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6560 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006561 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006562 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006563 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006564 NoteRange = ErrorRange = FirstBinOp
6565 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006566 : SourceRange(ErrorLoc, ErrorLoc);
6567 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006568 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6569 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6570 ErrorFound = NotAnAssignmentOp;
6571 NoteLoc = ErrorLoc = SecondBinOp
6572 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006573 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006574 NoteRange = ErrorRange =
6575 SecondBinOp ? SecondBinOp->getSourceRange()
6576 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006577 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006578 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006579 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006580 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006581 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6582 llvm::FoldingSetNodeID X1Id, X2Id;
6583 PossibleXRHSInFirst->Profile(X1Id, Context,
6584 /*Canonical=*/true);
6585 PossibleXLHSInSecond->Profile(X2Id, Context,
6586 /*Canonical=*/true);
6587 IsUpdateExprFound = X1Id == X2Id;
6588 if (IsUpdateExprFound) {
6589 V = FirstBinOp->getLHS();
6590 X = SecondBinOp->getLHS();
6591 E = SecondBinOp->getRHS();
6592 UE = nullptr;
6593 IsXLHSInRHSPart = false;
6594 IsPostfixUpdate = true;
6595 } else {
6596 ErrorFound = NotASpecificExpression;
6597 ErrorLoc = FirstBinOp->getExprLoc();
6598 ErrorRange = FirstBinOp->getSourceRange();
6599 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6600 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6601 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006602 }
6603 }
6604 }
6605 }
6606 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006607 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006608 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006609 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006610 ErrorFound = NotTwoSubstatements;
6611 }
6612 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006613 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006614 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006615 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006616 ErrorFound = NotACompoundStatement;
6617 }
6618 if (ErrorFound != NoError) {
6619 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6620 << ErrorRange;
6621 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6622 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006623 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006624 if (CurContext->isDependentContext())
6625 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006626 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006627 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006628
Reid Kleckner87a31802018-03-12 21:43:02 +00006629 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006630
Alexey Bataev62cec442014-11-18 10:14:22 +00006631 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006632 X, V, E, UE, IsXLHSInRHSPart,
6633 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006634}
6635
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006636StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6637 Stmt *AStmt,
6638 SourceLocation StartLoc,
6639 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006640 if (!AStmt)
6641 return StmtError();
6642
Alexey Bataeve3727102018-04-18 15:57:46 +00006643 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006644 // 1.2.2 OpenMP Language Terminology
6645 // Structured block - An executable statement with a single entry at the
6646 // top and a single exit at the bottom.
6647 // The point of exit cannot be a branch out of the structured block.
6648 // longjmp() and throw() must not violate the entry/exit criteria.
6649 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006650 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6651 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6652 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6653 // 1.2.2 OpenMP Language Terminology
6654 // Structured block - An executable statement with a single entry at the
6655 // top and a single exit at the bottom.
6656 // The point of exit cannot be a branch out of the structured block.
6657 // longjmp() and throw() must not violate the entry/exit criteria.
6658 CS->getCapturedDecl()->setNothrow();
6659 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006660
Alexey Bataev13314bf2014-10-09 04:18:56 +00006661 // OpenMP [2.16, Nesting of Regions]
6662 // If specified, a teams construct must be contained within a target
6663 // construct. That target construct must contain no statements or directives
6664 // outside of the teams construct.
6665 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006666 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006667 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006668 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006669 auto I = CS->body_begin();
6670 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006671 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006672 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6673 OMPTeamsFound = false;
6674 break;
6675 }
6676 ++I;
6677 }
6678 assert(I != CS->body_end() && "Not found statement");
6679 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006680 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006681 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006682 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006683 }
6684 if (!OMPTeamsFound) {
6685 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6686 Diag(DSAStack->getInnerTeamsRegionLoc(),
6687 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006688 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006689 << isa<OMPExecutableDirective>(S);
6690 return StmtError();
6691 }
6692 }
6693
Reid Kleckner87a31802018-03-12 21:43:02 +00006694 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006695
6696 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6697}
6698
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006699StmtResult
6700Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6701 Stmt *AStmt, SourceLocation StartLoc,
6702 SourceLocation EndLoc) {
6703 if (!AStmt)
6704 return StmtError();
6705
Alexey Bataeve3727102018-04-18 15:57:46 +00006706 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006707 // 1.2.2 OpenMP Language Terminology
6708 // Structured block - An executable statement with a single entry at the
6709 // top and a single exit at the bottom.
6710 // The point of exit cannot be a branch out of the structured block.
6711 // longjmp() and throw() must not violate the entry/exit criteria.
6712 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006713 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6714 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6715 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6716 // 1.2.2 OpenMP Language Terminology
6717 // Structured block - An executable statement with a single entry at the
6718 // top and a single exit at the bottom.
6719 // The point of exit cannot be a branch out of the structured block.
6720 // longjmp() and throw() must not violate the entry/exit criteria.
6721 CS->getCapturedDecl()->setNothrow();
6722 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006723
Reid Kleckner87a31802018-03-12 21:43:02 +00006724 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006725
6726 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6727 AStmt);
6728}
6729
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006730StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6731 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006732 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006733 if (!AStmt)
6734 return StmtError();
6735
Alexey Bataeve3727102018-04-18 15:57:46 +00006736 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006737 // 1.2.2 OpenMP Language Terminology
6738 // Structured block - An executable statement with a single entry at the
6739 // top and a single exit at the bottom.
6740 // The point of exit cannot be a branch out of the structured block.
6741 // longjmp() and throw() must not violate the entry/exit criteria.
6742 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006743 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6744 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6745 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6746 // 1.2.2 OpenMP Language Terminology
6747 // Structured block - An executable statement with a single entry at the
6748 // top and a single exit at the bottom.
6749 // The point of exit cannot be a branch out of the structured block.
6750 // longjmp() and throw() must not violate the entry/exit criteria.
6751 CS->getCapturedDecl()->setNothrow();
6752 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006753
6754 OMPLoopDirective::HelperExprs B;
6755 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6756 // define the nested loops number.
6757 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006758 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006759 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006760 VarsWithImplicitDSA, B);
6761 if (NestedLoopCount == 0)
6762 return StmtError();
6763
6764 assert((CurContext->isDependentContext() || B.builtAll()) &&
6765 "omp target parallel for loop exprs were not built");
6766
6767 if (!CurContext->isDependentContext()) {
6768 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006769 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006770 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006771 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006772 B.NumIterations, *this, CurScope,
6773 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006774 return StmtError();
6775 }
6776 }
6777
Reid Kleckner87a31802018-03-12 21:43:02 +00006778 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006779 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6780 NestedLoopCount, Clauses, AStmt,
6781 B, DSAStack->isCancelRegion());
6782}
6783
Alexey Bataev95b64a92017-05-30 16:00:04 +00006784/// Check for existence of a map clause in the list of clauses.
6785static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6786 const OpenMPClauseKind K) {
6787 return llvm::any_of(
6788 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6789}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006790
Alexey Bataev95b64a92017-05-30 16:00:04 +00006791template <typename... Params>
6792static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6793 const Params... ClauseTypes) {
6794 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006795}
6796
Michael Wong65f367f2015-07-21 13:44:28 +00006797StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6798 Stmt *AStmt,
6799 SourceLocation StartLoc,
6800 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006801 if (!AStmt)
6802 return StmtError();
6803
6804 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6805
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006806 // OpenMP [2.10.1, Restrictions, p. 97]
6807 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006808 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6809 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6810 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006811 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006812 return StmtError();
6813 }
6814
Reid Kleckner87a31802018-03-12 21:43:02 +00006815 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00006816
6817 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6818 AStmt);
6819}
6820
Samuel Antaodf67fc42016-01-19 19:15:56 +00006821StmtResult
6822Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6823 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006824 SourceLocation EndLoc, Stmt *AStmt) {
6825 if (!AStmt)
6826 return StmtError();
6827
Alexey Bataeve3727102018-04-18 15:57:46 +00006828 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006829 // 1.2.2 OpenMP Language Terminology
6830 // Structured block - An executable statement with a single entry at the
6831 // top and a single exit at the bottom.
6832 // The point of exit cannot be a branch out of the structured block.
6833 // longjmp() and throw() must not violate the entry/exit criteria.
6834 CS->getCapturedDecl()->setNothrow();
6835 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6836 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6837 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6838 // 1.2.2 OpenMP Language Terminology
6839 // Structured block - An executable statement with a single entry at the
6840 // top and a single exit at the bottom.
6841 // The point of exit cannot be a branch out of the structured block.
6842 // longjmp() and throw() must not violate the entry/exit criteria.
6843 CS->getCapturedDecl()->setNothrow();
6844 }
6845
Samuel Antaodf67fc42016-01-19 19:15:56 +00006846 // OpenMP [2.10.2, Restrictions, p. 99]
6847 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006848 if (!hasClauses(Clauses, OMPC_map)) {
6849 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6850 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006851 return StmtError();
6852 }
6853
Alexey Bataev7828b252017-11-21 17:08:48 +00006854 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6855 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006856}
6857
Samuel Antao72590762016-01-19 20:04:50 +00006858StmtResult
6859Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6860 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006861 SourceLocation EndLoc, Stmt *AStmt) {
6862 if (!AStmt)
6863 return StmtError();
6864
Alexey Bataeve3727102018-04-18 15:57:46 +00006865 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006866 // 1.2.2 OpenMP Language Terminology
6867 // Structured block - An executable statement with a single entry at the
6868 // top and a single exit at the bottom.
6869 // The point of exit cannot be a branch out of the structured block.
6870 // longjmp() and throw() must not violate the entry/exit criteria.
6871 CS->getCapturedDecl()->setNothrow();
6872 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6873 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6874 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6875 // 1.2.2 OpenMP Language Terminology
6876 // Structured block - An executable statement with a single entry at the
6877 // top and a single exit at the bottom.
6878 // The point of exit cannot be a branch out of the structured block.
6879 // longjmp() and throw() must not violate the entry/exit criteria.
6880 CS->getCapturedDecl()->setNothrow();
6881 }
6882
Samuel Antao72590762016-01-19 20:04:50 +00006883 // OpenMP [2.10.3, Restrictions, p. 102]
6884 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006885 if (!hasClauses(Clauses, OMPC_map)) {
6886 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6887 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006888 return StmtError();
6889 }
6890
Alexey Bataev7828b252017-11-21 17:08:48 +00006891 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6892 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006893}
6894
Samuel Antao686c70c2016-05-26 17:30:50 +00006895StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6896 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006897 SourceLocation EndLoc,
6898 Stmt *AStmt) {
6899 if (!AStmt)
6900 return StmtError();
6901
Alexey Bataeve3727102018-04-18 15:57:46 +00006902 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006903 // 1.2.2 OpenMP Language Terminology
6904 // Structured block - An executable statement with a single entry at the
6905 // top and a single exit at the bottom.
6906 // The point of exit cannot be a branch out of the structured block.
6907 // longjmp() and throw() must not violate the entry/exit criteria.
6908 CS->getCapturedDecl()->setNothrow();
6909 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6910 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6911 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6912 // 1.2.2 OpenMP Language Terminology
6913 // Structured block - An executable statement with a single entry at the
6914 // top and a single exit at the bottom.
6915 // The point of exit cannot be a branch out of the structured block.
6916 // longjmp() and throw() must not violate the entry/exit criteria.
6917 CS->getCapturedDecl()->setNothrow();
6918 }
6919
Alexey Bataev95b64a92017-05-30 16:00:04 +00006920 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006921 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6922 return StmtError();
6923 }
Alexey Bataev7828b252017-11-21 17:08:48 +00006924 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6925 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00006926}
6927
Alexey Bataev13314bf2014-10-09 04:18:56 +00006928StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6929 Stmt *AStmt, SourceLocation StartLoc,
6930 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006931 if (!AStmt)
6932 return StmtError();
6933
Alexey Bataeve3727102018-04-18 15:57:46 +00006934 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006935 // 1.2.2 OpenMP Language Terminology
6936 // Structured block - An executable statement with a single entry at the
6937 // top and a single exit at the bottom.
6938 // The point of exit cannot be a branch out of the structured block.
6939 // longjmp() and throw() must not violate the entry/exit criteria.
6940 CS->getCapturedDecl()->setNothrow();
6941
Reid Kleckner87a31802018-03-12 21:43:02 +00006942 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00006943
Alexey Bataevceabd412017-11-30 18:01:54 +00006944 DSAStack->setParentTeamsRegionLoc(StartLoc);
6945
Alexey Bataev13314bf2014-10-09 04:18:56 +00006946 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6947}
6948
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006949StmtResult
6950Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6951 SourceLocation EndLoc,
6952 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006953 if (DSAStack->isParentNowaitRegion()) {
6954 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6955 return StmtError();
6956 }
6957 if (DSAStack->isParentOrderedRegion()) {
6958 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6959 return StmtError();
6960 }
6961 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6962 CancelRegion);
6963}
6964
Alexey Bataev87933c72015-09-18 08:07:34 +00006965StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6966 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006967 SourceLocation EndLoc,
6968 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00006969 if (DSAStack->isParentNowaitRegion()) {
6970 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6971 return StmtError();
6972 }
6973 if (DSAStack->isParentOrderedRegion()) {
6974 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6975 return StmtError();
6976 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006977 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006978 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6979 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006980}
6981
Alexey Bataev382967a2015-12-08 12:06:20 +00006982static bool checkGrainsizeNumTasksClauses(Sema &S,
6983 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006984 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00006985 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006986 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00006987 if (C->getClauseKind() == OMPC_grainsize ||
6988 C->getClauseKind() == OMPC_num_tasks) {
6989 if (!PrevClause)
6990 PrevClause = C;
6991 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006992 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00006993 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6994 << getOpenMPClauseName(C->getClauseKind())
6995 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006996 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00006997 diag::note_omp_previous_grainsize_num_tasks)
6998 << getOpenMPClauseName(PrevClause->getClauseKind());
6999 ErrorFound = true;
7000 }
7001 }
7002 }
7003 return ErrorFound;
7004}
7005
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007006static bool checkReductionClauseWithNogroup(Sema &S,
7007 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007008 const OMPClause *ReductionClause = nullptr;
7009 const OMPClause *NogroupClause = nullptr;
7010 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007011 if (C->getClauseKind() == OMPC_reduction) {
7012 ReductionClause = C;
7013 if (NogroupClause)
7014 break;
7015 continue;
7016 }
7017 if (C->getClauseKind() == OMPC_nogroup) {
7018 NogroupClause = C;
7019 if (ReductionClause)
7020 break;
7021 continue;
7022 }
7023 }
7024 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007025 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7026 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007027 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007028 return true;
7029 }
7030 return false;
7031}
7032
Alexey Bataev49f6e782015-12-01 04:18:41 +00007033StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7034 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007035 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007036 if (!AStmt)
7037 return StmtError();
7038
7039 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7040 OMPLoopDirective::HelperExprs B;
7041 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7042 // define the nested loops number.
7043 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007044 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007045 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007046 VarsWithImplicitDSA, B);
7047 if (NestedLoopCount == 0)
7048 return StmtError();
7049
7050 assert((CurContext->isDependentContext() || B.builtAll()) &&
7051 "omp for loop exprs were not built");
7052
Alexey Bataev382967a2015-12-08 12:06:20 +00007053 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7054 // The grainsize clause and num_tasks clause are mutually exclusive and may
7055 // not appear on the same taskloop directive.
7056 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7057 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007058 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7059 // If a reduction clause is present on the taskloop directive, the nogroup
7060 // clause must not be specified.
7061 if (checkReductionClauseWithNogroup(*this, Clauses))
7062 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007063
Reid Kleckner87a31802018-03-12 21:43:02 +00007064 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007065 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7066 NestedLoopCount, Clauses, AStmt, B);
7067}
7068
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007069StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007071 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007072 if (!AStmt)
7073 return StmtError();
7074
7075 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7076 OMPLoopDirective::HelperExprs B;
7077 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7078 // define the nested loops number.
7079 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007080 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007081 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7082 VarsWithImplicitDSA, B);
7083 if (NestedLoopCount == 0)
7084 return StmtError();
7085
7086 assert((CurContext->isDependentContext() || B.builtAll()) &&
7087 "omp for loop exprs were not built");
7088
Alexey Bataev5a3af132016-03-29 08:58:54 +00007089 if (!CurContext->isDependentContext()) {
7090 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007091 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007092 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007093 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007094 B.NumIterations, *this, CurScope,
7095 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007096 return StmtError();
7097 }
7098 }
7099
Alexey Bataev382967a2015-12-08 12:06:20 +00007100 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7101 // The grainsize clause and num_tasks clause are mutually exclusive and may
7102 // not appear on the same taskloop directive.
7103 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7104 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007105 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7106 // If a reduction clause is present on the taskloop directive, the nogroup
7107 // clause must not be specified.
7108 if (checkReductionClauseWithNogroup(*this, Clauses))
7109 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007110 if (checkSimdlenSafelenSpecified(*this, Clauses))
7111 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007112
Reid Kleckner87a31802018-03-12 21:43:02 +00007113 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007114 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7115 NestedLoopCount, Clauses, AStmt, B);
7116}
7117
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007118StmtResult Sema::ActOnOpenMPDistributeDirective(
7119 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007120 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007121 if (!AStmt)
7122 return StmtError();
7123
7124 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7125 OMPLoopDirective::HelperExprs B;
7126 // In presence of clause 'collapse' with number of loops, it will
7127 // define the nested loops number.
7128 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007129 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007130 nullptr /*ordered not a clause on distribute*/, AStmt,
7131 *this, *DSAStack, VarsWithImplicitDSA, B);
7132 if (NestedLoopCount == 0)
7133 return StmtError();
7134
7135 assert((CurContext->isDependentContext() || B.builtAll()) &&
7136 "omp for loop exprs were not built");
7137
Reid Kleckner87a31802018-03-12 21:43:02 +00007138 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007139 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7140 NestedLoopCount, Clauses, AStmt, B);
7141}
7142
Carlo Bertolli9925f152016-06-27 14:55:37 +00007143StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7144 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007145 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007146 if (!AStmt)
7147 return StmtError();
7148
Alexey Bataeve3727102018-04-18 15:57:46 +00007149 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007150 // 1.2.2 OpenMP Language Terminology
7151 // Structured block - An executable statement with a single entry at the
7152 // top and a single exit at the bottom.
7153 // The point of exit cannot be a branch out of the structured block.
7154 // longjmp() and throw() must not violate the entry/exit criteria.
7155 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007156 for (int ThisCaptureLevel =
7157 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7158 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7159 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7160 // 1.2.2 OpenMP Language Terminology
7161 // Structured block - An executable statement with a single entry at the
7162 // top and a single exit at the bottom.
7163 // The point of exit cannot be a branch out of the structured block.
7164 // longjmp() and throw() must not violate the entry/exit criteria.
7165 CS->getCapturedDecl()->setNothrow();
7166 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007167
7168 OMPLoopDirective::HelperExprs B;
7169 // In presence of clause 'collapse' with number of loops, it will
7170 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007171 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007172 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007173 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007174 VarsWithImplicitDSA, B);
7175 if (NestedLoopCount == 0)
7176 return StmtError();
7177
7178 assert((CurContext->isDependentContext() || B.builtAll()) &&
7179 "omp for loop exprs were not built");
7180
Reid Kleckner87a31802018-03-12 21:43:02 +00007181 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007182 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007183 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7184 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007185}
7186
Kelvin Li4a39add2016-07-05 05:00:15 +00007187StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7188 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007189 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007190 if (!AStmt)
7191 return StmtError();
7192
Alexey Bataeve3727102018-04-18 15:57:46 +00007193 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007194 // 1.2.2 OpenMP Language Terminology
7195 // Structured block - An executable statement with a single entry at the
7196 // top and a single exit at the bottom.
7197 // The point of exit cannot be a branch out of the structured block.
7198 // longjmp() and throw() must not violate the entry/exit criteria.
7199 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007200 for (int ThisCaptureLevel =
7201 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7202 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7203 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7204 // 1.2.2 OpenMP Language Terminology
7205 // Structured block - An executable statement with a single entry at the
7206 // top and a single exit at the bottom.
7207 // The point of exit cannot be a branch out of the structured block.
7208 // longjmp() and throw() must not violate the entry/exit criteria.
7209 CS->getCapturedDecl()->setNothrow();
7210 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007211
7212 OMPLoopDirective::HelperExprs B;
7213 // In presence of clause 'collapse' with number of loops, it will
7214 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007215 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007216 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007217 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007218 VarsWithImplicitDSA, B);
7219 if (NestedLoopCount == 0)
7220 return StmtError();
7221
7222 assert((CurContext->isDependentContext() || B.builtAll()) &&
7223 "omp for loop exprs were not built");
7224
Alexey Bataev438388c2017-11-22 18:34:02 +00007225 if (!CurContext->isDependentContext()) {
7226 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007227 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007228 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7229 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7230 B.NumIterations, *this, CurScope,
7231 DSAStack))
7232 return StmtError();
7233 }
7234 }
7235
Kelvin Lic5609492016-07-15 04:39:07 +00007236 if (checkSimdlenSafelenSpecified(*this, Clauses))
7237 return StmtError();
7238
Reid Kleckner87a31802018-03-12 21:43:02 +00007239 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007240 return OMPDistributeParallelForSimdDirective::Create(
7241 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7242}
7243
Kelvin Li787f3fc2016-07-06 04:45:38 +00007244StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7245 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007246 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007247 if (!AStmt)
7248 return StmtError();
7249
Alexey Bataeve3727102018-04-18 15:57:46 +00007250 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007251 // 1.2.2 OpenMP Language Terminology
7252 // Structured block - An executable statement with a single entry at the
7253 // top and a single exit at the bottom.
7254 // The point of exit cannot be a branch out of the structured block.
7255 // longjmp() and throw() must not violate the entry/exit criteria.
7256 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007257 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7258 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7259 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7260 // 1.2.2 OpenMP Language Terminology
7261 // Structured block - An executable statement with a single entry at the
7262 // top and a single exit at the bottom.
7263 // The point of exit cannot be a branch out of the structured block.
7264 // longjmp() and throw() must not violate the entry/exit criteria.
7265 CS->getCapturedDecl()->setNothrow();
7266 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007267
7268 OMPLoopDirective::HelperExprs B;
7269 // In presence of clause 'collapse' with number of loops, it will
7270 // define the nested loops number.
7271 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007272 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007273 nullptr /*ordered not a clause on distribute*/, CS, *this,
7274 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007275 if (NestedLoopCount == 0)
7276 return StmtError();
7277
7278 assert((CurContext->isDependentContext() || B.builtAll()) &&
7279 "omp for loop exprs were not built");
7280
Alexey Bataev438388c2017-11-22 18:34:02 +00007281 if (!CurContext->isDependentContext()) {
7282 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007283 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007284 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7285 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7286 B.NumIterations, *this, CurScope,
7287 DSAStack))
7288 return StmtError();
7289 }
7290 }
7291
Kelvin Lic5609492016-07-15 04:39:07 +00007292 if (checkSimdlenSafelenSpecified(*this, Clauses))
7293 return StmtError();
7294
Reid Kleckner87a31802018-03-12 21:43:02 +00007295 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007296 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7297 NestedLoopCount, Clauses, AStmt, B);
7298}
7299
Kelvin Lia579b912016-07-14 02:54:56 +00007300StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7301 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007302 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007303 if (!AStmt)
7304 return StmtError();
7305
Alexey Bataeve3727102018-04-18 15:57:46 +00007306 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007307 // 1.2.2 OpenMP Language Terminology
7308 // Structured block - An executable statement with a single entry at the
7309 // top and a single exit at the bottom.
7310 // The point of exit cannot be a branch out of the structured block.
7311 // longjmp() and throw() must not violate the entry/exit criteria.
7312 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007313 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7314 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7315 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7316 // 1.2.2 OpenMP Language Terminology
7317 // Structured block - An executable statement with a single entry at the
7318 // top and a single exit at the bottom.
7319 // The point of exit cannot be a branch out of the structured block.
7320 // longjmp() and throw() must not violate the entry/exit criteria.
7321 CS->getCapturedDecl()->setNothrow();
7322 }
Kelvin Lia579b912016-07-14 02:54:56 +00007323
7324 OMPLoopDirective::HelperExprs B;
7325 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7326 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007327 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007328 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007329 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007330 VarsWithImplicitDSA, B);
7331 if (NestedLoopCount == 0)
7332 return StmtError();
7333
7334 assert((CurContext->isDependentContext() || B.builtAll()) &&
7335 "omp target parallel for simd loop exprs were not built");
7336
7337 if (!CurContext->isDependentContext()) {
7338 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007339 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007340 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007341 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7342 B.NumIterations, *this, CurScope,
7343 DSAStack))
7344 return StmtError();
7345 }
7346 }
Kelvin Lic5609492016-07-15 04:39:07 +00007347 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007348 return StmtError();
7349
Reid Kleckner87a31802018-03-12 21:43:02 +00007350 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007351 return OMPTargetParallelForSimdDirective::Create(
7352 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7353}
7354
Kelvin Li986330c2016-07-20 22:57:10 +00007355StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7356 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007357 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007358 if (!AStmt)
7359 return StmtError();
7360
Alexey Bataeve3727102018-04-18 15:57:46 +00007361 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007362 // 1.2.2 OpenMP Language Terminology
7363 // Structured block - An executable statement with a single entry at the
7364 // top and a single exit at the bottom.
7365 // The point of exit cannot be a branch out of the structured block.
7366 // longjmp() and throw() must not violate the entry/exit criteria.
7367 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007368 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7369 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7370 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7371 // 1.2.2 OpenMP Language Terminology
7372 // Structured block - An executable statement with a single entry at the
7373 // top and a single exit at the bottom.
7374 // The point of exit cannot be a branch out of the structured block.
7375 // longjmp() and throw() must not violate the entry/exit criteria.
7376 CS->getCapturedDecl()->setNothrow();
7377 }
7378
Kelvin Li986330c2016-07-20 22:57:10 +00007379 OMPLoopDirective::HelperExprs B;
7380 // In presence of clause 'collapse' with number of loops, it will define the
7381 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007382 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007383 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007384 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007385 VarsWithImplicitDSA, B);
7386 if (NestedLoopCount == 0)
7387 return StmtError();
7388
7389 assert((CurContext->isDependentContext() || B.builtAll()) &&
7390 "omp target simd loop exprs were not built");
7391
7392 if (!CurContext->isDependentContext()) {
7393 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007394 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007395 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007396 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7397 B.NumIterations, *this, CurScope,
7398 DSAStack))
7399 return StmtError();
7400 }
7401 }
7402
7403 if (checkSimdlenSafelenSpecified(*this, Clauses))
7404 return StmtError();
7405
Reid Kleckner87a31802018-03-12 21:43:02 +00007406 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007407 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7408 NestedLoopCount, Clauses, AStmt, B);
7409}
7410
Kelvin Li02532872016-08-05 14:37:37 +00007411StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7412 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007413 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007414 if (!AStmt)
7415 return StmtError();
7416
Alexey Bataeve3727102018-04-18 15:57:46 +00007417 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007418 // 1.2.2 OpenMP Language Terminology
7419 // Structured block - An executable statement with a single entry at the
7420 // top and a single exit at the bottom.
7421 // The point of exit cannot be a branch out of the structured block.
7422 // longjmp() and throw() must not violate the entry/exit criteria.
7423 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007424 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7425 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7426 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7427 // 1.2.2 OpenMP Language Terminology
7428 // Structured block - An executable statement with a single entry at the
7429 // top and a single exit at the bottom.
7430 // The point of exit cannot be a branch out of the structured block.
7431 // longjmp() and throw() must not violate the entry/exit criteria.
7432 CS->getCapturedDecl()->setNothrow();
7433 }
Kelvin Li02532872016-08-05 14:37:37 +00007434
7435 OMPLoopDirective::HelperExprs B;
7436 // In presence of clause 'collapse' with number of loops, it will
7437 // define the nested loops number.
7438 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007439 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007440 nullptr /*ordered not a clause on distribute*/, CS, *this,
7441 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007442 if (NestedLoopCount == 0)
7443 return StmtError();
7444
7445 assert((CurContext->isDependentContext() || B.builtAll()) &&
7446 "omp teams distribute loop exprs were not built");
7447
Reid Kleckner87a31802018-03-12 21:43:02 +00007448 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007449
7450 DSAStack->setParentTeamsRegionLoc(StartLoc);
7451
David Majnemer9d168222016-08-05 17:44:54 +00007452 return OMPTeamsDistributeDirective::Create(
7453 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007454}
7455
Kelvin Li4e325f72016-10-25 12:50:55 +00007456StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7457 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007458 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007459 if (!AStmt)
7460 return StmtError();
7461
Alexey Bataeve3727102018-04-18 15:57:46 +00007462 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007463 // 1.2.2 OpenMP Language Terminology
7464 // Structured block - An executable statement with a single entry at the
7465 // top and a single exit at the bottom.
7466 // The point of exit cannot be a branch out of the structured block.
7467 // longjmp() and throw() must not violate the entry/exit criteria.
7468 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007469 for (int ThisCaptureLevel =
7470 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7471 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7472 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7473 // 1.2.2 OpenMP Language Terminology
7474 // Structured block - An executable statement with a single entry at the
7475 // top and a single exit at the bottom.
7476 // The point of exit cannot be a branch out of the structured block.
7477 // longjmp() and throw() must not violate the entry/exit criteria.
7478 CS->getCapturedDecl()->setNothrow();
7479 }
7480
Kelvin Li4e325f72016-10-25 12:50:55 +00007481
7482 OMPLoopDirective::HelperExprs B;
7483 // In presence of clause 'collapse' with number of loops, it will
7484 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007485 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007486 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007487 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007488 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007489
7490 if (NestedLoopCount == 0)
7491 return StmtError();
7492
7493 assert((CurContext->isDependentContext() || B.builtAll()) &&
7494 "omp teams distribute simd loop exprs were not built");
7495
7496 if (!CurContext->isDependentContext()) {
7497 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007498 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007499 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7500 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7501 B.NumIterations, *this, CurScope,
7502 DSAStack))
7503 return StmtError();
7504 }
7505 }
7506
7507 if (checkSimdlenSafelenSpecified(*this, Clauses))
7508 return StmtError();
7509
Reid Kleckner87a31802018-03-12 21:43:02 +00007510 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007511
7512 DSAStack->setParentTeamsRegionLoc(StartLoc);
7513
Kelvin Li4e325f72016-10-25 12:50:55 +00007514 return OMPTeamsDistributeSimdDirective::Create(
7515 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7516}
7517
Kelvin Li579e41c2016-11-30 23:51:03 +00007518StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7519 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007520 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007521 if (!AStmt)
7522 return StmtError();
7523
Alexey Bataeve3727102018-04-18 15:57:46 +00007524 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007525 // 1.2.2 OpenMP Language Terminology
7526 // Structured block - An executable statement with a single entry at the
7527 // top and a single exit at the bottom.
7528 // The point of exit cannot be a branch out of the structured block.
7529 // longjmp() and throw() must not violate the entry/exit criteria.
7530 CS->getCapturedDecl()->setNothrow();
7531
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007532 for (int ThisCaptureLevel =
7533 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7534 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7535 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7536 // 1.2.2 OpenMP Language Terminology
7537 // Structured block - An executable statement with a single entry at the
7538 // top and a single exit at the bottom.
7539 // The point of exit cannot be a branch out of the structured block.
7540 // longjmp() and throw() must not violate the entry/exit criteria.
7541 CS->getCapturedDecl()->setNothrow();
7542 }
7543
Kelvin Li579e41c2016-11-30 23:51:03 +00007544 OMPLoopDirective::HelperExprs B;
7545 // In presence of clause 'collapse' with number of loops, it will
7546 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007547 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007548 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007549 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007550 VarsWithImplicitDSA, B);
7551
7552 if (NestedLoopCount == 0)
7553 return StmtError();
7554
7555 assert((CurContext->isDependentContext() || B.builtAll()) &&
7556 "omp for loop exprs were not built");
7557
7558 if (!CurContext->isDependentContext()) {
7559 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007560 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007561 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7562 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7563 B.NumIterations, *this, CurScope,
7564 DSAStack))
7565 return StmtError();
7566 }
7567 }
7568
7569 if (checkSimdlenSafelenSpecified(*this, Clauses))
7570 return StmtError();
7571
Reid Kleckner87a31802018-03-12 21:43:02 +00007572 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007573
7574 DSAStack->setParentTeamsRegionLoc(StartLoc);
7575
Kelvin Li579e41c2016-11-30 23:51:03 +00007576 return OMPTeamsDistributeParallelForSimdDirective::Create(
7577 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7578}
7579
Kelvin Li7ade93f2016-12-09 03:24:30 +00007580StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7581 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007582 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007583 if (!AStmt)
7584 return StmtError();
7585
Alexey Bataeve3727102018-04-18 15:57:46 +00007586 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007587 // 1.2.2 OpenMP Language Terminology
7588 // Structured block - An executable statement with a single entry at the
7589 // top and a single exit at the bottom.
7590 // The point of exit cannot be a branch out of the structured block.
7591 // longjmp() and throw() must not violate the entry/exit criteria.
7592 CS->getCapturedDecl()->setNothrow();
7593
Carlo Bertolli62fae152017-11-20 20:46:39 +00007594 for (int ThisCaptureLevel =
7595 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7596 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7597 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7598 // 1.2.2 OpenMP Language Terminology
7599 // Structured block - An executable statement with a single entry at the
7600 // top and a single exit at the bottom.
7601 // The point of exit cannot be a branch out of the structured block.
7602 // longjmp() and throw() must not violate the entry/exit criteria.
7603 CS->getCapturedDecl()->setNothrow();
7604 }
7605
Kelvin Li7ade93f2016-12-09 03:24:30 +00007606 OMPLoopDirective::HelperExprs B;
7607 // In presence of clause 'collapse' with number of loops, it will
7608 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007609 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007610 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007611 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007612 VarsWithImplicitDSA, B);
7613
7614 if (NestedLoopCount == 0)
7615 return StmtError();
7616
7617 assert((CurContext->isDependentContext() || B.builtAll()) &&
7618 "omp for loop exprs were not built");
7619
Reid Kleckner87a31802018-03-12 21:43:02 +00007620 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007621
7622 DSAStack->setParentTeamsRegionLoc(StartLoc);
7623
Kelvin Li7ade93f2016-12-09 03:24:30 +00007624 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007625 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7626 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007627}
7628
Kelvin Libf594a52016-12-17 05:48:59 +00007629StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7630 Stmt *AStmt,
7631 SourceLocation StartLoc,
7632 SourceLocation EndLoc) {
7633 if (!AStmt)
7634 return StmtError();
7635
Alexey Bataeve3727102018-04-18 15:57:46 +00007636 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007637 // 1.2.2 OpenMP Language Terminology
7638 // Structured block - An executable statement with a single entry at the
7639 // top and a single exit at the bottom.
7640 // The point of exit cannot be a branch out of the structured block.
7641 // longjmp() and throw() must not violate the entry/exit criteria.
7642 CS->getCapturedDecl()->setNothrow();
7643
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007644 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7645 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7646 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7647 // 1.2.2 OpenMP Language Terminology
7648 // Structured block - An executable statement with a single entry at the
7649 // top and a single exit at the bottom.
7650 // The point of exit cannot be a branch out of the structured block.
7651 // longjmp() and throw() must not violate the entry/exit criteria.
7652 CS->getCapturedDecl()->setNothrow();
7653 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007654 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007655
7656 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7657 AStmt);
7658}
7659
Kelvin Li83c451e2016-12-25 04:52:54 +00007660StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7661 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007662 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007663 if (!AStmt)
7664 return StmtError();
7665
Alexey Bataeve3727102018-04-18 15:57:46 +00007666 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007667 // 1.2.2 OpenMP Language Terminology
7668 // Structured block - An executable statement with a single entry at the
7669 // top and a single exit at the bottom.
7670 // The point of exit cannot be a branch out of the structured block.
7671 // longjmp() and throw() must not violate the entry/exit criteria.
7672 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007673 for (int ThisCaptureLevel =
7674 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7675 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7676 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7677 // 1.2.2 OpenMP Language Terminology
7678 // Structured block - An executable statement with a single entry at the
7679 // top and a single exit at the bottom.
7680 // The point of exit cannot be a branch out of the structured block.
7681 // longjmp() and throw() must not violate the entry/exit criteria.
7682 CS->getCapturedDecl()->setNothrow();
7683 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007684
7685 OMPLoopDirective::HelperExprs B;
7686 // In presence of clause 'collapse' with number of loops, it will
7687 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007688 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007689 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7690 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007691 VarsWithImplicitDSA, B);
7692 if (NestedLoopCount == 0)
7693 return StmtError();
7694
7695 assert((CurContext->isDependentContext() || B.builtAll()) &&
7696 "omp target teams distribute loop exprs were not built");
7697
Reid Kleckner87a31802018-03-12 21:43:02 +00007698 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007699 return OMPTargetTeamsDistributeDirective::Create(
7700 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7701}
7702
Kelvin Li80e8f562016-12-29 22:16:30 +00007703StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7704 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007705 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007706 if (!AStmt)
7707 return StmtError();
7708
Alexey Bataeve3727102018-04-18 15:57:46 +00007709 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007710 // 1.2.2 OpenMP Language Terminology
7711 // Structured block - An executable statement with a single entry at the
7712 // top and a single exit at the bottom.
7713 // The point of exit cannot be a branch out of the structured block.
7714 // longjmp() and throw() must not violate the entry/exit criteria.
7715 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007716 for (int ThisCaptureLevel =
7717 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7718 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7719 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7720 // 1.2.2 OpenMP Language Terminology
7721 // Structured block - An executable statement with a single entry at the
7722 // top and a single exit at the bottom.
7723 // The point of exit cannot be a branch out of the structured block.
7724 // longjmp() and throw() must not violate the entry/exit criteria.
7725 CS->getCapturedDecl()->setNothrow();
7726 }
7727
Kelvin Li80e8f562016-12-29 22:16:30 +00007728 OMPLoopDirective::HelperExprs B;
7729 // In presence of clause 'collapse' with number of loops, it will
7730 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007731 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007732 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7733 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007734 VarsWithImplicitDSA, B);
7735 if (NestedLoopCount == 0)
7736 return StmtError();
7737
7738 assert((CurContext->isDependentContext() || B.builtAll()) &&
7739 "omp target teams distribute parallel for loop exprs were not built");
7740
Alexey Bataev647dd842018-01-15 20:59:40 +00007741 if (!CurContext->isDependentContext()) {
7742 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007743 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007744 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7745 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7746 B.NumIterations, *this, CurScope,
7747 DSAStack))
7748 return StmtError();
7749 }
7750 }
7751
Reid Kleckner87a31802018-03-12 21:43:02 +00007752 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007753 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007754 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7755 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007756}
7757
Kelvin Li1851df52017-01-03 05:23:48 +00007758StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7759 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007760 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007761 if (!AStmt)
7762 return StmtError();
7763
Alexey Bataeve3727102018-04-18 15:57:46 +00007764 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007765 // 1.2.2 OpenMP Language Terminology
7766 // Structured block - An executable statement with a single entry at the
7767 // top and a single exit at the bottom.
7768 // The point of exit cannot be a branch out of the structured block.
7769 // longjmp() and throw() must not violate the entry/exit criteria.
7770 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007771 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7772 OMPD_target_teams_distribute_parallel_for_simd);
7773 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7774 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7775 // 1.2.2 OpenMP Language Terminology
7776 // Structured block - An executable statement with a single entry at the
7777 // top and a single exit at the bottom.
7778 // The point of exit cannot be a branch out of the structured block.
7779 // longjmp() and throw() must not violate the entry/exit criteria.
7780 CS->getCapturedDecl()->setNothrow();
7781 }
Kelvin Li1851df52017-01-03 05:23:48 +00007782
7783 OMPLoopDirective::HelperExprs B;
7784 // In presence of clause 'collapse' with number of loops, it will
7785 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007786 unsigned NestedLoopCount =
7787 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00007788 getCollapseNumberExpr(Clauses),
7789 nullptr /*ordered not a clause on distribute*/, CS, *this,
7790 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00007791 if (NestedLoopCount == 0)
7792 return StmtError();
7793
7794 assert((CurContext->isDependentContext() || B.builtAll()) &&
7795 "omp target teams distribute parallel for simd loop exprs were not "
7796 "built");
7797
7798 if (!CurContext->isDependentContext()) {
7799 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007800 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00007801 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7802 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7803 B.NumIterations, *this, CurScope,
7804 DSAStack))
7805 return StmtError();
7806 }
7807 }
7808
Alexey Bataev438388c2017-11-22 18:34:02 +00007809 if (checkSimdlenSafelenSpecified(*this, Clauses))
7810 return StmtError();
7811
Reid Kleckner87a31802018-03-12 21:43:02 +00007812 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00007813 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7814 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7815}
7816
Kelvin Lida681182017-01-10 18:08:18 +00007817StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7818 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007819 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00007820 if (!AStmt)
7821 return StmtError();
7822
7823 auto *CS = cast<CapturedStmt>(AStmt);
7824 // 1.2.2 OpenMP Language Terminology
7825 // Structured block - An executable statement with a single entry at the
7826 // top and a single exit at the bottom.
7827 // The point of exit cannot be a branch out of the structured block.
7828 // longjmp() and throw() must not violate the entry/exit criteria.
7829 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007830 for (int ThisCaptureLevel =
7831 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7832 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7833 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7834 // 1.2.2 OpenMP Language Terminology
7835 // Structured block - An executable statement with a single entry at the
7836 // top and a single exit at the bottom.
7837 // The point of exit cannot be a branch out of the structured block.
7838 // longjmp() and throw() must not violate the entry/exit criteria.
7839 CS->getCapturedDecl()->setNothrow();
7840 }
Kelvin Lida681182017-01-10 18:08:18 +00007841
7842 OMPLoopDirective::HelperExprs B;
7843 // In presence of clause 'collapse' with number of loops, it will
7844 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007845 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00007846 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007847 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007848 VarsWithImplicitDSA, B);
7849 if (NestedLoopCount == 0)
7850 return StmtError();
7851
7852 assert((CurContext->isDependentContext() || B.builtAll()) &&
7853 "omp target teams distribute simd loop exprs were not built");
7854
Alexey Bataev438388c2017-11-22 18:34:02 +00007855 if (!CurContext->isDependentContext()) {
7856 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007857 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007858 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7859 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7860 B.NumIterations, *this, CurScope,
7861 DSAStack))
7862 return StmtError();
7863 }
7864 }
7865
7866 if (checkSimdlenSafelenSpecified(*this, Clauses))
7867 return StmtError();
7868
Reid Kleckner87a31802018-03-12 21:43:02 +00007869 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00007870 return OMPTargetTeamsDistributeSimdDirective::Create(
7871 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7872}
7873
Alexey Bataeved09d242014-05-28 05:53:51 +00007874OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007875 SourceLocation StartLoc,
7876 SourceLocation LParenLoc,
7877 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007878 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007879 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007880 case OMPC_final:
7881 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7882 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007883 case OMPC_num_threads:
7884 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7885 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007886 case OMPC_safelen:
7887 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7888 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007889 case OMPC_simdlen:
7890 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7891 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007892 case OMPC_collapse:
7893 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7894 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007895 case OMPC_ordered:
7896 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7897 break;
Michael Wonge710d542015-08-07 16:16:36 +00007898 case OMPC_device:
7899 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7900 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007901 case OMPC_num_teams:
7902 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7903 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007904 case OMPC_thread_limit:
7905 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7906 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007907 case OMPC_priority:
7908 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7909 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007910 case OMPC_grainsize:
7911 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7912 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007913 case OMPC_num_tasks:
7914 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7915 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007916 case OMPC_hint:
7917 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7918 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007919 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007920 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007921 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007922 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007923 case OMPC_private:
7924 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007925 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007926 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007927 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00007928 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00007929 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007930 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007931 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007932 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007933 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007934 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007935 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007936 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007937 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007938 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007939 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007940 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007941 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007942 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007943 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007944 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007945 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007946 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007947 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007948 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007949 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007950 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007951 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007952 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007953 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007954 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007955 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007956 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007957 llvm_unreachable("Clause is not allowed.");
7958 }
7959 return Res;
7960}
7961
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007962// An OpenMP directive such as 'target parallel' has two captured regions:
7963// for the 'target' and 'parallel' respectively. This function returns
7964// the region in which to capture expressions associated with a clause.
7965// A return value of OMPD_unknown signifies that the expression should not
7966// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00007967static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7968 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7969 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007970 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007971 switch (CKind) {
7972 case OMPC_if:
7973 switch (DKind) {
7974 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007975 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007976 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007977 // If this clause applies to the nested 'parallel' region, capture within
7978 // the 'target' region, otherwise do not capture.
7979 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7980 CaptureRegion = OMPD_target;
7981 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00007982 case OMPD_target_teams_distribute_parallel_for:
7983 case OMPD_target_teams_distribute_parallel_for_simd:
7984 // If this clause applies to the nested 'parallel' region, capture within
7985 // the 'teams' region, otherwise do not capture.
7986 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7987 CaptureRegion = OMPD_teams;
7988 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00007989 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00007990 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00007991 CaptureRegion = OMPD_teams;
7992 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007993 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00007994 case OMPD_target_enter_data:
7995 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00007996 CaptureRegion = OMPD_task;
7997 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00007998 case OMPD_cancel:
7999 case OMPD_parallel:
8000 case OMPD_parallel_sections:
8001 case OMPD_parallel_for:
8002 case OMPD_parallel_for_simd:
8003 case OMPD_target:
8004 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008005 case OMPD_target_teams:
8006 case OMPD_target_teams_distribute:
8007 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008008 case OMPD_distribute_parallel_for:
8009 case OMPD_distribute_parallel_for_simd:
8010 case OMPD_task:
8011 case OMPD_taskloop:
8012 case OMPD_taskloop_simd:
8013 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008014 // Do not capture if-clause expressions.
8015 break;
8016 case OMPD_threadprivate:
8017 case OMPD_taskyield:
8018 case OMPD_barrier:
8019 case OMPD_taskwait:
8020 case OMPD_cancellation_point:
8021 case OMPD_flush:
8022 case OMPD_declare_reduction:
8023 case OMPD_declare_simd:
8024 case OMPD_declare_target:
8025 case OMPD_end_declare_target:
8026 case OMPD_teams:
8027 case OMPD_simd:
8028 case OMPD_for:
8029 case OMPD_for_simd:
8030 case OMPD_sections:
8031 case OMPD_section:
8032 case OMPD_single:
8033 case OMPD_master:
8034 case OMPD_critical:
8035 case OMPD_taskgroup:
8036 case OMPD_distribute:
8037 case OMPD_ordered:
8038 case OMPD_atomic:
8039 case OMPD_distribute_simd:
8040 case OMPD_teams_distribute:
8041 case OMPD_teams_distribute_simd:
8042 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8043 case OMPD_unknown:
8044 llvm_unreachable("Unknown OpenMP directive");
8045 }
8046 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008047 case OMPC_num_threads:
8048 switch (DKind) {
8049 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008050 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008051 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008052 CaptureRegion = OMPD_target;
8053 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008054 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008055 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008056 case OMPD_target_teams_distribute_parallel_for:
8057 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008058 CaptureRegion = OMPD_teams;
8059 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008060 case OMPD_parallel:
8061 case OMPD_parallel_sections:
8062 case OMPD_parallel_for:
8063 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008064 case OMPD_distribute_parallel_for:
8065 case OMPD_distribute_parallel_for_simd:
8066 // Do not capture num_threads-clause expressions.
8067 break;
8068 case OMPD_target_data:
8069 case OMPD_target_enter_data:
8070 case OMPD_target_exit_data:
8071 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008072 case OMPD_target:
8073 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008074 case OMPD_target_teams:
8075 case OMPD_target_teams_distribute:
8076 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008077 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008078 case OMPD_task:
8079 case OMPD_taskloop:
8080 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008081 case OMPD_threadprivate:
8082 case OMPD_taskyield:
8083 case OMPD_barrier:
8084 case OMPD_taskwait:
8085 case OMPD_cancellation_point:
8086 case OMPD_flush:
8087 case OMPD_declare_reduction:
8088 case OMPD_declare_simd:
8089 case OMPD_declare_target:
8090 case OMPD_end_declare_target:
8091 case OMPD_teams:
8092 case OMPD_simd:
8093 case OMPD_for:
8094 case OMPD_for_simd:
8095 case OMPD_sections:
8096 case OMPD_section:
8097 case OMPD_single:
8098 case OMPD_master:
8099 case OMPD_critical:
8100 case OMPD_taskgroup:
8101 case OMPD_distribute:
8102 case OMPD_ordered:
8103 case OMPD_atomic:
8104 case OMPD_distribute_simd:
8105 case OMPD_teams_distribute:
8106 case OMPD_teams_distribute_simd:
8107 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8108 case OMPD_unknown:
8109 llvm_unreachable("Unknown OpenMP directive");
8110 }
8111 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008112 case OMPC_num_teams:
8113 switch (DKind) {
8114 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008115 case OMPD_target_teams_distribute:
8116 case OMPD_target_teams_distribute_simd:
8117 case OMPD_target_teams_distribute_parallel_for:
8118 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008119 CaptureRegion = OMPD_target;
8120 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008121 case OMPD_teams_distribute_parallel_for:
8122 case OMPD_teams_distribute_parallel_for_simd:
8123 case OMPD_teams:
8124 case OMPD_teams_distribute:
8125 case OMPD_teams_distribute_simd:
8126 // Do not capture num_teams-clause expressions.
8127 break;
8128 case OMPD_distribute_parallel_for:
8129 case OMPD_distribute_parallel_for_simd:
8130 case OMPD_task:
8131 case OMPD_taskloop:
8132 case OMPD_taskloop_simd:
8133 case OMPD_target_data:
8134 case OMPD_target_enter_data:
8135 case OMPD_target_exit_data:
8136 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008137 case OMPD_cancel:
8138 case OMPD_parallel:
8139 case OMPD_parallel_sections:
8140 case OMPD_parallel_for:
8141 case OMPD_parallel_for_simd:
8142 case OMPD_target:
8143 case OMPD_target_simd:
8144 case OMPD_target_parallel:
8145 case OMPD_target_parallel_for:
8146 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008147 case OMPD_threadprivate:
8148 case OMPD_taskyield:
8149 case OMPD_barrier:
8150 case OMPD_taskwait:
8151 case OMPD_cancellation_point:
8152 case OMPD_flush:
8153 case OMPD_declare_reduction:
8154 case OMPD_declare_simd:
8155 case OMPD_declare_target:
8156 case OMPD_end_declare_target:
8157 case OMPD_simd:
8158 case OMPD_for:
8159 case OMPD_for_simd:
8160 case OMPD_sections:
8161 case OMPD_section:
8162 case OMPD_single:
8163 case OMPD_master:
8164 case OMPD_critical:
8165 case OMPD_taskgroup:
8166 case OMPD_distribute:
8167 case OMPD_ordered:
8168 case OMPD_atomic:
8169 case OMPD_distribute_simd:
8170 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8171 case OMPD_unknown:
8172 llvm_unreachable("Unknown OpenMP directive");
8173 }
8174 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008175 case OMPC_thread_limit:
8176 switch (DKind) {
8177 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008178 case OMPD_target_teams_distribute:
8179 case OMPD_target_teams_distribute_simd:
8180 case OMPD_target_teams_distribute_parallel_for:
8181 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008182 CaptureRegion = OMPD_target;
8183 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008184 case OMPD_teams_distribute_parallel_for:
8185 case OMPD_teams_distribute_parallel_for_simd:
8186 case OMPD_teams:
8187 case OMPD_teams_distribute:
8188 case OMPD_teams_distribute_simd:
8189 // Do not capture thread_limit-clause expressions.
8190 break;
8191 case OMPD_distribute_parallel_for:
8192 case OMPD_distribute_parallel_for_simd:
8193 case OMPD_task:
8194 case OMPD_taskloop:
8195 case OMPD_taskloop_simd:
8196 case OMPD_target_data:
8197 case OMPD_target_enter_data:
8198 case OMPD_target_exit_data:
8199 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008200 case OMPD_cancel:
8201 case OMPD_parallel:
8202 case OMPD_parallel_sections:
8203 case OMPD_parallel_for:
8204 case OMPD_parallel_for_simd:
8205 case OMPD_target:
8206 case OMPD_target_simd:
8207 case OMPD_target_parallel:
8208 case OMPD_target_parallel_for:
8209 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008210 case OMPD_threadprivate:
8211 case OMPD_taskyield:
8212 case OMPD_barrier:
8213 case OMPD_taskwait:
8214 case OMPD_cancellation_point:
8215 case OMPD_flush:
8216 case OMPD_declare_reduction:
8217 case OMPD_declare_simd:
8218 case OMPD_declare_target:
8219 case OMPD_end_declare_target:
8220 case OMPD_simd:
8221 case OMPD_for:
8222 case OMPD_for_simd:
8223 case OMPD_sections:
8224 case OMPD_section:
8225 case OMPD_single:
8226 case OMPD_master:
8227 case OMPD_critical:
8228 case OMPD_taskgroup:
8229 case OMPD_distribute:
8230 case OMPD_ordered:
8231 case OMPD_atomic:
8232 case OMPD_distribute_simd:
8233 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8234 case OMPD_unknown:
8235 llvm_unreachable("Unknown OpenMP directive");
8236 }
8237 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008238 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008239 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008240 case OMPD_parallel_for:
8241 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008242 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008243 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008244 case OMPD_teams_distribute_parallel_for:
8245 case OMPD_teams_distribute_parallel_for_simd:
8246 case OMPD_target_parallel_for:
8247 case OMPD_target_parallel_for_simd:
8248 case OMPD_target_teams_distribute_parallel_for:
8249 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008250 CaptureRegion = OMPD_parallel;
8251 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008252 case OMPD_for:
8253 case OMPD_for_simd:
8254 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008255 break;
8256 case OMPD_task:
8257 case OMPD_taskloop:
8258 case OMPD_taskloop_simd:
8259 case OMPD_target_data:
8260 case OMPD_target_enter_data:
8261 case OMPD_target_exit_data:
8262 case OMPD_target_update:
8263 case OMPD_teams:
8264 case OMPD_teams_distribute:
8265 case OMPD_teams_distribute_simd:
8266 case OMPD_target_teams_distribute:
8267 case OMPD_target_teams_distribute_simd:
8268 case OMPD_target:
8269 case OMPD_target_simd:
8270 case OMPD_target_parallel:
8271 case OMPD_cancel:
8272 case OMPD_parallel:
8273 case OMPD_parallel_sections:
8274 case OMPD_threadprivate:
8275 case OMPD_taskyield:
8276 case OMPD_barrier:
8277 case OMPD_taskwait:
8278 case OMPD_cancellation_point:
8279 case OMPD_flush:
8280 case OMPD_declare_reduction:
8281 case OMPD_declare_simd:
8282 case OMPD_declare_target:
8283 case OMPD_end_declare_target:
8284 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008285 case OMPD_sections:
8286 case OMPD_section:
8287 case OMPD_single:
8288 case OMPD_master:
8289 case OMPD_critical:
8290 case OMPD_taskgroup:
8291 case OMPD_distribute:
8292 case OMPD_ordered:
8293 case OMPD_atomic:
8294 case OMPD_distribute_simd:
8295 case OMPD_target_teams:
8296 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8297 case OMPD_unknown:
8298 llvm_unreachable("Unknown OpenMP directive");
8299 }
8300 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008301 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008302 switch (DKind) {
8303 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008304 case OMPD_teams_distribute_parallel_for_simd:
8305 case OMPD_teams_distribute:
8306 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008307 case OMPD_target_teams_distribute_parallel_for:
8308 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008309 case OMPD_target_teams_distribute:
8310 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008311 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008312 break;
8313 case OMPD_distribute_parallel_for:
8314 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008315 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008316 case OMPD_distribute_simd:
8317 // Do not capture thread_limit-clause expressions.
8318 break;
8319 case OMPD_parallel_for:
8320 case OMPD_parallel_for_simd:
8321 case OMPD_target_parallel_for_simd:
8322 case OMPD_target_parallel_for:
8323 case OMPD_task:
8324 case OMPD_taskloop:
8325 case OMPD_taskloop_simd:
8326 case OMPD_target_data:
8327 case OMPD_target_enter_data:
8328 case OMPD_target_exit_data:
8329 case OMPD_target_update:
8330 case OMPD_teams:
8331 case OMPD_target:
8332 case OMPD_target_simd:
8333 case OMPD_target_parallel:
8334 case OMPD_cancel:
8335 case OMPD_parallel:
8336 case OMPD_parallel_sections:
8337 case OMPD_threadprivate:
8338 case OMPD_taskyield:
8339 case OMPD_barrier:
8340 case OMPD_taskwait:
8341 case OMPD_cancellation_point:
8342 case OMPD_flush:
8343 case OMPD_declare_reduction:
8344 case OMPD_declare_simd:
8345 case OMPD_declare_target:
8346 case OMPD_end_declare_target:
8347 case OMPD_simd:
8348 case OMPD_for:
8349 case OMPD_for_simd:
8350 case OMPD_sections:
8351 case OMPD_section:
8352 case OMPD_single:
8353 case OMPD_master:
8354 case OMPD_critical:
8355 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008356 case OMPD_ordered:
8357 case OMPD_atomic:
8358 case OMPD_target_teams:
8359 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8360 case OMPD_unknown:
8361 llvm_unreachable("Unknown OpenMP directive");
8362 }
8363 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008364 case OMPC_device:
8365 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008366 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008367 case OMPD_target_enter_data:
8368 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008369 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008370 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008371 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008372 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008373 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008374 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008375 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008376 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008377 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008378 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008379 CaptureRegion = OMPD_task;
8380 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008381 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008382 // Do not capture device-clause expressions.
8383 break;
8384 case OMPD_teams_distribute_parallel_for:
8385 case OMPD_teams_distribute_parallel_for_simd:
8386 case OMPD_teams:
8387 case OMPD_teams_distribute:
8388 case OMPD_teams_distribute_simd:
8389 case OMPD_distribute_parallel_for:
8390 case OMPD_distribute_parallel_for_simd:
8391 case OMPD_task:
8392 case OMPD_taskloop:
8393 case OMPD_taskloop_simd:
8394 case OMPD_cancel:
8395 case OMPD_parallel:
8396 case OMPD_parallel_sections:
8397 case OMPD_parallel_for:
8398 case OMPD_parallel_for_simd:
8399 case OMPD_threadprivate:
8400 case OMPD_taskyield:
8401 case OMPD_barrier:
8402 case OMPD_taskwait:
8403 case OMPD_cancellation_point:
8404 case OMPD_flush:
8405 case OMPD_declare_reduction:
8406 case OMPD_declare_simd:
8407 case OMPD_declare_target:
8408 case OMPD_end_declare_target:
8409 case OMPD_simd:
8410 case OMPD_for:
8411 case OMPD_for_simd:
8412 case OMPD_sections:
8413 case OMPD_section:
8414 case OMPD_single:
8415 case OMPD_master:
8416 case OMPD_critical:
8417 case OMPD_taskgroup:
8418 case OMPD_distribute:
8419 case OMPD_ordered:
8420 case OMPD_atomic:
8421 case OMPD_distribute_simd:
8422 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8423 case OMPD_unknown:
8424 llvm_unreachable("Unknown OpenMP directive");
8425 }
8426 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008427 case OMPC_firstprivate:
8428 case OMPC_lastprivate:
8429 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008430 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008431 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008432 case OMPC_linear:
8433 case OMPC_default:
8434 case OMPC_proc_bind:
8435 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008436 case OMPC_safelen:
8437 case OMPC_simdlen:
8438 case OMPC_collapse:
8439 case OMPC_private:
8440 case OMPC_shared:
8441 case OMPC_aligned:
8442 case OMPC_copyin:
8443 case OMPC_copyprivate:
8444 case OMPC_ordered:
8445 case OMPC_nowait:
8446 case OMPC_untied:
8447 case OMPC_mergeable:
8448 case OMPC_threadprivate:
8449 case OMPC_flush:
8450 case OMPC_read:
8451 case OMPC_write:
8452 case OMPC_update:
8453 case OMPC_capture:
8454 case OMPC_seq_cst:
8455 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008456 case OMPC_threads:
8457 case OMPC_simd:
8458 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008459 case OMPC_priority:
8460 case OMPC_grainsize:
8461 case OMPC_nogroup:
8462 case OMPC_num_tasks:
8463 case OMPC_hint:
8464 case OMPC_defaultmap:
8465 case OMPC_unknown:
8466 case OMPC_uniform:
8467 case OMPC_to:
8468 case OMPC_from:
8469 case OMPC_use_device_ptr:
8470 case OMPC_is_device_ptr:
8471 llvm_unreachable("Unexpected OpenMP clause.");
8472 }
8473 return CaptureRegion;
8474}
8475
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008476OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8477 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008478 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008479 SourceLocation NameModifierLoc,
8480 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008481 SourceLocation EndLoc) {
8482 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008483 Stmt *HelperValStmt = nullptr;
8484 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008485 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8486 !Condition->isInstantiationDependent() &&
8487 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008488 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008489 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008490 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008491
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008492 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008493
8494 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8495 CaptureRegion =
8496 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008497 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008498 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008499 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008500 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8501 HelperValStmt = buildPreInits(Context, Captures);
8502 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008503 }
8504
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008505 return new (Context)
8506 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8507 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008508}
8509
Alexey Bataev3778b602014-07-17 07:32:53 +00008510OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8511 SourceLocation StartLoc,
8512 SourceLocation LParenLoc,
8513 SourceLocation EndLoc) {
8514 Expr *ValExpr = Condition;
8515 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8516 !Condition->isInstantiationDependent() &&
8517 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008518 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008519 if (Val.isInvalid())
8520 return nullptr;
8521
Richard Smith03a4aa32016-06-23 19:02:52 +00008522 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008523 }
8524
8525 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8526}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008527ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8528 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008529 if (!Op)
8530 return ExprError();
8531
8532 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8533 public:
8534 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008535 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008536 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8537 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008538 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8539 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008540 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8541 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008542 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8543 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008544 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8545 QualType T,
8546 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008547 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8548 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008549 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8550 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008551 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008552 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008553 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008554 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8555 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008556 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8557 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008558 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8559 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008560 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008561 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008562 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008563 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8564 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008565 llvm_unreachable("conversion functions are permitted");
8566 }
8567 } ConvertDiagnoser;
8568 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8569}
8570
Alexey Bataeve3727102018-04-18 15:57:46 +00008571static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008572 OpenMPClauseKind CKind,
8573 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008574 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8575 !ValExpr->isInstantiationDependent()) {
8576 SourceLocation Loc = ValExpr->getExprLoc();
8577 ExprResult Value =
8578 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8579 if (Value.isInvalid())
8580 return false;
8581
8582 ValExpr = Value.get();
8583 // The expression must evaluate to a non-negative integer value.
8584 llvm::APSInt Result;
8585 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008586 Result.isSigned() &&
8587 !((!StrictlyPositive && Result.isNonNegative()) ||
8588 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008589 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008590 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8591 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008592 return false;
8593 }
8594 }
8595 return true;
8596}
8597
Alexey Bataev568a8332014-03-06 06:15:19 +00008598OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8599 SourceLocation StartLoc,
8600 SourceLocation LParenLoc,
8601 SourceLocation EndLoc) {
8602 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008603 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008604
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008605 // OpenMP [2.5, Restrictions]
8606 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008607 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008608 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008609 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008610
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008611 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008612 OpenMPDirectiveKind CaptureRegion =
8613 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8614 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008615 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008616 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008617 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8618 HelperValStmt = buildPreInits(Context, Captures);
8619 }
8620
8621 return new (Context) OMPNumThreadsClause(
8622 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008623}
8624
Alexey Bataev62c87d22014-03-21 04:51:18 +00008625ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008626 OpenMPClauseKind CKind,
8627 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008628 if (!E)
8629 return ExprError();
8630 if (E->isValueDependent() || E->isTypeDependent() ||
8631 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008632 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008633 llvm::APSInt Result;
8634 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8635 if (ICE.isInvalid())
8636 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008637 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8638 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008639 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008640 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8641 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008642 return ExprError();
8643 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008644 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8645 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8646 << E->getSourceRange();
8647 return ExprError();
8648 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008649 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8650 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008651 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008652 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008653 return ICE;
8654}
8655
8656OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8657 SourceLocation LParenLoc,
8658 SourceLocation EndLoc) {
8659 // OpenMP [2.8.1, simd construct, Description]
8660 // The parameter of the safelen clause must be a constant
8661 // positive integer expression.
8662 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8663 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008664 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008665 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008666 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008667}
8668
Alexey Bataev66b15b52015-08-21 11:14:16 +00008669OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8670 SourceLocation LParenLoc,
8671 SourceLocation EndLoc) {
8672 // OpenMP [2.8.1, simd construct, Description]
8673 // The parameter of the simdlen clause must be a constant
8674 // positive integer expression.
8675 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8676 if (Simdlen.isInvalid())
8677 return nullptr;
8678 return new (Context)
8679 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8680}
8681
Alexander Musman64d33f12014-06-04 07:53:32 +00008682OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8683 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008684 SourceLocation LParenLoc,
8685 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008686 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008687 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008688 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008689 // The parameter of the collapse clause must be a constant
8690 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008691 ExprResult NumForLoopsResult =
8692 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8693 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008694 return nullptr;
8695 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008696 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008697}
8698
Alexey Bataev10e775f2015-07-30 11:36:16 +00008699OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8700 SourceLocation EndLoc,
8701 SourceLocation LParenLoc,
8702 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008703 // OpenMP [2.7.1, loop construct, Description]
8704 // OpenMP [2.8.1, simd construct, Description]
8705 // OpenMP [2.9.6, distribute construct, Description]
8706 // The parameter of the ordered clause must be a constant
8707 // positive integer expression if any.
8708 if (NumForLoops && LParenLoc.isValid()) {
8709 ExprResult NumForLoopsResult =
8710 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8711 if (NumForLoopsResult.isInvalid())
8712 return nullptr;
8713 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008714 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008715 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008716 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008717 auto *Clause = OMPOrderedClause::Create(
8718 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8719 StartLoc, LParenLoc, EndLoc);
8720 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8721 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008722}
8723
Alexey Bataeved09d242014-05-28 05:53:51 +00008724OMPClause *Sema::ActOnOpenMPSimpleClause(
8725 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8726 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008727 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008728 switch (Kind) {
8729 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008730 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008731 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8732 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008733 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008734 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008735 Res = ActOnOpenMPProcBindClause(
8736 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8737 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008738 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008739 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008740 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008741 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008742 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008743 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008744 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008745 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008746 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008747 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008748 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008749 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008750 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008751 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008752 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008753 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008754 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008755 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008756 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008757 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008758 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008759 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008760 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008761 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008762 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008763 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008764 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008765 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008766 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008767 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008768 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008769 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008770 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008771 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008772 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008773 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008774 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008775 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008776 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008777 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008778 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008779 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008780 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008781 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008782 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008783 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008784 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008785 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008786 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008787 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008788 llvm_unreachable("Clause is not allowed.");
8789 }
8790 return Res;
8791}
8792
Alexey Bataev6402bca2015-12-28 07:25:51 +00008793static std::string
8794getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8795 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008796 SmallString<256> Buffer;
8797 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00008798 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8799 unsigned Skipped = Exclude.size();
8800 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00008801 for (unsigned I = First; I < Last; ++I) {
8802 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008803 --Skipped;
8804 continue;
8805 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008806 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
8807 if (I == Bound - Skipped)
8808 Out << " or ";
8809 else if (I != Bound + 1 - Skipped)
8810 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00008811 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008812 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00008813}
8814
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008815OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8816 SourceLocation KindKwLoc,
8817 SourceLocation StartLoc,
8818 SourceLocation LParenLoc,
8819 SourceLocation EndLoc) {
8820 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008821 static_assert(OMPC_DEFAULT_unknown > 0,
8822 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008823 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008824 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8825 /*Last=*/OMPC_DEFAULT_unknown)
8826 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008827 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008828 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008829 switch (Kind) {
8830 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008831 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008832 break;
8833 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008834 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008835 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008836 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008837 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008838 break;
8839 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008840 return new (Context)
8841 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008842}
8843
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008844OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8845 SourceLocation KindKwLoc,
8846 SourceLocation StartLoc,
8847 SourceLocation LParenLoc,
8848 SourceLocation EndLoc) {
8849 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008850 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008851 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8852 /*Last=*/OMPC_PROC_BIND_unknown)
8853 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008854 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008855 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008856 return new (Context)
8857 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008858}
8859
Alexey Bataev56dafe82014-06-20 07:16:17 +00008860OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008861 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008862 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008863 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008864 SourceLocation EndLoc) {
8865 OMPClause *Res = nullptr;
8866 switch (Kind) {
8867 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008868 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8869 assert(Argument.size() == NumberOfElements &&
8870 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008871 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008872 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8873 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8874 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8875 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8876 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008877 break;
8878 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008879 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8880 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8881 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8882 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008883 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008884 case OMPC_dist_schedule:
8885 Res = ActOnOpenMPDistScheduleClause(
8886 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8887 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8888 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008889 case OMPC_defaultmap:
8890 enum { Modifier, DefaultmapKind };
8891 Res = ActOnOpenMPDefaultmapClause(
8892 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8893 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008894 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8895 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008896 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00008897 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008898 case OMPC_num_threads:
8899 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008900 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008901 case OMPC_collapse:
8902 case OMPC_default:
8903 case OMPC_proc_bind:
8904 case OMPC_private:
8905 case OMPC_firstprivate:
8906 case OMPC_lastprivate:
8907 case OMPC_shared:
8908 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008909 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008910 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008911 case OMPC_linear:
8912 case OMPC_aligned:
8913 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008914 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008915 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008916 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008917 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008918 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008919 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008920 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008921 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008922 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008923 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008924 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008925 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008926 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008927 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008928 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008929 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008930 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008931 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008932 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008933 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008934 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008935 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008936 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008937 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008938 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008939 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008940 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008941 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008942 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008943 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008944 llvm_unreachable("Clause is not allowed.");
8945 }
8946 return Res;
8947}
8948
Alexey Bataev6402bca2015-12-28 07:25:51 +00008949static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8950 OpenMPScheduleClauseModifier M2,
8951 SourceLocation M1Loc, SourceLocation M2Loc) {
8952 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8953 SmallVector<unsigned, 2> Excluded;
8954 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8955 Excluded.push_back(M2);
8956 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8957 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8958 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8959 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8960 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8961 << getListOfPossibleValues(OMPC_schedule,
8962 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8963 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8964 Excluded)
8965 << getOpenMPClauseName(OMPC_schedule);
8966 return true;
8967 }
8968 return false;
8969}
8970
Alexey Bataev56dafe82014-06-20 07:16:17 +00008971OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008972 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008973 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008974 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8975 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8976 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8977 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8978 return nullptr;
8979 // OpenMP, 2.7.1, Loop Construct, Restrictions
8980 // Either the monotonic modifier or the nonmonotonic modifier can be specified
8981 // but not both.
8982 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8983 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8984 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8985 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8986 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8987 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8988 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8989 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8990 return nullptr;
8991 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00008992 if (Kind == OMPC_SCHEDULE_unknown) {
8993 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00008994 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8995 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8996 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8997 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8998 Exclude);
8999 } else {
9000 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9001 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009002 }
9003 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9004 << Values << getOpenMPClauseName(OMPC_schedule);
9005 return nullptr;
9006 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009007 // OpenMP, 2.7.1, Loop Construct, Restrictions
9008 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9009 // schedule(guided).
9010 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9011 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9012 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9013 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9014 diag::err_omp_schedule_nonmonotonic_static);
9015 return nullptr;
9016 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009017 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009018 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009019 if (ChunkSize) {
9020 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9021 !ChunkSize->isInstantiationDependent() &&
9022 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009023 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009024 ExprResult Val =
9025 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9026 if (Val.isInvalid())
9027 return nullptr;
9028
9029 ValExpr = Val.get();
9030
9031 // OpenMP [2.7.1, Restrictions]
9032 // chunk_size must be a loop invariant integer expression with a positive
9033 // value.
9034 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009035 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9036 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9037 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009038 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009039 return nullptr;
9040 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009041 } else if (getOpenMPCaptureRegionForClause(
9042 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9043 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009044 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009045 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009046 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009047 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9048 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009049 }
9050 }
9051 }
9052
Alexey Bataev6402bca2015-12-28 07:25:51 +00009053 return new (Context)
9054 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009055 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009056}
9057
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009058OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9059 SourceLocation StartLoc,
9060 SourceLocation EndLoc) {
9061 OMPClause *Res = nullptr;
9062 switch (Kind) {
9063 case OMPC_ordered:
9064 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9065 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009066 case OMPC_nowait:
9067 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9068 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009069 case OMPC_untied:
9070 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9071 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009072 case OMPC_mergeable:
9073 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9074 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009075 case OMPC_read:
9076 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9077 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009078 case OMPC_write:
9079 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9080 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009081 case OMPC_update:
9082 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9083 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009084 case OMPC_capture:
9085 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9086 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009087 case OMPC_seq_cst:
9088 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9089 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009090 case OMPC_threads:
9091 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9092 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009093 case OMPC_simd:
9094 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9095 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009096 case OMPC_nogroup:
9097 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9098 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009099 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009100 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009101 case OMPC_num_threads:
9102 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009103 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009104 case OMPC_collapse:
9105 case OMPC_schedule:
9106 case OMPC_private:
9107 case OMPC_firstprivate:
9108 case OMPC_lastprivate:
9109 case OMPC_shared:
9110 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009111 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009112 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009113 case OMPC_linear:
9114 case OMPC_aligned:
9115 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009116 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009117 case OMPC_default:
9118 case OMPC_proc_bind:
9119 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009120 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009121 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009122 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009123 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009124 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009125 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009126 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009127 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009128 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009129 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009130 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009131 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009132 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009133 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009134 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009135 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009136 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009137 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009138 llvm_unreachable("Clause is not allowed.");
9139 }
9140 return Res;
9141}
9142
Alexey Bataev236070f2014-06-20 11:19:47 +00009143OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9144 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009145 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009146 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9147}
9148
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009149OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9150 SourceLocation EndLoc) {
9151 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9152}
9153
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009154OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9155 SourceLocation EndLoc) {
9156 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9157}
9158
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009159OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9160 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009161 return new (Context) OMPReadClause(StartLoc, EndLoc);
9162}
9163
Alexey Bataevdea47612014-07-23 07:46:59 +00009164OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9165 SourceLocation EndLoc) {
9166 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9167}
9168
Alexey Bataev67a4f222014-07-23 10:25:33 +00009169OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9170 SourceLocation EndLoc) {
9171 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9172}
9173
Alexey Bataev459dec02014-07-24 06:46:57 +00009174OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9175 SourceLocation EndLoc) {
9176 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9177}
9178
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009179OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9180 SourceLocation EndLoc) {
9181 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9182}
9183
Alexey Bataev346265e2015-09-25 10:37:12 +00009184OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9185 SourceLocation EndLoc) {
9186 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9187}
9188
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009189OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9190 SourceLocation EndLoc) {
9191 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9192}
9193
Alexey Bataevb825de12015-12-07 10:51:44 +00009194OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9195 SourceLocation EndLoc) {
9196 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9197}
9198
Alexey Bataevc5e02582014-06-16 07:08:35 +00009199OMPClause *Sema::ActOnOpenMPVarListClause(
9200 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9201 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9202 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009203 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009204 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9205 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9206 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009207 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009208 switch (Kind) {
9209 case OMPC_private:
9210 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9211 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009212 case OMPC_firstprivate:
9213 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9214 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009215 case OMPC_lastprivate:
9216 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9217 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009218 case OMPC_shared:
9219 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9220 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009221 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009222 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9223 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009224 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009225 case OMPC_task_reduction:
9226 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9227 EndLoc, ReductionIdScopeSpec,
9228 ReductionId);
9229 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009230 case OMPC_in_reduction:
9231 Res =
9232 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9233 EndLoc, ReductionIdScopeSpec, ReductionId);
9234 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009235 case OMPC_linear:
9236 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009237 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009238 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009239 case OMPC_aligned:
9240 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9241 ColonLoc, EndLoc);
9242 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009243 case OMPC_copyin:
9244 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9245 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009246 case OMPC_copyprivate:
9247 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9248 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009249 case OMPC_flush:
9250 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9251 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009252 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009253 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009254 StartLoc, LParenLoc, EndLoc);
9255 break;
9256 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009257 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9258 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9259 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009260 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009261 case OMPC_to:
9262 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9263 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009264 case OMPC_from:
9265 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9266 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009267 case OMPC_use_device_ptr:
9268 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9269 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009270 case OMPC_is_device_ptr:
9271 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9272 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009273 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009274 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009275 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009276 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009277 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009278 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009279 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009280 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009281 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009282 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009283 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009284 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009285 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009286 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009287 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009288 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009289 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009290 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009291 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009292 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009293 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009294 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009295 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009296 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009297 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009298 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009299 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009300 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009301 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009302 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009303 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009304 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009305 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009306 llvm_unreachable("Clause is not allowed.");
9307 }
9308 return Res;
9309}
9310
Alexey Bataev90c228f2016-02-08 09:29:13 +00009311ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009312 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009313 ExprResult Res = BuildDeclRefExpr(
9314 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9315 if (!Res.isUsable())
9316 return ExprError();
9317 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9318 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9319 if (!Res.isUsable())
9320 return ExprError();
9321 }
9322 if (VK != VK_LValue && Res.get()->isGLValue()) {
9323 Res = DefaultLvalueConversion(Res.get());
9324 if (!Res.isUsable())
9325 return ExprError();
9326 }
9327 return Res;
9328}
9329
Alexey Bataev60da77e2016-02-29 05:54:20 +00009330static std::pair<ValueDecl *, bool>
9331getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9332 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009333 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9334 RefExpr->containsUnexpandedParameterPack())
9335 return std::make_pair(nullptr, true);
9336
Alexey Bataevd985eda2016-02-10 11:29:16 +00009337 // OpenMP [3.1, C/C++]
9338 // A list item is a variable name.
9339 // OpenMP [2.9.3.3, Restrictions, p.1]
9340 // A variable that is part of another variable (as an array or
9341 // structure element) cannot appear in a private clause.
9342 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009343 enum {
9344 NoArrayExpr = -1,
9345 ArraySubscript = 0,
9346 OMPArraySection = 1
9347 } IsArrayExpr = NoArrayExpr;
9348 if (AllowArraySection) {
9349 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009350 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009351 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9352 Base = TempASE->getBase()->IgnoreParenImpCasts();
9353 RefExpr = Base;
9354 IsArrayExpr = ArraySubscript;
9355 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009356 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009357 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9358 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9359 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9360 Base = TempASE->getBase()->IgnoreParenImpCasts();
9361 RefExpr = Base;
9362 IsArrayExpr = OMPArraySection;
9363 }
9364 }
9365 ELoc = RefExpr->getExprLoc();
9366 ERange = RefExpr->getSourceRange();
9367 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009368 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9369 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9370 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9371 (S.getCurrentThisType().isNull() || !ME ||
9372 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9373 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009374 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009375 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9376 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009377 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009378 S.Diag(ELoc,
9379 AllowArraySection
9380 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9381 : diag::err_omp_expected_var_name_member_expr)
9382 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9383 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009384 return std::make_pair(nullptr, false);
9385 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009386 return std::make_pair(
9387 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009388}
9389
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009390OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9391 SourceLocation StartLoc,
9392 SourceLocation LParenLoc,
9393 SourceLocation EndLoc) {
9394 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009395 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009396 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009397 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009398 SourceLocation ELoc;
9399 SourceRange ERange;
9400 Expr *SimpleRefExpr = RefExpr;
9401 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009402 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009403 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009404 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009405 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009406 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009407 ValueDecl *D = Res.first;
9408 if (!D)
9409 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009410
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009411 QualType Type = D->getType();
9412 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009413
9414 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9415 // A variable that appears in a private clause must not have an incomplete
9416 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009417 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009418 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009419 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009420
Alexey Bataev758e55e2013-09-06 18:03:48 +00009421 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9422 // in a Construct]
9423 // Variables with the predetermined data-sharing attributes may not be
9424 // listed in data-sharing attributes clauses, except for the cases
9425 // listed below. For these exceptions only, listing a predetermined
9426 // variable in a data-sharing attribute clause is allowed and overrides
9427 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009428 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009429 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009430 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9431 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009432 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009433 continue;
9434 }
9435
Alexey Bataeve3727102018-04-18 15:57:46 +00009436 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009437 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009438 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009439 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009440 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9441 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009442 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009443 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009444 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009445 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009446 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009447 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009448 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009449 continue;
9450 }
9451
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009452 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9453 // A list item cannot appear in both a map clause and a data-sharing
9454 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009455 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009456 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009457 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009458 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009459 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9460 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9461 ConflictKind = WhereFoundClauseKind;
9462 return true;
9463 })) {
9464 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009465 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009466 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009467 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009468 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009469 continue;
9470 }
9471 }
9472
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009473 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9474 // A variable of class type (or array thereof) that appears in a private
9475 // clause requires an accessible, unambiguous default constructor for the
9476 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009477 // Generate helper private variable and initialize it with the default
9478 // value. The address of the original variable is replaced by the address of
9479 // the new private variable in CodeGen. This new variable is not added to
9480 // IdResolver, so the code in the OpenMP region uses original variable for
9481 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009482 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009483 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009484 buildVarDecl(*this, ELoc, Type, D->getName(),
9485 D->hasAttrs() ? &D->getAttrs() : nullptr,
9486 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009487 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009488 if (VDPrivate->isInvalidDecl())
9489 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009490 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009491 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009492
Alexey Bataev90c228f2016-02-08 09:29:13 +00009493 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009494 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009495 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009496 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009497 Vars.push_back((VD || CurContext->isDependentContext())
9498 ? RefExpr->IgnoreParens()
9499 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009500 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009501 }
9502
Alexey Bataeved09d242014-05-28 05:53:51 +00009503 if (Vars.empty())
9504 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009505
Alexey Bataev03b340a2014-10-21 03:16:40 +00009506 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9507 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009508}
9509
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009510namespace {
9511class DiagsUninitializedSeveretyRAII {
9512private:
9513 DiagnosticsEngine &Diags;
9514 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009515 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009516
9517public:
9518 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9519 bool IsIgnored)
9520 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9521 if (!IsIgnored) {
9522 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9523 /*Map*/ diag::Severity::Ignored, Loc);
9524 }
9525 }
9526 ~DiagsUninitializedSeveretyRAII() {
9527 if (!IsIgnored)
9528 Diags.popMappings(SavedLoc);
9529 }
9530};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009531}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009532
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009533OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9534 SourceLocation StartLoc,
9535 SourceLocation LParenLoc,
9536 SourceLocation EndLoc) {
9537 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009538 SmallVector<Expr *, 8> PrivateCopies;
9539 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009540 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009541 bool IsImplicitClause =
9542 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009543 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009544
Alexey Bataeve3727102018-04-18 15:57:46 +00009545 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009546 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009547 SourceLocation ELoc;
9548 SourceRange ERange;
9549 Expr *SimpleRefExpr = RefExpr;
9550 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009551 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009552 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009553 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009554 PrivateCopies.push_back(nullptr);
9555 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009556 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009557 ValueDecl *D = Res.first;
9558 if (!D)
9559 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009560
Alexey Bataev60da77e2016-02-29 05:54:20 +00009561 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009562 QualType Type = D->getType();
9563 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009564
9565 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9566 // A variable that appears in a private clause must not have an incomplete
9567 // type or a reference type.
9568 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009569 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009570 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009571 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009572
9573 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9574 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009575 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009576 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009577 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009578
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009579 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009580 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009581 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009582 DSAStackTy::DSAVarData DVar =
9583 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009584 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009585 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009586 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009587 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9588 // A list item that specifies a given variable may not appear in more
9589 // than one clause on the same directive, except that a variable may be
9590 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009591 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9592 // A list item may appear in a firstprivate or lastprivate clause but not
9593 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009594 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009595 (isOpenMPDistributeDirective(CurrDir) ||
9596 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009597 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009598 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009599 << getOpenMPClauseName(DVar.CKind)
9600 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009601 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009602 continue;
9603 }
9604
9605 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9606 // in a Construct]
9607 // Variables with the predetermined data-sharing attributes may not be
9608 // listed in data-sharing attributes clauses, except for the cases
9609 // listed below. For these exceptions only, listing a predetermined
9610 // variable in a data-sharing attribute clause is allowed and overrides
9611 // the variable's predetermined data-sharing attributes.
9612 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9613 // in a Construct, C/C++, p.2]
9614 // Variables with const-qualified type having no mutable member may be
9615 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009616 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009617 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9618 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009619 << getOpenMPClauseName(DVar.CKind)
9620 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009621 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009622 continue;
9623 }
9624
9625 // OpenMP [2.9.3.4, Restrictions, p.2]
9626 // A list item that is private within a parallel region must not appear
9627 // in a firstprivate clause on a worksharing construct if any of the
9628 // worksharing regions arising from the worksharing construct ever bind
9629 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009630 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9631 // A list item that is private within a teams region must not appear in a
9632 // firstprivate clause on a distribute construct if any of the distribute
9633 // regions arising from the distribute construct ever bind to any of the
9634 // teams regions arising from the teams construct.
9635 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9636 // A list item that appears in a reduction clause of a teams construct
9637 // must not appear in a firstprivate clause on a distribute construct if
9638 // any of the distribute regions arising from the distribute construct
9639 // ever bind to any of the teams regions arising from the teams construct.
9640 if ((isOpenMPWorksharingDirective(CurrDir) ||
9641 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009642 !isOpenMPParallelDirective(CurrDir) &&
9643 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009644 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009645 if (DVar.CKind != OMPC_shared &&
9646 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009647 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009648 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009649 Diag(ELoc, diag::err_omp_required_access)
9650 << getOpenMPClauseName(OMPC_firstprivate)
9651 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009652 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009653 continue;
9654 }
9655 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009656 // OpenMP [2.9.3.4, Restrictions, p.3]
9657 // A list item that appears in a reduction clause of a parallel construct
9658 // must not appear in a firstprivate clause on a worksharing or task
9659 // construct if any of the worksharing or task regions arising from the
9660 // worksharing or task construct ever bind to any of the parallel regions
9661 // arising from the parallel construct.
9662 // OpenMP [2.9.3.4, Restrictions, p.4]
9663 // A list item that appears in a reduction clause in worksharing
9664 // construct must not appear in a firstprivate clause in a task construct
9665 // encountered during execution of any of the worksharing regions arising
9666 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009667 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009668 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009669 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9670 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009671 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009672 isOpenMPWorksharingDirective(K) ||
9673 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009674 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009675 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009676 if (DVar.CKind == OMPC_reduction &&
9677 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009678 isOpenMPWorksharingDirective(DVar.DKind) ||
9679 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009680 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9681 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009682 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009683 continue;
9684 }
9685 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009686
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009687 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9688 // A list item cannot appear in both a map clause and a data-sharing
9689 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009690 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009691 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009692 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009693 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009694 [&ConflictKind](
9695 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9696 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009697 ConflictKind = WhereFoundClauseKind;
9698 return true;
9699 })) {
9700 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009701 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009702 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009703 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009704 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009705 continue;
9706 }
9707 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009708 }
9709
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009710 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009711 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009712 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009713 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9714 << getOpenMPClauseName(OMPC_firstprivate) << Type
9715 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9716 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009717 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009718 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009719 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009720 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009721 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009722 continue;
9723 }
9724
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009725 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009726 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009727 buildVarDecl(*this, ELoc, Type, D->getName(),
9728 D->hasAttrs() ? &D->getAttrs() : nullptr,
9729 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009730 // Generate helper private variable and initialize it with the value of the
9731 // original variable. The address of the original variable is replaced by
9732 // the address of the new private variable in the CodeGen. This new variable
9733 // is not added to IdResolver, so the code in the OpenMP region uses
9734 // original variable for proper diagnostics and variable capturing.
9735 Expr *VDInitRefExpr = nullptr;
9736 // For arrays generate initializer for single element and replace it by the
9737 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009738 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009739 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009740 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009741 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009742 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009743 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009744 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9745 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009746 InitializedEntity Entity =
9747 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009748 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9749
9750 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9751 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9752 if (Result.isInvalid())
9753 VDPrivate->setInvalidDecl();
9754 else
9755 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009756 // Remove temp variable declaration.
9757 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009758 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00009759 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9760 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +00009761 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9762 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009763 AddInitializerToDecl(VDPrivate,
9764 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009765 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009766 }
9767 if (VDPrivate->isInvalidDecl()) {
9768 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009769 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009770 diag::note_omp_task_predetermined_firstprivate_here);
9771 }
9772 continue;
9773 }
9774 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009775 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009776 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9777 RefExpr->getExprLoc());
9778 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009779 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009780 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009781 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009782 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009783 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +00009784 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009785 ExprCaptures.push_back(Ref->getDecl());
9786 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009787 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009788 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009789 Vars.push_back((VD || CurContext->isDependentContext())
9790 ? RefExpr->IgnoreParens()
9791 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009792 PrivateCopies.push_back(VDPrivateRefExpr);
9793 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009794 }
9795
Alexey Bataeved09d242014-05-28 05:53:51 +00009796 if (Vars.empty())
9797 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009798
9799 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009800 Vars, PrivateCopies, Inits,
9801 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009802}
9803
Alexander Musman1bb328c2014-06-04 13:06:39 +00009804OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9805 SourceLocation StartLoc,
9806 SourceLocation LParenLoc,
9807 SourceLocation EndLoc) {
9808 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009809 SmallVector<Expr *, 8> SrcExprs;
9810 SmallVector<Expr *, 8> DstExprs;
9811 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009812 SmallVector<Decl *, 4> ExprCaptures;
9813 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +00009814 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009815 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009816 SourceLocation ELoc;
9817 SourceRange ERange;
9818 Expr *SimpleRefExpr = RefExpr;
9819 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009820 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009821 // It will be analyzed later.
9822 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009823 SrcExprs.push_back(nullptr);
9824 DstExprs.push_back(nullptr);
9825 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009826 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009827 ValueDecl *D = Res.first;
9828 if (!D)
9829 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009830
Alexey Bataev74caaf22016-02-20 04:09:36 +00009831 QualType Type = D->getType();
9832 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009833
9834 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9835 // A variable that appears in a lastprivate clause must not have an
9836 // incomplete type or a reference type.
9837 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009838 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009839 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009840 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009841
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009842 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009843 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9844 // in a Construct]
9845 // Variables with the predetermined data-sharing attributes may not be
9846 // listed in data-sharing attributes clauses, except for the cases
9847 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009848 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9849 // A list item may appear in a firstprivate or lastprivate clause but not
9850 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +00009851 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009852 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009853 (isOpenMPDistributeDirective(CurrDir) ||
9854 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009855 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9856 Diag(ELoc, diag::err_omp_wrong_dsa)
9857 << getOpenMPClauseName(DVar.CKind)
9858 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009859 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009860 continue;
9861 }
9862
Alexey Bataevf29276e2014-06-18 04:14:57 +00009863 // OpenMP [2.14.3.5, Restrictions, p.2]
9864 // A list item that is private within a parallel region, or that appears in
9865 // the reduction clause of a parallel construct, must not appear in a
9866 // lastprivate clause on a worksharing construct if any of the corresponding
9867 // worksharing regions ever binds to any of the corresponding parallel
9868 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00009869 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00009870 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009871 !isOpenMPParallelDirective(CurrDir) &&
9872 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00009873 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009874 if (DVar.CKind != OMPC_shared) {
9875 Diag(ELoc, diag::err_omp_required_access)
9876 << getOpenMPClauseName(OMPC_lastprivate)
9877 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009878 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009879 continue;
9880 }
9881 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009882
Alexander Musman1bb328c2014-06-04 13:06:39 +00009883 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00009884 // A variable of class type (or array thereof) that appears in a
9885 // lastprivate clause requires an accessible, unambiguous default
9886 // constructor for the class type, unless the list item is also specified
9887 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00009888 // A variable of class type (or array thereof) that appears in a
9889 // lastprivate clause requires an accessible, unambiguous copy assignment
9890 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00009891 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009892 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
9893 Type.getUnqualifiedType(), ".lastprivate.src",
9894 D->hasAttrs() ? &D->getAttrs() : nullptr);
9895 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +00009896 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009897 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009898 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00009899 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +00009900 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00009901 // For arrays generate assignment operation for single element and replace
9902 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00009903 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
9904 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009905 if (AssignmentOp.isInvalid())
9906 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00009907 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00009908 /*DiscardedValue=*/true);
9909 if (AssignmentOp.isInvalid())
9910 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009911
Alexey Bataev74caaf22016-02-20 04:09:36 +00009912 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009913 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009914 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009915 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009916 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009917 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00009918 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009919 ExprCaptures.push_back(Ref->getDecl());
9920 }
9921 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +00009922 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009923 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009924 ExprResult RefRes = DefaultLvalueConversion(Ref);
9925 if (!RefRes.isUsable())
9926 continue;
9927 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00009928 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9929 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009930 if (!PostUpdateRes.isUsable())
9931 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009932 ExprPostUpdates.push_back(
9933 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00009934 }
9935 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009936 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009937 Vars.push_back((VD || CurContext->isDependentContext())
9938 ? RefExpr->IgnoreParens()
9939 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00009940 SrcExprs.push_back(PseudoSrcExpr);
9941 DstExprs.push_back(PseudoDstExpr);
9942 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00009943 }
9944
9945 if (Vars.empty())
9946 return nullptr;
9947
9948 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00009949 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009950 buildPreInits(Context, ExprCaptures),
9951 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00009952}
9953
Alexey Bataev758e55e2013-09-06 18:03:48 +00009954OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9955 SourceLocation StartLoc,
9956 SourceLocation LParenLoc,
9957 SourceLocation EndLoc) {
9958 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00009959 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009960 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009961 SourceLocation ELoc;
9962 SourceRange ERange;
9963 Expr *SimpleRefExpr = RefExpr;
9964 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009965 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00009966 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009967 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009968 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009969 ValueDecl *D = Res.first;
9970 if (!D)
9971 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009972
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009973 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009974 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9975 // in a Construct]
9976 // Variables with the predetermined data-sharing attributes may not be
9977 // listed in data-sharing attributes clauses, except for the cases
9978 // listed below. For these exceptions only, listing a predetermined
9979 // variable in a data-sharing attribute clause is allowed and overrides
9980 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009981 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +00009982 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9983 DVar.RefExpr) {
9984 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9985 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009986 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009987 continue;
9988 }
9989
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009990 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009991 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009992 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00009993 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009994 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9995 ? RefExpr->IgnoreParens()
9996 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009997 }
9998
Alexey Bataeved09d242014-05-28 05:53:51 +00009999 if (Vars.empty())
10000 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010001
10002 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10003}
10004
Alexey Bataevc5e02582014-06-16 07:08:35 +000010005namespace {
10006class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10007 DSAStackTy *Stack;
10008
10009public:
10010 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010011 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10012 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010013 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10014 return false;
10015 if (DVar.CKind != OMPC_unknown)
10016 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010017 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010018 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010019 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010020 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010021 }
10022 return false;
10023 }
10024 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010025 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010026 if (Child && Visit(Child))
10027 return true;
10028 }
10029 return false;
10030 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010031 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010032};
Alexey Bataev23b69422014-06-18 07:08:49 +000010033} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010034
Alexey Bataev60da77e2016-02-29 05:54:20 +000010035namespace {
10036// Transform MemberExpression for specified FieldDecl of current class to
10037// DeclRefExpr to specified OMPCapturedExprDecl.
10038class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10039 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010040 ValueDecl *Field = nullptr;
10041 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010042
10043public:
10044 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10045 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10046
10047 ExprResult TransformMemberExpr(MemberExpr *E) {
10048 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10049 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010050 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010051 return CapturedExpr;
10052 }
10053 return BaseTransform::TransformMemberExpr(E);
10054 }
10055 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10056};
10057} // namespace
10058
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010059template <typename T, typename U>
10060static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10061 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010062 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010063 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010064 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010065 return Res;
10066 }
10067 }
10068 return T();
10069}
10070
10071static ExprResult
10072buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10073 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10074 const DeclarationNameInfo &ReductionId, QualType Ty,
10075 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10076 if (ReductionIdScopeSpec.isInvalid())
10077 return ExprError();
10078 SmallVector<UnresolvedSet<8>, 4> Lookups;
10079 if (S) {
10080 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10081 Lookup.suppressDiagnostics();
10082 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010083 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010084 do {
10085 S = S->getParent();
10086 } while (S && !S->isDeclScope(D));
10087 if (S)
10088 S = S->getParent();
10089 Lookups.push_back(UnresolvedSet<8>());
10090 Lookups.back().append(Lookup.begin(), Lookup.end());
10091 Lookup.clear();
10092 }
10093 } else if (auto *ULE =
10094 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10095 Lookups.push_back(UnresolvedSet<8>());
10096 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010097 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010098 if (D == PrevD)
10099 Lookups.push_back(UnresolvedSet<8>());
10100 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10101 Lookups.back().addDecl(DRD);
10102 PrevD = D;
10103 }
10104 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010105 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10106 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010107 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010108 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010109 return !D->isInvalidDecl() &&
10110 (D->getType()->isDependentType() ||
10111 D->getType()->isInstantiationDependentType() ||
10112 D->getType()->containsUnexpandedParameterPack());
10113 })) {
10114 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010115 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010116 ResSet.append(Set.begin(), Set.end());
10117 // The last item marks the end of all declarations at the specified scope.
10118 ResSet.addDecl(Set[Set.size() - 1]);
10119 }
10120 return UnresolvedLookupExpr::Create(
10121 SemaRef.Context, /*NamingClass=*/nullptr,
10122 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10123 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10124 }
10125 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10126 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10127 if (!D->isInvalidDecl() &&
10128 SemaRef.Context.hasSameType(D->getType(), Ty))
10129 return D;
10130 return nullptr;
10131 }))
10132 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10133 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10134 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10135 if (!D->isInvalidDecl() &&
10136 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10137 !Ty.isMoreQualifiedThan(D->getType()))
10138 return D;
10139 return nullptr;
10140 })) {
10141 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10142 /*DetectVirtual=*/false);
10143 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10144 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10145 VD->getType().getUnqualifiedType()))) {
10146 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10147 /*DiagID=*/0) !=
10148 Sema::AR_inaccessible) {
10149 SemaRef.BuildBasePathArray(Paths, BasePath);
10150 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10151 }
10152 }
10153 }
10154 }
10155 if (ReductionIdScopeSpec.isSet()) {
10156 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10157 return ExprError();
10158 }
10159 return ExprEmpty();
10160}
10161
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010162namespace {
10163/// Data for the reduction-based clauses.
10164struct ReductionData {
10165 /// List of original reduction items.
10166 SmallVector<Expr *, 8> Vars;
10167 /// List of private copies of the reduction items.
10168 SmallVector<Expr *, 8> Privates;
10169 /// LHS expressions for the reduction_op expressions.
10170 SmallVector<Expr *, 8> LHSs;
10171 /// RHS expressions for the reduction_op expressions.
10172 SmallVector<Expr *, 8> RHSs;
10173 /// Reduction operation expression.
10174 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010175 /// Taskgroup descriptors for the corresponding reduction items in
10176 /// in_reduction clauses.
10177 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010178 /// List of captures for clause.
10179 SmallVector<Decl *, 4> ExprCaptures;
10180 /// List of postupdate expressions.
10181 SmallVector<Expr *, 4> ExprPostUpdates;
10182 ReductionData() = delete;
10183 /// Reserves required memory for the reduction data.
10184 ReductionData(unsigned Size) {
10185 Vars.reserve(Size);
10186 Privates.reserve(Size);
10187 LHSs.reserve(Size);
10188 RHSs.reserve(Size);
10189 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010190 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010191 ExprCaptures.reserve(Size);
10192 ExprPostUpdates.reserve(Size);
10193 }
10194 /// Stores reduction item and reduction operation only (required for dependent
10195 /// reduction item).
10196 void push(Expr *Item, Expr *ReductionOp) {
10197 Vars.emplace_back(Item);
10198 Privates.emplace_back(nullptr);
10199 LHSs.emplace_back(nullptr);
10200 RHSs.emplace_back(nullptr);
10201 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010202 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010203 }
10204 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010205 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10206 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010207 Vars.emplace_back(Item);
10208 Privates.emplace_back(Private);
10209 LHSs.emplace_back(LHS);
10210 RHSs.emplace_back(RHS);
10211 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010212 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010213 }
10214};
10215} // namespace
10216
Alexey Bataeve3727102018-04-18 15:57:46 +000010217static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010218 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10219 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10220 const Expr *Length = OASE->getLength();
10221 if (Length == nullptr) {
10222 // For array sections of the form [1:] or [:], we would need to analyze
10223 // the lower bound...
10224 if (OASE->getColonLoc().isValid())
10225 return false;
10226
10227 // This is an array subscript which has implicit length 1!
10228 SingleElement = true;
10229 ArraySizes.push_back(llvm::APSInt::get(1));
10230 } else {
10231 llvm::APSInt ConstantLengthValue;
10232 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10233 return false;
10234
10235 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10236 ArraySizes.push_back(ConstantLengthValue);
10237 }
10238
10239 // Get the base of this array section and walk up from there.
10240 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10241
10242 // We require length = 1 for all array sections except the right-most to
10243 // guarantee that the memory region is contiguous and has no holes in it.
10244 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10245 Length = TempOASE->getLength();
10246 if (Length == nullptr) {
10247 // For array sections of the form [1:] or [:], we would need to analyze
10248 // the lower bound...
10249 if (OASE->getColonLoc().isValid())
10250 return false;
10251
10252 // This is an array subscript which has implicit length 1!
10253 ArraySizes.push_back(llvm::APSInt::get(1));
10254 } else {
10255 llvm::APSInt ConstantLengthValue;
10256 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10257 ConstantLengthValue.getSExtValue() != 1)
10258 return false;
10259
10260 ArraySizes.push_back(ConstantLengthValue);
10261 }
10262 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10263 }
10264
10265 // If we have a single element, we don't need to add the implicit lengths.
10266 if (!SingleElement) {
10267 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10268 // Has implicit length 1!
10269 ArraySizes.push_back(llvm::APSInt::get(1));
10270 Base = TempASE->getBase()->IgnoreParenImpCasts();
10271 }
10272 }
10273
10274 // This array section can be privatized as a single value or as a constant
10275 // sized array.
10276 return true;
10277}
10278
Alexey Bataeve3727102018-04-18 15:57:46 +000010279static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010280 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10281 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10282 SourceLocation ColonLoc, SourceLocation EndLoc,
10283 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010284 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010285 DeclarationName DN = ReductionId.getName();
10286 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010287 BinaryOperatorKind BOK = BO_Comma;
10288
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010289 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010290 // OpenMP [2.14.3.6, reduction clause]
10291 // C
10292 // reduction-identifier is either an identifier or one of the following
10293 // operators: +, -, *, &, |, ^, && and ||
10294 // C++
10295 // reduction-identifier is either an id-expression or one of the following
10296 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010297 switch (OOK) {
10298 case OO_Plus:
10299 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010300 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010301 break;
10302 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010303 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010304 break;
10305 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010306 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010307 break;
10308 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010309 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010310 break;
10311 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010312 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010313 break;
10314 case OO_AmpAmp:
10315 BOK = BO_LAnd;
10316 break;
10317 case OO_PipePipe:
10318 BOK = BO_LOr;
10319 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010320 case OO_New:
10321 case OO_Delete:
10322 case OO_Array_New:
10323 case OO_Array_Delete:
10324 case OO_Slash:
10325 case OO_Percent:
10326 case OO_Tilde:
10327 case OO_Exclaim:
10328 case OO_Equal:
10329 case OO_Less:
10330 case OO_Greater:
10331 case OO_LessEqual:
10332 case OO_GreaterEqual:
10333 case OO_PlusEqual:
10334 case OO_MinusEqual:
10335 case OO_StarEqual:
10336 case OO_SlashEqual:
10337 case OO_PercentEqual:
10338 case OO_CaretEqual:
10339 case OO_AmpEqual:
10340 case OO_PipeEqual:
10341 case OO_LessLess:
10342 case OO_GreaterGreater:
10343 case OO_LessLessEqual:
10344 case OO_GreaterGreaterEqual:
10345 case OO_EqualEqual:
10346 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010347 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010348 case OO_PlusPlus:
10349 case OO_MinusMinus:
10350 case OO_Comma:
10351 case OO_ArrowStar:
10352 case OO_Arrow:
10353 case OO_Call:
10354 case OO_Subscript:
10355 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010356 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010357 case NUM_OVERLOADED_OPERATORS:
10358 llvm_unreachable("Unexpected reduction identifier");
10359 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010360 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010361 if (II->isStr("max"))
10362 BOK = BO_GT;
10363 else if (II->isStr("min"))
10364 BOK = BO_LT;
10365 }
10366 break;
10367 }
10368 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010369 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010370 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010371 else
10372 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010373 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010374
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010375 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10376 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010377 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010378 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010379 // OpenMP [2.1, C/C++]
10380 // A list item is a variable or array section, subject to the restrictions
10381 // specified in Section 2.4 on page 42 and in each of the sections
10382 // describing clauses and directives for which a list appears.
10383 // OpenMP [2.14.3.3, Restrictions, p.1]
10384 // A variable that is part of another variable (as an array or
10385 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010386 if (!FirstIter && IR != ER)
10387 ++IR;
10388 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010389 SourceLocation ELoc;
10390 SourceRange ERange;
10391 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010392 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010393 /*AllowArraySection=*/true);
10394 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010395 // Try to find 'declare reduction' corresponding construct before using
10396 // builtin/overloaded operators.
10397 QualType Type = Context.DependentTy;
10398 CXXCastPath BasePath;
10399 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010400 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010401 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010402 Expr *ReductionOp = nullptr;
10403 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010404 (DeclareReductionRef.isUnset() ||
10405 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010406 ReductionOp = DeclareReductionRef.get();
10407 // It will be analyzed later.
10408 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010409 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010410 ValueDecl *D = Res.first;
10411 if (!D)
10412 continue;
10413
Alexey Bataev88202be2017-07-27 13:20:36 +000010414 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010415 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010416 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10417 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010418 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010419 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010420 } else if (OASE) {
10421 QualType BaseType =
10422 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10423 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010424 Type = ATy->getElementType();
10425 else
10426 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010427 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010428 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010429 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010430 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010431 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010432
Alexey Bataevc5e02582014-06-16 07:08:35 +000010433 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10434 // A variable that appears in a private clause must not have an incomplete
10435 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010436 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010437 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010438 continue;
10439 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010440 // A list item that appears in a reduction clause must not be
10441 // const-qualified.
10442 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010443 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010444 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010445 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10446 VarDecl::DeclarationOnly;
10447 S.Diag(D->getLocation(),
10448 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010449 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010450 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010451 continue;
10452 }
10453 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10454 // If a list-item is a reference type then it must bind to the same object
10455 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +000010456 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +000010457 VarDecl *VDDef = VD->getDefinition();
David Sheinkman92589992016-10-04 14:41:36 +000010458 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010459 DSARefChecker Check(Stack);
Alexey Bataeva1764212015-09-30 09:22:36 +000010460 if (Check.Visit(VDDef->getInit())) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010461 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10462 << getOpenMPClauseName(ClauseKind) << ERange;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010463 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
Alexey Bataeva1764212015-09-30 09:22:36 +000010464 continue;
10465 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010466 }
10467 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010468
Alexey Bataevc5e02582014-06-16 07:08:35 +000010469 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10470 // in a Construct]
10471 // Variables with the predetermined data-sharing attributes may not be
10472 // listed in data-sharing attributes clauses, except for the cases
10473 // listed below. For these exceptions only, listing a predetermined
10474 // variable in a data-sharing attribute clause is allowed and overrides
10475 // the variable's predetermined data-sharing attributes.
10476 // OpenMP [2.14.3.6, Restrictions, p.3]
10477 // Any number of reduction clauses can be specified on the directive,
10478 // but a list item can appear only once in the reduction clauses for that
10479 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +000010480 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010481 if (DVar.CKind == OMPC_reduction) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010482 S.Diag(ELoc, diag::err_omp_once_referenced)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010483 << getOpenMPClauseName(ClauseKind);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010484 if (DVar.RefExpr)
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010485 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010486 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000010487 }
10488 if (DVar.CKind != OMPC_unknown) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010489 S.Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010490 << getOpenMPClauseName(DVar.CKind)
10491 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010492 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010493 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010494 }
10495
10496 // OpenMP [2.14.3.6, Restrictions, p.1]
10497 // A list item that appears in a reduction clause of a worksharing
10498 // construct must be shared in the parallel regions to which any of the
10499 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010500 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010501 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010502 !isOpenMPParallelDirective(CurrDir) &&
10503 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010504 DVar = Stack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010505 if (DVar.CKind != OMPC_shared) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010506 S.Diag(ELoc, diag::err_omp_required_access)
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010507 << getOpenMPClauseName(OMPC_reduction)
10508 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010509 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010510 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010511 }
10512 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010513
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010514 // Try to find 'declare reduction' corresponding construct before using
10515 // builtin/overloaded operators.
10516 CXXCastPath BasePath;
10517 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010518 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010519 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10520 if (DeclareReductionRef.isInvalid())
10521 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010522 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010523 (DeclareReductionRef.isUnset() ||
10524 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010525 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010526 continue;
10527 }
10528 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10529 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010530 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010531 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010532 << Type << ReductionIdRange;
10533 continue;
10534 }
10535
10536 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10537 // The type of a list item that appears in a reduction clause must be valid
10538 // for the reduction-identifier. For a max or min reduction in C, the type
10539 // of the list item must be an allowed arithmetic data type: char, int,
10540 // float, double, or _Bool, possibly modified with long, short, signed, or
10541 // unsigned. For a max or min reduction in C++, the type of the list item
10542 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10543 // double, or bool, possibly modified with long, short, signed, or unsigned.
10544 if (DeclareReductionRef.isUnset()) {
10545 if ((BOK == BO_GT || BOK == BO_LT) &&
10546 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010547 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10548 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010549 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010550 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010551 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10552 VarDecl::DeclarationOnly;
10553 S.Diag(D->getLocation(),
10554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010555 << D;
10556 }
10557 continue;
10558 }
10559 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010560 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010561 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10562 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010563 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010564 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10565 VarDecl::DeclarationOnly;
10566 S.Diag(D->getLocation(),
10567 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010568 << D;
10569 }
10570 continue;
10571 }
10572 }
10573
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010574 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010575 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10576 D->hasAttrs() ? &D->getAttrs() : nullptr);
10577 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10578 D->hasAttrs() ? &D->getAttrs() : nullptr);
10579 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010580
10581 // Try if we can determine constant lengths for all array sections and avoid
10582 // the VLA.
10583 bool ConstantLengthOASE = false;
10584 if (OASE) {
10585 bool SingleElement;
10586 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010587 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010588 Context, OASE, SingleElement, ArraySizes);
10589
10590 // If we don't have a single element, we must emit a constant array type.
10591 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010592 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010593 PrivateTy = Context.getConstantArrayType(
10594 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010595 }
10596 }
10597
10598 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010599 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010600 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010601 if (!Context.getTargetInfo().isVLASupported() &&
10602 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10603 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10604 S.Diag(ELoc, diag::note_vla_unsupported);
10605 continue;
10606 }
David Majnemer9d168222016-08-05 17:44:54 +000010607 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010608 // Create pseudo array type for private copy. The size for this array will
10609 // be generated during codegen.
10610 // For array subscripts or single variables Private Ty is the same as Type
10611 // (type of the variable or single array element).
10612 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010613 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010614 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010615 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010616 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000010617 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010618 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010619 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010620 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000010621 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010622 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10623 D->hasAttrs() ? &D->getAttrs() : nullptr,
10624 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010625 // Add initializer for private variable.
10626 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010627 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10628 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010629 if (DeclareReductionRef.isUsable()) {
10630 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10631 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10632 if (DRD->getInitializer()) {
10633 Init = DRDRef;
10634 RHSVD->setInit(DRDRef);
10635 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010636 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010637 } else {
10638 switch (BOK) {
10639 case BO_Add:
10640 case BO_Xor:
10641 case BO_Or:
10642 case BO_LOr:
10643 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10644 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010645 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010646 break;
10647 case BO_Mul:
10648 case BO_LAnd:
10649 if (Type->isScalarType() || Type->isAnyComplexType()) {
10650 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010651 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010652 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010653 break;
10654 case BO_And: {
10655 // '&' reduction op - initializer is '~0'.
10656 QualType OrigType = Type;
10657 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10658 Type = ComplexTy->getElementType();
10659 if (Type->isRealFloatingType()) {
10660 llvm::APFloat InitValue =
10661 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10662 /*isIEEE=*/true);
10663 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10664 Type, ELoc);
10665 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010666 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010667 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10668 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10669 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10670 }
10671 if (Init && OrigType->isAnyComplexType()) {
10672 // Init = 0xFFFF + 0xFFFFi;
10673 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010674 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010675 }
10676 Type = OrigType;
10677 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010678 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010679 case BO_LT:
10680 case BO_GT: {
10681 // 'min' reduction op - initializer is 'Largest representable number in
10682 // the reduction list item type'.
10683 // 'max' reduction op - initializer is 'Least representable number in
10684 // the reduction list item type'.
10685 if (Type->isIntegerType() || Type->isPointerType()) {
10686 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000010687 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010688 QualType IntTy =
10689 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10690 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010691 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10692 : llvm::APInt::getMinValue(Size)
10693 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10694 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010695 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10696 if (Type->isPointerType()) {
10697 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010698 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010699 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010700 if (CastExpr.isInvalid())
10701 continue;
10702 Init = CastExpr.get();
10703 }
10704 } else if (Type->isRealFloatingType()) {
10705 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10706 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10707 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10708 Type, ELoc);
10709 }
10710 break;
10711 }
10712 case BO_PtrMemD:
10713 case BO_PtrMemI:
10714 case BO_MulAssign:
10715 case BO_Div:
10716 case BO_Rem:
10717 case BO_Sub:
10718 case BO_Shl:
10719 case BO_Shr:
10720 case BO_LE:
10721 case BO_GE:
10722 case BO_EQ:
10723 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010724 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010725 case BO_AndAssign:
10726 case BO_XorAssign:
10727 case BO_OrAssign:
10728 case BO_Assign:
10729 case BO_AddAssign:
10730 case BO_SubAssign:
10731 case BO_DivAssign:
10732 case BO_RemAssign:
10733 case BO_ShlAssign:
10734 case BO_ShrAssign:
10735 case BO_Comma:
10736 llvm_unreachable("Unexpected reduction operation");
10737 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010738 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010739 if (Init && DeclareReductionRef.isUnset())
10740 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10741 else if (!Init)
10742 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010743 if (RHSVD->isInvalidDecl())
10744 continue;
10745 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010746 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10747 << Type << ReductionIdRange;
10748 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10749 VarDecl::DeclarationOnly;
10750 S.Diag(D->getLocation(),
10751 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010752 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010753 continue;
10754 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010755 // Store initializer for single element in private copy. Will be used during
10756 // codegen.
10757 PrivateVD->setInit(RHSVD->getInit());
10758 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000010759 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010760 ExprResult ReductionOp;
10761 if (DeclareReductionRef.isUsable()) {
10762 QualType RedTy = DeclareReductionRef.get()->getType();
10763 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010764 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10765 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010766 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010767 LHS = S.DefaultLvalueConversion(LHS.get());
10768 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010769 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10770 CK_UncheckedDerivedToBase, LHS.get(),
10771 &BasePath, LHS.get()->getValueKind());
10772 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10773 CK_UncheckedDerivedToBase, RHS.get(),
10774 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010775 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010776 FunctionProtoType::ExtProtoInfo EPI;
10777 QualType Params[] = {PtrRedTy, PtrRedTy};
10778 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10779 auto *OVE = new (Context) OpaqueValueExpr(
10780 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010781 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010782 Expr *Args[] = {LHS.get(), RHS.get()};
10783 ReductionOp = new (Context)
10784 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10785 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010786 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010787 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010788 if (ReductionOp.isUsable()) {
10789 if (BOK != BO_LT && BOK != BO_GT) {
10790 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010791 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010792 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010793 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000010794 auto *ConditionalOp = new (Context)
10795 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10796 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010797 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010798 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010799 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010800 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010801 if (ReductionOp.isUsable())
10802 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010803 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010804 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010805 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010806 }
10807
Alexey Bataevfa312f32017-07-21 18:48:21 +000010808 // OpenMP [2.15.4.6, Restrictions, p.2]
10809 // A list item that appears in an in_reduction clause of a task construct
10810 // must appear in a task_reduction clause of a construct associated with a
10811 // taskgroup region that includes the participating task in its taskgroup
10812 // set. The construct associated with the innermost region that meets this
10813 // condition must specify the same reduction-identifier as the in_reduction
10814 // clause.
10815 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000010816 SourceRange ParentSR;
10817 BinaryOperatorKind ParentBOK;
10818 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000010819 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000010820 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010821 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10822 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010823 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000010824 Stack->getTopMostTaskgroupReductionData(
10825 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010826 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10827 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10828 if (!IsParentBOK && !IsParentReductionOp) {
10829 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10830 continue;
10831 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000010832 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10833 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10834 IsParentReductionOp) {
10835 bool EmitError = true;
10836 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10837 llvm::FoldingSetNodeID RedId, ParentRedId;
10838 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10839 DeclareReductionRef.get()->Profile(RedId, Context,
10840 /*Canonical=*/true);
10841 EmitError = RedId != ParentRedId;
10842 }
10843 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010844 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000010845 diag::err_omp_reduction_identifier_mismatch)
10846 << ReductionIdRange << RefExpr->getSourceRange();
10847 S.Diag(ParentSR.getBegin(),
10848 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000010849 << ParentSR
10850 << (IsParentBOK ? ParentBOKDSA.RefExpr
10851 : ParentReductionOpDSA.RefExpr)
10852 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000010853 continue;
10854 }
10855 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010856 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10857 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000010858 }
10859
Alexey Bataev60da77e2016-02-29 05:54:20 +000010860 DeclRefExpr *Ref = nullptr;
10861 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010862 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010863 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010864 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010865 VarsExpr =
10866 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10867 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000010868 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010869 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010870 }
Alexey Bataeve3727102018-04-18 15:57:46 +000010871 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010872 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010873 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010874 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000010875 if (!RefRes.isUsable())
10876 continue;
10877 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010878 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10879 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000010880 if (!PostUpdateRes.isUsable())
10881 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010882 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10883 Stack->getCurrentDirective() == OMPD_taskgroup) {
10884 S.Diag(RefExpr->getExprLoc(),
10885 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000010886 << RefExpr->getSourceRange();
10887 continue;
10888 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010889 RD.ExprPostUpdates.emplace_back(
10890 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000010891 }
10892 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010893 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000010894 // All reduction items are still marked as reduction (to do not increase
10895 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010896 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010897 if (CurrDir == OMPD_taskgroup) {
10898 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010899 Stack->addTaskgroupReductionData(D, ReductionIdRange,
10900 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000010901 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000010902 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000010903 }
Alexey Bataev88202be2017-07-27 13:20:36 +000010904 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10905 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010906 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010907 return RD.Vars.empty();
10908}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010909
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010910OMPClause *Sema::ActOnOpenMPReductionClause(
10911 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10912 SourceLocation ColonLoc, SourceLocation EndLoc,
10913 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10914 ArrayRef<Expr *> UnresolvedReductions) {
10915 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010916 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000010917 StartLoc, LParenLoc, ColonLoc, EndLoc,
10918 ReductionIdScopeSpec, ReductionId,
10919 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010920 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000010921
Alexey Bataevc5e02582014-06-16 07:08:35 +000010922 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010923 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10924 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10925 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10926 buildPreInits(Context, RD.ExprCaptures),
10927 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000010928}
10929
Alexey Bataev169d96a2017-07-18 20:17:46 +000010930OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10931 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10932 SourceLocation ColonLoc, SourceLocation EndLoc,
10933 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10934 ArrayRef<Expr *> UnresolvedReductions) {
10935 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010936 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
10937 StartLoc, LParenLoc, ColonLoc, EndLoc,
10938 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000010939 UnresolvedReductions, RD))
10940 return nullptr;
10941
10942 return OMPTaskReductionClause::Create(
10943 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10944 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10945 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10946 buildPreInits(Context, RD.ExprCaptures),
10947 buildPostUpdate(*this, RD.ExprPostUpdates));
10948}
10949
Alexey Bataevfa312f32017-07-21 18:48:21 +000010950OMPClause *Sema::ActOnOpenMPInReductionClause(
10951 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10952 SourceLocation ColonLoc, SourceLocation EndLoc,
10953 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10954 ArrayRef<Expr *> UnresolvedReductions) {
10955 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000010956 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010957 StartLoc, LParenLoc, ColonLoc, EndLoc,
10958 ReductionIdScopeSpec, ReductionId,
10959 UnresolvedReductions, RD))
10960 return nullptr;
10961
10962 return OMPInReductionClause::Create(
10963 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10964 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000010965 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000010966 buildPreInits(Context, RD.ExprCaptures),
10967 buildPostUpdate(*this, RD.ExprPostUpdates));
10968}
10969
Alexey Bataevecba70f2016-04-12 11:02:11 +000010970bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10971 SourceLocation LinLoc) {
10972 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10973 LinKind == OMPC_LINEAR_unknown) {
10974 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10975 return true;
10976 }
10977 return false;
10978}
10979
Alexey Bataeve3727102018-04-18 15:57:46 +000010980bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000010981 OpenMPLinearClauseKind LinKind,
10982 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010983 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000010984 // A variable must not have an incomplete type or a reference type.
10985 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10986 return true;
10987 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10988 !Type->isReferenceType()) {
10989 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10990 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10991 return true;
10992 }
10993 Type = Type.getNonReferenceType();
10994
10995 // A list item must not be const-qualified.
10996 if (Type.isConstant(Context)) {
10997 Diag(ELoc, diag::err_omp_const_variable)
10998 << getOpenMPClauseName(OMPC_linear);
10999 if (D) {
11000 bool IsDecl =
11001 !VD ||
11002 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11003 Diag(D->getLocation(),
11004 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11005 << D;
11006 }
11007 return true;
11008 }
11009
11010 // A list item must be of integral or pointer type.
11011 Type = Type.getUnqualifiedType().getCanonicalType();
11012 const auto *Ty = Type.getTypePtrOrNull();
11013 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11014 !Ty->isPointerType())) {
11015 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11016 if (D) {
11017 bool IsDecl =
11018 !VD ||
11019 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11020 Diag(D->getLocation(),
11021 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11022 << D;
11023 }
11024 return true;
11025 }
11026 return false;
11027}
11028
Alexey Bataev182227b2015-08-20 10:54:39 +000011029OMPClause *Sema::ActOnOpenMPLinearClause(
11030 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11031 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11032 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011033 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011034 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011035 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011036 SmallVector<Decl *, 4> ExprCaptures;
11037 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011038 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011039 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011040 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011041 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011042 SourceLocation ELoc;
11043 SourceRange ERange;
11044 Expr *SimpleRefExpr = RefExpr;
11045 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11046 /*AllowArraySection=*/false);
11047 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011048 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011049 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011050 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011051 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011052 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011053 ValueDecl *D = Res.first;
11054 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011055 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011056
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011057 QualType Type = D->getType();
11058 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011059
11060 // OpenMP [2.14.3.7, linear clause]
11061 // A list-item cannot appear in more than one linear clause.
11062 // A list-item that appears in a linear clause cannot appear in any
11063 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011064 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011065 if (DVar.RefExpr) {
11066 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11067 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011068 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011069 continue;
11070 }
11071
Alexey Bataevecba70f2016-04-12 11:02:11 +000011072 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011073 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011074 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011075
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011076 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011077 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011078 buildVarDecl(*this, ELoc, Type, D->getName(),
11079 D->hasAttrs() ? &D->getAttrs() : nullptr,
11080 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011081 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011082 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011083 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011084 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011085 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011086 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011087 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011088 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011089 ExprCaptures.push_back(Ref->getDecl());
11090 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11091 ExprResult RefRes = DefaultLvalueConversion(Ref);
11092 if (!RefRes.isUsable())
11093 continue;
11094 ExprResult PostUpdateRes =
11095 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11096 SimpleRefExpr, RefRes.get());
11097 if (!PostUpdateRes.isUsable())
11098 continue;
11099 ExprPostUpdates.push_back(
11100 IgnoredValueConversions(PostUpdateRes.get()).get());
11101 }
11102 }
11103 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011104 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011105 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011106 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011107 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011108 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011109 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011110 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011111
11112 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011113 Vars.push_back((VD || CurContext->isDependentContext())
11114 ? RefExpr->IgnoreParens()
11115 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011116 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011117 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011118 }
11119
11120 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011121 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011122
11123 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011124 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011125 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11126 !Step->isInstantiationDependent() &&
11127 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011128 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011129 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011130 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011131 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011132 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011133
Alexander Musman3276a272015-03-21 10:12:56 +000011134 // Build var to save the step value.
11135 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011136 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011137 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011138 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011139 ExprResult CalcStep =
11140 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011141 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011142
Alexander Musman8dba6642014-04-22 13:09:42 +000011143 // Warn about zero linear step (it would be probably better specified as
11144 // making corresponding variables 'const').
11145 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011146 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11147 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011148 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11149 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011150 if (!IsConstant && CalcStep.isUsable()) {
11151 // Calculate the step beforehand instead of doing this on each iteration.
11152 // (This is not used if the number of iterations may be kfold-ed).
11153 CalcStepExpr = CalcStep.get();
11154 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011155 }
11156
Alexey Bataev182227b2015-08-20 10:54:39 +000011157 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11158 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011159 StepExpr, CalcStepExpr,
11160 buildPreInits(Context, ExprCaptures),
11161 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011162}
11163
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011164static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11165 Expr *NumIterations, Sema &SemaRef,
11166 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011167 // Walk the vars and build update/final expressions for the CodeGen.
11168 SmallVector<Expr *, 8> Updates;
11169 SmallVector<Expr *, 8> Finals;
11170 Expr *Step = Clause.getStep();
11171 Expr *CalcStep = Clause.getCalcStep();
11172 // OpenMP [2.14.3.7, linear clause]
11173 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011174 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011175 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011176 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011177 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11178 bool HasErrors = false;
11179 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011180 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011181 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11182 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011183 SourceLocation ELoc;
11184 SourceRange ERange;
11185 Expr *SimpleRefExpr = RefExpr;
11186 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
11187 /*AllowArraySection=*/false);
11188 ValueDecl *D = Res.first;
11189 if (Res.second || !D) {
11190 Updates.push_back(nullptr);
11191 Finals.push_back(nullptr);
11192 HasErrors = true;
11193 continue;
11194 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011195 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011196 // OpenMP [2.15.11, distribute simd Construct]
11197 // A list item may not appear in a linear clause, unless it is the loop
11198 // iteration variable.
11199 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11200 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11201 SemaRef.Diag(ELoc,
11202 diag::err_omp_linear_distribute_var_non_loop_iteration);
11203 Updates.push_back(nullptr);
11204 Finals.push_back(nullptr);
11205 HasErrors = true;
11206 continue;
11207 }
Alexander Musman3276a272015-03-21 10:12:56 +000011208 Expr *InitExpr = *CurInit;
11209
11210 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011211 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011212 Expr *CapturedRef;
11213 if (LinKind == OMPC_LINEAR_uval)
11214 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11215 else
11216 CapturedRef =
11217 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11218 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11219 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011220
11221 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011222 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011223 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011224 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011225 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011226 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011227 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011228 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011229 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011230 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011231
11232 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011233 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011234 if (!Info.first)
11235 Final =
11236 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11237 InitExpr, NumIterations, Step, /*Subtract=*/false);
11238 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011239 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011240 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011241 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011242
Alexander Musman3276a272015-03-21 10:12:56 +000011243 if (!Update.isUsable() || !Final.isUsable()) {
11244 Updates.push_back(nullptr);
11245 Finals.push_back(nullptr);
11246 HasErrors = true;
11247 } else {
11248 Updates.push_back(Update.get());
11249 Finals.push_back(Final.get());
11250 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011251 ++CurInit;
11252 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011253 }
11254 Clause.setUpdates(Updates);
11255 Clause.setFinals(Finals);
11256 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011257}
11258
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011259OMPClause *Sema::ActOnOpenMPAlignedClause(
11260 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11261 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011262 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011263 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011264 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11265 SourceLocation ELoc;
11266 SourceRange ERange;
11267 Expr *SimpleRefExpr = RefExpr;
11268 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11269 /*AllowArraySection=*/false);
11270 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011271 // It will be analyzed later.
11272 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011273 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011274 ValueDecl *D = Res.first;
11275 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011276 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011277
Alexey Bataev1efd1662016-03-29 10:59:56 +000011278 QualType QType = D->getType();
11279 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011280
11281 // OpenMP [2.8.1, simd construct, Restrictions]
11282 // The type of list items appearing in the aligned clause must be
11283 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011284 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011285 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011286 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011287 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011288 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011289 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011290 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011291 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011292 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011293 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011294 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011295 continue;
11296 }
11297
11298 // OpenMP [2.8.1, simd construct, Restrictions]
11299 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011300 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011301 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011302 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11303 << getOpenMPClauseName(OMPC_aligned);
11304 continue;
11305 }
11306
Alexey Bataev1efd1662016-03-29 10:59:56 +000011307 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011308 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011309 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11310 Vars.push_back(DefaultFunctionArrayConversion(
11311 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11312 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011313 }
11314
11315 // OpenMP [2.8.1, simd construct, Description]
11316 // The parameter of the aligned clause, alignment, must be a constant
11317 // positive integer expression.
11318 // If no optional parameter is specified, implementation-defined default
11319 // alignments for SIMD instructions on the target platforms are assumed.
11320 if (Alignment != nullptr) {
11321 ExprResult AlignResult =
11322 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11323 if (AlignResult.isInvalid())
11324 return nullptr;
11325 Alignment = AlignResult.get();
11326 }
11327 if (Vars.empty())
11328 return nullptr;
11329
11330 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11331 EndLoc, Vars, Alignment);
11332}
11333
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011334OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11335 SourceLocation StartLoc,
11336 SourceLocation LParenLoc,
11337 SourceLocation EndLoc) {
11338 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011339 SmallVector<Expr *, 8> SrcExprs;
11340 SmallVector<Expr *, 8> DstExprs;
11341 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011342 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011343 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11344 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011345 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011346 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011347 SrcExprs.push_back(nullptr);
11348 DstExprs.push_back(nullptr);
11349 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011350 continue;
11351 }
11352
Alexey Bataeved09d242014-05-28 05:53:51 +000011353 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011354 // OpenMP [2.1, C/C++]
11355 // A list item is a variable name.
11356 // OpenMP [2.14.4.1, Restrictions, p.1]
11357 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011358 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011359 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011360 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11361 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011362 continue;
11363 }
11364
11365 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011366 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011367
11368 QualType Type = VD->getType();
11369 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11370 // It will be analyzed later.
11371 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011372 SrcExprs.push_back(nullptr);
11373 DstExprs.push_back(nullptr);
11374 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011375 continue;
11376 }
11377
11378 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11379 // A list item that appears in a copyin clause must be threadprivate.
11380 if (!DSAStack->isThreadPrivate(VD)) {
11381 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011382 << getOpenMPClauseName(OMPC_copyin)
11383 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011384 continue;
11385 }
11386
11387 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11388 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011389 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011390 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011391 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11392 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011393 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011394 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011395 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011396 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011397 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011398 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011399 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011400 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011401 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011402 // For arrays generate assignment operation for single element and replace
11403 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011404 ExprResult AssignmentOp =
11405 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11406 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011407 if (AssignmentOp.isInvalid())
11408 continue;
11409 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11410 /*DiscardedValue=*/true);
11411 if (AssignmentOp.isInvalid())
11412 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011413
11414 DSAStack->addDSA(VD, DE, OMPC_copyin);
11415 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011416 SrcExprs.push_back(PseudoSrcExpr);
11417 DstExprs.push_back(PseudoDstExpr);
11418 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011419 }
11420
Alexey Bataeved09d242014-05-28 05:53:51 +000011421 if (Vars.empty())
11422 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011423
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011424 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11425 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011426}
11427
Alexey Bataevbae9a792014-06-27 10:37:06 +000011428OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11429 SourceLocation StartLoc,
11430 SourceLocation LParenLoc,
11431 SourceLocation EndLoc) {
11432 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011433 SmallVector<Expr *, 8> SrcExprs;
11434 SmallVector<Expr *, 8> DstExprs;
11435 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011436 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011437 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11438 SourceLocation ELoc;
11439 SourceRange ERange;
11440 Expr *SimpleRefExpr = RefExpr;
11441 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11442 /*AllowArraySection=*/false);
11443 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011444 // It will be analyzed later.
11445 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011446 SrcExprs.push_back(nullptr);
11447 DstExprs.push_back(nullptr);
11448 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011449 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011450 ValueDecl *D = Res.first;
11451 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011452 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011453
Alexey Bataeve122da12016-03-17 10:50:17 +000011454 QualType Type = D->getType();
11455 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011456
11457 // OpenMP [2.14.4.2, Restrictions, p.2]
11458 // A list item that appears in a copyprivate clause may not appear in a
11459 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011460 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011461 DSAStackTy::DSAVarData DVar =
11462 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011463 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11464 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011465 Diag(ELoc, diag::err_omp_wrong_dsa)
11466 << getOpenMPClauseName(DVar.CKind)
11467 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011468 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011469 continue;
11470 }
11471
11472 // OpenMP [2.11.4.2, Restrictions, p.1]
11473 // All list items that appear in a copyprivate clause must be either
11474 // threadprivate or private in the enclosing context.
11475 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011476 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011477 if (DVar.CKind == OMPC_shared) {
11478 Diag(ELoc, diag::err_omp_required_access)
11479 << getOpenMPClauseName(OMPC_copyprivate)
11480 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011481 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011482 continue;
11483 }
11484 }
11485 }
11486
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011487 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011488 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011489 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011490 << getOpenMPClauseName(OMPC_copyprivate) << Type
11491 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011492 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011493 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011494 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011495 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011496 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011497 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011498 continue;
11499 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011500
Alexey Bataevbae9a792014-06-27 10:37:06 +000011501 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11502 // A variable of class type (or array thereof) that appears in a
11503 // copyin clause requires an accessible, unambiguous copy assignment
11504 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011505 Type = Context.getBaseElementType(Type.getNonReferenceType())
11506 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011507 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011508 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011509 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011510 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11511 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011512 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011513 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011514 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11515 ExprResult AssignmentOp = BuildBinOp(
11516 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011517 if (AssignmentOp.isInvalid())
11518 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011519 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011520 /*DiscardedValue=*/true);
11521 if (AssignmentOp.isInvalid())
11522 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011523
11524 // No need to mark vars as copyprivate, they are already threadprivate or
11525 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011526 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011527 Vars.push_back(
11528 VD ? RefExpr->IgnoreParens()
11529 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011530 SrcExprs.push_back(PseudoSrcExpr);
11531 DstExprs.push_back(PseudoDstExpr);
11532 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011533 }
11534
11535 if (Vars.empty())
11536 return nullptr;
11537
Alexey Bataeva63048e2015-03-23 06:18:07 +000011538 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11539 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011540}
11541
Alexey Bataev6125da92014-07-21 11:26:11 +000011542OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11543 SourceLocation StartLoc,
11544 SourceLocation LParenLoc,
11545 SourceLocation EndLoc) {
11546 if (VarList.empty())
11547 return nullptr;
11548
11549 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11550}
Alexey Bataevdea47612014-07-23 07:46:59 +000011551
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011552OMPClause *
11553Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11554 SourceLocation DepLoc, SourceLocation ColonLoc,
11555 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11556 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011557 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011558 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011559 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011560 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011561 return nullptr;
11562 }
11563 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011564 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11565 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011566 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011567 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011568 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11569 /*Last=*/OMPC_DEPEND_unknown, Except)
11570 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011571 return nullptr;
11572 }
11573 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011574 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011575 llvm::APSInt DepCounter(/*BitWidth=*/32);
11576 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011577 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11578 if (const Expr *OrderedCountExpr =
11579 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011580 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11581 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011582 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011583 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011584 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011585 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11586 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11587 // It will be analyzed later.
11588 Vars.push_back(RefExpr);
11589 continue;
11590 }
11591
11592 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011593 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011594 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000011595 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011596 DepCounter >= TotalDepCount) {
11597 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11598 continue;
11599 }
11600 ++DepCounter;
11601 // OpenMP [2.13.9, Summary]
11602 // depend(dependence-type : vec), where dependence-type is:
11603 // 'sink' and where vec is the iteration vector, which has the form:
11604 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11605 // where n is the value specified by the ordered clause in the loop
11606 // directive, xi denotes the loop iteration variable of the i-th nested
11607 // loop associated with the loop directive, and di is a constant
11608 // non-negative integer.
11609 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011610 // It will be analyzed later.
11611 Vars.push_back(RefExpr);
11612 continue;
11613 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011614 SimpleExpr = SimpleExpr->IgnoreImplicit();
11615 OverloadedOperatorKind OOK = OO_None;
11616 SourceLocation OOLoc;
11617 Expr *LHS = SimpleExpr;
11618 Expr *RHS = nullptr;
11619 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11620 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11621 OOLoc = BO->getOperatorLoc();
11622 LHS = BO->getLHS()->IgnoreParenImpCasts();
11623 RHS = BO->getRHS()->IgnoreParenImpCasts();
11624 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11625 OOK = OCE->getOperator();
11626 OOLoc = OCE->getOperatorLoc();
11627 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11628 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11629 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11630 OOK = MCE->getMethodDecl()
11631 ->getNameInfo()
11632 .getName()
11633 .getCXXOverloadedOperator();
11634 OOLoc = MCE->getCallee()->getExprLoc();
11635 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11636 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011637 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011638 SourceLocation ELoc;
11639 SourceRange ERange;
11640 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11641 /*AllowArraySection=*/false);
11642 if (Res.second) {
11643 // It will be analyzed later.
11644 Vars.push_back(RefExpr);
11645 }
11646 ValueDecl *D = Res.first;
11647 if (!D)
11648 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011649
Alexey Bataev17daedf2018-02-15 22:42:57 +000011650 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11651 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11652 continue;
11653 }
11654 if (RHS) {
11655 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11656 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11657 if (RHSRes.isInvalid())
11658 continue;
11659 }
11660 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011661 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011662 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011663 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000011664 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000011665 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000011666 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11667 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000011668 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000011669 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000011670 continue;
11671 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011672 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011673 } else {
11674 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11675 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11676 (ASE &&
11677 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11678 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11679 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11680 << RefExpr->getSourceRange();
11681 continue;
11682 }
11683 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11684 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11685 ExprResult Res =
11686 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11687 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11688 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11689 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11690 << RefExpr->getSourceRange();
11691 continue;
11692 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011693 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011694 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011695 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011696
11697 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11698 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011699 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011700 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11701 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11702 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11703 }
11704 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11705 Vars.empty())
11706 return nullptr;
11707
Alexey Bataev8b427062016-05-25 12:36:08 +000011708 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000011709 DepKind, DepLoc, ColonLoc, Vars,
11710 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000011711 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11712 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000011713 DSAStack->addDoacrossDependClause(C, OpsOffs);
11714 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011715}
Michael Wonge710d542015-08-07 16:16:36 +000011716
11717OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11718 SourceLocation LParenLoc,
11719 SourceLocation EndLoc) {
11720 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011721 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011722
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011723 // OpenMP [2.9.1, Restrictions]
11724 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000011725 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000011726 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011727 return nullptr;
11728
Alexey Bataev931e19b2017-10-02 16:32:39 +000011729 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011730 OpenMPDirectiveKind CaptureRegion =
11731 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11732 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011733 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011734 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011735 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11736 HelperValStmt = buildPreInits(Context, Captures);
11737 }
11738
Alexey Bataev8451efa2018-01-15 19:06:12 +000011739 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11740 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011741}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011742
Alexey Bataeve3727102018-04-18 15:57:46 +000011743static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000011744 DSAStackTy *Stack, QualType QTy,
11745 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000011746 NamedDecl *ND;
11747 if (QTy->isIncompleteType(&ND)) {
11748 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11749 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011750 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000011751 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11752 !QTy.isTrivialType(SemaRef.Context))
11753 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011754 return true;
11755}
11756
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011757/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011758/// (array section or array subscript) does NOT specify the whole size of the
11759/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011760static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011761 const Expr *E,
11762 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011763 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011764
11765 // If this is an array subscript, it refers to the whole size if the size of
11766 // the dimension is constant and equals 1. Also, an array section assumes the
11767 // format of an array subscript if no colon is used.
11768 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011769 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011770 return ATy->getSize().getSExtValue() != 1;
11771 // Size can't be evaluated statically.
11772 return false;
11773 }
11774
11775 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000011776 const Expr *LowerBound = OASE->getLowerBound();
11777 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011778
11779 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000011780 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011781 if (LowerBound) {
11782 llvm::APSInt ConstLowerBound;
11783 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11784 return false; // Can't get the integer value as a constant.
11785 if (ConstLowerBound.getSExtValue())
11786 return true;
11787 }
11788
11789 // If we don't have a length we covering the whole dimension.
11790 if (!Length)
11791 return false;
11792
11793 // If the base is a pointer, we don't have a way to get the size of the
11794 // pointee.
11795 if (BaseQTy->isPointerType())
11796 return false;
11797
11798 // We can only check if the length is the same as the size of the dimension
11799 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000011800 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011801 if (!CATy)
11802 return false;
11803
11804 llvm::APSInt ConstLength;
11805 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11806 return false; // Can't get the integer value as a constant.
11807
11808 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11809}
11810
11811// Return true if it can be proven that the provided array expression (array
11812// section or array subscript) does NOT specify a single element of the array
11813// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011814static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000011815 const Expr *E,
11816 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011817 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011818
11819 // An array subscript always refer to a single element. Also, an array section
11820 // assumes the format of an array subscript if no colon is used.
11821 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11822 return false;
11823
11824 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000011825 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011826
11827 // If we don't have a length we have to check if the array has unitary size
11828 // for this dimension. Also, we should always expect a length if the base type
11829 // is pointer.
11830 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011831 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011832 return ATy->getSize().getSExtValue() != 1;
11833 // We cannot assume anything.
11834 return false;
11835 }
11836
11837 // Check if the length evaluates to 1.
11838 llvm::APSInt ConstLength;
11839 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11840 return false; // Can't get the integer value as a constant.
11841
11842 return ConstLength.getSExtValue() != 1;
11843}
11844
Samuel Antao661c0902016-05-26 17:39:58 +000011845// Return the expression of the base of the mappable expression or null if it
11846// cannot be determined and do all the necessary checks to see if the expression
11847// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000011848// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000011849static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000011850 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000011851 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011852 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011853 SourceLocation ELoc = E->getExprLoc();
11854 SourceRange ERange = E->getSourceRange();
11855
11856 // The base of elements of list in a map clause have to be either:
11857 // - a reference to variable or field.
11858 // - a member expression.
11859 // - an array expression.
11860 //
11861 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11862 // reference to 'r'.
11863 //
11864 // If we have:
11865 //
11866 // struct SS {
11867 // Bla S;
11868 // foo() {
11869 // #pragma omp target map (S.Arr[:12]);
11870 // }
11871 // }
11872 //
11873 // We want to retrieve the member expression 'this->S';
11874
Alexey Bataeve3727102018-04-18 15:57:46 +000011875 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011876
Samuel Antao5de996e2016-01-22 20:21:36 +000011877 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11878 // If a list item is an array section, it must specify contiguous storage.
11879 //
11880 // For this restriction it is sufficient that we make sure only references
11881 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011882 // exist except in the rightmost expression (unless they cover the whole
11883 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000011884 //
11885 // r.ArrS[3:5].Arr[6:7]
11886 //
11887 // r.ArrS[3:5].x
11888 //
11889 // but these would be valid:
11890 // r.ArrS[3].Arr[6:7]
11891 //
11892 // r.ArrS[3].x
11893
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011894 bool AllowUnitySizeArraySection = true;
11895 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000011896
Dmitry Polukhin644a9252016-03-11 07:58:34 +000011897 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011898 E = E->IgnoreParenImpCasts();
11899
11900 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11901 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000011902 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011903
11904 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011905
11906 // If we got a reference to a declaration, we should not expect any array
11907 // section before that.
11908 AllowUnitySizeArraySection = false;
11909 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011910
11911 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011912 CurComponents.emplace_back(CurE, CurE->getDecl());
11913 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011914 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000011915
11916 if (isa<CXXThisExpr>(BaseE))
11917 // We found a base expression: this->Val.
11918 RelevantExpr = CurE;
11919 else
11920 E = BaseE;
11921
11922 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011923 if (!NoDiagnose) {
11924 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11925 << CurE->getSourceRange();
11926 return nullptr;
11927 }
11928 if (RelevantExpr)
11929 return nullptr;
11930 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011931 }
11932
11933 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11934
11935 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11936 // A bit-field cannot appear in a map clause.
11937 //
11938 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011939 if (!NoDiagnose) {
11940 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11941 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11942 return nullptr;
11943 }
11944 if (RelevantExpr)
11945 return nullptr;
11946 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011947 }
11948
11949 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11950 // If the type of a list item is a reference to a type T then the type
11951 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011952 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000011953
11954 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11955 // A list item cannot be a variable that is a member of a structure with
11956 // a union type.
11957 //
Alexey Bataeve3727102018-04-18 15:57:46 +000011958 if (CurType->isUnionType()) {
11959 if (!NoDiagnose) {
11960 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11961 << CurE->getSourceRange();
11962 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000011963 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011964 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011965 }
Samuel Antao5de996e2016-01-22 20:21:36 +000011966
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011967 // If we got a member expression, we should not expect any array section
11968 // before that:
11969 //
11970 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11971 // If a list item is an element of a structure, only the rightmost symbol
11972 // of the variable reference can be an array section.
11973 //
11974 AllowUnitySizeArraySection = false;
11975 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011976
11977 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011978 CurComponents.emplace_back(CurE, FD);
11979 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000011980 E = CurE->getBase()->IgnoreParenImpCasts();
11981
11982 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000011983 if (!NoDiagnose) {
11984 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11985 << 0 << CurE->getSourceRange();
11986 return nullptr;
11987 }
11988 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000011989 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011990
11991 // If we got an array subscript that express the whole dimension we
11992 // can have any array expressions before. If it only expressing part of
11993 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000011994 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000011995 E->getType()))
11996 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000011997
11998 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000011999 CurComponents.emplace_back(CurE, nullptr);
12000 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012001 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012002 E = CurE->getBase()->IgnoreParenImpCasts();
12003
Alexey Bataev27041fa2017-12-05 15:22:49 +000012004 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012005 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12006
Samuel Antao5de996e2016-01-22 20:21:36 +000012007 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12008 // If the type of a list item is a reference to a type T then the type
12009 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012010 if (CurType->isReferenceType())
12011 CurType = CurType->getPointeeType();
12012
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012013 bool IsPointer = CurType->isAnyPointerType();
12014
12015 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012016 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12017 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012018 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012019 }
12020
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012021 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012022 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012023 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012024 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012025
Samuel Antaodab51bb2016-07-18 23:22:11 +000012026 if (AllowWholeSizeArraySection) {
12027 // Any array section is currently allowed. Allowing a whole size array
12028 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012029 //
12030 // If this array section refers to the whole dimension we can still
12031 // accept other array sections before this one, except if the base is a
12032 // pointer. Otherwise, only unitary sections are accepted.
12033 if (NotWhole || IsPointer)
12034 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012035 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012036 // A unity or whole array section is not allowed and that is not
12037 // compatible with the properties of the current array section.
12038 SemaRef.Diag(
12039 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12040 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012041 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012042 }
Samuel Antao90927002016-04-26 14:54:23 +000012043
12044 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012045 CurComponents.emplace_back(CurE, nullptr);
12046 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012047 if (!NoDiagnose) {
12048 // If nothing else worked, this is not a valid map clause expression.
12049 SemaRef.Diag(
12050 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12051 << ERange;
12052 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012053 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012054 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012055 }
12056
12057 return RelevantExpr;
12058}
12059
12060// Return true if expression E associated with value VD has conflicts with other
12061// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012062static bool checkMapConflicts(
12063 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012064 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012065 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12066 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012067 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012068 SourceLocation ELoc = E->getExprLoc();
12069 SourceRange ERange = E->getSourceRange();
12070
12071 // In order to easily check the conflicts we need to match each component of
12072 // the expression under test with the components of the expressions that are
12073 // already in the stack.
12074
Samuel Antao5de996e2016-01-22 20:21:36 +000012075 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012076 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012077 "Map clause expression with unexpected base!");
12078
12079 // Variables to help detecting enclosing problems in data environment nests.
12080 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012081 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012082
Samuel Antao90927002016-04-26 14:54:23 +000012083 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12084 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012085 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12086 ERange, CKind, &EnclosingExpr,
12087 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12088 StackComponents,
12089 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012090 assert(!StackComponents.empty() &&
12091 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012092 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012093 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012094 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012095
Samuel Antao90927002016-04-26 14:54:23 +000012096 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012097 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012098
Samuel Antao5de996e2016-01-22 20:21:36 +000012099 // Expressions must start from the same base. Here we detect at which
12100 // point both expressions diverge from each other and see if we can
12101 // detect if the memory referred to both expressions is contiguous and
12102 // do not overlap.
12103 auto CI = CurComponents.rbegin();
12104 auto CE = CurComponents.rend();
12105 auto SI = StackComponents.rbegin();
12106 auto SE = StackComponents.rend();
12107 for (; CI != CE && SI != SE; ++CI, ++SI) {
12108
12109 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12110 // At most one list item can be an array item derived from a given
12111 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012112 if (CurrentRegionOnly &&
12113 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12114 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12115 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12116 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12117 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012118 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012119 << CI->getAssociatedExpression()->getSourceRange();
12120 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12121 diag::note_used_here)
12122 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012123 return true;
12124 }
12125
12126 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012127 if (CI->getAssociatedExpression()->getStmtClass() !=
12128 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012129 break;
12130
12131 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012132 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012133 break;
12134 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012135 // Check if the extra components of the expressions in the enclosing
12136 // data environment are redundant for the current base declaration.
12137 // If they are, the maps completely overlap, which is legal.
12138 for (; SI != SE; ++SI) {
12139 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012140 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012141 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012142 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012143 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012144 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012145 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012146 Type =
12147 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12148 }
12149 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012150 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012151 SemaRef, SI->getAssociatedExpression(), Type))
12152 break;
12153 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012154
12155 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12156 // List items of map clauses in the same construct must not share
12157 // original storage.
12158 //
12159 // If the expressions are exactly the same or one is a subset of the
12160 // other, it means they are sharing storage.
12161 if (CI == CE && SI == SE) {
12162 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012163 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012164 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012165 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012166 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012167 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12168 << ERange;
12169 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012170 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12171 << RE->getSourceRange();
12172 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012173 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012174 // If we find the same expression in the enclosing data environment,
12175 // that is legal.
12176 IsEnclosedByDataEnvironmentExpr = true;
12177 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012178 }
12179
Samuel Antao90927002016-04-26 14:54:23 +000012180 QualType DerivedType =
12181 std::prev(CI)->getAssociatedDeclaration()->getType();
12182 SourceLocation DerivedLoc =
12183 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012184
12185 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12186 // If the type of a list item is a reference to a type T then the type
12187 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012188 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012189
12190 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12191 // A variable for which the type is pointer and an array section
12192 // derived from that variable must not appear as list items of map
12193 // clauses of the same construct.
12194 //
12195 // Also, cover one of the cases in:
12196 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12197 // If any part of the original storage of a list item has corresponding
12198 // storage in the device data environment, all of the original storage
12199 // must have corresponding storage in the device data environment.
12200 //
12201 if (DerivedType->isAnyPointerType()) {
12202 if (CI == CE || SI == SE) {
12203 SemaRef.Diag(
12204 DerivedLoc,
12205 diag::err_omp_pointer_mapped_along_with_derived_section)
12206 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012207 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12208 << RE->getSourceRange();
12209 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012210 }
12211 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012212 SI->getAssociatedExpression()->getStmtClass() ||
12213 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12214 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012215 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012216 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012217 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012218 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12219 << RE->getSourceRange();
12220 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012221 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012222 }
12223
12224 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12225 // List items of map clauses in the same construct must not share
12226 // original storage.
12227 //
12228 // An expression is a subset of the other.
12229 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012230 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012231 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012232 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012233 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012234 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12235 << ERange;
12236 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012237 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12238 << RE->getSourceRange();
12239 return true;
12240 }
12241
12242 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012243 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012244 if (!CurrentRegionOnly && SI != SE)
12245 EnclosingExpr = RE;
12246
12247 // The current expression is a subset of the expression in the data
12248 // environment.
12249 IsEnclosedByDataEnvironmentExpr |=
12250 (!CurrentRegionOnly && CI != CE && SI == SE);
12251
12252 return false;
12253 });
12254
12255 if (CurrentRegionOnly)
12256 return FoundError;
12257
12258 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12259 // If any part of the original storage of a list item has corresponding
12260 // storage in the device data environment, all of the original storage must
12261 // have corresponding storage in the device data environment.
12262 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12263 // If a list item is an element of a structure, and a different element of
12264 // the structure has a corresponding list item in the device data environment
12265 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012266 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012267 // data environment prior to the task encountering the construct.
12268 //
12269 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12270 SemaRef.Diag(ELoc,
12271 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12272 << ERange;
12273 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12274 << EnclosingExpr->getSourceRange();
12275 return true;
12276 }
12277
12278 return FoundError;
12279}
12280
Samuel Antao661c0902016-05-26 17:39:58 +000012281namespace {
12282// Utility struct that gathers all the related lists associated with a mappable
12283// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012284struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012285 // The list of expressions.
12286 ArrayRef<Expr *> VarList;
12287 // The list of processed expressions.
12288 SmallVector<Expr *, 16> ProcessedVarList;
12289 // The mappble components for each expression.
12290 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12291 // The base declaration of the variable.
12292 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12293
12294 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12295 // We have a list of components and base declarations for each entry in the
12296 // variable list.
12297 VarComponents.reserve(VarList.size());
12298 VarBaseDeclarations.reserve(VarList.size());
12299 }
12300};
12301}
12302
12303// Check the validity of the provided variable list for the provided clause kind
12304// \a CKind. In the check process the valid expressions, and mappable expression
12305// components and variables are extracted and used to fill \a Vars,
12306// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12307// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12308static void
12309checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12310 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12311 SourceLocation StartLoc,
12312 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12313 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012314 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12315 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012316 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012317
Samuel Antao90927002016-04-26 14:54:23 +000012318 // Keep track of the mappable components and base declarations in this clause.
12319 // Each entry in the list is going to have a list of components associated. We
12320 // record each set of the components so that we can build the clause later on.
12321 // In the end we should have the same amount of declarations and component
12322 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012323
Alexey Bataeve3727102018-04-18 15:57:46 +000012324 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012325 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012326 SourceLocation ELoc = RE->getExprLoc();
12327
Alexey Bataeve3727102018-04-18 15:57:46 +000012328 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012329
12330 if (VE->isValueDependent() || VE->isTypeDependent() ||
12331 VE->isInstantiationDependent() ||
12332 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012333 // We can only analyze this information once the missing information is
12334 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012335 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012336 continue;
12337 }
12338
Alexey Bataeve3727102018-04-18 15:57:46 +000012339 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012340
Samuel Antao5de996e2016-01-22 20:21:36 +000012341 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012342 SemaRef.Diag(ELoc,
12343 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012344 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012345 continue;
12346 }
12347
Samuel Antao90927002016-04-26 14:54:23 +000012348 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12349 ValueDecl *CurDeclaration = nullptr;
12350
12351 // Obtain the array or member expression bases if required. Also, fill the
12352 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012353 const Expr *BE = checkMapClauseExpressionBase(
12354 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012355 if (!BE)
12356 continue;
12357
Samuel Antao90927002016-04-26 14:54:23 +000012358 assert(!CurComponents.empty() &&
12359 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012360
Samuel Antao90927002016-04-26 14:54:23 +000012361 // For the following checks, we rely on the base declaration which is
12362 // expected to be associated with the last component. The declaration is
12363 // expected to be a variable or a field (if 'this' is being mapped).
12364 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12365 assert(CurDeclaration && "Null decl on map clause.");
12366 assert(
12367 CurDeclaration->isCanonicalDecl() &&
12368 "Expecting components to have associated only canonical declarations.");
12369
12370 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012371 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012372
12373 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012374 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012375
12376 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012377 // threadprivate variables cannot appear in a map clause.
12378 // OpenMP 4.5 [2.10.5, target update Construct]
12379 // threadprivate variables cannot appear in a from clause.
12380 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012381 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012382 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12383 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012384 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012385 continue;
12386 }
12387
Samuel Antao5de996e2016-01-22 20:21:36 +000012388 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12389 // A list item cannot appear in both a map clause and a data-sharing
12390 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012391
Samuel Antao5de996e2016-01-22 20:21:36 +000012392 // Check conflicts with other map clause expressions. We check the conflicts
12393 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012394 // environment, because the restrictions are different. We only have to
12395 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012396 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012397 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012398 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012399 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012400 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012401 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012402 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012403
Samuel Antao661c0902016-05-26 17:39:58 +000012404 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012405 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12406 // If the type of a list item is a reference to a type T then the type will
12407 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012408 auto I = llvm::find_if(
12409 CurComponents,
12410 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12411 return MC.getAssociatedDeclaration();
12412 });
12413 assert(I != CurComponents.end() && "Null decl on map clause.");
12414 QualType Type =
12415 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012416
Samuel Antao661c0902016-05-26 17:39:58 +000012417 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12418 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012419 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012420 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012421 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012422 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012423 continue;
12424
Samuel Antao661c0902016-05-26 17:39:58 +000012425 if (CKind == OMPC_map) {
12426 // target enter data
12427 // OpenMP [2.10.2, Restrictions, p. 99]
12428 // A map-type must be specified in all map clauses and must be either
12429 // to or alloc.
12430 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12431 if (DKind == OMPD_target_enter_data &&
12432 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12433 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12434 << (IsMapTypeImplicit ? 1 : 0)
12435 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12436 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012437 continue;
12438 }
Samuel Antao661c0902016-05-26 17:39:58 +000012439
12440 // target exit_data
12441 // OpenMP [2.10.3, Restrictions, p. 102]
12442 // A map-type must be specified in all map clauses and must be either
12443 // from, release, or delete.
12444 if (DKind == OMPD_target_exit_data &&
12445 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12446 MapType == OMPC_MAP_delete)) {
12447 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12448 << (IsMapTypeImplicit ? 1 : 0)
12449 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12450 << getOpenMPDirectiveName(DKind);
12451 continue;
12452 }
12453
12454 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12455 // A list item cannot appear in both a map clause and a data-sharing
12456 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012457 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12458 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012459 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012460 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012461 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012462 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012463 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012464 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012465 continue;
12466 }
12467 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012468 }
12469
Samuel Antao90927002016-04-26 14:54:23 +000012470 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012471 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012472
12473 // Store the components in the stack so that they can be used to check
12474 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012475 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12476 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012477
12478 // Save the components and declaration to create the clause. For purposes of
12479 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012480 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012481 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12482 MVLI.VarComponents.back().append(CurComponents.begin(),
12483 CurComponents.end());
12484 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12485 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012486 }
Samuel Antao661c0902016-05-26 17:39:58 +000012487}
12488
12489OMPClause *
12490Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12491 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12492 SourceLocation MapLoc, SourceLocation ColonLoc,
12493 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12494 SourceLocation LParenLoc, SourceLocation EndLoc) {
12495 MappableVarListInfo MVLI(VarList);
12496 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12497 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012498
Samuel Antao5de996e2016-01-22 20:21:36 +000012499 // We need to produce a map clause even if we don't have variables so that
12500 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012501 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12502 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12503 MVLI.VarComponents, MapTypeModifier, MapType,
12504 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012505}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012506
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012507QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12508 TypeResult ParsedType) {
12509 assert(ParsedType.isUsable());
12510
12511 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12512 if (ReductionType.isNull())
12513 return QualType();
12514
12515 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12516 // A type name in a declare reduction directive cannot be a function type, an
12517 // array type, a reference type, or a type qualified with const, volatile or
12518 // restrict.
12519 if (ReductionType.hasQualifiers()) {
12520 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12521 return QualType();
12522 }
12523
12524 if (ReductionType->isFunctionType()) {
12525 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12526 return QualType();
12527 }
12528 if (ReductionType->isReferenceType()) {
12529 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12530 return QualType();
12531 }
12532 if (ReductionType->isArrayType()) {
12533 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12534 return QualType();
12535 }
12536 return ReductionType;
12537}
12538
12539Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12540 Scope *S, DeclContext *DC, DeclarationName Name,
12541 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12542 AccessSpecifier AS, Decl *PrevDeclInScope) {
12543 SmallVector<Decl *, 8> Decls;
12544 Decls.reserve(ReductionTypes.size());
12545
12546 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012547 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012548 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12549 // A reduction-identifier may not be re-declared in the current scope for the
12550 // same type or for a type that is compatible according to the base language
12551 // rules.
12552 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12553 OMPDeclareReductionDecl *PrevDRD = nullptr;
12554 bool InCompoundScope = true;
12555 if (S != nullptr) {
12556 // Find previous declaration with the same name not referenced in other
12557 // declarations.
12558 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12559 InCompoundScope =
12560 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12561 LookupName(Lookup, S);
12562 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12563 /*AllowInlineNamespace=*/false);
12564 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012565 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012566 while (Filter.hasNext()) {
12567 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12568 if (InCompoundScope) {
12569 auto I = UsedAsPrevious.find(PrevDecl);
12570 if (I == UsedAsPrevious.end())
12571 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012572 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012573 UsedAsPrevious[D] = true;
12574 }
12575 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12576 PrevDecl->getLocation();
12577 }
12578 Filter.done();
12579 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012580 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012581 if (!PrevData.second) {
12582 PrevDRD = PrevData.first;
12583 break;
12584 }
12585 }
12586 }
12587 } else if (PrevDeclInScope != nullptr) {
12588 auto *PrevDRDInScope = PrevDRD =
12589 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12590 do {
12591 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12592 PrevDRDInScope->getLocation();
12593 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12594 } while (PrevDRDInScope != nullptr);
12595 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012596 for (const auto &TyData : ReductionTypes) {
12597 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012598 bool Invalid = false;
12599 if (I != PreviousRedeclTypes.end()) {
12600 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12601 << TyData.first;
12602 Diag(I->second, diag::note_previous_definition);
12603 Invalid = true;
12604 }
12605 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12606 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12607 Name, TyData.first, PrevDRD);
12608 DC->addDecl(DRD);
12609 DRD->setAccess(AS);
12610 Decls.push_back(DRD);
12611 if (Invalid)
12612 DRD->setInvalidDecl();
12613 else
12614 PrevDRD = DRD;
12615 }
12616
12617 return DeclGroupPtrTy::make(
12618 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12619}
12620
12621void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12622 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12623
12624 // Enter new function scope.
12625 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012626 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012627 getCurFunction()->setHasOMPDeclareReductionCombiner();
12628
12629 if (S != nullptr)
12630 PushDeclContext(S, DRD);
12631 else
12632 CurContext = DRD;
12633
Faisal Valid143a0c2017-04-01 21:30:49 +000012634 PushExpressionEvaluationContext(
12635 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012636
12637 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012638 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12639 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12640 // uses semantics of argument handles by value, but it should be passed by
12641 // reference. C lang does not support references, so pass all parameters as
12642 // pointers.
12643 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012644 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012645 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012646 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12647 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12648 // uses semantics of argument handles by value, but it should be passed by
12649 // reference. C lang does not support references, so pass all parameters as
12650 // pointers.
12651 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012652 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012653 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12654 if (S != nullptr) {
12655 PushOnScopeChains(OmpInParm, S);
12656 PushOnScopeChains(OmpOutParm, S);
12657 } else {
12658 DRD->addDecl(OmpInParm);
12659 DRD->addDecl(OmpOutParm);
12660 }
12661}
12662
12663void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12664 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12665 DiscardCleanupsInEvaluationContext();
12666 PopExpressionEvaluationContext();
12667
12668 PopDeclContext();
12669 PopFunctionScopeInfo();
12670
12671 if (Combiner != nullptr)
12672 DRD->setCombiner(Combiner);
12673 else
12674 DRD->setInvalidDecl();
12675}
12676
Alexey Bataev070f43a2017-09-06 14:49:58 +000012677VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012678 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12679
12680 // Enter new function scope.
12681 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012682 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012683
12684 if (S != nullptr)
12685 PushDeclContext(S, DRD);
12686 else
12687 CurContext = DRD;
12688
Faisal Valid143a0c2017-04-01 21:30:49 +000012689 PushExpressionEvaluationContext(
12690 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012691
12692 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012693 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12694 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12695 // uses semantics of argument handles by value, but it should be passed by
12696 // reference. C lang does not support references, so pass all parameters as
12697 // pointers.
12698 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012699 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012700 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012701 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12702 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12703 // uses semantics of argument handles by value, but it should be passed by
12704 // reference. C lang does not support references, so pass all parameters as
12705 // pointers.
12706 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012707 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012708 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012709 if (S != nullptr) {
12710 PushOnScopeChains(OmpPrivParm, S);
12711 PushOnScopeChains(OmpOrigParm, S);
12712 } else {
12713 DRD->addDecl(OmpPrivParm);
12714 DRD->addDecl(OmpOrigParm);
12715 }
Alexey Bataev070f43a2017-09-06 14:49:58 +000012716 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012717}
12718
Alexey Bataev070f43a2017-09-06 14:49:58 +000012719void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12720 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012721 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12722 DiscardCleanupsInEvaluationContext();
12723 PopExpressionEvaluationContext();
12724
12725 PopDeclContext();
12726 PopFunctionScopeInfo();
12727
Alexey Bataev070f43a2017-09-06 14:49:58 +000012728 if (Initializer != nullptr) {
12729 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12730 } else if (OmpPrivParm->hasInit()) {
12731 DRD->setInitializer(OmpPrivParm->getInit(),
12732 OmpPrivParm->isDirectInit()
12733 ? OMPDeclareReductionDecl::DirectInit
12734 : OMPDeclareReductionDecl::CopyInit);
12735 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012736 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000012737 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012738}
12739
12740Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12741 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012742 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012743 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012744 if (S)
12745 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
12746 /*AddToContext=*/false);
12747 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012748 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000012749 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012750 }
12751 return DeclReductions;
12752}
12753
David Majnemer9d168222016-08-05 17:44:54 +000012754OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000012755 SourceLocation StartLoc,
12756 SourceLocation LParenLoc,
12757 SourceLocation EndLoc) {
12758 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012759 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012760
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012761 // OpenMP [teams Constrcut, Restrictions]
12762 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012763 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000012764 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012765 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000012766
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012767 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012768 OpenMPDirectiveKind CaptureRegion =
12769 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12770 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012771 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012772 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000012773 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12774 HelperValStmt = buildPreInits(Context, Captures);
12775 }
12776
12777 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12778 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000012779}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012780
12781OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12782 SourceLocation StartLoc,
12783 SourceLocation LParenLoc,
12784 SourceLocation EndLoc) {
12785 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012786 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012787
12788 // OpenMP [teams Constrcut, Restrictions]
12789 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012790 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000012791 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012792 return nullptr;
12793
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012794 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012795 OpenMPDirectiveKind CaptureRegion =
12796 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12797 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012798 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012799 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000012800 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12801 HelperValStmt = buildPreInits(Context, Captures);
12802 }
12803
12804 return new (Context) OMPThreadLimitClause(
12805 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012806}
Alexey Bataeva0569352015-12-01 10:17:31 +000012807
12808OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12809 SourceLocation StartLoc,
12810 SourceLocation LParenLoc,
12811 SourceLocation EndLoc) {
12812 Expr *ValExpr = Priority;
12813
12814 // OpenMP [2.9.1, task Constrcut]
12815 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012816 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000012817 /*StrictlyPositive=*/false))
12818 return nullptr;
12819
12820 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12821}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012822
12823OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12824 SourceLocation StartLoc,
12825 SourceLocation LParenLoc,
12826 SourceLocation EndLoc) {
12827 Expr *ValExpr = Grainsize;
12828
12829 // OpenMP [2.9.2, taskloop Constrcut]
12830 // The parameter of the grainsize clause must be a positive integer
12831 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012832 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000012833 /*StrictlyPositive=*/true))
12834 return nullptr;
12835
12836 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12837}
Alexey Bataev382967a2015-12-08 12:06:20 +000012838
12839OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12840 SourceLocation StartLoc,
12841 SourceLocation LParenLoc,
12842 SourceLocation EndLoc) {
12843 Expr *ValExpr = NumTasks;
12844
12845 // OpenMP [2.9.2, taskloop Constrcut]
12846 // The parameter of the num_tasks clause must be a positive integer
12847 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012848 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000012849 /*StrictlyPositive=*/true))
12850 return nullptr;
12851
12852 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12853}
12854
Alexey Bataev28c75412015-12-15 08:19:24 +000012855OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12856 SourceLocation LParenLoc,
12857 SourceLocation EndLoc) {
12858 // OpenMP [2.13.2, critical construct, Description]
12859 // ... where hint-expression is an integer constant expression that evaluates
12860 // to a valid lock hint.
12861 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12862 if (HintExpr.isInvalid())
12863 return nullptr;
12864 return new (Context)
12865 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12866}
12867
Carlo Bertollib4adf552016-01-15 18:50:31 +000012868OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12869 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12870 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12871 SourceLocation EndLoc) {
12872 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12873 std::string Values;
12874 Values += "'";
12875 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12876 Values += "'";
12877 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12878 << Values << getOpenMPClauseName(OMPC_dist_schedule);
12879 return nullptr;
12880 }
12881 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000012882 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000012883 if (ChunkSize) {
12884 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12885 !ChunkSize->isInstantiationDependent() &&
12886 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012887 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000012888 ExprResult Val =
12889 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12890 if (Val.isInvalid())
12891 return nullptr;
12892
12893 ValExpr = Val.get();
12894
12895 // OpenMP [2.7.1, Restrictions]
12896 // chunk_size must be a loop invariant integer expression with a positive
12897 // value.
12898 llvm::APSInt Result;
12899 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12900 if (Result.isSigned() && !Result.isStrictlyPositive()) {
12901 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12902 << "dist_schedule" << ChunkSize->getSourceRange();
12903 return nullptr;
12904 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000012905 } else if (getOpenMPCaptureRegionForClause(
12906 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12907 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000012908 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012909 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012910 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000012911 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12912 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012913 }
12914 }
12915 }
12916
12917 return new (Context)
12918 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000012919 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000012920}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012921
12922OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12923 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12924 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12925 SourceLocation KindLoc, SourceLocation EndLoc) {
12926 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000012927 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012928 std::string Value;
12929 SourceLocation Loc;
12930 Value += "'";
12931 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12932 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012933 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012934 Loc = MLoc;
12935 } else {
12936 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000012937 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012938 Loc = KindLoc;
12939 }
12940 Value += "'";
12941 Diag(Loc, diag::err_omp_unexpected_clause_value)
12942 << Value << getOpenMPClauseName(OMPC_defaultmap);
12943 return nullptr;
12944 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000012945 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000012946
12947 return new (Context)
12948 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12949}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012950
12951bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12952 DeclContext *CurLexicalContext = getCurLexicalContext();
12953 if (!CurLexicalContext->isFileContext() &&
12954 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000012955 !CurLexicalContext->isExternCXXContext() &&
12956 !isa<CXXRecordDecl>(CurLexicalContext) &&
12957 !isa<ClassTemplateDecl>(CurLexicalContext) &&
12958 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12959 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012960 Diag(Loc, diag::err_omp_region_not_file_context);
12961 return false;
12962 }
12963 if (IsInOpenMPDeclareTargetContext) {
12964 Diag(Loc, diag::err_omp_enclosed_declare_target);
12965 return false;
12966 }
12967
12968 IsInOpenMPDeclareTargetContext = true;
12969 return true;
12970}
12971
12972void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12973 assert(IsInOpenMPDeclareTargetContext &&
12974 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000012975 IsInOpenMPDeclareTargetContext = false;
12976}
12977
David Majnemer9d168222016-08-05 17:44:54 +000012978void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12979 CXXScopeSpec &ScopeSpec,
12980 const DeclarationNameInfo &Id,
12981 OMPDeclareTargetDeclAttr::MapTypeTy MT,
12982 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000012983 LookupResult Lookup(*this, Id, LookupOrdinaryName);
12984 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12985
12986 if (Lookup.isAmbiguous())
12987 return;
12988 Lookup.suppressDiagnostics();
12989
12990 if (!Lookup.isSingleResult()) {
12991 if (TypoCorrection Corrected =
12992 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12993 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12994 CTK_ErrorRecovery)) {
12995 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12996 << Id.getName());
12997 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12998 return;
12999 }
13000
13001 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13002 return;
13003 }
13004
13005 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
13006 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
13007 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13008 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013009 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013010 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013011 ND->addAttr(A);
13012 if (ASTMutationListener *ML = Context.getASTMutationListener())
13013 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013014 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013015 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
13016 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13017 << Id.getName();
13018 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013019 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013020 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013021 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013022}
13023
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013024static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13025 Sema &SemaRef, Decl *D) {
13026 if (!D)
13027 return;
Alexey Bataev8e39c342018-02-16 21:23:23 +000013028 const Decl *LD = nullptr;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013029 if (isa<TagDecl>(D)) {
13030 LD = cast<TagDecl>(D)->getDefinition();
13031 } else if (isa<VarDecl>(D)) {
13032 LD = cast<VarDecl>(D)->getDefinition();
13033
13034 // If this is an implicit variable that is legal and we do not need to do
13035 // anything.
13036 if (cast<VarDecl>(D)->isImplicit()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013037 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013038 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
13039 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013040 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013041 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013042 return;
13043 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013044 } else if (const auto *F = dyn_cast<FunctionDecl>(D)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013045 const FunctionDecl *FD = nullptr;
Alexey Bataev8e39c342018-02-16 21:23:23 +000013046 if (cast<FunctionDecl>(D)->hasBody(FD)) {
13047 LD = FD;
13048 // If the definition is associated with the current declaration in the
13049 // target region (it can be e.g. a lambda) that is legal and we do not
13050 // need to do anything else.
13051 if (LD == D) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013052 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Alexey Bataev8e39c342018-02-16 21:23:23 +000013053 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
13054 D->addAttr(A);
13055 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
13056 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13057 return;
13058 }
13059 } else if (F->isFunctionTemplateSpecialization() &&
13060 F->getTemplateSpecializationKind() ==
13061 TSK_ImplicitInstantiation) {
13062 // Check if the function is implicitly instantiated from the template
13063 // defined in the declare target region.
13064 const FunctionTemplateDecl *FTD = F->getPrimaryTemplate();
13065 if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>())
13066 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013067 }
13068 }
13069 if (!LD)
13070 LD = D;
13071 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
Alexey Bataevd0df36a2018-04-30 18:28:08 +000013072 ((isa<VarDecl>(LD) && !isa<ParmVarDecl>(LD)) || isa<FunctionDecl>(LD))) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013073 // Outlined declaration is not declared target.
Alexey Bataevdcc815d2018-05-02 17:39:00 +000013074 if (!isa<FunctionDecl>(LD)) {
13075 if (LD->isOutOfLine()) {
13076 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
13077 SemaRef.Diag(SL, diag::note_used_here) << SR;
13078 } else {
13079 const DeclContext *DC = LD->getDeclContext();
13080 while (DC &&
13081 (!isa<FunctionDecl>(DC) ||
13082 !cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>()))
13083 DC = DC->getParent();
13084 if (DC)
13085 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013086
Alexey Bataevdcc815d2018-05-02 17:39:00 +000013087 // Is not declared in target context.
13088 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
13089 SemaRef.Diag(SL, diag::note_used_here) << SR;
13090 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013091 }
13092 // Mark decl as declared target to prevent further diagnostic.
Alexey Bataeve3727102018-04-18 15:57:46 +000013093 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013094 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
13095 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013096 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013097 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013098 }
13099}
13100
13101static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13102 Sema &SemaRef, DSAStackTy *Stack,
13103 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013104 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13105 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13106 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013107}
13108
Kelvin Li1ce87c72017-12-12 20:08:12 +000013109void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13110 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013111 if (!D || D->isInvalidDecl())
13112 return;
13113 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013114 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013115 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013116 // Only global variables can be marked as declare target.
13117 if (VD->isLocalVarDeclOrParm())
13118 return;
13119 // 2.10.6: threadprivate variable cannot appear in a declare target
13120 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013121 if (DSAStack->isThreadPrivate(VD)) {
13122 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013123 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013124 return;
13125 }
13126 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013127 if (auto *VD = dyn_cast<ValueDecl>(D)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013128 // Problem if any with var declared with incomplete type will be reported
13129 // as normal, so no need to check it here.
13130 if ((E || !VD->getType()->isIncompleteType()) &&
13131 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
13132 // Mark decl as declared target to prevent further diagnostic.
Alexey Bataev8e39c342018-02-16 21:23:23 +000013133 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) ||
13134 isa<FunctionTemplateDecl>(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013135 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013136 Context, OMPDeclareTargetDeclAttr::MT_To);
13137 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013138 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013139 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013140 }
13141 return;
13142 }
13143 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013144 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13145 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013146 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013147 if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13148 (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13149 OMPDeclareTargetDeclAttr::MT_Link)) {
13150 assert(IdLoc.isValid() && "Source location is expected");
13151 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13152 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13153 return;
13154 }
13155 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013156 if (!E) {
13157 // Checking declaration inside declare target region.
13158 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
Alexey Bataev8e39c342018-02-16 21:23:23 +000013159 (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13160 isa<FunctionTemplateDecl>(D))) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013161 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013162 Context, OMPDeclareTargetDeclAttr::MT_To);
13163 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013164 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013165 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013166 }
13167 return;
13168 }
13169 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13170}
Samuel Antao661c0902016-05-26 17:39:58 +000013171
13172OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13173 SourceLocation StartLoc,
13174 SourceLocation LParenLoc,
13175 SourceLocation EndLoc) {
13176 MappableVarListInfo MVLI(VarList);
13177 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13178 if (MVLI.ProcessedVarList.empty())
13179 return nullptr;
13180
13181 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13182 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13183 MVLI.VarComponents);
13184}
Samuel Antaoec172c62016-05-26 17:49:04 +000013185
13186OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13187 SourceLocation StartLoc,
13188 SourceLocation LParenLoc,
13189 SourceLocation EndLoc) {
13190 MappableVarListInfo MVLI(VarList);
13191 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13192 if (MVLI.ProcessedVarList.empty())
13193 return nullptr;
13194
13195 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13196 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13197 MVLI.VarComponents);
13198}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013199
13200OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13201 SourceLocation StartLoc,
13202 SourceLocation LParenLoc,
13203 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013204 MappableVarListInfo MVLI(VarList);
13205 SmallVector<Expr *, 8> PrivateCopies;
13206 SmallVector<Expr *, 8> Inits;
13207
Alexey Bataeve3727102018-04-18 15:57:46 +000013208 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013209 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13210 SourceLocation ELoc;
13211 SourceRange ERange;
13212 Expr *SimpleRefExpr = RefExpr;
13213 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13214 if (Res.second) {
13215 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013216 MVLI.ProcessedVarList.push_back(RefExpr);
13217 PrivateCopies.push_back(nullptr);
13218 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013219 }
13220 ValueDecl *D = Res.first;
13221 if (!D)
13222 continue;
13223
13224 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013225 Type = Type.getNonReferenceType().getUnqualifiedType();
13226
13227 auto *VD = dyn_cast<VarDecl>(D);
13228
13229 // Item should be a pointer or reference to pointer.
13230 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013231 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13232 << 0 << RefExpr->getSourceRange();
13233 continue;
13234 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013235
13236 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013237 auto VDPrivate =
13238 buildVarDecl(*this, ELoc, Type, D->getName(),
13239 D->hasAttrs() ? &D->getAttrs() : nullptr,
13240 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013241 if (VDPrivate->isInvalidDecl())
13242 continue;
13243
13244 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013245 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013246 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13247
13248 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013249 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013250 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013251 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13252 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013253 AddInitializerToDecl(VDPrivate,
13254 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013255 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013256
13257 // If required, build a capture to implement the privatization initialized
13258 // with the current list item value.
13259 DeclRefExpr *Ref = nullptr;
13260 if (!VD)
13261 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13262 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13263 PrivateCopies.push_back(VDPrivateRefExpr);
13264 Inits.push_back(VDInitRefExpr);
13265
13266 // We need to add a data sharing attribute for this variable to make sure it
13267 // is correctly captured. A variable that shows up in a use_device_ptr has
13268 // similar properties of a first private variable.
13269 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13270
13271 // Create a mappable component for the list item. List items in this clause
13272 // only need a component.
13273 MVLI.VarBaseDeclarations.push_back(D);
13274 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13275 MVLI.VarComponents.back().push_back(
13276 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013277 }
13278
Samuel Antaocc10b852016-07-28 14:23:26 +000013279 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013280 return nullptr;
13281
Samuel Antaocc10b852016-07-28 14:23:26 +000013282 return OMPUseDevicePtrClause::Create(
13283 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13284 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013285}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013286
13287OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13288 SourceLocation StartLoc,
13289 SourceLocation LParenLoc,
13290 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013291 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013292 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013293 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013294 SourceLocation ELoc;
13295 SourceRange ERange;
13296 Expr *SimpleRefExpr = RefExpr;
13297 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13298 if (Res.second) {
13299 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013300 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013301 }
13302 ValueDecl *D = Res.first;
13303 if (!D)
13304 continue;
13305
13306 QualType Type = D->getType();
13307 // item should be a pointer or array or reference to pointer or array
13308 if (!Type.getNonReferenceType()->isPointerType() &&
13309 !Type.getNonReferenceType()->isArrayType()) {
13310 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13311 << 0 << RefExpr->getSourceRange();
13312 continue;
13313 }
Samuel Antao6890b092016-07-28 14:25:09 +000013314
13315 // Check if the declaration in the clause does not show up in any data
13316 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013317 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013318 if (isOpenMPPrivate(DVar.CKind)) {
13319 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13320 << getOpenMPClauseName(DVar.CKind)
13321 << getOpenMPClauseName(OMPC_is_device_ptr)
13322 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013323 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013324 continue;
13325 }
13326
Alexey Bataeve3727102018-04-18 15:57:46 +000013327 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013328 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013329 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013330 [&ConflictExpr](
13331 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13332 OpenMPClauseKind) -> bool {
13333 ConflictExpr = R.front().getAssociatedExpression();
13334 return true;
13335 })) {
13336 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13337 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13338 << ConflictExpr->getSourceRange();
13339 continue;
13340 }
13341
13342 // Store the components in the stack so that they can be used to check
13343 // against other clauses later on.
13344 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13345 DSAStack->addMappableExpressionComponents(
13346 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13347
13348 // Record the expression we've just processed.
13349 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13350
13351 // Create a mappable component for the list item. List items in this clause
13352 // only need a component. We use a null declaration to signal fields in
13353 // 'this'.
13354 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13355 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13356 "Unexpected device pointer expression!");
13357 MVLI.VarBaseDeclarations.push_back(
13358 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13359 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13360 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013361 }
13362
Samuel Antao6890b092016-07-28 14:25:09 +000013363 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013364 return nullptr;
13365
Samuel Antao6890b092016-07-28 14:25:09 +000013366 return OMPIsDevicePtrClause::Create(
13367 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13368 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013369}