blob: d1f857bb78093f4bd128be6c1728dedb47f7a479 [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
10/// \brief 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 Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataev758e55e2013-09-06 18:03:48 +000048/// \brief Stack for tracking declarations used in OpenMP directives and
49/// clauses and their data-sharing attributes.
Alexey Bataev7ace49d2016-05-17 08:55:33 +000050class DSAStackTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000051public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000052 struct DSAVarData final {
53 OpenMPDirectiveKind DKind = OMPD_unknown;
54 OpenMPClauseKind CKind = OMPC_unknown;
55 Expr *RefExpr = nullptr;
56 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000057 SourceLocation ImplicitDSALoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000058 DSAVarData() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000059 };
Alexey Bataev8b427062016-05-25 12:36:08 +000060 typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61 OperatorOffsetTy;
Alexey Bataeved09d242014-05-28 05:53:51 +000062
Alexey Bataev758e55e2013-09-06 18:03:48 +000063private:
Alexey Bataev7ace49d2016-05-17 08:55:33 +000064 struct DSAInfo final {
65 OpenMPClauseKind Attributes = OMPC_unknown;
66 /// Pointer to a reference expression and a flag which shows that the
67 /// variable is marked as lastprivate(true) or not (false).
68 llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000070 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000071 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000073 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000075 typedef llvm::DenseMap<
76 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77 MappedExprComponentsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000078 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79 CriticalsWithHintsTy;
Alexey Bataev8b427062016-05-25 12:36:08 +000080 typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81 DoacrossDependMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000082
Alexey Bataev7ace49d2016-05-17 08:55:33 +000083 struct SharingMapTy final {
Alexey Bataev758e55e2013-09-06 18:03:48 +000084 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +000086 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000087 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000088 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000090 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000092 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +000094 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95 /// get the data (loop counters etc.) about enclosing loop-based construct.
96 /// This data is required during codegen.
97 DoacrossDependMapTy DoacrossDepends;
Alexey Bataev346265e2015-09-25 10:37:12 +000098 /// \brief first argument (Expr *) contains optional argument of the
99 /// 'ordered' clause, the second one is true if the regions has 'ordered'
100 /// clause, false otherwise.
101 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000102 bool NowaitRegion = false;
103 bool CancelRegion = false;
104 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000106 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000108 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109 ConstructLoc(Loc) {}
110 SharingMapTy() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111 };
112
Axel Naumann323862e2016-02-03 10:45:22 +0000113 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114
115 /// \brief Stack of used declaration and their data-sharing attributes.
116 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000117 /// \brief true, if check for DSA must be from parent directive, false, if
118 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000119 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000121 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000122 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000126 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool isForceVarCapturing() const { return ForceCapturing; }
138 void setForceVarCapturing(bool V) { ForceCapturing = V; }
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000141 Scope *CurScope, SourceLocation Loc) {
142 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 }
145
146 void pop() {
147 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148 Stack.pop_back();
149 }
150
Alexey Bataev28c75412015-12-15 08:19:24 +0000151 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153 }
154 const std::pair<OMPCriticalDirective *, llvm::APSInt>
155 getCriticalWithHint(const DeclarationNameInfo &Name) const {
156 auto I = Criticals.find(Name.getAsString());
157 if (I != Criticals.end())
158 return I->second;
159 return std::make_pair(nullptr, llvm::APSInt());
160 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000161 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000162 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000163 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000164 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000165
Alexey Bataev9c821032015-04-30 04:23:23 +0000166 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000167 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000168 /// \brief Check if the specified variable is a loop control variable for
169 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000170 /// \return The index of the loop control variable in the list of associated
171 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000172 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000173 /// \brief Check if the specified variable is a loop control variable for
174 /// parent region.
175 /// \return The index of the loop control variable in the list of associated
176 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000177 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000178 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000180 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000181
Alexey Bataev758e55e2013-09-06 18:03:48 +0000182 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000183 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186 /// \brief Returns data sharing attributes from top of the stack for the
187 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000188 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000190 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any directive which matches \a DPred
193 /// predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000194 DSAVarData hasDSA(ValueDecl *D,
195 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197 bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any innermost directive which
200 /// matches \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000201 DSAVarData
202 hasInnermostDSA(ValueDecl *D,
203 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000206 /// \brief Checks if the specified variables has explicit data-sharing
207 /// attributes which match specified \a CPred predicate at the specified
208 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000210 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000211 unsigned Level, bool NotLastprivate = false);
Samuel Antao4be30e92015-10-02 17:14:03 +0000212
213 /// \brief Returns true if the directive at level \Level matches in the
214 /// specified \a DPred predicate.
215 bool hasExplicitDirective(
216 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217 unsigned Level);
218
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000219 /// \brief Finds a directive which matches specified \a DPred predicate.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000220 bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221 const DeclarationNameInfo &,
222 SourceLocation)> &DPred,
223 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000224
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225 /// \brief Returns currently analyzed directive.
226 OpenMPDirectiveKind getCurrentDirective() const {
227 return Stack.back().Directive;
228 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000229 /// \brief Returns parent directive.
230 OpenMPDirectiveKind getParentDirective() const {
231 if (Stack.size() > 2)
232 return Stack[Stack.size() - 2].Directive;
233 return OMPD_unknown;
234 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000235
236 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000237 void setDefaultDSANone(SourceLocation Loc) {
238 Stack.back().DefaultAttr = DSA_none;
239 Stack.back().DefaultAttrLoc = Loc;
240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000241 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSAShared(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_shared;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246
247 DefaultDataSharingAttributes getDefaultDSA() const {
248 return Stack.back().DefaultAttr;
249 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000250 SourceLocation getDefaultDSALocation() const {
251 return Stack.back().DefaultAttrLoc;
252 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253
Alexey Bataevf29276e2014-06-18 04:14:57 +0000254 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000255 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000256 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000257 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000258 }
259
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000260 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 void setOrderedRegion(bool IsOrdered, Expr *Param) {
262 Stack.back().OrderedRegion.setInt(IsOrdered);
263 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000264 }
265 /// \brief Returns true, if parent region is ordered (has associated
266 /// 'ordered' clause), false - otherwise.
267 bool isParentOrderedRegion() const {
268 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000269 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000270 return false;
271 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000272 /// \brief Returns optional parameter for the ordered region.
273 Expr *getParentOrderedRegionParam() const {
274 if (Stack.size() > 2)
275 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276 return nullptr;
277 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000278 /// \brief Marks current region as nowait (it has a 'nowait' clause).
279 void setNowaitRegion(bool IsNowait = true) {
280 Stack.back().NowaitRegion = IsNowait;
281 }
282 /// \brief Returns true, if parent region is nowait (has associated
283 /// 'nowait' clause), false - otherwise.
284 bool isParentNowaitRegion() const {
285 if (Stack.size() > 2)
286 return Stack[Stack.size() - 2].NowaitRegion;
287 return false;
288 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000289 /// \brief Marks parent region as cancel region.
290 void setParentCancelRegion(bool Cancel = true) {
291 if (Stack.size() > 2)
292 Stack[Stack.size() - 2].CancelRegion =
293 Stack[Stack.size() - 2].CancelRegion || Cancel;
294 }
295 /// \brief Return true if current region has inner cancel construct.
296 bool isCancelRegion() const {
297 return Stack.back().CancelRegion;
298 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000299
Alexey Bataev9c821032015-04-30 04:23:23 +0000300 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000302 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000304
Alexey Bataev13314bf2014-10-09 04:18:56 +0000305 /// \brief Marks current target region as one with closely nested teams
306 /// region.
307 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310 }
311 /// \brief Returns true, if current region has closely nested teams region.
312 bool hasInnerTeamsRegion() const {
313 return getInnerTeamsRegionLoc().isValid();
314 }
315 /// \brief Returns location of the nested teams region (if any).
316 SourceLocation getInnerTeamsRegionLoc() const {
317 if (Stack.size() > 1)
318 return Stack.back().InnerTeamsRegionLoc;
319 return SourceLocation();
320 }
321
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000322 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000324 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000325
Samuel Antao90927002016-04-26 14:54:23 +0000326 // Do the check specified in \a Check to all component lists and return true
327 // if any issue is found.
328 bool checkMappableExprComponentListsForDecl(
329 ValueDecl *VD, bool CurrentRegionOnly,
330 const llvm::function_ref<bool(
331 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000332 auto SI = Stack.rbegin();
333 auto SE = Stack.rend();
334
335 if (SI == SE)
336 return false;
337
338 if (CurrentRegionOnly) {
339 SE = std::next(SI);
340 } else {
341 ++SI;
342 }
343
344 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000345 auto MI = SI->MappedExprComponents.find(VD);
346 if (MI != SI->MappedExprComponents.end())
347 for (auto &L : MI->second)
348 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000350 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000351 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000352 }
353
Samuel Antao90927002016-04-26 14:54:23 +0000354 // Create a new mappable expression component list associated with a given
355 // declaration and initialize it with the provided list of components.
356 void addMappableExpressionComponents(
357 ValueDecl *VD,
358 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359 assert(Stack.size() > 1 &&
360 "Not expecting to retrieve components from a empty stack!");
361 auto &MEC = Stack.back().MappedExprComponents[VD];
362 // Create new entry and append the new components there.
363 MEC.resize(MEC.size() + 1);
364 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000365 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000366
367 unsigned getNestingLevel() const {
368 assert(Stack.size() > 1);
369 return Stack.size() - 2;
370 }
Alexey Bataev8b427062016-05-25 12:36:08 +0000371 void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372 assert(Stack.size() > 2);
373 assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374 Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375 }
376 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
377 getDoacrossDependClauses() const {
378 assert(Stack.size() > 1);
379 if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380 auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381 return llvm::make_range(Ref.begin(), Ref.end());
382 }
383 return llvm::make_range(Stack[0].DoacrossDepends.end(),
384 Stack[0].DoacrossDepends.end());
385 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000388 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000390}
Alexey Bataeved09d242014-05-28 05:53:51 +0000391} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000393static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394 auto *VD = dyn_cast<VarDecl>(D);
395 auto *FD = dyn_cast<FieldDecl>(D);
396 if (VD != nullptr) {
397 VD = VD->getCanonicalDecl();
398 D = VD;
399 } else {
400 assert(FD);
401 FD = FD->getCanonicalDecl();
402 D = FD;
403 }
404 return D;
405}
406
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000407DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000408 ValueDecl *D) {
409 D = getCanonicalDecl(D);
410 auto *VD = dyn_cast<VarDecl>(D);
411 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000412 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000413 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000414 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415 // in a region but not in construct]
416 // File-scope or namespace-scope variables referenced in called routines
417 // in the region are shared unless they appear in a threadprivate
418 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000419 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000420 DVar.CKind = OMPC_shared;
421
422 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423 // in a region but not in construct]
424 // Variables with static storage duration that are declared in called
425 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000426 if (VD && VD->hasGlobalStorage())
427 DVar.CKind = OMPC_shared;
428
429 // Non-static data members are shared by default.
430 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000431 DVar.CKind = OMPC_shared;
432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 return DVar;
434 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438 // in a Construct, C/C++, predetermined, p.1]
439 // Variables with automatic storage duration that are declared in a scope
440 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000441 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000443 DVar.CKind = OMPC_private;
444 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000445 }
446
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 // Explicitly specified attributes and local variables with predetermined
448 // attributes.
449 if (Iter->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000450 DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000451 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000453 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 return DVar;
455 }
456
457 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458 // in a Construct, C/C++, implicitly determined, p.1]
459 // In a parallel or task construct, the data-sharing attributes of these
460 // variables are determined by the default clause, if present.
461 switch (Iter->DefaultAttr) {
462 case DSA_shared:
463 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000465 return DVar;
466 case DSA_none:
467 return DVar;
468 case DSA_unspecified:
469 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470 // in a Construct, implicitly determined, p.2]
471 // In a parallel construct, if no default clause is present, these
472 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000473 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000474 if (isOpenMPParallelDirective(DVar.DKind) ||
475 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DVar.CKind = OMPC_shared;
477 return DVar;
478 }
479
480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481 // in a Construct, implicitly determined, p.4]
482 // In a task construct, if no default clause is present, a variable that in
483 // the enclosing context is determined to be shared by all implicit tasks
484 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000485 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000487 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000489 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000490 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 // In a task construct, if no default clause is present, a variable
492 // whose data-sharing attribute is not determined by the rules above is
493 // firstprivate.
494 DVarTemp = getDSA(I, D);
495 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000496 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000497 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498 return DVar;
499 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000500 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000504 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505 return DVar;
506 }
507 }
508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, implicitly determined, p.3]
510 // For constructs other than task, if no default clause is present, these
511 // variables inherit their data-sharing attributes from the enclosing
512 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000513 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000514}
515
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000516Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000517 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000518 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000519 auto It = Stack.back().AlignedMap.find(D);
520 if (It == Stack.back().AlignedMap.end()) {
521 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522 Stack.back().AlignedMap[D] = NewDE;
523 return nullptr;
524 } else {
525 assert(It->second && "Unexpected nullptr expr in the aligned map");
526 return It->second;
527 }
528 return nullptr;
529}
530
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000532 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000533 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000534 Stack.back().LCVMap.insert(
535 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000536}
537
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000539 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000540 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543}
544
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000545DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000547 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000550 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551}
552
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000553ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000554 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555 if (Stack[Stack.size() - 2].LCVMap.size() < I)
556 return nullptr;
557 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000558 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000559 return Pair.first;
560 }
561 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000562}
563
Alexey Bataev90c228f2016-02-08 09:29:13 +0000564void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000566 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 if (A == OMPC_threadprivate) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000568 auto &Data = Stack[0].SharingMap[D];
569 Data.Attributes = A;
570 Data.RefExpr.setPointer(E);
571 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572 } else {
573 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000574 auto &Data = Stack.back().SharingMap[D];
575 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578 (isLoopControlVariable(D).first && A == OMPC_private));
579 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580 Data.RefExpr.setInt(/*IntVal=*/true);
581 return;
582 }
583 const bool IsLastprivate =
584 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585 Data.Attributes = A;
586 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587 Data.PrivateCopy = PrivateCopy;
588 if (PrivateCopy) {
589 auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590 Data.Attributes = A;
591 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592 Data.PrivateCopy = nullptr;
593 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
595}
596
Alexey Bataeved09d242014-05-28 05:53:51 +0000597bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000599 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000601 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000603 ++I;
604 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000605 if (I == E)
606 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000607 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000608 Scope *CurScope = getCurScope();
609 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000610 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000611 }
612 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000614 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615}
616
Alexey Bataev39f915b82015-05-08 10:41:21 +0000617/// \brief Build a variable declaration for OpenMP loop iteration variable.
618static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000619 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000620 DeclContext *DC = SemaRef.CurContext;
621 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623 VarDecl *Decl =
624 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000625 if (Attrs) {
626 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627 I != E; ++I)
628 Decl->addAttr(*I);
629 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000630 Decl->setImplicit();
631 return Decl;
632}
633
634static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635 SourceLocation Loc,
636 bool RefersToCapture = false) {
637 D->setReferenced();
638 D->markUsed(S.Context);
639 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640 SourceLocation(), D, RefersToCapture, Loc, Ty,
641 VK_LValue);
642}
643
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000644DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646 DSAVarData DVar;
647
648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649 // in a Construct, C/C++, predetermined, p.1]
650 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 auto *VD = dyn_cast<VarDecl>(D);
652 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000654 SemaRef.getLangOpts().OpenMPUseTLS &&
655 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000656 (VD && VD->getStorageClass() == SC_Register &&
657 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000659 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000660 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000661 }
662 if (Stack[0].SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000663 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 DVar.CKind = OMPC_threadprivate;
665 return DVar;
666 }
667
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000668 if (Stack.size() == 1) {
669 // Not in OpenMP execution region and top scope was already checked.
670 return DVar;
671 }
672
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000674 // in a Construct, C/C++, predetermined, p.4]
675 // Static data members are shared.
676 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677 // in a Construct, C/C++, predetermined, p.7]
678 // Variables with static storage duration that are declared in a scope
679 // inside the construct are shared.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000680 auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000681 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000682 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000683 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000684 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000686 DVar.CKind = OMPC_shared;
687 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 }
689
690 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000691 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694 // in a Construct, C/C++, predetermined, p.6]
695 // Variables with const qualified type having no mutable member are
696 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000697 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000698 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000699 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700 if (auto *CTD = CTSD->getSpecializedTemplate())
701 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000703 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705 // Variables with const-qualified type having no mutable member may be
706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000707 DSAVarData DVarTemp = hasDSA(
708 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000710 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711 return DVar;
712
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 DVar.CKind = OMPC_shared;
714 return DVar;
715 }
716
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 // Explicitly specified attributes and local variables with predetermined
718 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000724 auto I = std::prev(StartI);
725 if (I->SharingMap.count(D)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000726 DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
Alexey Bataev90c228f2016-02-08 09:29:13 +0000727 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DVar.CKind = I->SharingMap[D].Attributes;
729 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000730 }
731
732 return DVar;
733}
734
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736 bool FromParent) {
737 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000738 auto StartI = Stack.rbegin();
739 auto EndI = std::prev(Stack.rend());
740 if (FromParent && StartI != EndI) {
741 StartI = std::next(StartI);
742 }
743 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000744}
745
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000746DSAStackTy::DSAVarData
747DSAStackTy::hasDSA(ValueDecl *D,
748 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000751 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000753 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000754 if (FromParent && StartI != EndI) {
755 StartI = std::next(StartI);
756 }
757 for (auto I = StartI, EE = EndI; I != EE; ++I) {
758 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000759 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000760 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000761 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000762 return DVar;
763 }
764 return DSAVarData();
765}
766
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000767DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000771 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000772 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000773 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000774 if (FromParent && StartI != EndI) {
775 StartI = std::next(StartI);
776 }
777 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000778 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000779 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000780 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000781 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000782 return DVar;
783 return DSAVarData();
784 }
785 return DSAVarData();
786}
787
Alexey Bataevaac108a2015-06-23 04:51:00 +0000788bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000789 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000790 unsigned Level, bool NotLastprivate) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000791 if (CPred(ClauseKindMode))
792 return true;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000793 D = getCanonicalDecl(D);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000794 auto StartI = std::next(Stack.begin());
795 auto EndI = Stack.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000796 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000797 return false;
798 std::advance(StartI, Level);
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000799 return (StartI->SharingMap.count(D) > 0) &&
800 StartI->SharingMap[D].RefExpr.getPointer() &&
801 CPred(StartI->SharingMap[D].Attributes) &&
802 (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +0000803}
804
Samuel Antao4be30e92015-10-02 17:14:03 +0000805bool DSAStackTy::hasExplicitDirective(
806 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807 unsigned Level) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000808 auto StartI = std::next(Stack.begin());
809 auto EndI = Stack.end();
Samuel Antao4be30e92015-10-02 17:14:03 +0000810 if (std::distance(StartI, EndI) <= (int)Level)
811 return false;
812 std::advance(StartI, Level);
813 return DPred(StartI->Directive);
814}
815
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000816bool DSAStackTy::hasDirective(
817 const llvm::function_ref<bool(OpenMPDirectiveKind,
818 const DeclarationNameInfo &, SourceLocation)>
819 &DPred,
820 bool FromParent) {
Samuel Antaof0d79752016-05-27 15:21:27 +0000821 // We look only in the enclosing region.
822 if (Stack.size() < 2)
823 return false;
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000824 auto StartI = std::next(Stack.rbegin());
825 auto EndI = std::prev(Stack.rend());
826 if (FromParent && StartI != EndI) {
827 StartI = std::next(StartI);
828 }
829 for (auto I = StartI, EE = EndI; I != EE; ++I) {
830 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831 return true;
832 }
833 return false;
834}
835
Alexey Bataev758e55e2013-09-06 18:03:48 +0000836void Sema::InitDataSharingAttributesStack() {
837 VarDataSharingAttributesStack = new DSAStackTy(*this);
838}
839
840#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000842bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000843 assert(LangOpts.OpenMP && "OpenMP is not allowed");
844
845 auto &Ctx = getASTContext();
846 bool IsByRef = true;
847
848 // Find the directive that is associated with the provided scope.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000850
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000852 // This table summarizes how a given variable should be passed to the device
853 // given its type and the clauses where it appears. This table is based on
854 // the description in OpenMP 4.5 [2.10.4, target Construct] and
855 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856 //
857 // =========================================================================
858 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
859 // | |(tofrom:scalar)| | pvt | | | |
860 // =========================================================================
861 // | scl | | | | - | | bycopy|
862 // | scl | | - | x | - | - | bycopy|
863 // | scl | | x | - | - | - | null |
864 // | scl | x | | | - | | byref |
865 // | scl | x | - | x | - | - | bycopy|
866 // | scl | x | x | - | - | - | null |
867 // | scl | | - | - | - | x | byref |
868 // | scl | x | - | - | - | x | byref |
869 //
870 // | agg | n.a. | | | - | | byref |
871 // | agg | n.a. | - | x | - | - | byref |
872 // | agg | n.a. | x | - | - | - | null |
873 // | agg | n.a. | - | - | - | x | byref |
874 // | agg | n.a. | - | - | - | x[] | byref |
875 //
876 // | ptr | n.a. | | | - | | bycopy|
877 // | ptr | n.a. | - | x | - | - | bycopy|
878 // | ptr | n.a. | x | - | - | - | null |
879 // | ptr | n.a. | - | - | - | x | byref |
880 // | ptr | n.a. | - | - | - | x[] | bycopy|
881 // | ptr | n.a. | - | - | x | | bycopy|
882 // | ptr | n.a. | - | - | x | x | bycopy|
883 // | ptr | n.a. | - | - | x | x[] | bycopy|
884 // =========================================================================
885 // Legend:
886 // scl - scalar
887 // ptr - pointer
888 // agg - aggregate
889 // x - applies
890 // - - invalid in this combination
891 // [] - mapped with an array section
892 // byref - should be mapped by reference
893 // byval - should be mapped by value
894 // null - initialize a local variable to null on the device
895 //
896 // Observations:
897 // - All scalar declarations that show up in a map clause have to be passed
898 // by reference, because they may have been mapped in the enclosing data
899 // environment.
900 // - If the scalar value does not fit the size of uintptr, it has to be
901 // passed by reference, regardless the result in the table above.
902 // - For pointers mapped by value that have either an implicit map or an
903 // array section, the runtime library may pass the NULL value to the
904 // device instead of the value passed to it by the compiler.
905
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000906
907 if (Ty->isReferenceType())
908 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000909
910 // Locate map clauses and see if the variable being captured is referred to
911 // in any of those clauses. Here we only care about variables, not fields,
912 // because fields are part of aggregates.
913 bool IsVariableUsedInMapClause = false;
914 bool IsVariableAssociatedWithSection = false;
915
916 DSAStack->checkMappableExprComponentListsForDecl(
917 D, /*CurrentRegionOnly=*/true,
918 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
919 MapExprComponents) {
920
921 auto EI = MapExprComponents.rbegin();
922 auto EE = MapExprComponents.rend();
923
924 assert(EI != EE && "Invalid map expression!");
925
926 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
927 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
928
929 ++EI;
930 if (EI == EE)
931 return false;
932
933 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
934 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
935 isa<MemberExpr>(EI->getAssociatedExpression())) {
936 IsVariableAssociatedWithSection = true;
937 // There is nothing more we need to know about this variable.
938 return true;
939 }
940
941 // Keep looking for more map info.
942 return false;
943 });
944
945 if (IsVariableUsedInMapClause) {
946 // If variable is identified in a map clause it is always captured by
947 // reference except if it is a pointer that is dereferenced somehow.
948 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
949 } else {
950 // By default, all the data that has a scalar type is mapped by copy.
951 IsByRef = !Ty->isScalarType();
952 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000953 }
954
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000955 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
956 IsByRef = !DSAStack->hasExplicitDSA(
957 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
958 Level, /*NotLastprivate=*/true);
959 }
960
Samuel Antao86ace552016-04-27 22:40:57 +0000961 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000962 // and alignment, because the runtime library only deals with uintptr types.
963 // If it does not fit the uintptr size, we need to pass the data by reference
964 // instead.
965 if (!IsByRef &&
966 (Ctx.getTypeSizeInChars(Ty) >
967 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000968 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000969 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000970 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000971
972 return IsByRef;
973}
974
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000975unsigned Sema::getOpenMPNestingLevel() const {
976 assert(getLangOpts().OpenMP);
977 return DSAStack->getNestingLevel();
978}
979
Alexey Bataev90c228f2016-02-08 09:29:13 +0000980VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000981 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000982 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000983
984 // If we are attempting to capture a global variable in a directive with
985 // 'target' we return true so that this global is also mapped to the device.
986 //
987 // FIXME: If the declaration is enclosed in a 'declare target' directive,
988 // then it should not be captured. Therefore, an extra check has to be
989 // inserted here once support for 'declare target' is added.
990 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000991 auto *VD = dyn_cast<VarDecl>(D);
992 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000993 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000994 !DSAStack->isClauseParsingMode())
995 return VD;
Samuel Antaof0d79752016-05-27 15:21:27 +0000996 if (DSAStack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000997 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
998 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000999 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +00001000 },
Alexey Bataev90c228f2016-02-08 09:29:13 +00001001 false))
1002 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +00001003 }
1004
Alexey Bataev48977c32015-08-04 08:10:48 +00001005 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1006 (!DSAStack->isClauseParsingMode() ||
1007 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001008 auto &&Info = DSAStack->isLoopControlVariable(D);
1009 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001010 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001011 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001012 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001013 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001014 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001015 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001016 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001017 DVarPrivate = DSAStack->hasDSA(
1018 D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1019 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001020 if (DVarPrivate.CKind != OMPC_unknown)
1021 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001022 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001023 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001024}
1025
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001026bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001027 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001029 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001030}
1031
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001032bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001033 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1034 // Return true if the current level is no longer enclosed in a target region.
1035
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001036 auto *VD = dyn_cast<VarDecl>(D);
1037 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001038 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1039 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001040}
1041
Alexey Bataeved09d242014-05-28 05:53:51 +00001042void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001043
1044void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1045 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 Scope *CurScope, SourceLocation Loc) {
1047 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001048 PushExpressionEvaluationContext(PotentiallyEvaluated);
1049}
1050
Alexey Bataevaac108a2015-06-23 04:51:00 +00001051void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1052 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001053}
1054
Alexey Bataevaac108a2015-06-23 04:51:00 +00001055void Sema::EndOpenMPClause() {
1056 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001057}
1058
Alexey Bataev758e55e2013-09-06 18:03:48 +00001059void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001060 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1061 // A variable of class type (or array thereof) that appears in a lastprivate
1062 // clause requires an accessible, unambiguous default constructor for the
1063 // class type, unless the list item is also specified in a firstprivate
1064 // clause.
1065 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001066 for (auto *C : D->clauses()) {
1067 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1068 SmallVector<Expr *, 8> PrivateCopies;
1069 for (auto *DE : Clause->varlists()) {
1070 if (DE->isValueDependent() || DE->isTypeDependent()) {
1071 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001072 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001073 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001074 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001075 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1076 QualType Type = VD->getType().getNonReferenceType();
1077 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001078 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001079 // Generate helper private variable and initialize it with the
1080 // default value. The address of the original variable is replaced
1081 // by the address of the new private variable in CodeGen. This new
1082 // variable is not added to IdResolver, so the code in the OpenMP
1083 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001084 auto *VDPrivate = buildVarDecl(
1085 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001086 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001087 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1088 if (VDPrivate->isInvalidDecl())
1089 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 PrivateCopies.push_back(buildDeclRefExpr(
1091 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001092 } else {
1093 // The variable is also a firstprivate, so initialization sequence
1094 // for private copy is generated already.
1095 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001096 }
1097 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001098 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001099 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001100 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001101 }
1102 }
1103 }
1104
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105 DSAStack->pop();
1106 DiscardCleanupsInEvaluationContext();
1107 PopExpressionEvaluationContext();
1108}
1109
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001110static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1111 Expr *NumIterations, Sema &SemaRef,
1112 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001113
Alexey Bataeva769e072013-03-22 06:34:35 +00001114namespace {
1115
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116class VarDeclFilterCCC : public CorrectionCandidateCallback {
1117private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001118 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001119
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001121 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001122 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001123 NamedDecl *ND = Candidate.getCorrectionDecl();
1124 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1125 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001126 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1127 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001128 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001129 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001130 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001132
1133class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1134private:
1135 Sema &SemaRef;
1136
1137public:
1138 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1139 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1140 NamedDecl *ND = Candidate.getCorrectionDecl();
1141 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1142 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1143 SemaRef.getCurScope());
1144 }
1145 return false;
1146 }
1147};
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150
1151ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1152 CXXScopeSpec &ScopeSpec,
1153 const DeclarationNameInfo &Id) {
1154 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1155 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1156
1157 if (Lookup.isAmbiguous())
1158 return ExprError();
1159
1160 VarDecl *VD;
1161 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001162 if (TypoCorrection Corrected = CorrectTypo(
1163 Id, LookupOrdinaryName, CurScope, nullptr,
1164 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001165 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001166 PDiag(Lookup.empty()
1167 ? diag::err_undeclared_var_use_suggest
1168 : diag::err_omp_expected_var_arg_suggest)
1169 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001170 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001172 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1173 : diag::err_omp_expected_var_arg)
1174 << Id.getName();
1175 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001177 } else {
1178 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001179 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1181 return ExprError();
1182 }
1183 }
1184 Lookup.suppressDiagnostics();
1185
1186 // OpenMP [2.9.2, Syntax, C/C++]
1187 // Variables must be file-scope, namespace-scope, or static block-scope.
1188 if (!VD->hasGlobalStorage()) {
1189 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001190 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1191 bool IsDecl =
1192 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001193 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001196 return ExprError();
1197 }
1198
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1200 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1202 // A threadprivate directive for file-scope variables must appear outside
1203 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1205 !getCurLexicalContext()->isTranslationUnit()) {
1206 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208 bool IsDecl =
1209 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(),
1211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001213 return ExprError();
1214 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001215 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1216 // A threadprivate directive for static class member variables must appear
1217 // in the class definition, in the same scope in which the member
1218 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001219 if (CanonicalVD->isStaticDataMember() &&
1220 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1221 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001222 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1223 bool IsDecl =
1224 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1225 Diag(VD->getLocation(),
1226 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1227 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001228 return ExprError();
1229 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1231 // A threadprivate directive for namespace-scope variables must appear
1232 // outside any definition or declaration other than the namespace
1233 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001234 if (CanonicalVD->getDeclContext()->isNamespace() &&
1235 (!getCurLexicalContext()->isFileContext() ||
1236 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1237 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001238 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1239 bool IsDecl =
1240 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1241 Diag(VD->getLocation(),
1242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1243 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001244 return ExprError();
1245 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001246 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1247 // A threadprivate directive for static block-scope variables must appear
1248 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001249 if (CanonicalVD->isStaticLocal() && CurScope &&
1250 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001251 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1253 bool IsDecl =
1254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1255 Diag(VD->getLocation(),
1256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1257 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001258 return ExprError();
1259 }
1260
1261 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1262 // A threadprivate directive must lexically precede all references to any
1263 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001264 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001265 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001267 return ExprError();
1268 }
1269
1270 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001271 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1272 SourceLocation(), VD,
1273 /*RefersToEnclosingVariableOrCapture=*/false,
1274 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001275}
1276
Alexey Bataeved09d242014-05-28 05:53:51 +00001277Sema::DeclGroupPtrTy
1278Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1279 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001280 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001281 CurContext->addDecl(D);
1282 return DeclGroupPtrTy::make(DeclGroupRef(D));
1283 }
David Blaikie0403cb12016-01-15 23:43:25 +00001284 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001285}
1286
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001287namespace {
1288class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1289 Sema &SemaRef;
1290
1291public:
1292 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1293 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1294 if (VD->hasLocalStorage()) {
1295 SemaRef.Diag(E->getLocStart(),
1296 diag::err_omp_local_var_in_threadprivate_init)
1297 << E->getSourceRange();
1298 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1299 << VD << VD->getSourceRange();
1300 return true;
1301 }
1302 }
1303 return false;
1304 }
1305 bool VisitStmt(const Stmt *S) {
1306 for (auto Child : S->children()) {
1307 if (Child && Visit(Child))
1308 return true;
1309 }
1310 return false;
1311 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001312 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001313};
1314} // namespace
1315
Alexey Bataeved09d242014-05-28 05:53:51 +00001316OMPThreadPrivateDecl *
1317Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001318 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001319 for (auto &RefExpr : VarList) {
1320 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001321 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1322 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001323
Alexey Bataev376b4a42016-02-09 09:41:09 +00001324 // Mark variable as used.
1325 VD->setReferenced();
1326 VD->markUsed(Context);
1327
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001328 QualType QType = VD->getType();
1329 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1330 // It will be analyzed later.
1331 Vars.push_back(DE);
1332 continue;
1333 }
1334
Alexey Bataeva769e072013-03-22 06:34:35 +00001335 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1336 // A threadprivate variable must not have an incomplete type.
1337 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001338 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001339 continue;
1340 }
1341
1342 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1343 // A threadprivate variable must not have a reference type.
1344 if (VD->getType()->isReferenceType()) {
1345 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001346 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1347 bool IsDecl =
1348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1349 Diag(VD->getLocation(),
1350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1351 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001352 continue;
1353 }
1354
Samuel Antaof8b50122015-07-13 22:54:53 +00001355 // Check if this is a TLS variable. If TLS is not being supported, produce
1356 // the corresponding diagnostic.
1357 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1358 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1359 getLangOpts().OpenMPUseTLS &&
1360 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001361 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1362 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001363 Diag(ILoc, diag::err_omp_var_thread_local)
1364 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001365 bool IsDecl =
1366 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1367 Diag(VD->getLocation(),
1368 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1369 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001370 continue;
1371 }
1372
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001373 // Check if initial value of threadprivate variable reference variable with
1374 // local storage (it is not supported by runtime).
1375 if (auto Init = VD->getAnyInitializer()) {
1376 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001377 if (Checker.Visit(Init))
1378 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001379 }
1380
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001382 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001383 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1384 Context, SourceRange(Loc, Loc)));
1385 if (auto *ML = Context.getASTMutationListener())
1386 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001387 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001388 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001389 if (!Vars.empty()) {
1390 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1391 Vars);
1392 D->setAccess(AS_public);
1393 }
1394 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001395}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001396
Alexey Bataev7ff55242014-06-19 09:13:45 +00001397static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001398 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001399 bool IsLoopIterVar = false) {
1400 if (DVar.RefExpr) {
1401 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1402 << getOpenMPClauseName(DVar.CKind);
1403 return;
1404 }
1405 enum {
1406 PDSA_StaticMemberShared,
1407 PDSA_StaticLocalVarShared,
1408 PDSA_LoopIterVarPrivate,
1409 PDSA_LoopIterVarLinear,
1410 PDSA_LoopIterVarLastprivate,
1411 PDSA_ConstVarShared,
1412 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001413 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001414 PDSA_LocalVarPrivate,
1415 PDSA_Implicit
1416 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001417 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001418 auto ReportLoc = D->getLocation();
1419 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001420 if (IsLoopIterVar) {
1421 if (DVar.CKind == OMPC_private)
1422 Reason = PDSA_LoopIterVarPrivate;
1423 else if (DVar.CKind == OMPC_lastprivate)
1424 Reason = PDSA_LoopIterVarLastprivate;
1425 else
1426 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001427 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1428 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001429 Reason = PDSA_TaskVarFirstprivate;
1430 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001431 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001432 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001433 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001434 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001436 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001437 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001438 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001439 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001440 ReportHint = true;
1441 Reason = PDSA_LocalVarPrivate;
1442 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001443 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001445 << Reason << ReportHint
1446 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1447 } else if (DVar.ImplicitDSALoc.isValid()) {
1448 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1449 << getOpenMPClauseName(DVar.CKind);
1450 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001451}
1452
Alexey Bataev758e55e2013-09-06 18:03:48 +00001453namespace {
1454class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1455 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001456 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001457 bool ErrorFound;
1458 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001459 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001461
Alexey Bataev758e55e2013-09-06 18:03:48 +00001462public:
1463 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001464 if (E->isTypeDependent() || E->isValueDependent() ||
1465 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1466 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001467 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001469 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1470 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001471
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 auto DVar = Stack->getTopDSA(VD, false);
1473 // Check if the variable has explicit DSA set and stop analysis if it so.
1474 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 auto ELoc = E->getExprLoc();
1477 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478 // The default(none) clause requires that each variable that is referenced
1479 // in the construct, and does not have a predetermined data-sharing
1480 // attribute, must have its data-sharing attribute explicitly determined
1481 // by being listed in a data-sharing attribute clause.
1482 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001483 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001484 VarsWithInheritedDSA.count(VD) == 0) {
1485 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486 return;
1487 }
1488
1489 // OpenMP [2.9.3.6, Restrictions, p.2]
1490 // A list item that appears in a reduction clause of the innermost
1491 // enclosing worksharing or parallel construct may not be accessed in an
1492 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001493 DVar = Stack->hasInnermostDSA(
1494 VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1495 [](OpenMPDirectiveKind K) -> bool {
1496 return isOpenMPParallelDirective(K) ||
1497 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1498 },
1499 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001500 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001501 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001502 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1503 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001504 return;
1505 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001506
1507 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001508 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001509 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1510 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001512 }
1513 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001514 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001515 if (E->isTypeDependent() || E->isValueDependent() ||
1516 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1517 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001518 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1519 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1520 auto DVar = Stack->getTopDSA(FD, false);
1521 // Check if the variable has explicit DSA set and stop analysis if it
1522 // so.
1523 if (DVar.RefExpr)
1524 return;
1525
1526 auto ELoc = E->getExprLoc();
1527 auto DKind = Stack->getCurrentDirective();
1528 // OpenMP [2.9.3.6, Restrictions, p.2]
1529 // A list item that appears in a reduction clause of the innermost
1530 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001531 // an explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001532 DVar = Stack->hasInnermostDSA(
1533 FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1534 [](OpenMPDirectiveKind K) -> bool {
1535 return isOpenMPParallelDirective(K) ||
1536 isOpenMPWorksharingDirective(K) ||
1537 isOpenMPTeamsDirective(K);
1538 },
1539 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001540 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001541 ErrorFound = true;
1542 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1543 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1544 return;
1545 }
1546
1547 // Define implicit data-sharing attributes for task.
1548 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001549 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1550 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001551 ImplicitFirstprivate.push_back(E);
1552 }
1553 }
1554 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001555 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001556 for (auto *C : S->clauses()) {
1557 // Skip analysis of arguments of implicitly defined firstprivate clause
1558 // for task directives.
1559 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1560 for (auto *CC : C->children()) {
1561 if (CC)
1562 Visit(CC);
1563 }
1564 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001565 }
1566 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001567 for (auto *C : S->children()) {
1568 if (C && !isa<OMPExecutableDirective>(C))
1569 Visit(C);
1570 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001571 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001572
1573 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001574 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001575 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001576 return VarsWithInheritedDSA;
1577 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001578
Alexey Bataev7ff55242014-06-19 09:13:45 +00001579 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1580 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001581};
Alexey Bataeved09d242014-05-28 05:53:51 +00001582} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001583
Alexey Bataevbae9a792014-06-27 10:37:06 +00001584void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 switch (DKind) {
1586 case OMPD_parallel: {
1587 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001588 QualType KmpInt32PtrTy =
1589 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001590 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001591 std::make_pair(".global_tid.", KmpInt32PtrTy),
1592 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1593 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001594 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001595 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1596 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001597 break;
1598 }
1599 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001600 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001601 std::make_pair(StringRef(), QualType()) // __context with shared vars
1602 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001603 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1604 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001605 break;
1606 }
1607 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001608 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001609 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001610 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001611 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1612 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001613 break;
1614 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001615 case OMPD_for_simd: {
1616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(StringRef(), QualType()) // __context with shared vars
1618 };
1619 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1620 Params);
1621 break;
1622 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001623 case OMPD_sections: {
1624 Sema::CapturedParamNameType Params[] = {
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001629 break;
1630 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001631 case OMPD_section: {
1632 Sema::CapturedParamNameType Params[] = {
1633 std::make_pair(StringRef(), QualType()) // __context with shared vars
1634 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001635 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1636 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001637 break;
1638 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001639 case OMPD_single: {
1640 Sema::CapturedParamNameType Params[] = {
1641 std::make_pair(StringRef(), QualType()) // __context with shared vars
1642 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001643 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1644 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001645 break;
1646 }
Alexander Musman80c22892014-07-17 08:54:58 +00001647 case OMPD_master: {
1648 Sema::CapturedParamNameType Params[] = {
1649 std::make_pair(StringRef(), QualType()) // __context with shared vars
1650 };
1651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1652 Params);
1653 break;
1654 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001655 case OMPD_critical: {
1656 Sema::CapturedParamNameType Params[] = {
1657 std::make_pair(StringRef(), QualType()) // __context with shared vars
1658 };
1659 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1660 Params);
1661 break;
1662 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001663 case OMPD_parallel_for: {
1664 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001665 QualType KmpInt32PtrTy =
1666 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001667 Sema::CapturedParamNameType Params[] = {
1668 std::make_pair(".global_tid.", KmpInt32PtrTy),
1669 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
1674 break;
1675 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001676 case OMPD_parallel_for_simd: {
1677 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001678 QualType KmpInt32PtrTy =
1679 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001680 Sema::CapturedParamNameType Params[] = {
1681 std::make_pair(".global_tid.", KmpInt32PtrTy),
1682 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1683 std::make_pair(StringRef(), QualType()) // __context with shared vars
1684 };
1685 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1686 Params);
1687 break;
1688 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001689 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001690 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001691 QualType KmpInt32PtrTy =
1692 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001693 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001694 std::make_pair(".global_tid.", KmpInt32PtrTy),
1695 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001702 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001703 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001704 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1705 FunctionProtoType::ExtProtoInfo EPI;
1706 EPI.Variadic = true;
1707 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001709 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001710 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1711 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1712 std::make_pair(".copy_fn.",
1713 Context.getPointerType(CopyFnType).withConst()),
1714 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001715 std::make_pair(StringRef(), QualType()) // __context with shared vars
1716 };
1717 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1718 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001719 // Mark this captured region as inlined, because we don't use outlined
1720 // function directly.
1721 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1722 AlwaysInlineAttr::CreateImplicit(
1723 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001724 break;
1725 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001726 case OMPD_ordered: {
1727 Sema::CapturedParamNameType Params[] = {
1728 std::make_pair(StringRef(), QualType()) // __context with shared vars
1729 };
1730 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1731 Params);
1732 break;
1733 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001734 case OMPD_atomic: {
1735 Sema::CapturedParamNameType Params[] = {
1736 std::make_pair(StringRef(), QualType()) // __context with shared vars
1737 };
1738 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1739 Params);
1740 break;
1741 }
Michael Wong65f367f2015-07-21 13:44:28 +00001742 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001743 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001744 case OMPD_target_parallel:
1745 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001746 Sema::CapturedParamNameType Params[] = {
1747 std::make_pair(StringRef(), QualType()) // __context with shared vars
1748 };
1749 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1750 Params);
1751 break;
1752 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001753 case OMPD_teams: {
1754 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001755 QualType KmpInt32PtrTy =
1756 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001757 Sema::CapturedParamNameType Params[] = {
1758 std::make_pair(".global_tid.", KmpInt32PtrTy),
1759 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1760 std::make_pair(StringRef(), QualType()) // __context with shared vars
1761 };
1762 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1763 Params);
1764 break;
1765 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001766 case OMPD_taskgroup: {
1767 Sema::CapturedParamNameType Params[] = {
1768 std::make_pair(StringRef(), QualType()) // __context with shared vars
1769 };
1770 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1771 Params);
1772 break;
1773 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001774 case OMPD_taskloop:
1775 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001776 QualType KmpInt32Ty =
1777 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1778 QualType KmpUInt64Ty =
1779 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1780 QualType KmpInt64Ty =
1781 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1782 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1783 FunctionProtoType::ExtProtoInfo EPI;
1784 EPI.Variadic = true;
1785 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001786 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001787 std::make_pair(".global_tid.", KmpInt32Ty),
1788 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1789 std::make_pair(".privates.",
1790 Context.VoidPtrTy.withConst().withRestrict()),
1791 std::make_pair(
1792 ".copy_fn.",
1793 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1794 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1795 std::make_pair(".lb.", KmpUInt64Ty),
1796 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1797 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001798 std::make_pair(StringRef(), QualType()) // __context with shared vars
1799 };
1800 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1801 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001802 // Mark this captured region as inlined, because we don't use outlined
1803 // function directly.
1804 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1805 AlwaysInlineAttr::CreateImplicit(
1806 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001807 break;
1808 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001809 case OMPD_distribute: {
1810 Sema::CapturedParamNameType Params[] = {
1811 std::make_pair(StringRef(), QualType()) // __context with shared vars
1812 };
1813 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1814 Params);
1815 break;
1816 }
Kelvin Li4a39add2016-07-05 05:00:15 +00001817 case OMPD_distribute_parallel_for_simd:
Kelvin Li787f3fc2016-07-06 04:45:38 +00001818 case OMPD_distribute_simd:
Carlo Bertolli9925f152016-06-27 14:55:37 +00001819 case OMPD_distribute_parallel_for: {
1820 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1821 QualType KmpInt32PtrTy =
1822 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1823 Sema::CapturedParamNameType Params[] = {
1824 std::make_pair(".global_tid.", KmpInt32PtrTy),
1825 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1826 std::make_pair(".previous.lb.", Context.getSizeType()),
1827 std::make_pair(".previous.ub.", Context.getSizeType()),
1828 std::make_pair(StringRef(), QualType()) // __context with shared vars
1829 };
1830 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1831 Params);
1832 break;
1833 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001834 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001835 case OMPD_taskyield:
1836 case OMPD_barrier:
1837 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001838 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001839 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001840 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001841 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001842 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001843 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001844 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001845 case OMPD_declare_target:
1846 case OMPD_end_declare_target:
Samuel Antao686c70c2016-05-26 17:30:50 +00001847 case OMPD_target_update:
Alexey Bataev9959db52014-05-06 10:08:46 +00001848 llvm_unreachable("OpenMP Directive is not allowed");
1849 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001850 llvm_unreachable("Unknown OpenMP directive");
1851 }
1852}
1853
Alexey Bataev3392d762016-02-16 11:18:12 +00001854static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001855 Expr *CaptureExpr, bool WithInit,
1856 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001857 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001858 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001859 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001860 QualType Ty = Init->getType();
1861 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1862 if (S.getLangOpts().CPlusPlus)
1863 Ty = C.getLValueReferenceType(Ty);
1864 else {
1865 Ty = C.getPointerType(Ty);
1866 ExprResult Res =
1867 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1868 if (!Res.isUsable())
1869 return nullptr;
1870 Init = Res.get();
1871 }
Alexey Bataev61205072016-03-02 04:57:40 +00001872 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001873 }
1874 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001875 if (!WithInit)
1876 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001877 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001878 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1879 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001880 return CED;
1881}
1882
Alexey Bataev61205072016-03-02 04:57:40 +00001883static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1884 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001885 OMPCapturedExprDecl *CD;
1886 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1887 CD = cast<OMPCapturedExprDecl>(VD);
1888 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001889 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1890 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001891 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001892 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001893}
1894
Alexey Bataev5a3af132016-03-29 08:58:54 +00001895static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1896 if (!Ref) {
1897 auto *CD =
1898 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1899 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1900 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1901 CaptureExpr->getExprLoc());
1902 }
1903 ExprResult Res = Ref;
1904 if (!S.getLangOpts().CPlusPlus &&
1905 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1906 Ref->getType()->isPointerType())
1907 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1908 if (!Res.isUsable())
1909 return ExprError();
1910 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001911}
1912
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001913StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1914 ArrayRef<OMPClause *> Clauses) {
1915 if (!S.isUsable()) {
1916 ActOnCapturedRegionError();
1917 return StmtError();
1918 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001919
1920 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001921 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001922 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001923 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001924 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001925 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001926 Clause->getClauseKind() == OMPC_copyprivate ||
1927 (getLangOpts().OpenMPUseTLS &&
1928 getASTContext().getTargetInfo().isTLSSupported() &&
1929 Clause->getClauseKind() == OMPC_copyin)) {
1930 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001931 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001932 for (auto *VarRef : Clause->children()) {
1933 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001934 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001935 }
1936 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001937 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001938 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001939 // Mark all variables in private list clauses as used in inner region.
1940 // Required for proper codegen of combined directives.
1941 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001942 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001943 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1944 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001945 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1946 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001947 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001948 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1949 if (auto *E = C->getPostUpdateExpr())
1950 MarkDeclarationsReferencedInExpr(E);
1951 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001952 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001953 if (Clause->getClauseKind() == OMPC_schedule)
1954 SC = cast<OMPScheduleClause>(Clause);
1955 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001956 OC = cast<OMPOrderedClause>(Clause);
1957 else if (Clause->getClauseKind() == OMPC_linear)
1958 LCs.push_back(cast<OMPLinearClause>(Clause));
1959 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001960 bool ErrorFound = false;
1961 // OpenMP, 2.7.1 Loop Construct, Restrictions
1962 // The nonmonotonic modifier cannot be specified if an ordered clause is
1963 // specified.
1964 if (SC &&
1965 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1966 SC->getSecondScheduleModifier() ==
1967 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1968 OC) {
1969 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1970 ? SC->getFirstScheduleModifierLoc()
1971 : SC->getSecondScheduleModifierLoc(),
1972 diag::err_omp_schedule_nonmonotonic_ordered)
1973 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1974 ErrorFound = true;
1975 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001976 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1977 for (auto *C : LCs) {
1978 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1979 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1980 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001981 ErrorFound = true;
1982 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001983 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1984 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1985 OC->getNumForLoops()) {
1986 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1987 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1988 ErrorFound = true;
1989 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001990 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001991 ActOnCapturedRegionError();
1992 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001993 }
1994 return ActOnCapturedRegionEnd(S.get());
1995}
1996
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1998 OpenMPDirectiveKind CurrentRegion,
1999 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002000 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002001 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002002 // Allowed nesting of constructs
2003 // +------------------+-----------------+------------------------------------+
2004 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
2005 // +------------------+-----------------+------------------------------------+
2006 // | parallel | parallel | * |
2007 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002008 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002009 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002010 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002011 // | parallel | simd | * |
2012 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002013 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002014 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002015 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002016 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002017 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002018 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002019 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002020 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002021 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002022 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002023 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002024 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002025 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002026 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002027 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002028 // | parallel | target parallel | * |
2029 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002030 // | parallel | target enter | * |
2031 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002032 // | parallel | target exit | * |
2033 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002034 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002035 // | parallel | cancellation | |
2036 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002037 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002038 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002039 // | parallel | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002040 // | parallel | distribute | + |
2041 // | parallel | distribute | + |
2042 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002043 // | parallel | distribute | + |
2044 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002045 // | parallel | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002046 // +------------------+-----------------+------------------------------------+
2047 // | for | parallel | * |
2048 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002049 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002052 // | for | simd | * |
2053 // | for | sections | + |
2054 // | for | section | + |
2055 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002056 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002059 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002060 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002061 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002062 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002064 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002065 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002066 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002068 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002069 // | for | target parallel | * |
2070 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002071 // | for | target enter | * |
2072 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002073 // | for | target exit | * |
2074 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002075 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002076 // | for | cancellation | |
2077 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002078 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002079 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002080 // | for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002081 // | for | distribute | + |
2082 // | for | distribute | + |
2083 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002084 // | for | distribute | + |
2085 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002086 // | for | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002087 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002088 // | master | parallel | * |
2089 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002090 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002091 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002092 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002093 // | master | simd | * |
2094 // | master | sections | + |
2095 // | master | section | + |
2096 // | master | single | + |
2097 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002098 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002099 // | master |parallel sections| * |
2100 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002101 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002102 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002103 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002104 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002105 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002106 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002107 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002108 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002109 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002110 // | master | target parallel | * |
2111 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002112 // | master | target enter | * |
2113 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002114 // | master | target exit | * |
2115 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002116 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002117 // | master | cancellation | |
2118 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002119 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002120 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002121 // | master | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002122 // | master | distribute | + |
2123 // | master | distribute | + |
2124 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002125 // | master | distribute | + |
2126 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002127 // | master | distribute simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002128 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002129 // | critical | parallel | * |
2130 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002131 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002132 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002133 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002134 // | critical | simd | * |
2135 // | critical | sections | + |
2136 // | critical | section | + |
2137 // | critical | single | + |
2138 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002139 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002140 // | critical |parallel sections| * |
2141 // | critical | task | * |
2142 // | critical | taskyield | * |
2143 // | critical | barrier | + |
2144 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002145 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002146 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002147 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002148 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002149 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002150 // | critical | target parallel | * |
2151 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002152 // | critical | target enter | * |
2153 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002154 // | critical | target exit | * |
2155 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002156 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002157 // | critical | cancellation | |
2158 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002159 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002160 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002161 // | critical | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002162 // | critical | distribute | + |
2163 // | critical | distribute | + |
2164 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002165 // | critical | distribute | + |
2166 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002167 // | critical | distribute simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002168 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002169 // | simd | parallel | |
2170 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002171 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002172 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002173 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002174 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002175 // | simd | sections | |
2176 // | simd | section | |
2177 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002179 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002180 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002181 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002182 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002183 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002184 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002185 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002186 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002187 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002188 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002189 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002190 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002191 // | simd | target parallel | |
2192 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002193 // | simd | target enter | |
2194 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002195 // | simd | target exit | |
2196 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002197 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002198 // | simd | cancellation | |
2199 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002200 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002201 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002202 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002203 // | simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002204 // | simd | distribute | |
2205 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002206 // | simd | distribute | |
2207 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002208 // | simd | distribute simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002209 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002210 // | for simd | parallel | |
2211 // | for simd | for | |
2212 // | for simd | for simd | |
2213 // | for simd | master | |
2214 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002215 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002216 // | for simd | sections | |
2217 // | for simd | section | |
2218 // | for simd | single | |
2219 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002220 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002221 // | for simd |parallel sections| |
2222 // | for simd | task | |
2223 // | for simd | taskyield | |
2224 // | for simd | barrier | |
2225 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002226 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002227 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002228 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002229 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002230 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002231 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002232 // | for simd | target parallel | |
2233 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002234 // | for simd | target enter | |
2235 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002236 // | for simd | target exit | |
2237 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002238 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002239 // | for simd | cancellation | |
2240 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002241 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002242 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002243 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002244 // | for simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002245 // | for simd | distribute | |
2246 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002247 // | for simd | distribute | |
2248 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002249 // | for simd | distribute simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002250 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002251 // | parallel for simd| parallel | |
2252 // | parallel for simd| for | |
2253 // | parallel for simd| for simd | |
2254 // | parallel for simd| master | |
2255 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002256 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002257 // | parallel for simd| sections | |
2258 // | parallel for simd| section | |
2259 // | parallel for simd| single | |
2260 // | parallel for simd| parallel for | |
2261 // | parallel for simd|parallel for simd| |
2262 // | parallel for simd|parallel sections| |
2263 // | parallel for simd| task | |
2264 // | parallel for simd| taskyield | |
2265 // | parallel for simd| barrier | |
2266 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002267 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002268 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002269 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002270 // | parallel for simd| atomic | |
2271 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002272 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002273 // | parallel for simd| target parallel | |
2274 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002275 // | parallel for simd| target enter | |
2276 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002277 // | parallel for simd| target exit | |
2278 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002279 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002280 // | parallel for simd| cancellation | |
2281 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002282 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002283 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002284 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002285 // | parallel for simd| distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002286 // | parallel for simd| distribute | |
2287 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002288 // | parallel for simd| distribute | |
2289 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002290 // | parallel for simd| distribute simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002291 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002292 // | sections | parallel | * |
2293 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002294 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002295 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002296 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002297 // | sections | simd | * |
2298 // | sections | sections | + |
2299 // | sections | section | * |
2300 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002301 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002302 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002303 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002304 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002305 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002306 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002307 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002308 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002309 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002310 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002311 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002312 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002313 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002314 // | sections | target parallel | * |
2315 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002316 // | sections | target enter | * |
2317 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002318 // | sections | target exit | * |
2319 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002320 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002321 // | sections | cancellation | |
2322 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002323 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002324 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002325 // | sections | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002326 // | sections | distribute | + |
2327 // | sections | distribute | + |
2328 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002329 // | sections | distribute | + |
2330 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002331 // | sections | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002332 // +------------------+-----------------+------------------------------------+
2333 // | section | parallel | * |
2334 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002335 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002336 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002337 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002338 // | section | simd | * |
2339 // | section | sections | + |
2340 // | section | section | + |
2341 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002342 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002343 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002344 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002345 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002346 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002347 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002348 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002349 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002350 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002351 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002352 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002353 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002354 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002355 // | section | target parallel | * |
2356 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002357 // | section | target enter | * |
2358 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002359 // | section | target exit | * |
2360 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002361 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002362 // | section | cancellation | |
2363 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002364 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002365 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002366 // | section | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002367 // | section | distribute | + |
2368 // | section | distribute | + |
2369 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002370 // | section | distribute | + |
2371 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002372 // | section | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002373 // +------------------+-----------------+------------------------------------+
2374 // | single | parallel | * |
2375 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002376 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002377 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002378 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002379 // | single | simd | * |
2380 // | single | sections | + |
2381 // | single | section | + |
2382 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002383 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002384 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002385 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002387 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002388 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002389 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002390 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002391 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002392 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002393 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002395 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002396 // | single | target parallel | * |
2397 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002398 // | single | target enter | * |
2399 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002400 // | single | target exit | * |
2401 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002402 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002403 // | single | cancellation | |
2404 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002405 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002406 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002407 // | single | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002408 // | single | distribute | + |
2409 // | single | distribute | + |
2410 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002411 // | single | distribute | + |
2412 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002413 // | single | distribute simd | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002414 // +------------------+-----------------+------------------------------------+
2415 // | parallel for | parallel | * |
2416 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002417 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002418 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002419 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002420 // | parallel for | simd | * |
2421 // | parallel for | sections | + |
2422 // | parallel for | section | + |
2423 // | parallel for | single | + |
2424 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002425 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002426 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002427 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002428 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002429 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002430 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002431 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002432 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002433 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002434 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002435 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002436 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002437 // | parallel for | target parallel | * |
2438 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002439 // | parallel for | target enter | * |
2440 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002441 // | parallel for | target exit | * |
2442 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002443 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002444 // | parallel for | cancellation | |
2445 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002446 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002447 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002448 // | parallel for | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002449 // | parallel for | distribute | + |
2450 // | parallel for | distribute | + |
2451 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002452 // | parallel for | distribute | + |
2453 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002454 // | parallel for | distribute simd | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002455 // +------------------+-----------------+------------------------------------+
2456 // | parallel sections| parallel | * |
2457 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002458 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002459 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002460 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002461 // | parallel sections| simd | * |
2462 // | parallel sections| sections | + |
2463 // | parallel sections| section | * |
2464 // | parallel sections| single | + |
2465 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002466 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002467 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002468 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002469 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002470 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002471 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002472 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002473 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002474 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002475 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002476 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002477 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002478 // | parallel sections| target parallel | * |
2479 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002480 // | parallel sections| target enter | * |
2481 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002482 // | parallel sections| target exit | * |
2483 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002484 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002485 // | parallel sections| cancellation | |
2486 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002487 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002488 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002489 // | parallel sections| taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002490 // | parallel sections| distribute | + |
2491 // | parallel sections| distribute | + |
2492 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002493 // | parallel sections| distribute | + |
2494 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002495 // | parallel sections| distribute simd | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002496 // +------------------+-----------------+------------------------------------+
2497 // | task | parallel | * |
2498 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002499 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002500 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002501 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002502 // | task | simd | * |
2503 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002504 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002505 // | task | single | + |
2506 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002507 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002508 // | task |parallel sections| * |
2509 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002510 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002511 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002512 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002513 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002514 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002515 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002516 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002517 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002518 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002519 // | task | target parallel | * |
2520 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002521 // | task | target enter | * |
2522 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002523 // | task | target exit | * |
2524 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002525 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002526 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002527 // | | point | ! |
2528 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002529 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002530 // | task | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002531 // | task | distribute | + |
2532 // | task | distribute | + |
2533 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002534 // | task | distribute | + |
2535 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002536 // | task | distribute simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002537 // +------------------+-----------------+------------------------------------+
2538 // | ordered | parallel | * |
2539 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002540 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002541 // | ordered | master | * |
2542 // | ordered | critical | * |
2543 // | ordered | simd | * |
2544 // | ordered | sections | + |
2545 // | ordered | section | + |
2546 // | ordered | single | + |
2547 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002548 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002549 // | ordered |parallel sections| * |
2550 // | ordered | task | * |
2551 // | ordered | taskyield | * |
2552 // | ordered | barrier | + |
2553 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002554 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002555 // | ordered | flush | * |
2556 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002557 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002558 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002559 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002560 // | ordered | target parallel | * |
2561 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002562 // | ordered | target enter | * |
2563 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002564 // | ordered | target exit | * |
2565 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002566 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002567 // | ordered | cancellation | |
2568 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002569 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002570 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002571 // | ordered | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002572 // | ordered | distribute | + |
2573 // | ordered | distribute | + |
2574 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002575 // | ordered | distribute | + |
2576 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002577 // | ordered | distribute simd | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002578 // +------------------+-----------------+------------------------------------+
2579 // | atomic | parallel | |
2580 // | atomic | for | |
2581 // | atomic | for simd | |
2582 // | atomic | master | |
2583 // | atomic | critical | |
2584 // | atomic | simd | |
2585 // | atomic | sections | |
2586 // | atomic | section | |
2587 // | atomic | single | |
2588 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002589 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002590 // | atomic |parallel sections| |
2591 // | atomic | task | |
2592 // | atomic | taskyield | |
2593 // | atomic | barrier | |
2594 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002595 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002596 // | atomic | flush | |
2597 // | atomic | ordered | |
2598 // | atomic | atomic | |
2599 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002600 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002601 // | atomic | target parallel | |
2602 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002603 // | atomic | target enter | |
2604 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002605 // | atomic | target exit | |
2606 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002607 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002608 // | atomic | cancellation | |
2609 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002610 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002611 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002612 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002613 // | atomic | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002614 // | atomic | distribute | |
2615 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002616 // | atomic | distribute | |
2617 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002618 // | atomic | distribute simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002619 // +------------------+-----------------+------------------------------------+
2620 // | target | parallel | * |
2621 // | target | for | * |
2622 // | target | for simd | * |
2623 // | target | master | * |
2624 // | target | critical | * |
2625 // | target | simd | * |
2626 // | target | sections | * |
2627 // | target | section | * |
2628 // | target | single | * |
2629 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002630 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002631 // | target |parallel sections| * |
2632 // | target | task | * |
2633 // | target | taskyield | * |
2634 // | target | barrier | * |
2635 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002636 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002637 // | target | flush | * |
2638 // | target | ordered | * |
2639 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002640 // | target | target | |
2641 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002642 // | target | target parallel | |
2643 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002644 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002645 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002646 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002647 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002648 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002649 // | target | cancellation | |
2650 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002651 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002652 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002653 // | target | taskloop simd | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002654 // | target | distribute | + |
2655 // | target | distribute | + |
2656 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002657 // | target | distribute | + |
2658 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002659 // | target | distribute simd | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002660 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002661 // | target parallel | parallel | * |
2662 // | target parallel | for | * |
2663 // | target parallel | for simd | * |
2664 // | target parallel | master | * |
2665 // | target parallel | critical | * |
2666 // | target parallel | simd | * |
2667 // | target parallel | sections | * |
2668 // | target parallel | section | * |
2669 // | target parallel | single | * |
2670 // | target parallel | parallel for | * |
2671 // | target parallel |parallel for simd| * |
2672 // | target parallel |parallel sections| * |
2673 // | target parallel | task | * |
2674 // | target parallel | taskyield | * |
2675 // | target parallel | barrier | * |
2676 // | target parallel | taskwait | * |
2677 // | target parallel | taskgroup | * |
2678 // | target parallel | flush | * |
2679 // | target parallel | ordered | * |
2680 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002681 // | target parallel | target | |
2682 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002683 // | target parallel | target parallel | |
2684 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002685 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002686 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002687 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002688 // | | data | |
2689 // | target parallel | teams | |
2690 // | target parallel | cancellation | |
2691 // | | point | ! |
2692 // | target parallel | cancel | ! |
2693 // | target parallel | taskloop | * |
2694 // | target parallel | taskloop simd | * |
2695 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002696 // | target parallel | distribute | |
2697 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002698 // | target parallel | distribute | |
2699 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002700 // | target parallel | distribute simd | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002701 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002702 // | target parallel | parallel | * |
2703 // | for | | |
2704 // | target parallel | for | * |
2705 // | for | | |
2706 // | target parallel | for simd | * |
2707 // | for | | |
2708 // | target parallel | master | * |
2709 // | for | | |
2710 // | target parallel | critical | * |
2711 // | for | | |
2712 // | target parallel | simd | * |
2713 // | for | | |
2714 // | target parallel | sections | * |
2715 // | for | | |
2716 // | target parallel | section | * |
2717 // | for | | |
2718 // | target parallel | single | * |
2719 // | for | | |
2720 // | target parallel | parallel for | * |
2721 // | for | | |
2722 // | target parallel |parallel for simd| * |
2723 // | for | | |
2724 // | target parallel |parallel sections| * |
2725 // | for | | |
2726 // | target parallel | task | * |
2727 // | for | | |
2728 // | target parallel | taskyield | * |
2729 // | for | | |
2730 // | target parallel | barrier | * |
2731 // | for | | |
2732 // | target parallel | taskwait | * |
2733 // | for | | |
2734 // | target parallel | taskgroup | * |
2735 // | for | | |
2736 // | target parallel | flush | * |
2737 // | for | | |
2738 // | target parallel | ordered | * |
2739 // | for | | |
2740 // | target parallel | atomic | * |
2741 // | for | | |
2742 // | target parallel | target | |
2743 // | for | | |
2744 // | target parallel | target parallel | |
2745 // | for | | |
2746 // | target parallel | target parallel | |
2747 // | for | for | |
2748 // | target parallel | target enter | |
2749 // | for | data | |
2750 // | target parallel | target exit | |
2751 // | for | data | |
2752 // | target parallel | teams | |
2753 // | for | | |
2754 // | target parallel | cancellation | |
2755 // | for | point | ! |
2756 // | target parallel | cancel | ! |
2757 // | for | | |
2758 // | target parallel | taskloop | * |
2759 // | for | | |
2760 // | target parallel | taskloop simd | * |
2761 // | for | | |
2762 // | target parallel | distribute | |
2763 // | for | | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002764 // | target parallel | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002765 // | for | parallel for | |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002766 // | target parallel | distribute | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002767 // | for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002768 // | target parallel | distribute simd | |
2769 // | for | | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002770 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002771 // | teams | parallel | * |
2772 // | teams | for | + |
2773 // | teams | for simd | + |
2774 // | teams | master | + |
2775 // | teams | critical | + |
2776 // | teams | simd | + |
2777 // | teams | sections | + |
2778 // | teams | section | + |
2779 // | teams | single | + |
2780 // | teams | parallel for | * |
2781 // | teams |parallel for simd| * |
2782 // | teams |parallel sections| * |
2783 // | teams | task | + |
2784 // | teams | taskyield | + |
2785 // | teams | barrier | + |
2786 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002787 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002788 // | teams | flush | + |
2789 // | teams | ordered | + |
2790 // | teams | atomic | + |
2791 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002792 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002793 // | teams | target parallel | + |
2794 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002795 // | teams | target enter | + |
2796 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002797 // | teams | target exit | + |
2798 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002799 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002800 // | teams | cancellation | |
2801 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002802 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002803 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002804 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002805 // | teams | distribute | ! |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002806 // | teams | distribute | ! |
2807 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002808 // | teams | distribute | ! |
2809 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002810 // | teams | distribute simd | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002811 // +------------------+-----------------+------------------------------------+
2812 // | taskloop | parallel | * |
2813 // | taskloop | for | + |
2814 // | taskloop | for simd | + |
2815 // | taskloop | master | + |
2816 // | taskloop | critical | * |
2817 // | taskloop | simd | * |
2818 // | taskloop | sections | + |
2819 // | taskloop | section | + |
2820 // | taskloop | single | + |
2821 // | taskloop | parallel for | * |
2822 // | taskloop |parallel for simd| * |
2823 // | taskloop |parallel sections| * |
2824 // | taskloop | task | * |
2825 // | taskloop | taskyield | * |
2826 // | taskloop | barrier | + |
2827 // | taskloop | taskwait | * |
2828 // | taskloop | taskgroup | * |
2829 // | taskloop | flush | * |
2830 // | taskloop | ordered | + |
2831 // | taskloop | atomic | * |
2832 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002833 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002834 // | taskloop | target parallel | * |
2835 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002836 // | taskloop | target enter | * |
2837 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002838 // | taskloop | target exit | * |
2839 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002840 // | taskloop | teams | + |
2841 // | taskloop | cancellation | |
2842 // | | point | |
2843 // | taskloop | cancel | |
2844 // | taskloop | taskloop | * |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002845 // | taskloop | distribute | + |
2846 // | taskloop | distribute | + |
2847 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002848 // | taskloop | distribute | + |
2849 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002850 // | taskloop | distribute simd | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002851 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002852 // | taskloop simd | parallel | |
2853 // | taskloop simd | for | |
2854 // | taskloop simd | for simd | |
2855 // | taskloop simd | master | |
2856 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002857 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002858 // | taskloop simd | sections | |
2859 // | taskloop simd | section | |
2860 // | taskloop simd | single | |
2861 // | taskloop simd | parallel for | |
2862 // | taskloop simd |parallel for simd| |
2863 // | taskloop simd |parallel sections| |
2864 // | taskloop simd | task | |
2865 // | taskloop simd | taskyield | |
2866 // | taskloop simd | barrier | |
2867 // | taskloop simd | taskwait | |
2868 // | taskloop simd | taskgroup | |
2869 // | taskloop simd | flush | |
2870 // | taskloop simd | ordered | + (with simd clause) |
2871 // | taskloop simd | atomic | |
2872 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002873 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002874 // | taskloop simd | target parallel | |
2875 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002876 // | taskloop simd | target enter | |
2877 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002878 // | taskloop simd | target exit | |
2879 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002880 // | taskloop simd | teams | |
2881 // | taskloop simd | cancellation | |
2882 // | | point | |
2883 // | taskloop simd | cancel | |
2884 // | taskloop simd | taskloop | |
2885 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002886 // | taskloop simd | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002887 // | taskloop simd | distribute | |
2888 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002889 // | taskloop simd | distribute | |
2890 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002891 // | taskloop simd | distribute simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002892 // +------------------+-----------------+------------------------------------+
2893 // | distribute | parallel | * |
2894 // | distribute | for | * |
2895 // | distribute | for simd | * |
2896 // | distribute | master | * |
2897 // | distribute | critical | * |
2898 // | distribute | simd | * |
2899 // | distribute | sections | * |
2900 // | distribute | section | * |
2901 // | distribute | single | * |
2902 // | distribute | parallel for | * |
2903 // | distribute |parallel for simd| * |
2904 // | distribute |parallel sections| * |
2905 // | distribute | task | * |
2906 // | distribute | taskyield | * |
2907 // | distribute | barrier | * |
2908 // | distribute | taskwait | * |
2909 // | distribute | taskgroup | * |
2910 // | distribute | flush | * |
2911 // | distribute | ordered | + |
2912 // | distribute | atomic | * |
2913 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002914 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002915 // | distribute | target parallel | |
2916 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002917 // | distribute | target enter | |
2918 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002919 // | distribute | target exit | |
2920 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002921 // | distribute | teams | |
2922 // | distribute | cancellation | + |
2923 // | | point | |
2924 // | distribute | cancel | + |
2925 // | distribute | taskloop | * |
2926 // | distribute | taskloop simd | * |
2927 // | distribute | distribute | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002928 // | distribute | distribute | |
2929 // | | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002930 // | distribute | distribute | |
2931 // | |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00002932 // | distribute | distribute simd | |
Carlo Bertolli9925f152016-06-27 14:55:37 +00002933 // +------------------+-----------------+------------------------------------+
2934 // | distribute | parallel | * |
2935 // | parallel for | | |
2936 // | distribute | for | * |
2937 // | parallel for | | |
2938 // | distribute | for simd | * |
2939 // | parallel for | | |
2940 // | distribute | master | * |
2941 // | parallel for | | |
2942 // | distribute | critical | * |
2943 // | parallel for | | |
2944 // | distribute | simd | * |
2945 // | parallel for | | |
2946 // | distribute | sections | * |
2947 // | parallel for | | |
2948 // | distribute | section | * |
2949 // | parallel for | | |
2950 // | distribute | single | * |
2951 // | parallel for | | |
2952 // | distribute | parallel for | * |
2953 // | parallel for | | |
2954 // | distribute |parallel for simd| * |
2955 // | parallel for | | |
2956 // | distribute |parallel sections| * |
2957 // | parallel for | | |
2958 // | distribute | task | * |
2959 // | parallel for | | |
2960 // | parallel for | | |
2961 // | distribute | taskyield | * |
2962 // | parallel for | | |
2963 // | distribute | barrier | * |
2964 // | parallel for | | |
2965 // | distribute | taskwait | * |
2966 // | parallel for | | |
2967 // | distribute | taskgroup | * |
2968 // | parallel for | | |
2969 // | distribute | flush | * |
2970 // | parallel for | | |
2971 // | distribute | ordered | + |
2972 // | parallel for | | |
2973 // | distribute | atomic | * |
2974 // | parallel for | | |
2975 // | distribute | target | |
2976 // | parallel for | | |
2977 // | distribute | target parallel | |
2978 // | parallel for | | |
2979 // | distribute | target parallel | |
2980 // | parallel for | for | |
2981 // | distribute | target enter | |
2982 // | parallel for | data | |
2983 // | distribute | target exit | |
2984 // | parallel for | data | |
2985 // | distribute | teams | |
2986 // | parallel for | | |
2987 // | distribute | cancellation | + |
2988 // | parallel for | point | |
2989 // | distribute | cancel | + |
2990 // | parallel for | | |
2991 // | distribute | taskloop | * |
2992 // | parallel for | | |
2993 // | distribute | taskloop simd | * |
2994 // | parallel for | | |
2995 // | distribute | distribute | |
2996 // | parallel for | | |
2997 // | distribute | distribute | |
2998 // | parallel for | parallel for | |
Kelvin Li4a39add2016-07-05 05:00:15 +00002999 // | distribute | distribute | |
3000 // | parallel for |parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003001 // | distribute | distribute simd | |
3002 // | parallel for | | |
Kelvin Li4a39add2016-07-05 05:00:15 +00003003 // +------------------+-----------------+------------------------------------+
3004 // | distribute | parallel | * |
3005 // | parallel for simd| | |
3006 // | distribute | for | * |
3007 // | parallel for simd| | |
3008 // | distribute | for simd | * |
3009 // | parallel for simd| | |
3010 // | distribute | master | * |
3011 // | parallel for simd| | |
3012 // | distribute | critical | * |
3013 // | parallel for simd| | |
3014 // | distribute | simd | * |
3015 // | parallel for simd| | |
3016 // | distribute | sections | * |
3017 // | parallel for simd| | |
3018 // | distribute | section | * |
3019 // | parallel for simd| | |
3020 // | distribute | single | * |
3021 // | parallel for simd| | |
3022 // | distribute | parallel for | * |
3023 // | parallel for simd| | |
3024 // | distribute |parallel for simd| * |
3025 // | parallel for simd| | |
3026 // | distribute |parallel sections| * |
3027 // | parallel for simd| | |
3028 // | distribute | task | * |
3029 // | parallel for simd| | |
3030 // | distribute | taskyield | * |
3031 // | parallel for simd| | |
3032 // | distribute | barrier | * |
3033 // | parallel for simd| | |
3034 // | distribute | taskwait | * |
3035 // | parallel for simd| | |
3036 // | distribute | taskgroup | * |
3037 // | parallel for simd| | |
3038 // | distribute | flush | * |
3039 // | parallel for simd| | |
3040 // | distribute | ordered | + |
3041 // | parallel for simd| | |
3042 // | distribute | atomic | * |
3043 // | parallel for simd| | |
3044 // | distribute | target | |
3045 // | parallel for simd| | |
3046 // | distribute | target parallel | |
3047 // | parallel for simd| | |
3048 // | distribute | target parallel | |
3049 // | parallel for simd| for | |
3050 // | distribute | target enter | |
3051 // | parallel for simd| data | |
3052 // | distribute | target exit | |
3053 // | parallel for simd| data | |
3054 // | distribute | teams | |
3055 // | parallel for simd| | |
3056 // | distribute | cancellation | + |
3057 // | parallel for simd| point | |
3058 // | distribute | cancel | + |
3059 // | parallel for simd| | |
3060 // | distribute | taskloop | * |
3061 // | parallel for simd| | |
3062 // | distribute | taskloop simd | * |
3063 // | parallel for simd| | |
3064 // | distribute | distribute | |
3065 // | parallel for simd| | |
3066 // | distribute | distribute | * |
3067 // | parallel for simd| parallel for | |
3068 // | distribute | distribute | * |
3069 // | parallel for simd|parallel for simd| |
Kelvin Li787f3fc2016-07-06 04:45:38 +00003070 // | distribute | distribute simd | * |
3071 // | parallel for simd| | |
3072 // +------------------+-----------------+------------------------------------+
3073 // | distribute simd | parallel | * |
3074 // | distribute simd | for | * |
3075 // | distribute simd | for simd | * |
3076 // | distribute simd | master | * |
3077 // | distribute simd | critical | * |
3078 // | distribute simd | simd | * |
3079 // | distribute simd | sections | * |
3080 // | distribute simd | section | * |
3081 // | distribute simd | single | * |
3082 // | distribute simd | parallel for | * |
3083 // | distribute simd |parallel for simd| * |
3084 // | distribute simd |parallel sections| * |
3085 // | distribute simd | task | * |
3086 // | distribute simd | taskyield | * |
3087 // | distribute simd | barrier | * |
3088 // | distribute simd | taskwait | * |
3089 // | distribute simd | taskgroup | * |
3090 // | distribute simd | flush | * |
3091 // | distribute simd | ordered | + |
3092 // | distribute simd | atomic | * |
3093 // | distribute simd | target | * |
3094 // | distribute simd | target parallel | * |
3095 // | distribute simd | target parallel | * |
3096 // | | for | |
3097 // | distribute simd | target enter | * |
3098 // | | data | |
3099 // | distribute simd | target exit | * |
3100 // | | data | |
3101 // | distribute simd | teams | * |
3102 // | distribute simd | cancellation | + |
3103 // | | point | |
3104 // | distribute simd | cancel | + |
3105 // | distribute simd | taskloop | * |
3106 // | distribute simd | taskloop simd | * |
3107 // | distribute simd | distribute | |
3108 // | distribute simd | distribute | * |
3109 // | | parallel for | |
3110 // | distribute simd | distribute | * |
3111 // | |parallel for simd| |
3112 // | distribute simd | distribute simd | * |
3113 // | | | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003114 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00003115 if (Stack->getCurScope()) {
3116 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003117 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003118 bool NestingProhibited = false;
3119 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003120 enum {
3121 NoRecommend,
3122 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003123 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003124 ShouldBeInTargetRegion,
3125 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003126 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003127 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003128 // OpenMP [2.16, Nesting of Regions]
3129 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003130 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003131 // An ordered construct with the simd clause is the only OpenMP
3132 // construct that can appear in the simd region.
3133 // Allowing a SIMD consruct nested in another SIMD construct is an
3134 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3135 // message.
3136 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3137 ? diag::err_omp_prohibited_region_simd
3138 : diag::warn_omp_nesting_simd);
3139 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003140 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003141 if (ParentRegion == OMPD_atomic) {
3142 // OpenMP [2.16, Nesting of Regions]
3143 // OpenMP constructs may not be nested inside an atomic region.
3144 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3145 return true;
3146 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003147 if (CurrentRegion == OMPD_section) {
3148 // OpenMP [2.7.2, sections Construct, Restrictions]
3149 // Orphaned section directives are prohibited. That is, the section
3150 // directives must appear within the sections construct and must not be
3151 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003152 if (ParentRegion != OMPD_sections &&
3153 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003154 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3155 << (ParentRegion != OMPD_unknown)
3156 << getOpenMPDirectiveName(ParentRegion);
3157 return true;
3158 }
3159 return false;
3160 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003161 // Allow some constructs to be orphaned (they could be used in functions,
3162 // called from OpenMP regions with the required preconditions).
3163 if (ParentRegion == OMPD_unknown)
3164 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003165 if (CurrentRegion == OMPD_cancellation_point ||
3166 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003167 // OpenMP [2.16, Nesting of Regions]
3168 // A cancellation point construct for which construct-type-clause is
3169 // taskgroup must be nested inside a task construct. A cancellation
3170 // point construct for which construct-type-clause is not taskgroup must
3171 // be closely nested inside an OpenMP construct that matches the type
3172 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003173 // A cancel construct for which construct-type-clause is taskgroup must be
3174 // nested inside a task construct. A cancel construct for which
3175 // construct-type-clause is not taskgroup must be closely nested inside an
3176 // OpenMP construct that matches the type specified in
3177 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003178 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003179 !((CancelRegion == OMPD_parallel &&
3180 (ParentRegion == OMPD_parallel ||
3181 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003182 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003183 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3184 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003185 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3186 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003187 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3188 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003189 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003190 // OpenMP [2.16, Nesting of Regions]
3191 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003192 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003193 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003194 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003195 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3196 // OpenMP [2.16, Nesting of Regions]
3197 // A critical region may not be nested (closely or otherwise) inside a
3198 // critical region with the same name. Note that this restriction is not
3199 // sufficient to prevent deadlock.
3200 SourceLocation PreviousCriticalLoc;
3201 bool DeadLock =
3202 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3203 OpenMPDirectiveKind K,
3204 const DeclarationNameInfo &DNI,
3205 SourceLocation Loc)
3206 ->bool {
3207 if (K == OMPD_critical &&
3208 DNI.getName() == CurrentName.getName()) {
3209 PreviousCriticalLoc = Loc;
3210 return true;
3211 } else
3212 return false;
3213 },
3214 false /* skip top directive */);
3215 if (DeadLock) {
3216 SemaRef.Diag(StartLoc,
3217 diag::err_omp_prohibited_region_critical_same_name)
3218 << CurrentName.getName();
3219 if (PreviousCriticalLoc.isValid())
3220 SemaRef.Diag(PreviousCriticalLoc,
3221 diag::note_omp_previous_critical_region);
3222 return true;
3223 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003224 } else if (CurrentRegion == OMPD_barrier) {
3225 // OpenMP [2.16, Nesting of Regions]
3226 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003227 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003228 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3229 isOpenMPTaskingDirective(ParentRegion) ||
3230 ParentRegion == OMPD_master ||
3231 ParentRegion == OMPD_critical ||
3232 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003233 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00003234 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003235 // OpenMP [2.16, Nesting of Regions]
3236 // A worksharing region may not be closely nested inside a worksharing,
3237 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003238 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3239 isOpenMPTaskingDirective(ParentRegion) ||
3240 ParentRegion == OMPD_master ||
3241 ParentRegion == OMPD_critical ||
3242 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003243 Recommend = ShouldBeInParallelRegion;
3244 } else if (CurrentRegion == OMPD_ordered) {
3245 // OpenMP [2.16, Nesting of Regions]
3246 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003247 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003248 // An ordered region must be closely nested inside a loop region (or
3249 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003250 // OpenMP [2.8.1,simd Construct, Restrictions]
3251 // An ordered construct with the simd clause is the only OpenMP construct
3252 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003253 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003254 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003255 !(isOpenMPSimdDirective(ParentRegion) ||
3256 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003257 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003258 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3259 // OpenMP [2.16, Nesting of Regions]
3260 // If specified, a teams construct must be contained within a target
3261 // construct.
3262 NestingProhibited = ParentRegion != OMPD_target;
3263 Recommend = ShouldBeInTargetRegion;
3264 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3265 }
3266 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3267 // OpenMP [2.16, Nesting of Regions]
3268 // distribute, parallel, parallel sections, parallel workshare, and the
3269 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3270 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003271 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3272 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003273 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003274 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003275 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3276 // OpenMP 4.5 [2.17 Nesting of Regions]
3277 // The region associated with the distribute construct must be strictly
3278 // nested inside a teams region
3279 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3280 Recommend = ShouldBeInTeamsRegion;
3281 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003282 if (!NestingProhibited &&
3283 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3284 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3285 // OpenMP 4.5 [2.17 Nesting of Regions]
3286 // If a target, target update, target data, target enter data, or
3287 // target exit data construct is encountered during execution of a
3288 // target region, the behavior is unspecified.
3289 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003290 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3291 SourceLocation) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003292 if (isOpenMPTargetExecutionDirective(K)) {
3293 OffendingRegion = K;
3294 return true;
3295 } else
3296 return false;
3297 },
3298 false /* don't skip top directive */);
3299 CloseNesting = false;
3300 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003301 if (NestingProhibited) {
3302 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003303 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3304 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00003305 return true;
3306 }
3307 }
3308 return false;
3309}
3310
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003311static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3312 ArrayRef<OMPClause *> Clauses,
3313 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3314 bool ErrorFound = false;
3315 unsigned NamedModifiersNumber = 0;
3316 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3317 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003318 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003319 for (const auto *C : Clauses) {
3320 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3321 // At most one if clause without a directive-name-modifier can appear on
3322 // the directive.
3323 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3324 if (FoundNameModifiers[CurNM]) {
3325 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3326 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3327 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3328 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003329 } else if (CurNM != OMPD_unknown) {
3330 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003331 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003332 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003333 FoundNameModifiers[CurNM] = IC;
3334 if (CurNM == OMPD_unknown)
3335 continue;
3336 // Check if the specified name modifier is allowed for the current
3337 // directive.
3338 // At most one if clause with the particular directive-name-modifier can
3339 // appear on the directive.
3340 bool MatchFound = false;
3341 for (auto NM : AllowedNameModifiers) {
3342 if (CurNM == NM) {
3343 MatchFound = true;
3344 break;
3345 }
3346 }
3347 if (!MatchFound) {
3348 S.Diag(IC->getNameModifierLoc(),
3349 diag::err_omp_wrong_if_directive_name_modifier)
3350 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3351 ErrorFound = true;
3352 }
3353 }
3354 }
3355 // If any if clause on the directive includes a directive-name-modifier then
3356 // all if clauses on the directive must include a directive-name-modifier.
3357 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3358 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3359 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3360 diag::err_omp_no_more_if_clause);
3361 } else {
3362 std::string Values;
3363 std::string Sep(", ");
3364 unsigned AllowedCnt = 0;
3365 unsigned TotalAllowedNum =
3366 AllowedNameModifiers.size() - NamedModifiersNumber;
3367 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3368 ++Cnt) {
3369 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3370 if (!FoundNameModifiers[NM]) {
3371 Values += "'";
3372 Values += getOpenMPDirectiveName(NM);
3373 Values += "'";
3374 if (AllowedCnt + 2 == TotalAllowedNum)
3375 Values += " or ";
3376 else if (AllowedCnt + 1 != TotalAllowedNum)
3377 Values += Sep;
3378 ++AllowedCnt;
3379 }
3380 }
3381 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3382 diag::err_omp_unnamed_if_clause)
3383 << (TotalAllowedNum > 1) << Values;
3384 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003385 for (auto Loc : NameModifierLoc) {
3386 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3387 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003388 ErrorFound = true;
3389 }
3390 return ErrorFound;
3391}
3392
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003393StmtResult Sema::ActOnOpenMPExecutableDirective(
3394 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3395 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3396 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003397 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003398 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3399 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003400 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003401
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003402 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003403 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003404 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003405 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003406 if (AStmt) {
3407 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3408
3409 // Check default data sharing attributes for referenced variables.
3410 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3411 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3412 if (DSAChecker.isErrorFound())
3413 return StmtError();
3414 // Generate list of implicitly defined firstprivate variables.
3415 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003416
3417 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3418 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3419 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3420 SourceLocation(), SourceLocation())) {
3421 ClausesWithImplicit.push_back(Implicit);
3422 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3423 DSAChecker.getImplicitFirstprivate().size();
3424 } else
3425 ErrorFound = true;
3426 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003427 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003428
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003429 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003430 switch (Kind) {
3431 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003432 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3433 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003434 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003435 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003436 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003437 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3438 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003439 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003440 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003441 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3442 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003443 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003444 case OMPD_for_simd:
3445 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3446 EndLoc, VarsWithInheritedDSA);
3447 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003448 case OMPD_sections:
3449 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3450 EndLoc);
3451 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003452 case OMPD_section:
3453 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003454 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003455 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3456 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003457 case OMPD_single:
3458 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3459 EndLoc);
3460 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003461 case OMPD_master:
3462 assert(ClausesWithImplicit.empty() &&
3463 "No clauses are allowed for 'omp master' directive");
3464 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3465 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003466 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003467 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3468 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003469 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003470 case OMPD_parallel_for:
3471 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3472 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003473 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003474 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003475 case OMPD_parallel_for_simd:
3476 Res = ActOnOpenMPParallelForSimdDirective(
3477 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003478 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003479 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003480 case OMPD_parallel_sections:
3481 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3482 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003483 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003484 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003485 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003486 Res =
3487 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003488 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003489 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003490 case OMPD_taskyield:
3491 assert(ClausesWithImplicit.empty() &&
3492 "No clauses are allowed for 'omp taskyield' directive");
3493 assert(AStmt == nullptr &&
3494 "No associated statement allowed for 'omp taskyield' directive");
3495 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3496 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003497 case OMPD_barrier:
3498 assert(ClausesWithImplicit.empty() &&
3499 "No clauses are allowed for 'omp barrier' directive");
3500 assert(AStmt == nullptr &&
3501 "No associated statement allowed for 'omp barrier' directive");
3502 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3503 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003504 case OMPD_taskwait:
3505 assert(ClausesWithImplicit.empty() &&
3506 "No clauses are allowed for 'omp taskwait' directive");
3507 assert(AStmt == nullptr &&
3508 "No associated statement allowed for 'omp taskwait' directive");
3509 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3510 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003511 case OMPD_taskgroup:
3512 assert(ClausesWithImplicit.empty() &&
3513 "No clauses are allowed for 'omp taskgroup' directive");
3514 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3515 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003516 case OMPD_flush:
3517 assert(AStmt == nullptr &&
3518 "No associated statement allowed for 'omp flush' directive");
3519 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3520 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003521 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003522 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3523 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003524 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003525 case OMPD_atomic:
3526 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3527 EndLoc);
3528 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003529 case OMPD_teams:
3530 Res =
3531 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3532 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003533 case OMPD_target:
3534 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3535 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003536 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003537 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003538 case OMPD_target_parallel:
3539 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3540 StartLoc, EndLoc);
3541 AllowedNameModifiers.push_back(OMPD_target);
3542 AllowedNameModifiers.push_back(OMPD_parallel);
3543 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003544 case OMPD_target_parallel_for:
3545 Res = ActOnOpenMPTargetParallelForDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3547 AllowedNameModifiers.push_back(OMPD_target);
3548 AllowedNameModifiers.push_back(OMPD_parallel);
3549 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003550 case OMPD_cancellation_point:
3551 assert(ClausesWithImplicit.empty() &&
3552 "No clauses are allowed for 'omp cancellation point' directive");
3553 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3554 "cancellation point' directive");
3555 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3556 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003557 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003558 assert(AStmt == nullptr &&
3559 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003560 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3561 CancelRegion);
3562 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003563 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003564 case OMPD_target_data:
3565 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3566 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003567 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003568 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003569 case OMPD_target_enter_data:
3570 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3571 EndLoc);
3572 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3573 break;
Samuel Antao72590762016-01-19 20:04:50 +00003574 case OMPD_target_exit_data:
3575 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3576 EndLoc);
3577 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3578 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003579 case OMPD_taskloop:
3580 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3581 EndLoc, VarsWithInheritedDSA);
3582 AllowedNameModifiers.push_back(OMPD_taskloop);
3583 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003584 case OMPD_taskloop_simd:
3585 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc, VarsWithInheritedDSA);
3587 AllowedNameModifiers.push_back(OMPD_taskloop);
3588 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003589 case OMPD_distribute:
3590 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3591 EndLoc, VarsWithInheritedDSA);
3592 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003593 case OMPD_target_update:
3594 assert(!AStmt && "Statement is not allowed for target update");
3595 Res =
3596 ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3597 AllowedNameModifiers.push_back(OMPD_target_update);
3598 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003599 case OMPD_distribute_parallel_for:
3600 Res = ActOnOpenMPDistributeParallelForDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_parallel);
3603 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003604 case OMPD_distribute_parallel_for_simd:
3605 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3606 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3607 AllowedNameModifiers.push_back(OMPD_parallel);
3608 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003609 case OMPD_distribute_simd:
3610 Res = ActOnOpenMPDistributeSimdDirective(
3611 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3612 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003613 case OMPD_declare_target:
3614 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003615 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003616 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003617 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003618 llvm_unreachable("OpenMP Directive is not allowed");
3619 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003620 llvm_unreachable("Unknown OpenMP directive");
3621 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003622
Alexey Bataev4acb8592014-07-07 13:01:15 +00003623 for (auto P : VarsWithInheritedDSA) {
3624 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3625 << P.first << P.second->getSourceRange();
3626 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003627 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3628
3629 if (!AllowedNameModifiers.empty())
3630 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3631 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003632
Alexey Bataeved09d242014-05-28 05:53:51 +00003633 if (ErrorFound)
3634 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003635 return Res;
3636}
3637
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003638Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3639 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003640 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003641 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3642 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003643 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003644 assert(Linears.size() == LinModifiers.size());
3645 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003646 if (!DG || DG.get().isNull())
3647 return DeclGroupPtrTy();
3648
3649 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003650 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003651 return DG;
3652 }
3653 auto *ADecl = DG.get().getSingleDecl();
3654 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3655 ADecl = FTD->getTemplatedDecl();
3656
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003657 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3658 if (!FD) {
3659 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003660 return DeclGroupPtrTy();
3661 }
3662
Alexey Bataev2af33e32016-04-07 12:45:37 +00003663 // OpenMP [2.8.2, declare simd construct, Description]
3664 // The parameter of the simdlen clause must be a constant positive integer
3665 // expression.
3666 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003667 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003668 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003669 // OpenMP [2.8.2, declare simd construct, Description]
3670 // The special this pointer can be used as if was one of the arguments to the
3671 // function in any of the linear, aligned, or uniform clauses.
3672 // The uniform clause declares one or more arguments to have an invariant
3673 // value for all concurrent invocations of the function in the execution of a
3674 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003675 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3676 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003677 for (auto *E : Uniforms) {
3678 E = E->IgnoreParenImpCasts();
3679 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3680 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3681 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3682 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003683 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3684 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003685 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003686 }
3687 if (isa<CXXThisExpr>(E)) {
3688 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003689 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003690 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003691 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3692 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003693 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003694 // OpenMP [2.8.2, declare simd construct, Description]
3695 // The aligned clause declares that the object to which each list item points
3696 // is aligned to the number of bytes expressed in the optional parameter of
3697 // the aligned clause.
3698 // The special this pointer can be used as if was one of the arguments to the
3699 // function in any of the linear, aligned, or uniform clauses.
3700 // The type of list items appearing in the aligned clause must be array,
3701 // pointer, reference to array, or reference to pointer.
3702 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3703 Expr *AlignedThis = nullptr;
3704 for (auto *E : Aligneds) {
3705 E = E->IgnoreParenImpCasts();
3706 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3707 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3708 auto *CanonPVD = PVD->getCanonicalDecl();
3709 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3710 FD->getParamDecl(PVD->getFunctionScopeIndex())
3711 ->getCanonicalDecl() == CanonPVD) {
3712 // OpenMP [2.8.1, simd construct, Restrictions]
3713 // A list-item cannot appear in more than one aligned clause.
3714 if (AlignedArgs.count(CanonPVD) > 0) {
3715 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3716 << 1 << E->getSourceRange();
3717 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3718 diag::note_omp_explicit_dsa)
3719 << getOpenMPClauseName(OMPC_aligned);
3720 continue;
3721 }
3722 AlignedArgs[CanonPVD] = E;
3723 QualType QTy = PVD->getType()
3724 .getNonReferenceType()
3725 .getUnqualifiedType()
3726 .getCanonicalType();
3727 const Type *Ty = QTy.getTypePtrOrNull();
3728 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3729 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3730 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3731 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3732 }
3733 continue;
3734 }
3735 }
3736 if (isa<CXXThisExpr>(E)) {
3737 if (AlignedThis) {
3738 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3739 << 2 << E->getSourceRange();
3740 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3741 << getOpenMPClauseName(OMPC_aligned);
3742 }
3743 AlignedThis = E;
3744 continue;
3745 }
3746 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3747 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3748 }
3749 // The optional parameter of the aligned clause, alignment, must be a constant
3750 // positive integer expression. If no optional parameter is specified,
3751 // implementation-defined default alignments for SIMD instructions on the
3752 // target platforms are assumed.
3753 SmallVector<Expr *, 4> NewAligns;
3754 for (auto *E : Alignments) {
3755 ExprResult Align;
3756 if (E)
3757 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3758 NewAligns.push_back(Align.get());
3759 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003760 // OpenMP [2.8.2, declare simd construct, Description]
3761 // The linear clause declares one or more list items to be private to a SIMD
3762 // lane and to have a linear relationship with respect to the iteration space
3763 // of a loop.
3764 // The special this pointer can be used as if was one of the arguments to the
3765 // function in any of the linear, aligned, or uniform clauses.
3766 // When a linear-step expression is specified in a linear clause it must be
3767 // either a constant integer expression or an integer-typed parameter that is
3768 // specified in a uniform clause on the directive.
3769 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3770 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3771 auto MI = LinModifiers.begin();
3772 for (auto *E : Linears) {
3773 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3774 ++MI;
3775 E = E->IgnoreParenImpCasts();
3776 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3777 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3778 auto *CanonPVD = PVD->getCanonicalDecl();
3779 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3780 FD->getParamDecl(PVD->getFunctionScopeIndex())
3781 ->getCanonicalDecl() == CanonPVD) {
3782 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3783 // A list-item cannot appear in more than one linear clause.
3784 if (LinearArgs.count(CanonPVD) > 0) {
3785 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3786 << getOpenMPClauseName(OMPC_linear)
3787 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3788 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3789 diag::note_omp_explicit_dsa)
3790 << getOpenMPClauseName(OMPC_linear);
3791 continue;
3792 }
3793 // Each argument can appear in at most one uniform or linear clause.
3794 if (UniformedArgs.count(CanonPVD) > 0) {
3795 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3796 << getOpenMPClauseName(OMPC_linear)
3797 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3798 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3799 diag::note_omp_explicit_dsa)
3800 << getOpenMPClauseName(OMPC_uniform);
3801 continue;
3802 }
3803 LinearArgs[CanonPVD] = E;
3804 if (E->isValueDependent() || E->isTypeDependent() ||
3805 E->isInstantiationDependent() ||
3806 E->containsUnexpandedParameterPack())
3807 continue;
3808 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3809 PVD->getOriginalType());
3810 continue;
3811 }
3812 }
3813 if (isa<CXXThisExpr>(E)) {
3814 if (UniformedLinearThis) {
3815 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3816 << getOpenMPClauseName(OMPC_linear)
3817 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3818 << E->getSourceRange();
3819 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3820 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3821 : OMPC_linear);
3822 continue;
3823 }
3824 UniformedLinearThis = E;
3825 if (E->isValueDependent() || E->isTypeDependent() ||
3826 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3827 continue;
3828 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3829 E->getType());
3830 continue;
3831 }
3832 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3833 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3834 }
3835 Expr *Step = nullptr;
3836 Expr *NewStep = nullptr;
3837 SmallVector<Expr *, 4> NewSteps;
3838 for (auto *E : Steps) {
3839 // Skip the same step expression, it was checked already.
3840 if (Step == E || !E) {
3841 NewSteps.push_back(E ? NewStep : nullptr);
3842 continue;
3843 }
3844 Step = E;
3845 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3846 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3847 auto *CanonPVD = PVD->getCanonicalDecl();
3848 if (UniformedArgs.count(CanonPVD) == 0) {
3849 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3850 << Step->getSourceRange();
3851 } else if (E->isValueDependent() || E->isTypeDependent() ||
3852 E->isInstantiationDependent() ||
3853 E->containsUnexpandedParameterPack() ||
3854 CanonPVD->getType()->hasIntegerRepresentation())
3855 NewSteps.push_back(Step);
3856 else {
3857 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3858 << Step->getSourceRange();
3859 }
3860 continue;
3861 }
3862 NewStep = Step;
3863 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3864 !Step->isInstantiationDependent() &&
3865 !Step->containsUnexpandedParameterPack()) {
3866 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3867 .get();
3868 if (NewStep)
3869 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3870 }
3871 NewSteps.push_back(NewStep);
3872 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003873 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3874 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003875 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003876 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3877 const_cast<Expr **>(Linears.data()), Linears.size(),
3878 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3879 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003880 ADecl->addAttr(NewAttr);
3881 return ConvertDeclToDeclGroup(ADecl);
3882}
3883
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003884StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3885 Stmt *AStmt,
3886 SourceLocation StartLoc,
3887 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003888 if (!AStmt)
3889 return StmtError();
3890
Alexey Bataev9959db52014-05-06 10:08:46 +00003891 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3892 // 1.2.2 OpenMP Language Terminology
3893 // Structured block - An executable statement with a single entry at the
3894 // top and a single exit at the bottom.
3895 // The point of exit cannot be a branch out of the structured block.
3896 // longjmp() and throw() must not violate the entry/exit criteria.
3897 CS->getCapturedDecl()->setNothrow();
3898
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003899 getCurFunction()->setHasBranchProtectedScope();
3900
Alexey Bataev25e5b442015-09-15 12:52:43 +00003901 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3902 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003903}
3904
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905namespace {
3906/// \brief Helper class for checking canonical form of the OpenMP loops and
3907/// extracting iteration space of each loop in the loop nest, that will be used
3908/// for IR generation.
3909class OpenMPIterationSpaceChecker {
3910 /// \brief Reference to Sema.
3911 Sema &SemaRef;
3912 /// \brief A location for diagnostics (when there is no some better location).
3913 SourceLocation DefaultLoc;
3914 /// \brief A location for diagnostics (when increment is not compatible).
3915 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 /// \brief A source location for referring to loop init later.
3917 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 /// \brief A source location for referring to condition later.
3919 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003920 /// \brief A source location for referring to increment later.
3921 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003922 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003923 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003924 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003925 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003926 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003927 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003928 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003929 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003930 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003931 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003932 /// \brief This flag is true when condition is one of:
3933 /// Var < UB
3934 /// Var <= UB
3935 /// UB > Var
3936 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003939 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003941 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942
3943public:
3944 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003945 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 /// \brief Check init-expr for canonical loop form and save loop counter
3947 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003948 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3950 /// for less/greater and for strict/non-strict comparison.
3951 bool CheckCond(Expr *S);
3952 /// \brief Check incr-expr for canonical loop form and return true if it
3953 /// does not conform, otherwise save loop step (#Step).
3954 bool CheckInc(Expr *S);
3955 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003956 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003957 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003959 /// \brief Source range of the loop init.
3960 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3961 /// \brief Source range of the loop condition.
3962 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3963 /// \brief Source range of the loop increment.
3964 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3965 /// \brief True if the step should be subtracted.
3966 bool ShouldSubtractStep() const { return SubtractStep; }
3967 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003968 Expr *
3969 BuildNumIterations(Scope *S, const bool LimitedType,
3970 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003971 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003972 Expr *BuildPreCond(Scope *S, Expr *Cond,
3973 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003974 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003975 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3976 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003977 /// \brief Build reference expression to the private counter be used for
3978 /// codegen.
3979 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003980 /// \brief Build initization of the counter be used for codegen.
3981 Expr *BuildCounterInit() const;
3982 /// \brief Build step of the counter be used for codegen.
3983 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003984 /// \brief Return true if any expression is dependent.
3985 bool Dependent() const;
3986
3987private:
3988 /// \brief Check the right-hand side of an assignment in the increment
3989 /// expression.
3990 bool CheckIncRHS(Expr *RHS);
3991 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003992 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003993 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003994 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003995 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003996 /// \brief Helper to set loop increment.
3997 bool SetStep(Expr *NewStep, bool Subtract);
3998};
3999
4000bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 assert(!LB && !UB && !Step);
4003 return false;
4004 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004005 return LCDecl->getType()->isDependentType() ||
4006 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4007 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008}
4009
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004010static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004011 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4012 E = ExprTemp->getSubExpr();
4013
4014 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4015 E = MTE->GetTemporaryExpr();
4016
4017 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4018 E = Binder->getSubExpr();
4019
4020 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4021 E = ICE->getSubExprAsWritten();
4022 return E->IgnoreParens();
4023}
4024
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4026 Expr *NewLCRefExpr,
4027 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004030 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004031 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004032 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004033 LCDecl = getCanonicalDecl(NewLCDecl);
4034 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004035 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4036 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004037 if ((Ctor->isCopyOrMoveConstructor() ||
4038 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4039 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004040 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041 LB = NewLB;
4042 return false;
4043}
4044
4045bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004046 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4049 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 if (!NewUB)
4051 return true;
4052 UB = NewUB;
4053 TestIsLessOp = LessOp;
4054 TestIsStrictOp = StrictOp;
4055 ConditionSrcRange = SR;
4056 ConditionLoc = SL;
4057 return false;
4058}
4059
4060bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4061 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004062 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063 if (!NewStep)
4064 return true;
4065 if (!NewStep->isValueDependent()) {
4066 // Check that the step is integer expression.
4067 SourceLocation StepLoc = NewStep->getLocStart();
4068 ExprResult Val =
4069 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4070 if (Val.isInvalid())
4071 return true;
4072 NewStep = Val.get();
4073
4074 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4075 // If test-expr is of form var relational-op b and relational-op is < or
4076 // <= then incr-expr must cause var to increase on each iteration of the
4077 // loop. If test-expr is of form var relational-op b and relational-op is
4078 // > or >= then incr-expr must cause var to decrease on each iteration of
4079 // the loop.
4080 // If test-expr is of form b relational-op var and relational-op is < or
4081 // <= then incr-expr must cause var to decrease on each iteration of the
4082 // loop. If test-expr is of form b relational-op var and relational-op is
4083 // > or >= then incr-expr must cause var to increase on each iteration of
4084 // the loop.
4085 llvm::APSInt Result;
4086 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4087 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4088 bool IsConstNeg =
4089 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090 bool IsConstPos =
4091 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004092 bool IsConstZero = IsConstant && !Result.getBoolValue();
4093 if (UB && (IsConstZero ||
4094 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004095 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 SemaRef.Diag(NewStep->getExprLoc(),
4097 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004099 SemaRef.Diag(ConditionLoc,
4100 diag::note_omp_loop_cond_requres_compatible_incr)
4101 << TestIsLessOp << ConditionSrcRange;
4102 return true;
4103 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104 if (TestIsLessOp == Subtract) {
4105 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4106 NewStep).get();
4107 Subtract = !Subtract;
4108 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 }
4110
4111 Step = NewStep;
4112 SubtractStep = Subtract;
4113 return false;
4114}
4115
Alexey Bataev9c821032015-04-30 04:23:23 +00004116bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004117 // Check init-expr for canonical loop form and save loop counter
4118 // variable - #Var and its initialization value - #LB.
4119 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4120 // var = lb
4121 // integer-type var = lb
4122 // random-access-iterator-type var = lb
4123 // pointer-type var = lb
4124 //
4125 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004126 if (EmitDiags) {
4127 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4128 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129 return true;
4130 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004131 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4132 if (!ExprTemp->cleanupsHaveSideEffects())
4133 S = ExprTemp->getSubExpr();
4134
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004136 if (Expr *E = dyn_cast<Expr>(S))
4137 S = E->IgnoreParens();
4138 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004139 if (BO->getOpcode() == BO_Assign) {
4140 auto *LHS = BO->getLHS()->IgnoreParens();
4141 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4142 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4143 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4144 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4145 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4146 }
4147 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4148 if (ME->isArrow() &&
4149 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4150 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4151 }
4152 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4154 if (DS->isSingleDecl()) {
4155 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004156 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004157 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004158 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 SemaRef.Diag(S->getLocStart(),
4160 diag::ext_omp_loop_not_canonical_init)
4161 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004162 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163 }
4164 }
4165 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004166 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4167 if (CE->getOperator() == OO_Equal) {
4168 auto *LHS = CE->getArg(0);
4169 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4170 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4171 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4172 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4173 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4174 }
4175 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4176 if (ME->isArrow() &&
4177 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4178 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4179 }
4180 }
4181 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004182
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004183 if (Dependent() || SemaRef.CurContext->isDependentContext())
4184 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004185 if (EmitDiags) {
4186 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4187 << S->getSourceRange();
4188 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 return true;
4190}
4191
Alexey Bataev23b69422014-06-18 07:08:49 +00004192/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004194static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004196 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004197 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4199 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004200 if ((Ctor->isCopyOrMoveConstructor() ||
4201 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4202 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004204 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4205 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4206 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4207 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4208 return getCanonicalDecl(ME->getMemberDecl());
4209 return getCanonicalDecl(VD);
4210 }
4211 }
4212 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4213 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4214 return getCanonicalDecl(ME->getMemberDecl());
4215 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004216}
4217
4218bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4219 // Check test-expr for canonical form, save upper-bound UB, flags for
4220 // less/greater and for strict/non-strict comparison.
4221 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4222 // var relational-op b
4223 // b relational-op var
4224 //
4225 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004226 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227 return true;
4228 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004229 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 SourceLocation CondLoc = S->getLocStart();
4231 if (auto BO = dyn_cast<BinaryOperator>(S)) {
4232 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004233 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 return SetUB(BO->getRHS(),
4235 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4236 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4237 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004239 return SetUB(BO->getLHS(),
4240 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4241 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4242 BO->getSourceRange(), BO->getOperatorLoc());
4243 }
4244 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4245 if (CE->getNumArgs() == 2) {
4246 auto Op = CE->getOperator();
4247 switch (Op) {
4248 case OO_Greater:
4249 case OO_GreaterEqual:
4250 case OO_Less:
4251 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004252 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004253 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4254 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4255 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004256 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004257 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4258 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4259 CE->getOperatorLoc());
4260 break;
4261 default:
4262 break;
4263 }
4264 }
4265 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004266 if (Dependent() || SemaRef.CurContext->isDependentContext())
4267 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004269 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270 return true;
4271}
4272
4273bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4274 // RHS of canonical loop form increment can be:
4275 // var + incr
4276 // incr + var
4277 // var - incr
4278 //
4279 RHS = RHS->IgnoreParenImpCasts();
4280 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4281 if (BO->isAdditiveOp()) {
4282 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004283 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004284 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004285 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004286 return SetStep(BO->getLHS(), false);
4287 }
4288 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4289 bool IsAdd = CE->getOperator() == OO_Plus;
4290 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004291 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004292 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004293 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004294 return SetStep(CE->getArg(0), false);
4295 }
4296 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004297 if (Dependent() || SemaRef.CurContext->isDependentContext())
4298 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004299 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004300 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 return true;
4302}
4303
4304bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4305 // Check incr-expr for canonical loop form and return true if it
4306 // does not conform.
4307 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4308 // ++var
4309 // var++
4310 // --var
4311 // var--
4312 // var += incr
4313 // var -= incr
4314 // var = var + incr
4315 // var = incr + var
4316 // var = var - incr
4317 //
4318 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004319 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004320 return true;
4321 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004322 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4323 if (!ExprTemp->cleanupsHaveSideEffects())
4324 S = ExprTemp->getSubExpr();
4325
Alexander Musmana5f070a2014-10-01 06:03:56 +00004326 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 S = S->IgnoreParens();
4328 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004329 if (UO->isIncrementDecrementOp() &&
4330 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return SetStep(
4332 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4333 (UO->isDecrementOp() ? -1 : 1)).get(),
4334 false);
4335 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4336 switch (BO->getOpcode()) {
4337 case BO_AddAssign:
4338 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004339 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004340 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4341 break;
4342 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004343 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004344 return CheckIncRHS(BO->getRHS());
4345 break;
4346 default:
4347 break;
4348 }
4349 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4350 switch (CE->getOperator()) {
4351 case OO_PlusPlus:
4352 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004353 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004354 return SetStep(
4355 SemaRef.ActOnIntegerConstant(
4356 CE->getLocStart(),
4357 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4358 false);
4359 break;
4360 case OO_PlusEqual:
4361 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004362 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004363 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4364 break;
4365 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004366 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004367 return CheckIncRHS(CE->getArg(1));
4368 break;
4369 default:
4370 break;
4371 }
4372 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004373 if (Dependent() || SemaRef.CurContext->isDependentContext())
4374 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004375 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004376 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004377 return true;
4378}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379
Alexey Bataev5a3af132016-03-29 08:58:54 +00004380static ExprResult
4381tryBuildCapture(Sema &SemaRef, Expr *Capture,
4382 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004383 if (SemaRef.CurContext->isDependentContext())
4384 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004385 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4386 return SemaRef.PerformImplicitConversion(
4387 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4388 /*AllowExplicit=*/true);
4389 auto I = Captures.find(Capture);
4390 if (I != Captures.end())
4391 return buildCapture(SemaRef, Capture, I->second);
4392 DeclRefExpr *Ref = nullptr;
4393 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4394 Captures[Capture] = Ref;
4395 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004396}
4397
Alexander Musmana5f070a2014-10-01 06:03:56 +00004398/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004399Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4400 Scope *S, const bool LimitedType,
4401 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004402 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004403 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004404 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004405 SemaRef.getLangOpts().CPlusPlus) {
4406 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004407 auto *UBExpr = TestIsLessOp ? UB : LB;
4408 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004409 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4410 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004411 if (!Upper || !Lower)
4412 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413
4414 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4415
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004416 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 // BuildBinOp already emitted error, this one is to point user to upper
4418 // and lower bound, and to tell what is passed to 'operator-'.
4419 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4420 << Upper->getSourceRange() << Lower->getSourceRange();
4421 return nullptr;
4422 }
4423 }
4424
4425 if (!Diff.isUsable())
4426 return nullptr;
4427
4428 // Upper - Lower [- 1]
4429 if (TestIsStrictOp)
4430 Diff = SemaRef.BuildBinOp(
4431 S, DefaultLoc, BO_Sub, Diff.get(),
4432 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4433 if (!Diff.isUsable())
4434 return nullptr;
4435
4436 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004437 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4438 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004439 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004440 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004441 if (!Diff.isUsable())
4442 return nullptr;
4443
4444 // Parentheses (for dumping/debugging purposes only).
4445 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4446 if (!Diff.isUsable())
4447 return nullptr;
4448
4449 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004450 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004451 if (!Diff.isUsable())
4452 return nullptr;
4453
Alexander Musman174b3ca2014-10-06 11:16:29 +00004454 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004455 QualType Type = Diff.get()->getType();
4456 auto &C = SemaRef.Context;
4457 bool UseVarType = VarType->hasIntegerRepresentation() &&
4458 C.getTypeSize(Type) > C.getTypeSize(VarType);
4459 if (!Type->isIntegerType() || UseVarType) {
4460 unsigned NewSize =
4461 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4462 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4463 : Type->hasSignedIntegerRepresentation();
4464 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004465 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4466 Diff = SemaRef.PerformImplicitConversion(
4467 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4468 if (!Diff.isUsable())
4469 return nullptr;
4470 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004471 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004472 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004473 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4474 if (NewSize != C.getTypeSize(Type)) {
4475 if (NewSize < C.getTypeSize(Type)) {
4476 assert(NewSize == 64 && "incorrect loop var size");
4477 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4478 << InitSrcRange << ConditionSrcRange;
4479 }
4480 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004481 NewSize, Type->hasSignedIntegerRepresentation() ||
4482 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004483 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4484 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4485 Sema::AA_Converting, true);
4486 if (!Diff.isUsable())
4487 return nullptr;
4488 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004489 }
4490 }
4491
Alexander Musmana5f070a2014-10-01 06:03:56 +00004492 return Diff.get();
4493}
4494
Alexey Bataev5a3af132016-03-29 08:58:54 +00004495Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4496 Scope *S, Expr *Cond,
4497 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004498 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4499 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4500 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004501
Alexey Bataev5a3af132016-03-29 08:58:54 +00004502 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4503 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4504 if (!NewLB.isUsable() || !NewUB.isUsable())
4505 return nullptr;
4506
Alexey Bataev62dbb972015-04-22 11:59:37 +00004507 auto CondExpr = SemaRef.BuildBinOp(
4508 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4509 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004510 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004511 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004512 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4513 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004514 CondExpr = SemaRef.PerformImplicitConversion(
4515 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4516 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004517 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004518 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4519 // Otherwise use original loop conditon and evaluate it in runtime.
4520 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4521}
4522
Alexander Musmana5f070a2014-10-01 06:03:56 +00004523/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004524DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004525 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004526 auto *VD = dyn_cast<VarDecl>(LCDecl);
4527 if (!VD) {
4528 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4529 auto *Ref = buildDeclRefExpr(
4530 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004531 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4532 // If the loop control decl is explicitly marked as private, do not mark it
4533 // as captured again.
4534 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4535 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004536 return Ref;
4537 }
4538 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004539 DefaultLoc);
4540}
4541
4542Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004543 if (LCDecl && !LCDecl->isInvalidDecl()) {
4544 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004545 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004546 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4547 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004548 if (PrivateVar->isInvalidDecl())
4549 return nullptr;
4550 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4551 }
4552 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004553}
4554
4555/// \brief Build initization of the counter be used for codegen.
4556Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4557
4558/// \brief Build step of the counter be used for codegen.
4559Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4560
4561/// \brief Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004562struct LoopIterationSpace final {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004563 /// \brief Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004564 Expr *PreCond = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004565 /// \brief This expression calculates the number of iterations in the loop.
4566 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004567 Expr *NumIterations = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004568 /// \brief The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004569 Expr *CounterVar = nullptr;
Alexey Bataeva8899172015-08-06 12:30:57 +00004570 /// \brief Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004571 Expr *PrivateCounterVar = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004572 /// \brief This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004573 Expr *CounterInit = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 /// \brief This is step for the #CounterVar used to generate its update:
4575 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004576 Expr *CounterStep = nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004577 /// \brief Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004578 bool Subtract = false;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004579 /// \brief Source range of the loop init.
4580 SourceRange InitSrcRange;
4581 /// \brief Source range of the loop condition.
4582 SourceRange CondSrcRange;
4583 /// \brief Source range of the loop increment.
4584 SourceRange IncSrcRange;
4585};
4586
Alexey Bataev23b69422014-06-18 07:08:49 +00004587} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004588
Alexey Bataev9c821032015-04-30 04:23:23 +00004589void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4590 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4591 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004592 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4593 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004594 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4595 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004596 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4597 if (auto *D = ISC.GetLoopDecl()) {
4598 auto *VD = dyn_cast<VarDecl>(D);
4599 if (!VD) {
4600 if (auto *Private = IsOpenMPCapturedDecl(D))
4601 VD = Private;
4602 else {
4603 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4604 /*WithInit=*/false);
4605 VD = cast<VarDecl>(Ref->getDecl());
4606 }
4607 }
4608 DSAStack->addLoopControlVariable(D, VD);
4609 }
4610 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004611 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004612 }
4613}
4614
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004615/// \brief Called on a for stmt to check and extract its iteration space
4616/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004617static bool CheckOpenMPIterationSpace(
4618 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4619 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004620 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004621 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004622 LoopIterationSpace &ResultIterSpace,
4623 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004624 // OpenMP [2.6, Canonical Loop Form]
4625 // for (init-expr; test-expr; incr-expr) structured-block
4626 auto For = dyn_cast_or_null<ForStmt>(S);
4627 if (!For) {
4628 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004629 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4630 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4631 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4632 if (NestedLoopCount > 1) {
4633 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4634 SemaRef.Diag(DSA.getConstructLoc(),
4635 diag::note_omp_collapse_ordered_expr)
4636 << 2 << CollapseLoopCountExpr->getSourceRange()
4637 << OrderedLoopCountExpr->getSourceRange();
4638 else if (CollapseLoopCountExpr)
4639 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4640 diag::note_omp_collapse_ordered_expr)
4641 << 0 << CollapseLoopCountExpr->getSourceRange();
4642 else
4643 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4644 diag::note_omp_collapse_ordered_expr)
4645 << 1 << OrderedLoopCountExpr->getSourceRange();
4646 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004647 return true;
4648 }
4649 assert(For->getBody());
4650
4651 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4652
4653 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004654 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004655 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004656 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004657
4658 bool HasErrors = false;
4659
4660 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004661 if (auto *LCDecl = ISC.GetLoopDecl()) {
4662 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004663
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004664 // OpenMP [2.6, Canonical Loop Form]
4665 // Var is one of the following:
4666 // A variable of signed or unsigned integer type.
4667 // For C++, a variable of a random access iterator type.
4668 // For C, a variable of a pointer type.
4669 auto VarType = LCDecl->getType().getNonReferenceType();
4670 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4671 !VarType->isPointerType() &&
4672 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4673 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4674 << SemaRef.getLangOpts().CPlusPlus;
4675 HasErrors = true;
4676 }
4677
4678 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4679 // a Construct
4680 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4681 // parallel for construct is (are) private.
4682 // The loop iteration variable in the associated for-loop of a simd
4683 // construct with just one associated for-loop is linear with a
4684 // constant-linear-step that is the increment of the associated for-loop.
4685 // Exclude loop var from the list of variables with implicitly defined data
4686 // sharing attributes.
4687 VarsWithImplicitDSA.erase(LCDecl);
4688
4689 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4690 // in a Construct, C/C++].
4691 // The loop iteration variable in the associated for-loop of a simd
4692 // construct with just one associated for-loop may be listed in a linear
4693 // clause with a constant-linear-step that is the increment of the
4694 // associated for-loop.
4695 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4696 // parallel for construct may be listed in a private or lastprivate clause.
4697 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4698 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4699 // declared in the loop and it is predetermined as a private.
4700 auto PredeterminedCKind =
4701 isOpenMPSimdDirective(DKind)
4702 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4703 : OMPC_private;
4704 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4705 DVar.CKind != PredeterminedCKind) ||
4706 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4707 isOpenMPDistributeDirective(DKind)) &&
4708 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4709 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4710 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4711 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4712 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4713 << getOpenMPClauseName(PredeterminedCKind);
4714 if (DVar.RefExpr == nullptr)
4715 DVar.CKind = PredeterminedCKind;
4716 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4717 HasErrors = true;
4718 } else if (LoopDeclRefExpr != nullptr) {
4719 // Make the loop iteration variable private (for worksharing constructs),
4720 // linear (for simd directives with the only one associated loop) or
4721 // lastprivate (for simd directives with several collapsed or ordered
4722 // loops).
4723 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004724 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4725 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004726 /*FromParent=*/false);
4727 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4728 }
4729
4730 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4731
4732 // Check test-expr.
4733 HasErrors |= ISC.CheckCond(For->getCond());
4734
4735 // Check incr-expr.
4736 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004737 }
4738
Alexander Musmana5f070a2014-10-01 06:03:56 +00004739 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004740 return HasErrors;
4741
Alexander Musmana5f070a2014-10-01 06:03:56 +00004742 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004743 ResultIterSpace.PreCond =
4744 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004745 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004746 DSA.getCurScope(),
4747 (isOpenMPWorksharingDirective(DKind) ||
4748 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4749 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004750 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004751 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004752 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4753 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4754 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4755 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4756 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4757 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4758
Alexey Bataev62dbb972015-04-22 11:59:37 +00004759 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4760 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004761 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004762 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004763 ResultIterSpace.CounterInit == nullptr ||
4764 ResultIterSpace.CounterStep == nullptr);
4765
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004766 return HasErrors;
4767}
4768
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004769/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004770static ExprResult
4771BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4772 ExprResult Start,
4773 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004774 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004775 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4776 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004777 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004778 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004779 VarRef.get()->getType())) {
4780 NewStart = SemaRef.PerformImplicitConversion(
4781 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4782 /*AllowExplicit=*/true);
4783 if (!NewStart.isUsable())
4784 return ExprError();
4785 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004786
4787 auto Init =
4788 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4789 return Init;
4790}
4791
Alexander Musmana5f070a2014-10-01 06:03:56 +00004792/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004793static ExprResult
4794BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4795 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4796 ExprResult Step, bool Subtract,
4797 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 // Add parentheses (for debugging purposes only).
4799 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4800 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4801 !Step.isUsable())
4802 return ExprError();
4803
Alexey Bataev5a3af132016-03-29 08:58:54 +00004804 ExprResult NewStep = Step;
4805 if (Captures)
4806 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004807 if (NewStep.isInvalid())
4808 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004809 ExprResult Update =
4810 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004811 if (!Update.isUsable())
4812 return ExprError();
4813
Alexey Bataevc0214e02016-02-16 12:13:49 +00004814 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4815 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004816 ExprResult NewStart = Start;
4817 if (Captures)
4818 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004819 if (NewStart.isInvalid())
4820 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004821
Alexey Bataevc0214e02016-02-16 12:13:49 +00004822 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4823 ExprResult SavedUpdate = Update;
4824 ExprResult UpdateVal;
4825 if (VarRef.get()->getType()->isOverloadableType() ||
4826 NewStart.get()->getType()->isOverloadableType() ||
4827 Update.get()->getType()->isOverloadableType()) {
4828 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4829 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4830 Update =
4831 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4832 if (Update.isUsable()) {
4833 UpdateVal =
4834 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4835 VarRef.get(), SavedUpdate.get());
4836 if (UpdateVal.isUsable()) {
4837 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4838 UpdateVal.get());
4839 }
4840 }
4841 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4842 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004843
Alexey Bataevc0214e02016-02-16 12:13:49 +00004844 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4845 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4846 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4847 NewStart.get(), SavedUpdate.get());
4848 if (!Update.isUsable())
4849 return ExprError();
4850
Alexey Bataev11481f52016-02-17 10:29:05 +00004851 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4852 VarRef.get()->getType())) {
4853 Update = SemaRef.PerformImplicitConversion(
4854 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4855 if (!Update.isUsable())
4856 return ExprError();
4857 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004858
4859 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4860 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004861 return Update;
4862}
4863
4864/// \brief Convert integer expression \a E to make it have at least \a Bits
4865/// bits.
4866static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4867 Sema &SemaRef) {
4868 if (E == nullptr)
4869 return ExprError();
4870 auto &C = SemaRef.Context;
4871 QualType OldType = E->getType();
4872 unsigned HasBits = C.getTypeSize(OldType);
4873 if (HasBits >= Bits)
4874 return ExprResult(E);
4875 // OK to convert to signed, because new type has more bits than old.
4876 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4877 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4878 true);
4879}
4880
4881/// \brief Check if the given expression \a E is a constant integer that fits
4882/// into \a Bits bits.
4883static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4884 if (E == nullptr)
4885 return false;
4886 llvm::APSInt Result;
4887 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4888 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4889 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004890}
4891
Alexey Bataev5a3af132016-03-29 08:58:54 +00004892/// Build preinits statement for the given declarations.
4893static Stmt *buildPreInits(ASTContext &Context,
4894 SmallVectorImpl<Decl *> &PreInits) {
4895 if (!PreInits.empty()) {
4896 return new (Context) DeclStmt(
4897 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4898 SourceLocation(), SourceLocation());
4899 }
4900 return nullptr;
4901}
4902
4903/// Build preinits statement for the given declarations.
4904static Stmt *buildPreInits(ASTContext &Context,
4905 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4906 if (!Captures.empty()) {
4907 SmallVector<Decl *, 16> PreInits;
4908 for (auto &Pair : Captures)
4909 PreInits.push_back(Pair.second->getDecl());
4910 return buildPreInits(Context, PreInits);
4911 }
4912 return nullptr;
4913}
4914
4915/// Build postupdate expression for the given list of postupdates expressions.
4916static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4917 Expr *PostUpdate = nullptr;
4918 if (!PostUpdates.empty()) {
4919 for (auto *E : PostUpdates) {
4920 Expr *ConvE = S.BuildCStyleCastExpr(
4921 E->getExprLoc(),
4922 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4923 E->getExprLoc(), E)
4924 .get();
4925 PostUpdate = PostUpdate
4926 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4927 PostUpdate, ConvE)
4928 .get()
4929 : ConvE;
4930 }
4931 }
4932 return PostUpdate;
4933}
4934
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004935/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004936/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4937/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004938static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004939CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4940 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4941 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004942 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004943 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004944 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004945 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004946 // Found 'collapse' clause - calculate collapse number.
4947 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004948 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004949 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004950 }
4951 if (OrderedLoopCountExpr) {
4952 // Found 'ordered' clause - calculate collapse number.
4953 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004954 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4955 if (Result.getLimitedValue() < NestedLoopCount) {
4956 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4957 diag::err_omp_wrong_ordered_loop_count)
4958 << OrderedLoopCountExpr->getSourceRange();
4959 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4960 diag::note_collapse_loop_count)
4961 << CollapseLoopCountExpr->getSourceRange();
4962 }
4963 NestedLoopCount = Result.getLimitedValue();
4964 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004965 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004966 // This is helper routine for loop directives (e.g., 'for', 'simd',
4967 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004968 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004969 SmallVector<LoopIterationSpace, 4> IterSpaces;
4970 IterSpaces.resize(NestedLoopCount);
4971 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004972 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004973 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004974 NestedLoopCount, CollapseLoopCountExpr,
4975 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004976 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004977 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004978 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004979 // OpenMP [2.8.1, simd construct, Restrictions]
4980 // All loops associated with the construct must be perfectly nested; that
4981 // is, there must be no intervening code nor any OpenMP directive between
4982 // any two loops.
4983 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004984 }
4985
Alexander Musmana5f070a2014-10-01 06:03:56 +00004986 Built.clear(/* size */ NestedLoopCount);
4987
4988 if (SemaRef.CurContext->isDependentContext())
4989 return NestedLoopCount;
4990
4991 // An example of what is generated for the following code:
4992 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004993 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004994 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004995 // for (k = 0; k < NK; ++k)
4996 // for (j = J0; j < NJ; j+=2) {
4997 // <loop body>
4998 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004999 //
5000 // We generate the code below.
5001 // Note: the loop body may be outlined in CodeGen.
5002 // Note: some counters may be C++ classes, operator- is used to find number of
5003 // iterations and operator+= to calculate counter value.
5004 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5005 // or i64 is currently supported).
5006 //
5007 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5008 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5009 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5010 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5011 // // similar updates for vars in clauses (e.g. 'linear')
5012 // <loop body (using local i and j)>
5013 // }
5014 // i = NI; // assign final values of counters
5015 // j = NJ;
5016 //
5017
5018 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5019 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005020 // Precondition tests if there is at least one iteration (all conditions are
5021 // true).
5022 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005023 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005024 ExprResult LastIteration32 = WidenIterationCount(
5025 32 /* Bits */, SemaRef.PerformImplicitConversion(
5026 N0->IgnoreImpCasts(), N0->getType(),
5027 Sema::AA_Converting, /*AllowExplicit=*/true)
5028 .get(),
5029 SemaRef);
5030 ExprResult LastIteration64 = WidenIterationCount(
5031 64 /* Bits */, SemaRef.PerformImplicitConversion(
5032 N0->IgnoreImpCasts(), N0->getType(),
5033 Sema::AA_Converting, /*AllowExplicit=*/true)
5034 .get(),
5035 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005036
5037 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5038 return NestedLoopCount;
5039
5040 auto &C = SemaRef.Context;
5041 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5042
5043 Scope *CurScope = DSA.getCurScope();
5044 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005045 if (PreCond.isUsable()) {
5046 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5047 PreCond.get(), IterSpaces[Cnt].PreCond);
5048 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005049 auto N = IterSpaces[Cnt].NumIterations;
5050 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5051 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005052 LastIteration32 = SemaRef.BuildBinOp(
5053 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5054 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5055 Sema::AA_Converting,
5056 /*AllowExplicit=*/true)
5057 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005058 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005059 LastIteration64 = SemaRef.BuildBinOp(
5060 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5061 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5062 Sema::AA_Converting,
5063 /*AllowExplicit=*/true)
5064 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005065 }
5066
5067 // Choose either the 32-bit or 64-bit version.
5068 ExprResult LastIteration = LastIteration64;
5069 if (LastIteration32.isUsable() &&
5070 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5071 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5072 FitsInto(
5073 32 /* Bits */,
5074 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5075 LastIteration64.get(), SemaRef)))
5076 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005077 QualType VType = LastIteration.get()->getType();
5078 QualType RealVType = VType;
5079 QualType StrideVType = VType;
5080 if (isOpenMPTaskLoopDirective(DKind)) {
5081 VType =
5082 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5083 StrideVType =
5084 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5085 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086
5087 if (!LastIteration.isUsable())
5088 return 0;
5089
5090 // Save the number of iterations.
5091 ExprResult NumIterations = LastIteration;
5092 {
5093 LastIteration = SemaRef.BuildBinOp(
5094 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5095 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5096 if (!LastIteration.isUsable())
5097 return 0;
5098 }
5099
5100 // Calculate the last iteration number beforehand instead of doing this on
5101 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5102 llvm::APSInt Result;
5103 bool IsConstant =
5104 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5105 ExprResult CalcLastIteration;
5106 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005107 ExprResult SaveRef =
5108 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005109 LastIteration = SaveRef;
5110
5111 // Prepare SaveRef + 1.
5112 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005113 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005114 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5115 if (!NumIterations.isUsable())
5116 return 0;
5117 }
5118
5119 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5120
Alexander Musmanc6388682014-12-15 07:07:06 +00005121 // Build variables passed into runtime, nesessary for worksharing directives.
Carlo Bertolli9925f152016-06-27 14:55:37 +00005122 ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005123 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5124 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005125 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005126 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5127 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005128 SemaRef.AddInitializerToDecl(
5129 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5130 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5131
5132 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005133 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5134 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005135 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5136 /*DirectInit*/ false,
5137 /*TypeMayContainAuto*/ false);
5138
5139 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5140 // This will be used to implement clause 'lastprivate'.
5141 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005142 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5143 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005144 SemaRef.AddInitializerToDecl(
5145 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5146 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5147
5148 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005149 VarDecl *STDecl =
5150 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5151 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005152 SemaRef.AddInitializerToDecl(
5153 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5154 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5155
5156 // Build expression: UB = min(UB, LastIteration)
5157 // It is nesessary for CodeGen of directives with static scheduling.
5158 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5159 UB.get(), LastIteration.get());
5160 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5161 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5162 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5163 CondOp.get());
5164 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005165
5166 // If we have a combined directive that combines 'distribute', 'for' or
5167 // 'simd' we need to be able to access the bounds of the schedule of the
5168 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5169 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5170 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5171 auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5172
5173 // We expect to have at least 2 more parameters than the 'parallel'
5174 // directive does - the lower and upper bounds of the previous schedule.
5175 assert(CD->getNumParams() >= 4 &&
5176 "Unexpected number of parameters in loop combined directive");
5177
5178 // Set the proper type for the bounds given what we learned from the
5179 // enclosed loops.
5180 auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5181 auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5182
5183 // Previous lower and upper bounds are obtained from the region
5184 // parameters.
5185 PrevLB =
5186 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5187 PrevUB =
5188 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5189 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005190 }
5191
5192 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005193 ExprResult IV;
5194 ExprResult Init;
5195 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005196 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5197 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005198 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005199 isOpenMPTaskLoopDirective(DKind) ||
5200 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005201 ? LB.get()
5202 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5203 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5204 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005205 }
5206
Alexander Musmanc6388682014-12-15 07:07:06 +00005207 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005208 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00005209 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005210 (isOpenMPWorksharingDirective(DKind) ||
5211 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005212 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5213 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5214 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005215
5216 // Loop increment (IV = IV + 1)
5217 SourceLocation IncLoc;
5218 ExprResult Inc =
5219 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5220 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5221 if (!Inc.isUsable())
5222 return 0;
5223 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005224 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5225 if (!Inc.isUsable())
5226 return 0;
5227
5228 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5229 // Used for directives with static scheduling.
5230 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005231 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5232 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005233 // LB + ST
5234 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5235 if (!NextLB.isUsable())
5236 return 0;
5237 // LB = LB + ST
5238 NextLB =
5239 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5240 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5241 if (!NextLB.isUsable())
5242 return 0;
5243 // UB + ST
5244 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5245 if (!NextUB.isUsable())
5246 return 0;
5247 // UB = UB + ST
5248 NextUB =
5249 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5250 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5251 if (!NextUB.isUsable())
5252 return 0;
5253 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005254
5255 // Build updates and final values of the loop counters.
5256 bool HasErrors = false;
5257 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005258 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005259 Built.Updates.resize(NestedLoopCount);
5260 Built.Finals.resize(NestedLoopCount);
Alexey Bataev8b427062016-05-25 12:36:08 +00005261 SmallVector<Expr *, 4> LoopMultipliers;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005262 {
5263 ExprResult Div;
5264 // Go from inner nested loop to outer.
5265 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5266 LoopIterationSpace &IS = IterSpaces[Cnt];
5267 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5268 // Build: Iter = (IV / Div) % IS.NumIters
5269 // where Div is product of previous iterations' IS.NumIters.
5270 ExprResult Iter;
5271 if (Div.isUsable()) {
5272 Iter =
5273 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5274 } else {
5275 Iter = IV;
5276 assert((Cnt == (int)NestedLoopCount - 1) &&
5277 "unusable div expected on first iteration only");
5278 }
5279
5280 if (Cnt != 0 && Iter.isUsable())
5281 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5282 IS.NumIterations);
5283 if (!Iter.isUsable()) {
5284 HasErrors = true;
5285 break;
5286 }
5287
Alexey Bataev39f915b82015-05-08 10:41:21 +00005288 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005289 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5290 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5291 IS.CounterVar->getExprLoc(),
5292 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005293 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005294 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005295 if (!Init.isUsable()) {
5296 HasErrors = true;
5297 break;
5298 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00005299 ExprResult Update = BuildCounterUpdate(
5300 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5301 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005302 if (!Update.isUsable()) {
5303 HasErrors = true;
5304 break;
5305 }
5306
5307 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5308 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005309 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005310 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005311 if (!Final.isUsable()) {
5312 HasErrors = true;
5313 break;
5314 }
5315
5316 // Build Div for the next iteration: Div <- Div * IS.NumIters
5317 if (Cnt != 0) {
5318 if (Div.isUnset())
5319 Div = IS.NumIterations;
5320 else
5321 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5322 IS.NumIterations);
5323
5324 // Add parentheses (for debugging purposes only).
5325 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005326 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005327 if (!Div.isUsable()) {
5328 HasErrors = true;
5329 break;
5330 }
Alexey Bataev8b427062016-05-25 12:36:08 +00005331 LoopMultipliers.push_back(Div.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005332 }
5333 if (!Update.isUsable() || !Final.isUsable()) {
5334 HasErrors = true;
5335 break;
5336 }
5337 // Save results
5338 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005339 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005340 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 Built.Updates[Cnt] = Update.get();
5342 Built.Finals[Cnt] = Final.get();
5343 }
5344 }
5345
5346 if (HasErrors)
5347 return 0;
5348
5349 // Save results
5350 Built.IterationVarRef = IV.get();
5351 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005352 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005353 Built.CalcLastIteration =
5354 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005355 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005356 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005357 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005358 Built.Init = Init.get();
5359 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005360 Built.LB = LB.get();
5361 Built.UB = UB.get();
5362 Built.IL = IL.get();
5363 Built.ST = ST.get();
5364 Built.EUB = EUB.get();
5365 Built.NLB = NextLB.get();
5366 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005367 Built.PrevLB = PrevLB.get();
5368 Built.PrevUB = PrevUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005369
Alexey Bataev8b427062016-05-25 12:36:08 +00005370 Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5371 // Fill data for doacross depend clauses.
5372 for (auto Pair : DSA.getDoacrossDependClauses()) {
5373 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5374 Pair.first->setCounterValue(CounterVal);
5375 else {
5376 if (NestedLoopCount != Pair.second.size() ||
5377 NestedLoopCount != LoopMultipliers.size() + 1) {
5378 // Erroneous case - clause has some problems.
5379 Pair.first->setCounterValue(CounterVal);
5380 continue;
5381 }
5382 assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5383 auto I = Pair.second.rbegin();
5384 auto IS = IterSpaces.rbegin();
5385 auto ILM = LoopMultipliers.rbegin();
5386 Expr *UpCounterVal = CounterVal;
5387 Expr *Multiplier = nullptr;
5388 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5389 if (I->first) {
5390 assert(IS->CounterStep);
5391 Expr *NormalizedOffset =
5392 SemaRef
5393 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5394 I->first, IS->CounterStep)
5395 .get();
5396 if (Multiplier) {
5397 NormalizedOffset =
5398 SemaRef
5399 .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5400 NormalizedOffset, Multiplier)
5401 .get();
5402 }
5403 assert(I->second == OO_Plus || I->second == OO_Minus);
5404 BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5405 UpCounterVal =
5406 SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5407 UpCounterVal, NormalizedOffset).get();
5408 }
5409 Multiplier = *ILM;
5410 ++I;
5411 ++IS;
5412 ++ILM;
5413 }
5414 Pair.first->setCounterValue(UpCounterVal);
5415 }
5416 }
5417
Alexey Bataevabfc0692014-06-25 06:52:00 +00005418 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005419}
5420
Alexey Bataev10e775f2015-07-30 11:36:16 +00005421static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005422 auto CollapseClauses =
5423 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5424 if (CollapseClauses.begin() != CollapseClauses.end())
5425 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005426 return nullptr;
5427}
5428
Alexey Bataev10e775f2015-07-30 11:36:16 +00005429static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005430 auto OrderedClauses =
5431 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5432 if (OrderedClauses.begin() != OrderedClauses.end())
5433 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005434 return nullptr;
5435}
5436
Alexey Bataev66b15b52015-08-21 11:14:16 +00005437static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5438 const Expr *Safelen) {
5439 llvm::APSInt SimdlenRes, SafelenRes;
5440 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5441 Simdlen->isInstantiationDependent() ||
5442 Simdlen->containsUnexpandedParameterPack())
5443 return false;
5444 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5445 Safelen->isInstantiationDependent() ||
5446 Safelen->containsUnexpandedParameterPack())
5447 return false;
5448 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5449 Safelen->EvaluateAsInt(SafelenRes, S.Context);
5450 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5451 // If both simdlen and safelen clauses are specified, the value of the simdlen
5452 // parameter must be less than or equal to the value of the safelen parameter.
5453 if (SimdlenRes > SafelenRes) {
5454 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5455 << Simdlen->getSourceRange() << Safelen->getSourceRange();
5456 return true;
5457 }
5458 return false;
5459}
5460
Alexey Bataev4acb8592014-07-07 13:01:15 +00005461StmtResult Sema::ActOnOpenMPSimdDirective(
5462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5463 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005464 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005465 if (!AStmt)
5466 return StmtError();
5467
5468 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005469 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005470 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5471 // define the nested loops number.
5472 unsigned NestedLoopCount = CheckOpenMPLoop(
5473 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5474 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005475 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005476 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005477
Alexander Musmana5f070a2014-10-01 06:03:56 +00005478 assert((CurContext->isDependentContext() || B.builtAll()) &&
5479 "omp simd loop exprs were not built");
5480
Alexander Musman3276a272015-03-21 10:12:56 +00005481 if (!CurContext->isDependentContext()) {
5482 // Finalize the clauses that need pre-built expressions for CodeGen.
5483 for (auto C : Clauses) {
5484 if (auto LC = dyn_cast<OMPLinearClause>(C))
5485 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005486 B.NumIterations, *this, CurScope,
5487 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005488 return StmtError();
5489 }
5490 }
5491
Alexey Bataev66b15b52015-08-21 11:14:16 +00005492 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5493 // If both simdlen and safelen clauses are specified, the value of the simdlen
5494 // parameter must be less than or equal to the value of the safelen parameter.
5495 OMPSafelenClause *Safelen = nullptr;
5496 OMPSimdlenClause *Simdlen = nullptr;
5497 for (auto *Clause : Clauses) {
5498 if (Clause->getClauseKind() == OMPC_safelen)
5499 Safelen = cast<OMPSafelenClause>(Clause);
5500 else if (Clause->getClauseKind() == OMPC_simdlen)
5501 Simdlen = cast<OMPSimdlenClause>(Clause);
5502 if (Safelen && Simdlen)
5503 break;
5504 }
5505 if (Simdlen && Safelen &&
5506 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5507 Safelen->getSafelen()))
5508 return StmtError();
5509
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005510 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005511 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5512 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005513}
5514
Alexey Bataev4acb8592014-07-07 13:01:15 +00005515StmtResult Sema::ActOnOpenMPForDirective(
5516 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5517 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005518 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005519 if (!AStmt)
5520 return StmtError();
5521
5522 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005523 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005524 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5525 // define the nested loops number.
5526 unsigned NestedLoopCount = CheckOpenMPLoop(
5527 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5528 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005529 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005530 return StmtError();
5531
Alexander Musmana5f070a2014-10-01 06:03:56 +00005532 assert((CurContext->isDependentContext() || B.builtAll()) &&
5533 "omp for loop exprs were not built");
5534
Alexey Bataev54acd402015-08-04 11:18:19 +00005535 if (!CurContext->isDependentContext()) {
5536 // Finalize the clauses that need pre-built expressions for CodeGen.
5537 for (auto C : Clauses) {
5538 if (auto LC = dyn_cast<OMPLinearClause>(C))
5539 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005540 B.NumIterations, *this, CurScope,
5541 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005542 return StmtError();
5543 }
5544 }
5545
Alexey Bataevf29276e2014-06-18 04:14:57 +00005546 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005547 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005548 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005549}
5550
Alexander Musmanf82886e2014-09-18 05:12:34 +00005551StmtResult Sema::ActOnOpenMPForSimdDirective(
5552 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5553 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005554 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005555 if (!AStmt)
5556 return StmtError();
5557
5558 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005559 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005560 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5561 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005562 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005563 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5564 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5565 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005566 if (NestedLoopCount == 0)
5567 return StmtError();
5568
Alexander Musmanc6388682014-12-15 07:07:06 +00005569 assert((CurContext->isDependentContext() || B.builtAll()) &&
5570 "omp for simd loop exprs were not built");
5571
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005572 if (!CurContext->isDependentContext()) {
5573 // Finalize the clauses that need pre-built expressions for CodeGen.
5574 for (auto C : Clauses) {
5575 if (auto LC = dyn_cast<OMPLinearClause>(C))
5576 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005577 B.NumIterations, *this, CurScope,
5578 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005579 return StmtError();
5580 }
5581 }
5582
Alexey Bataev66b15b52015-08-21 11:14:16 +00005583 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5584 // If both simdlen and safelen clauses are specified, the value of the simdlen
5585 // parameter must be less than or equal to the value of the safelen parameter.
5586 OMPSafelenClause *Safelen = nullptr;
5587 OMPSimdlenClause *Simdlen = nullptr;
5588 for (auto *Clause : Clauses) {
5589 if (Clause->getClauseKind() == OMPC_safelen)
5590 Safelen = cast<OMPSafelenClause>(Clause);
5591 else if (Clause->getClauseKind() == OMPC_simdlen)
5592 Simdlen = cast<OMPSimdlenClause>(Clause);
5593 if (Safelen && Simdlen)
5594 break;
5595 }
5596 if (Simdlen && Safelen &&
5597 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5598 Safelen->getSafelen()))
5599 return StmtError();
5600
Alexander Musmanf82886e2014-09-18 05:12:34 +00005601 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005602 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5603 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005604}
5605
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005606StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5607 Stmt *AStmt,
5608 SourceLocation StartLoc,
5609 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005610 if (!AStmt)
5611 return StmtError();
5612
5613 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005614 auto BaseStmt = AStmt;
5615 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5616 BaseStmt = CS->getCapturedStmt();
5617 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5618 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005619 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005620 return StmtError();
5621 // All associated statements must be '#pragma omp section' except for
5622 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005623 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005624 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5625 if (SectionStmt)
5626 Diag(SectionStmt->getLocStart(),
5627 diag::err_omp_sections_substmt_not_section);
5628 return StmtError();
5629 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005630 cast<OMPSectionDirective>(SectionStmt)
5631 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005632 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005633 } else {
5634 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5635 return StmtError();
5636 }
5637
5638 getCurFunction()->setHasBranchProtectedScope();
5639
Alexey Bataev25e5b442015-09-15 12:52:43 +00005640 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5641 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005642}
5643
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005644StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5645 SourceLocation StartLoc,
5646 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005647 if (!AStmt)
5648 return StmtError();
5649
5650 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005651
5652 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005653 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005654
Alexey Bataev25e5b442015-09-15 12:52:43 +00005655 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5656 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005657}
5658
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005659StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5660 Stmt *AStmt,
5661 SourceLocation StartLoc,
5662 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005663 if (!AStmt)
5664 return StmtError();
5665
5666 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005667
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005668 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005669
Alexey Bataev3255bf32015-01-19 05:20:46 +00005670 // OpenMP [2.7.3, single Construct, Restrictions]
5671 // The copyprivate clause must not be used with the nowait clause.
5672 OMPClause *Nowait = nullptr;
5673 OMPClause *Copyprivate = nullptr;
5674 for (auto *Clause : Clauses) {
5675 if (Clause->getClauseKind() == OMPC_nowait)
5676 Nowait = Clause;
5677 else if (Clause->getClauseKind() == OMPC_copyprivate)
5678 Copyprivate = Clause;
5679 if (Copyprivate && Nowait) {
5680 Diag(Copyprivate->getLocStart(),
5681 diag::err_omp_single_copyprivate_with_nowait);
5682 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5683 return StmtError();
5684 }
5685 }
5686
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005687 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5688}
5689
Alexander Musman80c22892014-07-17 08:54:58 +00005690StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5691 SourceLocation StartLoc,
5692 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005693 if (!AStmt)
5694 return StmtError();
5695
5696 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005697
5698 getCurFunction()->setHasBranchProtectedScope();
5699
5700 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5701}
5702
Alexey Bataev28c75412015-12-15 08:19:24 +00005703StmtResult Sema::ActOnOpenMPCriticalDirective(
5704 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5705 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005706 if (!AStmt)
5707 return StmtError();
5708
5709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005710
Alexey Bataev28c75412015-12-15 08:19:24 +00005711 bool ErrorFound = false;
5712 llvm::APSInt Hint;
5713 SourceLocation HintLoc;
5714 bool DependentHint = false;
5715 for (auto *C : Clauses) {
5716 if (C->getClauseKind() == OMPC_hint) {
5717 if (!DirName.getName()) {
5718 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5719 ErrorFound = true;
5720 }
5721 Expr *E = cast<OMPHintClause>(C)->getHint();
5722 if (E->isTypeDependent() || E->isValueDependent() ||
5723 E->isInstantiationDependent())
5724 DependentHint = true;
5725 else {
5726 Hint = E->EvaluateKnownConstInt(Context);
5727 HintLoc = C->getLocStart();
5728 }
5729 }
5730 }
5731 if (ErrorFound)
5732 return StmtError();
5733 auto Pair = DSAStack->getCriticalWithHint(DirName);
5734 if (Pair.first && DirName.getName() && !DependentHint) {
5735 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5736 Diag(StartLoc, diag::err_omp_critical_with_hint);
5737 if (HintLoc.isValid()) {
5738 Diag(HintLoc, diag::note_omp_critical_hint_here)
5739 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5740 } else
5741 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5742 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5743 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5744 << 1
5745 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5746 /*Radix=*/10, /*Signed=*/false);
5747 } else
5748 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5749 }
5750 }
5751
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005752 getCurFunction()->setHasBranchProtectedScope();
5753
Alexey Bataev28c75412015-12-15 08:19:24 +00005754 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5755 Clauses, AStmt);
5756 if (!Pair.first && DirName.getName() && !DependentHint)
5757 DSAStack->addCriticalWithHint(Dir, Hint);
5758 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005759}
5760
Alexey Bataev4acb8592014-07-07 13:01:15 +00005761StmtResult Sema::ActOnOpenMPParallelForDirective(
5762 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5763 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005764 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005765 if (!AStmt)
5766 return StmtError();
5767
Alexey Bataev4acb8592014-07-07 13:01:15 +00005768 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5769 // 1.2.2 OpenMP Language Terminology
5770 // Structured block - An executable statement with a single entry at the
5771 // top and a single exit at the bottom.
5772 // The point of exit cannot be a branch out of the structured block.
5773 // longjmp() and throw() must not violate the entry/exit criteria.
5774 CS->getCapturedDecl()->setNothrow();
5775
Alexander Musmanc6388682014-12-15 07:07:06 +00005776 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005777 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5778 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005779 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005780 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5781 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5782 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005783 if (NestedLoopCount == 0)
5784 return StmtError();
5785
Alexander Musmana5f070a2014-10-01 06:03:56 +00005786 assert((CurContext->isDependentContext() || B.builtAll()) &&
5787 "omp parallel for loop exprs were not built");
5788
Alexey Bataev54acd402015-08-04 11:18:19 +00005789 if (!CurContext->isDependentContext()) {
5790 // Finalize the clauses that need pre-built expressions for CodeGen.
5791 for (auto C : Clauses) {
5792 if (auto LC = dyn_cast<OMPLinearClause>(C))
5793 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005794 B.NumIterations, *this, CurScope,
5795 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005796 return StmtError();
5797 }
5798 }
5799
Alexey Bataev4acb8592014-07-07 13:01:15 +00005800 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005801 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005802 NestedLoopCount, Clauses, AStmt, B,
5803 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005804}
5805
Alexander Musmane4e893b2014-09-23 09:33:00 +00005806StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5807 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5808 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005809 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005810 if (!AStmt)
5811 return StmtError();
5812
Alexander Musmane4e893b2014-09-23 09:33:00 +00005813 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5814 // 1.2.2 OpenMP Language Terminology
5815 // Structured block - An executable statement with a single entry at the
5816 // top and a single exit at the bottom.
5817 // The point of exit cannot be a branch out of the structured block.
5818 // longjmp() and throw() must not violate the entry/exit criteria.
5819 CS->getCapturedDecl()->setNothrow();
5820
Alexander Musmanc6388682014-12-15 07:07:06 +00005821 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005822 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5823 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005824 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005825 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5826 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5827 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005828 if (NestedLoopCount == 0)
5829 return StmtError();
5830
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005831 if (!CurContext->isDependentContext()) {
5832 // Finalize the clauses that need pre-built expressions for CodeGen.
5833 for (auto C : Clauses) {
5834 if (auto LC = dyn_cast<OMPLinearClause>(C))
5835 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005836 B.NumIterations, *this, CurScope,
5837 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005838 return StmtError();
5839 }
5840 }
5841
Alexey Bataev66b15b52015-08-21 11:14:16 +00005842 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5843 // If both simdlen and safelen clauses are specified, the value of the simdlen
5844 // parameter must be less than or equal to the value of the safelen parameter.
5845 OMPSafelenClause *Safelen = nullptr;
5846 OMPSimdlenClause *Simdlen = nullptr;
5847 for (auto *Clause : Clauses) {
5848 if (Clause->getClauseKind() == OMPC_safelen)
5849 Safelen = cast<OMPSafelenClause>(Clause);
5850 else if (Clause->getClauseKind() == OMPC_simdlen)
5851 Simdlen = cast<OMPSimdlenClause>(Clause);
5852 if (Safelen && Simdlen)
5853 break;
5854 }
5855 if (Simdlen && Safelen &&
5856 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5857 Safelen->getSafelen()))
5858 return StmtError();
5859
Alexander Musmane4e893b2014-09-23 09:33:00 +00005860 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005861 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005862 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005863}
5864
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005865StmtResult
5866Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5867 Stmt *AStmt, SourceLocation StartLoc,
5868 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005869 if (!AStmt)
5870 return StmtError();
5871
5872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005873 auto BaseStmt = AStmt;
5874 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5875 BaseStmt = CS->getCapturedStmt();
5876 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5877 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005878 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005879 return StmtError();
5880 // All associated statements must be '#pragma omp section' except for
5881 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005882 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005883 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5884 if (SectionStmt)
5885 Diag(SectionStmt->getLocStart(),
5886 diag::err_omp_parallel_sections_substmt_not_section);
5887 return StmtError();
5888 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005889 cast<OMPSectionDirective>(SectionStmt)
5890 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005891 }
5892 } else {
5893 Diag(AStmt->getLocStart(),
5894 diag::err_omp_parallel_sections_not_compound_stmt);
5895 return StmtError();
5896 }
5897
5898 getCurFunction()->setHasBranchProtectedScope();
5899
Alexey Bataev25e5b442015-09-15 12:52:43 +00005900 return OMPParallelSectionsDirective::Create(
5901 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005902}
5903
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005904StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5905 Stmt *AStmt, SourceLocation StartLoc,
5906 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005907 if (!AStmt)
5908 return StmtError();
5909
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005910 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5911 // 1.2.2 OpenMP Language Terminology
5912 // Structured block - An executable statement with a single entry at the
5913 // top and a single exit at the bottom.
5914 // The point of exit cannot be a branch out of the structured block.
5915 // longjmp() and throw() must not violate the entry/exit criteria.
5916 CS->getCapturedDecl()->setNothrow();
5917
5918 getCurFunction()->setHasBranchProtectedScope();
5919
Alexey Bataev25e5b442015-09-15 12:52:43 +00005920 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5921 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005922}
5923
Alexey Bataev68446b72014-07-18 07:47:19 +00005924StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5925 SourceLocation EndLoc) {
5926 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5927}
5928
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005929StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5930 SourceLocation EndLoc) {
5931 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5932}
5933
Alexey Bataev2df347a2014-07-18 10:17:07 +00005934StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5935 SourceLocation EndLoc) {
5936 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5937}
5938
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005939StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5940 SourceLocation StartLoc,
5941 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005942 if (!AStmt)
5943 return StmtError();
5944
5945 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005946
5947 getCurFunction()->setHasBranchProtectedScope();
5948
5949 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5950}
5951
Alexey Bataev6125da92014-07-21 11:26:11 +00005952StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5953 SourceLocation StartLoc,
5954 SourceLocation EndLoc) {
5955 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5956 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5957}
5958
Alexey Bataev346265e2015-09-25 10:37:12 +00005959StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5960 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005961 SourceLocation StartLoc,
5962 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005963 OMPClause *DependFound = nullptr;
5964 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005965 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005966 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005967 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005968 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005969 for (auto *C : Clauses) {
5970 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5971 DependFound = C;
5972 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5973 if (DependSourceClause) {
5974 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5975 << getOpenMPDirectiveName(OMPD_ordered)
5976 << getOpenMPClauseName(OMPC_depend) << 2;
5977 ErrorFound = true;
5978 } else
5979 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005980 if (DependSinkClause) {
5981 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5982 << 0;
5983 ErrorFound = true;
5984 }
5985 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5986 if (DependSourceClause) {
5987 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5988 << 1;
5989 ErrorFound = true;
5990 }
5991 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005992 }
5993 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005994 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005995 else if (C->getClauseKind() == OMPC_simd)
5996 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005997 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005998 if (!ErrorFound && !SC &&
5999 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006000 // OpenMP [2.8.1,simd Construct, Restrictions]
6001 // An ordered construct with the simd clause is the only OpenMP construct
6002 // that can appear in the simd region.
6003 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006004 ErrorFound = true;
6005 } else if (DependFound && (TC || SC)) {
6006 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6007 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6008 ErrorFound = true;
6009 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6010 Diag(DependFound->getLocStart(),
6011 diag::err_omp_ordered_directive_without_param);
6012 ErrorFound = true;
6013 } else if (TC || Clauses.empty()) {
6014 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6015 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6016 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6017 << (TC != nullptr);
6018 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6019 ErrorFound = true;
6020 }
6021 }
6022 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006023 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006024
6025 if (AStmt) {
6026 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6027
6028 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006029 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006030
6031 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006032}
6033
Alexey Bataev1d160b12015-03-13 12:27:31 +00006034namespace {
6035/// \brief Helper class for checking expression in 'omp atomic [update]'
6036/// construct.
6037class OpenMPAtomicUpdateChecker {
6038 /// \brief Error results for atomic update expressions.
6039 enum ExprAnalysisErrorCode {
6040 /// \brief A statement is not an expression statement.
6041 NotAnExpression,
6042 /// \brief Expression is not builtin binary or unary operation.
6043 NotABinaryOrUnaryExpression,
6044 /// \brief Unary operation is not post-/pre- increment/decrement operation.
6045 NotAnUnaryIncDecExpression,
6046 /// \brief An expression is not of scalar type.
6047 NotAScalarType,
6048 /// \brief A binary operation is not an assignment operation.
6049 NotAnAssignmentOp,
6050 /// \brief RHS part of the binary operation is not a binary expression.
6051 NotABinaryExpression,
6052 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6053 /// expression.
6054 NotABinaryOperator,
6055 /// \brief RHS binary operation does not have reference to the updated LHS
6056 /// part.
6057 NotAnUpdateExpression,
6058 /// \brief No errors is found.
6059 NoError
6060 };
6061 /// \brief Reference to Sema.
6062 Sema &SemaRef;
6063 /// \brief A location for note diagnostics (when error is found).
6064 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 /// \brief 'x' lvalue part of the source atomic expression.
6066 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006067 /// \brief 'expr' rvalue part of the source atomic expression.
6068 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006069 /// \brief Helper expression of the form
6070 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6071 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6072 Expr *UpdateExpr;
6073 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6074 /// important for non-associative operations.
6075 bool IsXLHSInRHSPart;
6076 BinaryOperatorKind Op;
6077 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006078 /// \brief true if the source expression is a postfix unary operation, false
6079 /// if it is a prefix unary operation.
6080 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006081
6082public:
6083 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006084 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006085 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00006086 /// \brief Check specified statement that it is suitable for 'atomic update'
6087 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006088 /// expression. If DiagId and NoteId == 0, then only check is performed
6089 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006090 /// \param DiagId Diagnostic which should be emitted if error is found.
6091 /// \param NoteId Diagnostic note for the main error message.
6092 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006093 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006094 /// \brief Return the 'x' lvalue part of the source atomic expression.
6095 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00006096 /// \brief Return the 'expr' rvalue part of the source atomic expression.
6097 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00006098 /// \brief Return the update expression used in calculation of the updated
6099 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6100 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6101 Expr *getUpdateExpr() const { return UpdateExpr; }
6102 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6103 /// false otherwise.
6104 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6105
Alexey Bataevb78ca832015-04-01 03:33:17 +00006106 /// \brief true if the source expression is a postfix unary operation, false
6107 /// if it is a prefix unary operation.
6108 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6109
Alexey Bataev1d160b12015-03-13 12:27:31 +00006110private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006111 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6112 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006113};
6114} // namespace
6115
6116bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6117 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6118 ExprAnalysisErrorCode ErrorFound = NoError;
6119 SourceLocation ErrorLoc, NoteLoc;
6120 SourceRange ErrorRange, NoteRange;
6121 // Allowed constructs are:
6122 // x = x binop expr;
6123 // x = expr binop x;
6124 if (AtomicBinOp->getOpcode() == BO_Assign) {
6125 X = AtomicBinOp->getLHS();
6126 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6127 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6128 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6129 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6130 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006131 Op = AtomicInnerBinOp->getOpcode();
6132 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006133 auto *LHS = AtomicInnerBinOp->getLHS();
6134 auto *RHS = AtomicInnerBinOp->getRHS();
6135 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6136 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6137 /*Canonical=*/true);
6138 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6139 /*Canonical=*/true);
6140 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6141 /*Canonical=*/true);
6142 if (XId == LHSId) {
6143 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006144 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006145 } else if (XId == RHSId) {
6146 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006147 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006148 } else {
6149 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6150 ErrorRange = AtomicInnerBinOp->getSourceRange();
6151 NoteLoc = X->getExprLoc();
6152 NoteRange = X->getSourceRange();
6153 ErrorFound = NotAnUpdateExpression;
6154 }
6155 } else {
6156 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6157 ErrorRange = AtomicInnerBinOp->getSourceRange();
6158 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6159 NoteRange = SourceRange(NoteLoc, NoteLoc);
6160 ErrorFound = NotABinaryOperator;
6161 }
6162 } else {
6163 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6164 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6165 ErrorFound = NotABinaryExpression;
6166 }
6167 } else {
6168 ErrorLoc = AtomicBinOp->getExprLoc();
6169 ErrorRange = AtomicBinOp->getSourceRange();
6170 NoteLoc = AtomicBinOp->getOperatorLoc();
6171 NoteRange = SourceRange(NoteLoc, NoteLoc);
6172 ErrorFound = NotAnAssignmentOp;
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;
6178 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006179 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006180 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006181}
6182
6183bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6184 unsigned NoteId) {
6185 ExprAnalysisErrorCode ErrorFound = NoError;
6186 SourceLocation ErrorLoc, NoteLoc;
6187 SourceRange ErrorRange, NoteRange;
6188 // Allowed constructs are:
6189 // x++;
6190 // x--;
6191 // ++x;
6192 // --x;
6193 // x binop= expr;
6194 // x = x binop expr;
6195 // x = expr binop x;
6196 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6197 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6198 if (AtomicBody->getType()->isScalarType() ||
6199 AtomicBody->isInstantiationDependent()) {
6200 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6201 AtomicBody->IgnoreParenImpCasts())) {
6202 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006203 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006204 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006205 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006207 X = AtomicCompAssignOp->getLHS();
6208 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006209 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6210 AtomicBody->IgnoreParenImpCasts())) {
6211 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006212 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6213 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006214 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00006215 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6216 // Check for Unary Operation
6217 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006218 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006219 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6220 OpLoc = AtomicUnaryOp->getOperatorLoc();
6221 X = AtomicUnaryOp->getSubExpr();
6222 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6223 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006224 } else {
6225 ErrorFound = NotAnUnaryIncDecExpression;
6226 ErrorLoc = AtomicUnaryOp->getExprLoc();
6227 ErrorRange = AtomicUnaryOp->getSourceRange();
6228 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6229 NoteRange = SourceRange(NoteLoc, NoteLoc);
6230 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006231 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006232 ErrorFound = NotABinaryOrUnaryExpression;
6233 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6234 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6235 }
6236 } else {
6237 ErrorFound = NotAScalarType;
6238 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6239 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6240 }
6241 } else {
6242 ErrorFound = NotAnExpression;
6243 NoteLoc = ErrorLoc = S->getLocStart();
6244 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6245 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006246 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006247 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6248 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6249 return true;
6250 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006251 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006252 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006253 // Build an update expression of form 'OpaqueValueExpr(x) binop
6254 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6255 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6256 auto *OVEX = new (SemaRef.getASTContext())
6257 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6258 auto *OVEExpr = new (SemaRef.getASTContext())
6259 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6260 auto Update =
6261 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6262 IsXLHSInRHSPart ? OVEExpr : OVEX);
6263 if (Update.isInvalid())
6264 return true;
6265 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6266 Sema::AA_Casting);
6267 if (Update.isInvalid())
6268 return true;
6269 UpdateExpr = Update.get();
6270 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006271 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006272}
6273
Alexey Bataev0162e452014-07-22 10:10:35 +00006274StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6275 Stmt *AStmt,
6276 SourceLocation StartLoc,
6277 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006278 if (!AStmt)
6279 return StmtError();
6280
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006281 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006282 // 1.2.2 OpenMP Language Terminology
6283 // Structured block - An executable statement with a single entry at the
6284 // top and a single exit at the bottom.
6285 // The point of exit cannot be a branch out of the structured block.
6286 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006287 OpenMPClauseKind AtomicKind = OMPC_unknown;
6288 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006289 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006290 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006291 C->getClauseKind() == OMPC_update ||
6292 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006293 if (AtomicKind != OMPC_unknown) {
6294 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6295 << SourceRange(C->getLocStart(), C->getLocEnd());
6296 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6297 << getOpenMPClauseName(AtomicKind);
6298 } else {
6299 AtomicKind = C->getClauseKind();
6300 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006301 }
6302 }
6303 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006304
Alexey Bataev459dec02014-07-24 06:46:57 +00006305 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006306 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6307 Body = EWC->getSubExpr();
6308
Alexey Bataev62cec442014-11-18 10:14:22 +00006309 Expr *X = nullptr;
6310 Expr *V = nullptr;
6311 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006312 Expr *UE = nullptr;
6313 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006314 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006315 // OpenMP [2.12.6, atomic Construct]
6316 // In the next expressions:
6317 // * x and v (as applicable) are both l-value expressions with scalar type.
6318 // * During the execution of an atomic region, multiple syntactic
6319 // occurrences of x must designate the same storage location.
6320 // * Neither of v and expr (as applicable) may access the storage location
6321 // designated by x.
6322 // * Neither of x and expr (as applicable) may access the storage location
6323 // designated by v.
6324 // * expr is an expression with scalar type.
6325 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6326 // * binop, binop=, ++, and -- are not overloaded operators.
6327 // * The expression x binop expr must be numerically equivalent to x binop
6328 // (expr). This requirement is satisfied if the operators in expr have
6329 // precedence greater than binop, or by using parentheses around expr or
6330 // subexpressions of expr.
6331 // * The expression expr binop x must be numerically equivalent to (expr)
6332 // binop x. This requirement is satisfied if the operators in expr have
6333 // precedence equal to or greater than binop, or by using parentheses around
6334 // expr or subexpressions of expr.
6335 // * For forms that allow multiple occurrences of x, the number of times
6336 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006337 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006338 enum {
6339 NotAnExpression,
6340 NotAnAssignmentOp,
6341 NotAScalarType,
6342 NotAnLValue,
6343 NoError
6344 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006345 SourceLocation ErrorLoc, NoteLoc;
6346 SourceRange ErrorRange, NoteRange;
6347 // If clause is read:
6348 // v = x;
6349 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6350 auto AtomicBinOp =
6351 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6352 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6353 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6354 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6355 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6356 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6357 if (!X->isLValue() || !V->isLValue()) {
6358 auto NotLValueExpr = X->isLValue() ? V : X;
6359 ErrorFound = NotAnLValue;
6360 ErrorLoc = AtomicBinOp->getExprLoc();
6361 ErrorRange = AtomicBinOp->getSourceRange();
6362 NoteLoc = NotLValueExpr->getExprLoc();
6363 NoteRange = NotLValueExpr->getSourceRange();
6364 }
6365 } else if (!X->isInstantiationDependent() ||
6366 !V->isInstantiationDependent()) {
6367 auto NotScalarExpr =
6368 (X->isInstantiationDependent() || X->getType()->isScalarType())
6369 ? V
6370 : X;
6371 ErrorFound = NotAScalarType;
6372 ErrorLoc = AtomicBinOp->getExprLoc();
6373 ErrorRange = AtomicBinOp->getSourceRange();
6374 NoteLoc = NotScalarExpr->getExprLoc();
6375 NoteRange = NotScalarExpr->getSourceRange();
6376 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006377 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006378 ErrorFound = NotAnAssignmentOp;
6379 ErrorLoc = AtomicBody->getExprLoc();
6380 ErrorRange = AtomicBody->getSourceRange();
6381 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6382 : AtomicBody->getExprLoc();
6383 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6384 : AtomicBody->getSourceRange();
6385 }
6386 } else {
6387 ErrorFound = NotAnExpression;
6388 NoteLoc = ErrorLoc = Body->getLocStart();
6389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006390 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006391 if (ErrorFound != NoError) {
6392 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6393 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006394 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6395 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006396 return StmtError();
6397 } else if (CurContext->isDependentContext())
6398 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006399 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006400 enum {
6401 NotAnExpression,
6402 NotAnAssignmentOp,
6403 NotAScalarType,
6404 NotAnLValue,
6405 NoError
6406 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006407 SourceLocation ErrorLoc, NoteLoc;
6408 SourceRange ErrorRange, NoteRange;
6409 // If clause is write:
6410 // x = expr;
6411 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6412 auto AtomicBinOp =
6413 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6414 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006415 X = AtomicBinOp->getLHS();
6416 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006417 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6418 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6419 if (!X->isLValue()) {
6420 ErrorFound = NotAnLValue;
6421 ErrorLoc = AtomicBinOp->getExprLoc();
6422 ErrorRange = AtomicBinOp->getSourceRange();
6423 NoteLoc = X->getExprLoc();
6424 NoteRange = X->getSourceRange();
6425 }
6426 } else if (!X->isInstantiationDependent() ||
6427 !E->isInstantiationDependent()) {
6428 auto NotScalarExpr =
6429 (X->isInstantiationDependent() || X->getType()->isScalarType())
6430 ? E
6431 : X;
6432 ErrorFound = NotAScalarType;
6433 ErrorLoc = AtomicBinOp->getExprLoc();
6434 ErrorRange = AtomicBinOp->getSourceRange();
6435 NoteLoc = NotScalarExpr->getExprLoc();
6436 NoteRange = NotScalarExpr->getSourceRange();
6437 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006438 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006439 ErrorFound = NotAnAssignmentOp;
6440 ErrorLoc = AtomicBody->getExprLoc();
6441 ErrorRange = AtomicBody->getSourceRange();
6442 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6443 : AtomicBody->getExprLoc();
6444 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6445 : AtomicBody->getSourceRange();
6446 }
6447 } else {
6448 ErrorFound = NotAnExpression;
6449 NoteLoc = ErrorLoc = Body->getLocStart();
6450 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006451 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006452 if (ErrorFound != NoError) {
6453 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6454 << ErrorRange;
6455 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6456 << NoteRange;
6457 return StmtError();
6458 } else if (CurContext->isDependentContext())
6459 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006460 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006461 // If clause is update:
6462 // x++;
6463 // x--;
6464 // ++x;
6465 // --x;
6466 // x binop= expr;
6467 // x = x binop expr;
6468 // x = expr binop x;
6469 OpenMPAtomicUpdateChecker Checker(*this);
6470 if (Checker.checkStatement(
6471 Body, (AtomicKind == OMPC_update)
6472 ? diag::err_omp_atomic_update_not_expression_statement
6473 : diag::err_omp_atomic_not_expression_statement,
6474 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006475 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006476 if (!CurContext->isDependentContext()) {
6477 E = Checker.getExpr();
6478 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006479 UE = Checker.getUpdateExpr();
6480 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006481 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006482 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006483 enum {
6484 NotAnAssignmentOp,
6485 NotACompoundStatement,
6486 NotTwoSubstatements,
6487 NotASpecificExpression,
6488 NoError
6489 } ErrorFound = NoError;
6490 SourceLocation ErrorLoc, NoteLoc;
6491 SourceRange ErrorRange, NoteRange;
6492 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6493 // If clause is a capture:
6494 // v = x++;
6495 // v = x--;
6496 // v = ++x;
6497 // v = --x;
6498 // v = x binop= expr;
6499 // v = x = x binop expr;
6500 // v = x = expr binop x;
6501 auto *AtomicBinOp =
6502 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6503 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6504 V = AtomicBinOp->getLHS();
6505 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6506 OpenMPAtomicUpdateChecker Checker(*this);
6507 if (Checker.checkStatement(
6508 Body, diag::err_omp_atomic_capture_not_expression_statement,
6509 diag::note_omp_atomic_update))
6510 return StmtError();
6511 E = Checker.getExpr();
6512 X = Checker.getX();
6513 UE = Checker.getUpdateExpr();
6514 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6515 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006516 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006517 ErrorLoc = AtomicBody->getExprLoc();
6518 ErrorRange = AtomicBody->getSourceRange();
6519 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6520 : AtomicBody->getExprLoc();
6521 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6522 : AtomicBody->getSourceRange();
6523 ErrorFound = NotAnAssignmentOp;
6524 }
6525 if (ErrorFound != NoError) {
6526 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6527 << ErrorRange;
6528 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6529 return StmtError();
6530 } else if (CurContext->isDependentContext()) {
6531 UE = V = E = X = nullptr;
6532 }
6533 } else {
6534 // If clause is a capture:
6535 // { v = x; x = expr; }
6536 // { v = x; x++; }
6537 // { v = x; x--; }
6538 // { v = x; ++x; }
6539 // { v = x; --x; }
6540 // { v = x; x binop= expr; }
6541 // { v = x; x = x binop expr; }
6542 // { v = x; x = expr binop x; }
6543 // { x++; v = x; }
6544 // { x--; v = x; }
6545 // { ++x; v = x; }
6546 // { --x; v = x; }
6547 // { x binop= expr; v = x; }
6548 // { x = x binop expr; v = x; }
6549 // { x = expr binop x; v = x; }
6550 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6551 // Check that this is { expr1; expr2; }
6552 if (CS->size() == 2) {
6553 auto *First = CS->body_front();
6554 auto *Second = CS->body_back();
6555 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6556 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6557 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6558 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6559 // Need to find what subexpression is 'v' and what is 'x'.
6560 OpenMPAtomicUpdateChecker Checker(*this);
6561 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6562 BinaryOperator *BinOp = nullptr;
6563 if (IsUpdateExprFound) {
6564 BinOp = dyn_cast<BinaryOperator>(First);
6565 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6566 }
6567 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6568 // { v = x; x++; }
6569 // { v = x; x--; }
6570 // { v = x; ++x; }
6571 // { v = x; --x; }
6572 // { v = x; x binop= expr; }
6573 // { v = x; x = x binop expr; }
6574 // { v = x; x = expr binop x; }
6575 // Check that the first expression has form v = x.
6576 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6577 llvm::FoldingSetNodeID XId, PossibleXId;
6578 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6579 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6580 IsUpdateExprFound = XId == PossibleXId;
6581 if (IsUpdateExprFound) {
6582 V = BinOp->getLHS();
6583 X = Checker.getX();
6584 E = Checker.getExpr();
6585 UE = Checker.getUpdateExpr();
6586 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006587 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006588 }
6589 }
6590 if (!IsUpdateExprFound) {
6591 IsUpdateExprFound = !Checker.checkStatement(First);
6592 BinOp = nullptr;
6593 if (IsUpdateExprFound) {
6594 BinOp = dyn_cast<BinaryOperator>(Second);
6595 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6596 }
6597 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6598 // { x++; v = x; }
6599 // { x--; v = x; }
6600 // { ++x; v = x; }
6601 // { --x; v = x; }
6602 // { x binop= expr; v = x; }
6603 // { x = x binop expr; v = x; }
6604 // { x = expr binop x; v = x; }
6605 // Check that the second expression has form v = x.
6606 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6607 llvm::FoldingSetNodeID XId, PossibleXId;
6608 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6609 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6610 IsUpdateExprFound = XId == PossibleXId;
6611 if (IsUpdateExprFound) {
6612 V = BinOp->getLHS();
6613 X = Checker.getX();
6614 E = Checker.getExpr();
6615 UE = Checker.getUpdateExpr();
6616 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006617 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006618 }
6619 }
6620 }
6621 if (!IsUpdateExprFound) {
6622 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006623 auto *FirstExpr = dyn_cast<Expr>(First);
6624 auto *SecondExpr = dyn_cast<Expr>(Second);
6625 if (!FirstExpr || !SecondExpr ||
6626 !(FirstExpr->isInstantiationDependent() ||
6627 SecondExpr->isInstantiationDependent())) {
6628 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6629 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006630 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006631 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6632 : First->getLocStart();
6633 NoteRange = ErrorRange = FirstBinOp
6634 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006635 : SourceRange(ErrorLoc, ErrorLoc);
6636 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006637 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6638 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6639 ErrorFound = NotAnAssignmentOp;
6640 NoteLoc = ErrorLoc = SecondBinOp
6641 ? SecondBinOp->getOperatorLoc()
6642 : Second->getLocStart();
6643 NoteRange = ErrorRange =
6644 SecondBinOp ? SecondBinOp->getSourceRange()
6645 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006646 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006647 auto *PossibleXRHSInFirst =
6648 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6649 auto *PossibleXLHSInSecond =
6650 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6651 llvm::FoldingSetNodeID X1Id, X2Id;
6652 PossibleXRHSInFirst->Profile(X1Id, Context,
6653 /*Canonical=*/true);
6654 PossibleXLHSInSecond->Profile(X2Id, Context,
6655 /*Canonical=*/true);
6656 IsUpdateExprFound = X1Id == X2Id;
6657 if (IsUpdateExprFound) {
6658 V = FirstBinOp->getLHS();
6659 X = SecondBinOp->getLHS();
6660 E = SecondBinOp->getRHS();
6661 UE = nullptr;
6662 IsXLHSInRHSPart = false;
6663 IsPostfixUpdate = true;
6664 } else {
6665 ErrorFound = NotASpecificExpression;
6666 ErrorLoc = FirstBinOp->getExprLoc();
6667 ErrorRange = FirstBinOp->getSourceRange();
6668 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6669 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6670 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006671 }
6672 }
6673 }
6674 }
6675 } else {
6676 NoteLoc = ErrorLoc = Body->getLocStart();
6677 NoteRange = ErrorRange =
6678 SourceRange(Body->getLocStart(), Body->getLocStart());
6679 ErrorFound = NotTwoSubstatements;
6680 }
6681 } else {
6682 NoteLoc = ErrorLoc = Body->getLocStart();
6683 NoteRange = ErrorRange =
6684 SourceRange(Body->getLocStart(), Body->getLocStart());
6685 ErrorFound = NotACompoundStatement;
6686 }
6687 if (ErrorFound != NoError) {
6688 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6689 << ErrorRange;
6690 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6691 return StmtError();
6692 } else if (CurContext->isDependentContext()) {
6693 UE = V = E = X = nullptr;
6694 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006695 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006696 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006697
6698 getCurFunction()->setHasBranchProtectedScope();
6699
Alexey Bataev62cec442014-11-18 10:14:22 +00006700 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006701 X, V, E, UE, IsXLHSInRHSPart,
6702 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006703}
6704
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006705StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6706 Stmt *AStmt,
6707 SourceLocation StartLoc,
6708 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006709 if (!AStmt)
6710 return StmtError();
6711
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006712 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6713 // 1.2.2 OpenMP Language Terminology
6714 // Structured block - An executable statement with a single entry at the
6715 // top and a single exit at the bottom.
6716 // The point of exit cannot be a branch out of the structured block.
6717 // longjmp() and throw() must not violate the entry/exit criteria.
6718 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006719
Alexey Bataev13314bf2014-10-09 04:18:56 +00006720 // OpenMP [2.16, Nesting of Regions]
6721 // If specified, a teams construct must be contained within a target
6722 // construct. That target construct must contain no statements or directives
6723 // outside of the teams construct.
6724 if (DSAStack->hasInnerTeamsRegion()) {
6725 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6726 bool OMPTeamsFound = true;
6727 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6728 auto I = CS->body_begin();
6729 while (I != CS->body_end()) {
6730 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6731 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6732 OMPTeamsFound = false;
6733 break;
6734 }
6735 ++I;
6736 }
6737 assert(I != CS->body_end() && "Not found statement");
6738 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006739 } else {
6740 auto *OED = dyn_cast<OMPExecutableDirective>(S);
6741 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006742 }
6743 if (!OMPTeamsFound) {
6744 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6745 Diag(DSAStack->getInnerTeamsRegionLoc(),
6746 diag::note_omp_nested_teams_construct_here);
6747 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6748 << isa<OMPExecutableDirective>(S);
6749 return StmtError();
6750 }
6751 }
6752
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006753 getCurFunction()->setHasBranchProtectedScope();
6754
6755 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6756}
6757
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006758StmtResult
6759Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6760 Stmt *AStmt, SourceLocation StartLoc,
6761 SourceLocation EndLoc) {
6762 if (!AStmt)
6763 return StmtError();
6764
6765 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6766 // 1.2.2 OpenMP Language Terminology
6767 // Structured block - An executable statement with a single entry at the
6768 // top and a single exit at the bottom.
6769 // The point of exit cannot be a branch out of the structured block.
6770 // longjmp() and throw() must not violate the entry/exit criteria.
6771 CS->getCapturedDecl()->setNothrow();
6772
6773 getCurFunction()->setHasBranchProtectedScope();
6774
6775 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6776 AStmt);
6777}
6778
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006779StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6780 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6781 SourceLocation EndLoc,
6782 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6783 if (!AStmt)
6784 return StmtError();
6785
6786 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6787 // 1.2.2 OpenMP Language Terminology
6788 // Structured block - An executable statement with a single entry at the
6789 // top and a single exit at the bottom.
6790 // The point of exit cannot be a branch out of the structured block.
6791 // longjmp() and throw() must not violate the entry/exit criteria.
6792 CS->getCapturedDecl()->setNothrow();
6793
6794 OMPLoopDirective::HelperExprs B;
6795 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6796 // define the nested loops number.
6797 unsigned NestedLoopCount =
6798 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6799 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6800 VarsWithImplicitDSA, B);
6801 if (NestedLoopCount == 0)
6802 return StmtError();
6803
6804 assert((CurContext->isDependentContext() || B.builtAll()) &&
6805 "omp target parallel for loop exprs were not built");
6806
6807 if (!CurContext->isDependentContext()) {
6808 // Finalize the clauses that need pre-built expressions for CodeGen.
6809 for (auto C : Clauses) {
6810 if (auto LC = dyn_cast<OMPLinearClause>(C))
6811 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006812 B.NumIterations, *this, CurScope,
6813 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006814 return StmtError();
6815 }
6816 }
6817
6818 getCurFunction()->setHasBranchProtectedScope();
6819 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6820 NestedLoopCount, Clauses, AStmt,
6821 B, DSAStack->isCancelRegion());
6822}
6823
Samuel Antaodf67fc42016-01-19 19:15:56 +00006824/// \brief Check for existence of a map clause in the list of clauses.
6825static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6826 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6827 I != E; ++I) {
6828 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6829 return true;
6830 }
6831 }
6832
6833 return false;
6834}
6835
Michael Wong65f367f2015-07-21 13:44:28 +00006836StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6837 Stmt *AStmt,
6838 SourceLocation StartLoc,
6839 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006840 if (!AStmt)
6841 return StmtError();
6842
6843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6844
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006845 // OpenMP [2.10.1, Restrictions, p. 97]
6846 // At least one map clause must appear on the directive.
6847 if (!HasMapClause(Clauses)) {
6848 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6849 getOpenMPDirectiveName(OMPD_target_data);
6850 return StmtError();
6851 }
6852
Michael Wong65f367f2015-07-21 13:44:28 +00006853 getCurFunction()->setHasBranchProtectedScope();
6854
6855 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6856 AStmt);
6857}
6858
Samuel Antaodf67fc42016-01-19 19:15:56 +00006859StmtResult
6860Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6861 SourceLocation StartLoc,
6862 SourceLocation EndLoc) {
6863 // OpenMP [2.10.2, Restrictions, p. 99]
6864 // At least one map clause must appear on the directive.
6865 if (!HasMapClause(Clauses)) {
6866 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6867 << getOpenMPDirectiveName(OMPD_target_enter_data);
6868 return StmtError();
6869 }
6870
6871 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6872 Clauses);
6873}
6874
Samuel Antao72590762016-01-19 20:04:50 +00006875StmtResult
6876Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6877 SourceLocation StartLoc,
6878 SourceLocation EndLoc) {
6879 // OpenMP [2.10.3, Restrictions, p. 102]
6880 // At least one map clause must appear on the directive.
6881 if (!HasMapClause(Clauses)) {
6882 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6883 << getOpenMPDirectiveName(OMPD_target_exit_data);
6884 return StmtError();
6885 }
6886
6887 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6888}
6889
Samuel Antao686c70c2016-05-26 17:30:50 +00006890StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6891 SourceLocation StartLoc,
6892 SourceLocation EndLoc) {
Samuel Antao686c70c2016-05-26 17:30:50 +00006893 bool seenMotionClause = false;
Samuel Antao661c0902016-05-26 17:39:58 +00006894 for (auto *C : Clauses) {
Samuel Antaoec172c62016-05-26 17:49:04 +00006895 if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
Samuel Antao661c0902016-05-26 17:39:58 +00006896 seenMotionClause = true;
6897 }
Samuel Antao686c70c2016-05-26 17:30:50 +00006898 if (!seenMotionClause) {
6899 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6900 return StmtError();
6901 }
6902 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6903}
6904
Alexey Bataev13314bf2014-10-09 04:18:56 +00006905StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6906 Stmt *AStmt, SourceLocation StartLoc,
6907 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006908 if (!AStmt)
6909 return StmtError();
6910
Alexey Bataev13314bf2014-10-09 04:18:56 +00006911 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
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 getCurFunction()->setHasBranchProtectedScope();
6920
6921 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6922}
6923
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006924StmtResult
6925Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6926 SourceLocation EndLoc,
6927 OpenMPDirectiveKind CancelRegion) {
6928 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6929 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6930 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6931 << getOpenMPDirectiveName(CancelRegion);
6932 return StmtError();
6933 }
6934 if (DSAStack->isParentNowaitRegion()) {
6935 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6936 return StmtError();
6937 }
6938 if (DSAStack->isParentOrderedRegion()) {
6939 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6940 return StmtError();
6941 }
6942 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6943 CancelRegion);
6944}
6945
Alexey Bataev87933c72015-09-18 08:07:34 +00006946StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6947 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006948 SourceLocation EndLoc,
6949 OpenMPDirectiveKind CancelRegion) {
6950 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6951 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6952 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6953 << getOpenMPDirectiveName(CancelRegion);
6954 return StmtError();
6955 }
6956 if (DSAStack->isParentNowaitRegion()) {
6957 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6958 return StmtError();
6959 }
6960 if (DSAStack->isParentOrderedRegion()) {
6961 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6962 return StmtError();
6963 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006964 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006965 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6966 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006967}
6968
Alexey Bataev382967a2015-12-08 12:06:20 +00006969static bool checkGrainsizeNumTasksClauses(Sema &S,
6970 ArrayRef<OMPClause *> Clauses) {
6971 OMPClause *PrevClause = nullptr;
6972 bool ErrorFound = false;
6973 for (auto *C : Clauses) {
6974 if (C->getClauseKind() == OMPC_grainsize ||
6975 C->getClauseKind() == OMPC_num_tasks) {
6976 if (!PrevClause)
6977 PrevClause = C;
6978 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6979 S.Diag(C->getLocStart(),
6980 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6981 << getOpenMPClauseName(C->getClauseKind())
6982 << getOpenMPClauseName(PrevClause->getClauseKind());
6983 S.Diag(PrevClause->getLocStart(),
6984 diag::note_omp_previous_grainsize_num_tasks)
6985 << getOpenMPClauseName(PrevClause->getClauseKind());
6986 ErrorFound = true;
6987 }
6988 }
6989 }
6990 return ErrorFound;
6991}
6992
Alexey Bataev49f6e782015-12-01 04:18:41 +00006993StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6994 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6995 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006996 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006997 if (!AStmt)
6998 return StmtError();
6999
7000 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7001 OMPLoopDirective::HelperExprs B;
7002 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7003 // define the nested loops number.
7004 unsigned NestedLoopCount =
7005 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007006 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007007 VarsWithImplicitDSA, B);
7008 if (NestedLoopCount == 0)
7009 return StmtError();
7010
7011 assert((CurContext->isDependentContext() || B.builtAll()) &&
7012 "omp for loop exprs were not built");
7013
Alexey Bataev382967a2015-12-08 12:06:20 +00007014 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7015 // The grainsize clause and num_tasks clause are mutually exclusive and may
7016 // not appear on the same taskloop directive.
7017 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7018 return StmtError();
7019
Alexey Bataev49f6e782015-12-01 04:18:41 +00007020 getCurFunction()->setHasBranchProtectedScope();
7021 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7022 NestedLoopCount, Clauses, AStmt, B);
7023}
7024
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007025StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7026 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7027 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007028 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007029 if (!AStmt)
7030 return StmtError();
7031
7032 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7033 OMPLoopDirective::HelperExprs B;
7034 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7035 // define the nested loops number.
7036 unsigned NestedLoopCount =
7037 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7038 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7039 VarsWithImplicitDSA, B);
7040 if (NestedLoopCount == 0)
7041 return StmtError();
7042
7043 assert((CurContext->isDependentContext() || B.builtAll()) &&
7044 "omp for loop exprs were not built");
7045
Alexey Bataev5a3af132016-03-29 08:58:54 +00007046 if (!CurContext->isDependentContext()) {
7047 // Finalize the clauses that need pre-built expressions for CodeGen.
7048 for (auto C : Clauses) {
7049 if (auto LC = dyn_cast<OMPLinearClause>(C))
7050 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007051 B.NumIterations, *this, CurScope,
7052 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007053 return StmtError();
7054 }
7055 }
7056
Alexey Bataev382967a2015-12-08 12:06:20 +00007057 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7058 // The grainsize clause and num_tasks clause are mutually exclusive and may
7059 // not appear on the same taskloop directive.
7060 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7061 return StmtError();
7062
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007063 getCurFunction()->setHasBranchProtectedScope();
7064 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7065 NestedLoopCount, Clauses, AStmt, B);
7066}
7067
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007068StmtResult Sema::ActOnOpenMPDistributeDirective(
7069 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7070 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007071 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +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' with number of loops, it will
7078 // define the nested loops number.
7079 unsigned NestedLoopCount =
7080 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7081 nullptr /*ordered not a clause on distribute*/, AStmt,
7082 *this, *DSAStack, 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
7089 getCurFunction()->setHasBranchProtectedScope();
7090 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7091 NestedLoopCount, Clauses, AStmt, B);
7092}
7093
Carlo Bertolli9925f152016-06-27 14:55:37 +00007094StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7095 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7096 SourceLocation EndLoc,
7097 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7098 if (!AStmt)
7099 return StmtError();
7100
7101 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7102 // 1.2.2 OpenMP Language Terminology
7103 // Structured block - An executable statement with a single entry at the
7104 // top and a single exit at the bottom.
7105 // The point of exit cannot be a branch out of the structured block.
7106 // longjmp() and throw() must not violate the entry/exit criteria.
7107 CS->getCapturedDecl()->setNothrow();
7108
7109 OMPLoopDirective::HelperExprs B;
7110 // In presence of clause 'collapse' with number of loops, it will
7111 // define the nested loops number.
7112 unsigned NestedLoopCount = CheckOpenMPLoop(
7113 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7114 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7115 VarsWithImplicitDSA, B);
7116 if (NestedLoopCount == 0)
7117 return StmtError();
7118
7119 assert((CurContext->isDependentContext() || B.builtAll()) &&
7120 "omp for loop exprs were not built");
7121
7122 getCurFunction()->setHasBranchProtectedScope();
7123 return OMPDistributeParallelForDirective::Create(
7124 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7125}
7126
Kelvin Li4a39add2016-07-05 05:00:15 +00007127StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7128 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7129 SourceLocation EndLoc,
7130 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7131 if (!AStmt)
7132 return StmtError();
7133
7134 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7135 // 1.2.2 OpenMP Language Terminology
7136 // Structured block - An executable statement with a single entry at the
7137 // top and a single exit at the bottom.
7138 // The point of exit cannot be a branch out of the structured block.
7139 // longjmp() and throw() must not violate the entry/exit criteria.
7140 CS->getCapturedDecl()->setNothrow();
7141
7142 OMPLoopDirective::HelperExprs B;
7143 // In presence of clause 'collapse' with number of loops, it will
7144 // define the nested loops number.
7145 unsigned NestedLoopCount = CheckOpenMPLoop(
7146 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7147 nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7148 VarsWithImplicitDSA, B);
7149 if (NestedLoopCount == 0)
7150 return StmtError();
7151
7152 assert((CurContext->isDependentContext() || B.builtAll()) &&
7153 "omp for loop exprs were not built");
7154
7155 getCurFunction()->setHasBranchProtectedScope();
7156 return OMPDistributeParallelForSimdDirective::Create(
7157 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7158}
7159
Kelvin Li787f3fc2016-07-06 04:45:38 +00007160StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7161 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7162 SourceLocation EndLoc,
7163 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7164 if (!AStmt)
7165 return StmtError();
7166
7167 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7168 // 1.2.2 OpenMP Language Terminology
7169 // Structured block - An executable statement with a single entry at the
7170 // top and a single exit at the bottom.
7171 // The point of exit cannot be a branch out of the structured block.
7172 // longjmp() and throw() must not violate the entry/exit criteria.
7173 CS->getCapturedDecl()->setNothrow();
7174
7175 OMPLoopDirective::HelperExprs B;
7176 // In presence of clause 'collapse' with number of loops, it will
7177 // define the nested loops number.
7178 unsigned NestedLoopCount =
7179 CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7180 nullptr /*ordered not a clause on distribute*/, AStmt,
7181 *this, *DSAStack, VarsWithImplicitDSA, B);
7182 if (NestedLoopCount == 0)
7183 return StmtError();
7184
7185 assert((CurContext->isDependentContext() || B.builtAll()) &&
7186 "omp for loop exprs were not built");
7187
7188 getCurFunction()->setHasBranchProtectedScope();
7189 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7190 NestedLoopCount, Clauses, AStmt, B);
7191}
7192
Alexey Bataeved09d242014-05-28 05:53:51 +00007193OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007194 SourceLocation StartLoc,
7195 SourceLocation LParenLoc,
7196 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007197 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007198 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007199 case OMPC_final:
7200 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7201 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007202 case OMPC_num_threads:
7203 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7204 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007205 case OMPC_safelen:
7206 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7207 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007208 case OMPC_simdlen:
7209 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7210 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007211 case OMPC_collapse:
7212 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7213 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007214 case OMPC_ordered:
7215 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7216 break;
Michael Wonge710d542015-08-07 16:16:36 +00007217 case OMPC_device:
7218 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7219 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007220 case OMPC_num_teams:
7221 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7222 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007223 case OMPC_thread_limit:
7224 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7225 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007226 case OMPC_priority:
7227 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7228 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007229 case OMPC_grainsize:
7230 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7231 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007232 case OMPC_num_tasks:
7233 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7234 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00007235 case OMPC_hint:
7236 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7237 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007238 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007239 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007240 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007241 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007242 case OMPC_private:
7243 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007244 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007245 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007246 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007247 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007248 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007249 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007250 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00007251 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007252 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007253 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007254 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007255 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007256 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007257 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007258 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007259 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007260 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007261 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00007262 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007263 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007264 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00007265 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007266 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007267 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007268 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007269 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007270 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007271 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007272 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007273 case OMPC_is_device_ptr:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007274 llvm_unreachable("Clause is not allowed.");
7275 }
7276 return Res;
7277}
7278
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007279OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7280 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007281 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007282 SourceLocation NameModifierLoc,
7283 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007284 SourceLocation EndLoc) {
7285 Expr *ValExpr = Condition;
7286 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7287 !Condition->isInstantiationDependent() &&
7288 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007289 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007290 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007291 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007292
Richard Smith03a4aa32016-06-23 19:02:52 +00007293 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007294 }
7295
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007296 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7297 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007298}
7299
Alexey Bataev3778b602014-07-17 07:32:53 +00007300OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7301 SourceLocation StartLoc,
7302 SourceLocation LParenLoc,
7303 SourceLocation EndLoc) {
7304 Expr *ValExpr = Condition;
7305 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7306 !Condition->isInstantiationDependent() &&
7307 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007308 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007309 if (Val.isInvalid())
7310 return nullptr;
7311
Richard Smith03a4aa32016-06-23 19:02:52 +00007312 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007313 }
7314
7315 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7316}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007317ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7318 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007319 if (!Op)
7320 return ExprError();
7321
7322 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7323 public:
7324 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007325 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007326 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7327 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007328 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7329 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007330 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7331 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007332 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7333 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007334 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7335 QualType T,
7336 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007337 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7338 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007339 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7340 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007341 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007342 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007343 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007344 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7345 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007346 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7347 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007348 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7349 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007350 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007351 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007352 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007353 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7354 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007355 llvm_unreachable("conversion functions are permitted");
7356 }
7357 } ConvertDiagnoser;
7358 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7359}
7360
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007361static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007362 OpenMPClauseKind CKind,
7363 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007364 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7365 !ValExpr->isInstantiationDependent()) {
7366 SourceLocation Loc = ValExpr->getExprLoc();
7367 ExprResult Value =
7368 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7369 if (Value.isInvalid())
7370 return false;
7371
7372 ValExpr = Value.get();
7373 // The expression must evaluate to a non-negative integer value.
7374 llvm::APSInt Result;
7375 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007376 Result.isSigned() &&
7377 !((!StrictlyPositive && Result.isNonNegative()) ||
7378 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007379 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007380 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7381 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007382 return false;
7383 }
7384 }
7385 return true;
7386}
7387
Alexey Bataev568a8332014-03-06 06:15:19 +00007388OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7389 SourceLocation StartLoc,
7390 SourceLocation LParenLoc,
7391 SourceLocation EndLoc) {
7392 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007393
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007394 // OpenMP [2.5, Restrictions]
7395 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007396 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7397 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007398 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007399
Alexey Bataeved09d242014-05-28 05:53:51 +00007400 return new (Context)
7401 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007402}
7403
Alexey Bataev62c87d22014-03-21 04:51:18 +00007404ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007405 OpenMPClauseKind CKind,
7406 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007407 if (!E)
7408 return ExprError();
7409 if (E->isValueDependent() || E->isTypeDependent() ||
7410 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007411 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007412 llvm::APSInt Result;
7413 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7414 if (ICE.isInvalid())
7415 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007416 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7417 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007418 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007419 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7420 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007421 return ExprError();
7422 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007423 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7424 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7425 << E->getSourceRange();
7426 return ExprError();
7427 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007428 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7429 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007430 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007431 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007432 return ICE;
7433}
7434
7435OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7436 SourceLocation LParenLoc,
7437 SourceLocation EndLoc) {
7438 // OpenMP [2.8.1, simd construct, Description]
7439 // The parameter of the safelen clause must be a constant
7440 // positive integer expression.
7441 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7442 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007443 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007444 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007445 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007446}
7447
Alexey Bataev66b15b52015-08-21 11:14:16 +00007448OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7449 SourceLocation LParenLoc,
7450 SourceLocation EndLoc) {
7451 // OpenMP [2.8.1, simd construct, Description]
7452 // The parameter of the simdlen clause must be a constant
7453 // positive integer expression.
7454 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7455 if (Simdlen.isInvalid())
7456 return nullptr;
7457 return new (Context)
7458 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7459}
7460
Alexander Musman64d33f12014-06-04 07:53:32 +00007461OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7462 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007463 SourceLocation LParenLoc,
7464 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007465 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007466 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007467 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007468 // The parameter of the collapse clause must be a constant
7469 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007470 ExprResult NumForLoopsResult =
7471 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7472 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007473 return nullptr;
7474 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007475 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007476}
7477
Alexey Bataev10e775f2015-07-30 11:36:16 +00007478OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7479 SourceLocation EndLoc,
7480 SourceLocation LParenLoc,
7481 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007482 // OpenMP [2.7.1, loop construct, Description]
7483 // OpenMP [2.8.1, simd construct, Description]
7484 // OpenMP [2.9.6, distribute construct, Description]
7485 // The parameter of the ordered clause must be a constant
7486 // positive integer expression if any.
7487 if (NumForLoops && LParenLoc.isValid()) {
7488 ExprResult NumForLoopsResult =
7489 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7490 if (NumForLoopsResult.isInvalid())
7491 return nullptr;
7492 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007493 } else
7494 NumForLoops = nullptr;
7495 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007496 return new (Context)
7497 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7498}
7499
Alexey Bataeved09d242014-05-28 05:53:51 +00007500OMPClause *Sema::ActOnOpenMPSimpleClause(
7501 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7502 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007503 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007504 switch (Kind) {
7505 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007506 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007507 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7508 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007509 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007510 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007511 Res = ActOnOpenMPProcBindClause(
7512 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7513 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007514 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007515 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007516 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007517 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007518 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007519 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007520 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007521 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007522 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007523 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007524 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007525 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007526 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007527 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007528 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007529 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007530 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007531 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007532 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007533 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007534 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007535 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007536 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007537 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007538 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007539 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007540 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007541 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007542 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007543 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007544 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007545 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007546 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007547 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007548 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007549 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007550 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007551 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007552 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007553 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007554 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007555 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007556 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007557 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007558 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007559 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007560 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007561 case OMPC_is_device_ptr:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007562 llvm_unreachable("Clause is not allowed.");
7563 }
7564 return Res;
7565}
7566
Alexey Bataev6402bca2015-12-28 07:25:51 +00007567static std::string
7568getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7569 ArrayRef<unsigned> Exclude = llvm::None) {
7570 std::string Values;
7571 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7572 unsigned Skipped = Exclude.size();
7573 auto S = Exclude.begin(), E = Exclude.end();
7574 for (unsigned i = First; i < Last; ++i) {
7575 if (std::find(S, E, i) != E) {
7576 --Skipped;
7577 continue;
7578 }
7579 Values += "'";
7580 Values += getOpenMPSimpleClauseTypeName(K, i);
7581 Values += "'";
7582 if (i == Bound - Skipped)
7583 Values += " or ";
7584 else if (i != Bound + 1 - Skipped)
7585 Values += ", ";
7586 }
7587 return Values;
7588}
7589
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007590OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7591 SourceLocation KindKwLoc,
7592 SourceLocation StartLoc,
7593 SourceLocation LParenLoc,
7594 SourceLocation EndLoc) {
7595 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007596 static_assert(OMPC_DEFAULT_unknown > 0,
7597 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007598 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007599 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7600 /*Last=*/OMPC_DEFAULT_unknown)
7601 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007602 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007603 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007604 switch (Kind) {
7605 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007606 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007607 break;
7608 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007609 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007610 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007611 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007612 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007613 break;
7614 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007615 return new (Context)
7616 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007617}
7618
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007619OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7620 SourceLocation KindKwLoc,
7621 SourceLocation StartLoc,
7622 SourceLocation LParenLoc,
7623 SourceLocation EndLoc) {
7624 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007625 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007626 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7627 /*Last=*/OMPC_PROC_BIND_unknown)
7628 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007629 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007630 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007631 return new (Context)
7632 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007633}
7634
Alexey Bataev56dafe82014-06-20 07:16:17 +00007635OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007636 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007637 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007638 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007639 SourceLocation EndLoc) {
7640 OMPClause *Res = nullptr;
7641 switch (Kind) {
7642 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007643 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7644 assert(Argument.size() == NumberOfElements &&
7645 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007646 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007647 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7648 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7649 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7650 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7651 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007652 break;
7653 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007654 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7655 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7656 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7657 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007658 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007659 case OMPC_dist_schedule:
7660 Res = ActOnOpenMPDistScheduleClause(
7661 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7662 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7663 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007664 case OMPC_defaultmap:
7665 enum { Modifier, DefaultmapKind };
7666 Res = ActOnOpenMPDefaultmapClause(
7667 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7668 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7669 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7670 ArgumentLoc[DefaultmapKind], EndLoc);
7671 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007672 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007673 case OMPC_num_threads:
7674 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007675 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007676 case OMPC_collapse:
7677 case OMPC_default:
7678 case OMPC_proc_bind:
7679 case OMPC_private:
7680 case OMPC_firstprivate:
7681 case OMPC_lastprivate:
7682 case OMPC_shared:
7683 case OMPC_reduction:
7684 case OMPC_linear:
7685 case OMPC_aligned:
7686 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007687 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007688 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007689 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007690 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007691 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007692 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007693 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007694 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007695 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007696 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007697 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007698 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007699 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007700 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007701 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007702 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007703 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007704 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007705 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007706 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007707 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007708 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007709 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007710 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007711 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007712 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007713 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007714 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007715 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007716 case OMPC_is_device_ptr:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007717 llvm_unreachable("Clause is not allowed.");
7718 }
7719 return Res;
7720}
7721
Alexey Bataev6402bca2015-12-28 07:25:51 +00007722static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7723 OpenMPScheduleClauseModifier M2,
7724 SourceLocation M1Loc, SourceLocation M2Loc) {
7725 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7726 SmallVector<unsigned, 2> Excluded;
7727 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7728 Excluded.push_back(M2);
7729 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7730 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7731 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7732 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7733 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7734 << getListOfPossibleValues(OMPC_schedule,
7735 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7736 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7737 Excluded)
7738 << getOpenMPClauseName(OMPC_schedule);
7739 return true;
7740 }
7741 return false;
7742}
7743
Alexey Bataev56dafe82014-06-20 07:16:17 +00007744OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007745 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007746 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007747 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7748 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7749 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7750 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7751 return nullptr;
7752 // OpenMP, 2.7.1, Loop Construct, Restrictions
7753 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7754 // but not both.
7755 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7756 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7757 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7758 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7759 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7760 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7761 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7762 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7763 return nullptr;
7764 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007765 if (Kind == OMPC_SCHEDULE_unknown) {
7766 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007767 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7768 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7769 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7770 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7771 Exclude);
7772 } else {
7773 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7774 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007775 }
7776 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7777 << Values << getOpenMPClauseName(OMPC_schedule);
7778 return nullptr;
7779 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007780 // OpenMP, 2.7.1, Loop Construct, Restrictions
7781 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7782 // schedule(guided).
7783 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7784 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7785 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7786 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7787 diag::err_omp_schedule_nonmonotonic_static);
7788 return nullptr;
7789 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007790 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007791 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007792 if (ChunkSize) {
7793 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7794 !ChunkSize->isInstantiationDependent() &&
7795 !ChunkSize->containsUnexpandedParameterPack()) {
7796 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7797 ExprResult Val =
7798 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7799 if (Val.isInvalid())
7800 return nullptr;
7801
7802 ValExpr = Val.get();
7803
7804 // OpenMP [2.7.1, Restrictions]
7805 // chunk_size must be a loop invariant integer expression with a positive
7806 // value.
7807 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007808 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7809 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7810 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007811 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007812 return nullptr;
7813 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007814 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7815 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007816 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7817 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7818 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007819 }
7820 }
7821 }
7822
Alexey Bataev6402bca2015-12-28 07:25:51 +00007823 return new (Context)
7824 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007825 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007826}
7827
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007828OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7829 SourceLocation StartLoc,
7830 SourceLocation EndLoc) {
7831 OMPClause *Res = nullptr;
7832 switch (Kind) {
7833 case OMPC_ordered:
7834 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7835 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007836 case OMPC_nowait:
7837 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7838 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007839 case OMPC_untied:
7840 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7841 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007842 case OMPC_mergeable:
7843 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7844 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007845 case OMPC_read:
7846 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7847 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007848 case OMPC_write:
7849 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7850 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007851 case OMPC_update:
7852 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7853 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007854 case OMPC_capture:
7855 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7856 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007857 case OMPC_seq_cst:
7858 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7859 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007860 case OMPC_threads:
7861 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7862 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007863 case OMPC_simd:
7864 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7865 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007866 case OMPC_nogroup:
7867 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7868 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007869 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007870 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007871 case OMPC_num_threads:
7872 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007873 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007874 case OMPC_collapse:
7875 case OMPC_schedule:
7876 case OMPC_private:
7877 case OMPC_firstprivate:
7878 case OMPC_lastprivate:
7879 case OMPC_shared:
7880 case OMPC_reduction:
7881 case OMPC_linear:
7882 case OMPC_aligned:
7883 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007884 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007885 case OMPC_default:
7886 case OMPC_proc_bind:
7887 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007888 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007889 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007890 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007891 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007892 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007893 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007894 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007895 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007896 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007897 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007898 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007899 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007900 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007901 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007902 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007903 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00007904 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00007905 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007906 llvm_unreachable("Clause is not allowed.");
7907 }
7908 return Res;
7909}
7910
Alexey Bataev236070f2014-06-20 11:19:47 +00007911OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7912 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007913 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007914 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7915}
7916
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007917OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7918 SourceLocation EndLoc) {
7919 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7920}
7921
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007922OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7923 SourceLocation EndLoc) {
7924 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7925}
7926
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007927OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7928 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007929 return new (Context) OMPReadClause(StartLoc, EndLoc);
7930}
7931
Alexey Bataevdea47612014-07-23 07:46:59 +00007932OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7933 SourceLocation EndLoc) {
7934 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7935}
7936
Alexey Bataev67a4f222014-07-23 10:25:33 +00007937OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7938 SourceLocation EndLoc) {
7939 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7940}
7941
Alexey Bataev459dec02014-07-24 06:46:57 +00007942OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7943 SourceLocation EndLoc) {
7944 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7945}
7946
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007947OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7948 SourceLocation EndLoc) {
7949 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7950}
7951
Alexey Bataev346265e2015-09-25 10:37:12 +00007952OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7953 SourceLocation EndLoc) {
7954 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7955}
7956
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007957OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7958 SourceLocation EndLoc) {
7959 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7960}
7961
Alexey Bataevb825de12015-12-07 10:51:44 +00007962OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7963 SourceLocation EndLoc) {
7964 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7965}
7966
Alexey Bataevc5e02582014-06-16 07:08:35 +00007967OMPClause *Sema::ActOnOpenMPVarListClause(
7968 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7969 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7970 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007971 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007972 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7973 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7974 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007975 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007976 switch (Kind) {
7977 case OMPC_private:
7978 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7979 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007980 case OMPC_firstprivate:
7981 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7982 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007983 case OMPC_lastprivate:
7984 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7985 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007986 case OMPC_shared:
7987 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7988 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007989 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007990 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7991 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007992 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007993 case OMPC_linear:
7994 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007995 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007996 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007997 case OMPC_aligned:
7998 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7999 ColonLoc, EndLoc);
8000 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008001 case OMPC_copyin:
8002 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8003 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008004 case OMPC_copyprivate:
8005 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8006 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00008007 case OMPC_flush:
8008 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8009 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008010 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008011 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8012 StartLoc, LParenLoc, EndLoc);
8013 break;
8014 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008015 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8016 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8017 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008018 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008019 case OMPC_to:
8020 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8021 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008022 case OMPC_from:
8023 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8024 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00008025 case OMPC_use_device_ptr:
8026 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8027 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00008028 case OMPC_is_device_ptr:
8029 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8030 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008031 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008032 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008033 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008034 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008035 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008036 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008037 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008038 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008039 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008040 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008041 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008042 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008043 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008044 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008045 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008046 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008047 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008048 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008049 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008050 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008051 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008052 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008053 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008054 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008055 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008056 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008057 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008058 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008059 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008060 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008061 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008062 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008063 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008064 llvm_unreachable("Clause is not allowed.");
8065 }
8066 return Res;
8067}
8068
Alexey Bataev90c228f2016-02-08 09:29:13 +00008069ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008070 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008071 ExprResult Res = BuildDeclRefExpr(
8072 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8073 if (!Res.isUsable())
8074 return ExprError();
8075 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8076 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8077 if (!Res.isUsable())
8078 return ExprError();
8079 }
8080 if (VK != VK_LValue && Res.get()->isGLValue()) {
8081 Res = DefaultLvalueConversion(Res.get());
8082 if (!Res.isUsable())
8083 return ExprError();
8084 }
8085 return Res;
8086}
8087
Alexey Bataev60da77e2016-02-29 05:54:20 +00008088static std::pair<ValueDecl *, bool>
8089getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8090 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008091 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8092 RefExpr->containsUnexpandedParameterPack())
8093 return std::make_pair(nullptr, true);
8094
Alexey Bataevd985eda2016-02-10 11:29:16 +00008095 // OpenMP [3.1, C/C++]
8096 // A list item is a variable name.
8097 // OpenMP [2.9.3.3, Restrictions, p.1]
8098 // A variable that is part of another variable (as an array or
8099 // structure element) cannot appear in a private clause.
8100 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008101 enum {
8102 NoArrayExpr = -1,
8103 ArraySubscript = 0,
8104 OMPArraySection = 1
8105 } IsArrayExpr = NoArrayExpr;
8106 if (AllowArraySection) {
8107 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8108 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8109 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8110 Base = TempASE->getBase()->IgnoreParenImpCasts();
8111 RefExpr = Base;
8112 IsArrayExpr = ArraySubscript;
8113 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8114 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8115 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8116 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8117 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8118 Base = TempASE->getBase()->IgnoreParenImpCasts();
8119 RefExpr = Base;
8120 IsArrayExpr = OMPArraySection;
8121 }
8122 }
8123 ELoc = RefExpr->getExprLoc();
8124 ERange = RefExpr->getSourceRange();
8125 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008126 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8127 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8128 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8129 (S.getCurrentThisType().isNull() || !ME ||
8130 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8131 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008132 if (IsArrayExpr != NoArrayExpr)
8133 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8134 << ERange;
8135 else {
8136 S.Diag(ELoc,
8137 AllowArraySection
8138 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8139 : diag::err_omp_expected_var_name_member_expr)
8140 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8141 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008142 return std::make_pair(nullptr, false);
8143 }
8144 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8145}
8146
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008147OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8148 SourceLocation StartLoc,
8149 SourceLocation LParenLoc,
8150 SourceLocation EndLoc) {
8151 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008152 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008153 for (auto &RefExpr : VarList) {
8154 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008155 SourceLocation ELoc;
8156 SourceRange ERange;
8157 Expr *SimpleRefExpr = RefExpr;
8158 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008159 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008160 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008161 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008162 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008163 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008164 ValueDecl *D = Res.first;
8165 if (!D)
8166 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008167
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008168 QualType Type = D->getType();
8169 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008170
8171 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8172 // A variable that appears in a private clause must not have an incomplete
8173 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008174 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008175 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008176 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008177
Alexey Bataev758e55e2013-09-06 18:03:48 +00008178 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8179 // in a Construct]
8180 // Variables with the predetermined data-sharing attributes may not be
8181 // listed in data-sharing attributes clauses, except for the cases
8182 // listed below. For these exceptions only, listing a predetermined
8183 // variable in a data-sharing attribute clause is allowed and overrides
8184 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008185 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008186 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008187 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8188 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008189 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008190 continue;
8191 }
8192
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008193 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008194 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008195 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008196 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8197 << getOpenMPClauseName(OMPC_private) << Type
8198 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8199 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008200 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008201 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008202 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008203 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008204 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008205 continue;
8206 }
8207
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008208 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8209 // A list item cannot appear in both a map clause and a data-sharing
8210 // attribute clause on the same construct
8211 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008212 if (DSAStack->checkMappableExprComponentListsForDecl(
8213 VD, /* CurrentRegionOnly = */ true,
8214 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8215 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008216 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8217 << getOpenMPClauseName(OMPC_private)
8218 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8219 ReportOriginalDSA(*this, DSAStack, D, DVar);
8220 continue;
8221 }
8222 }
8223
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008224 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8225 // A variable of class type (or array thereof) that appears in a private
8226 // clause requires an accessible, unambiguous default constructor for the
8227 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008228 // Generate helper private variable and initialize it with the default
8229 // value. The address of the original variable is replaced by the address of
8230 // the new private variable in CodeGen. This new variable is not added to
8231 // IdResolver, so the code in the OpenMP region uses original variable for
8232 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008233 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008234 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8235 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008236 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008237 if (VDPrivate->isInvalidDecl())
8238 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008239 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008240 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008241
Alexey Bataev90c228f2016-02-08 09:29:13 +00008242 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008243 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008244 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008245 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008246 Vars.push_back((VD || CurContext->isDependentContext())
8247 ? RefExpr->IgnoreParens()
8248 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008249 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008250 }
8251
Alexey Bataeved09d242014-05-28 05:53:51 +00008252 if (Vars.empty())
8253 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008254
Alexey Bataev03b340a2014-10-21 03:16:40 +00008255 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8256 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008257}
8258
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008259namespace {
8260class DiagsUninitializedSeveretyRAII {
8261private:
8262 DiagnosticsEngine &Diags;
8263 SourceLocation SavedLoc;
8264 bool IsIgnored;
8265
8266public:
8267 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8268 bool IsIgnored)
8269 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8270 if (!IsIgnored) {
8271 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8272 /*Map*/ diag::Severity::Ignored, Loc);
8273 }
8274 }
8275 ~DiagsUninitializedSeveretyRAII() {
8276 if (!IsIgnored)
8277 Diags.popMappings(SavedLoc);
8278 }
8279};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008280}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008281
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008282OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8283 SourceLocation StartLoc,
8284 SourceLocation LParenLoc,
8285 SourceLocation EndLoc) {
8286 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008287 SmallVector<Expr *, 8> PrivateCopies;
8288 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008289 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008290 bool IsImplicitClause =
8291 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8292 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8293
Alexey Bataeved09d242014-05-28 05:53:51 +00008294 for (auto &RefExpr : VarList) {
8295 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008296 SourceLocation ELoc;
8297 SourceRange ERange;
8298 Expr *SimpleRefExpr = RefExpr;
8299 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008300 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008301 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008302 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008303 PrivateCopies.push_back(nullptr);
8304 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008305 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008306 ValueDecl *D = Res.first;
8307 if (!D)
8308 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008309
Alexey Bataev60da77e2016-02-29 05:54:20 +00008310 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008311 QualType Type = D->getType();
8312 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008313
8314 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8315 // A variable that appears in a private clause must not have an incomplete
8316 // type or a reference type.
8317 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008318 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008319 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008320 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008321
8322 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8323 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008324 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008325 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008326 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008327
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008328 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008329 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008330 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008331 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008332 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008333 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008334 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8335 // A list item that specifies a given variable may not appear in more
8336 // than one clause on the same directive, except that a variable may be
8337 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008338 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008339 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008340 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008341 << getOpenMPClauseName(DVar.CKind)
8342 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008343 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008344 continue;
8345 }
8346
8347 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8348 // in a Construct]
8349 // Variables with the predetermined data-sharing attributes may not be
8350 // listed in data-sharing attributes clauses, except for the cases
8351 // listed below. For these exceptions only, listing a predetermined
8352 // variable in a data-sharing attribute clause is allowed and overrides
8353 // the variable's predetermined data-sharing attributes.
8354 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8355 // in a Construct, C/C++, p.2]
8356 // Variables with const-qualified type having no mutable member may be
8357 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008358 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008359 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8360 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008361 << getOpenMPClauseName(DVar.CKind)
8362 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008363 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008364 continue;
8365 }
8366
Alexey Bataevf29276e2014-06-18 04:14:57 +00008367 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008368 // OpenMP [2.9.3.4, Restrictions, p.2]
8369 // A list item that is private within a parallel region must not appear
8370 // in a firstprivate clause on a worksharing construct if any of the
8371 // worksharing regions arising from the worksharing construct ever bind
8372 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008373 if (isOpenMPWorksharingDirective(CurrDir) &&
8374 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008375 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008376 if (DVar.CKind != OMPC_shared &&
8377 (isOpenMPParallelDirective(DVar.DKind) ||
8378 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008379 Diag(ELoc, diag::err_omp_required_access)
8380 << getOpenMPClauseName(OMPC_firstprivate)
8381 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008382 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008383 continue;
8384 }
8385 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008386 // OpenMP [2.9.3.4, Restrictions, p.3]
8387 // A list item that appears in a reduction clause of a parallel construct
8388 // must not appear in a firstprivate clause on a worksharing or task
8389 // construct if any of the worksharing or task regions arising from the
8390 // worksharing or task construct ever bind to any of the parallel regions
8391 // arising from the parallel construct.
8392 // OpenMP [2.9.3.4, Restrictions, p.4]
8393 // A list item that appears in a reduction clause in worksharing
8394 // construct must not appear in a firstprivate clause in a task construct
8395 // encountered during execution of any of the worksharing regions arising
8396 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008397 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008398 DVar = DSAStack->hasInnermostDSA(
8399 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8400 [](OpenMPDirectiveKind K) -> bool {
8401 return isOpenMPParallelDirective(K) ||
8402 isOpenMPWorksharingDirective(K);
8403 },
8404 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008405 if (DVar.CKind == OMPC_reduction &&
8406 (isOpenMPParallelDirective(DVar.DKind) ||
8407 isOpenMPWorksharingDirective(DVar.DKind))) {
8408 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8409 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008410 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008411 continue;
8412 }
8413 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008414
8415 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8416 // A list item that is private within a teams region must not appear in a
8417 // firstprivate clause on a distribute construct if any of the distribute
8418 // regions arising from the distribute construct ever bind to any of the
8419 // teams regions arising from the teams construct.
8420 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8421 // A list item that appears in a reduction clause of a teams construct
8422 // must not appear in a firstprivate clause on a distribute construct if
8423 // any of the distribute regions arising from the distribute construct
8424 // ever bind to any of the teams regions arising from the teams construct.
8425 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8426 // A list item may appear in a firstprivate or lastprivate clause but not
8427 // both.
8428 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008429 DVar = DSAStack->hasInnermostDSA(
8430 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8431 [](OpenMPDirectiveKind K) -> bool {
8432 return isOpenMPTeamsDirective(K);
8433 },
8434 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008435 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8436 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008437 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008438 continue;
8439 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008440 DVar = DSAStack->hasInnermostDSA(
8441 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8442 [](OpenMPDirectiveKind K) -> bool {
8443 return isOpenMPTeamsDirective(K);
8444 },
8445 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008446 if (DVar.CKind == OMPC_reduction &&
8447 isOpenMPTeamsDirective(DVar.DKind)) {
8448 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008449 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008450 continue;
8451 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008452 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008453 if (DVar.CKind == OMPC_lastprivate) {
8454 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008455 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008456 continue;
8457 }
8458 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008459 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8460 // A list item cannot appear in both a map clause and a data-sharing
8461 // attribute clause on the same construct
8462 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008463 if (DSAStack->checkMappableExprComponentListsForDecl(
8464 VD, /* CurrentRegionOnly = */ true,
8465 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8466 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008467 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8468 << getOpenMPClauseName(OMPC_firstprivate)
8469 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8470 ReportOriginalDSA(*this, DSAStack, D, DVar);
8471 continue;
8472 }
8473 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008474 }
8475
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008476 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008477 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008478 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008479 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8480 << getOpenMPClauseName(OMPC_firstprivate) << Type
8481 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8482 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008483 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008484 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008485 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008486 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008487 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008488 continue;
8489 }
8490
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008491 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008492 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8493 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008494 // Generate helper private variable and initialize it with the value of the
8495 // original variable. The address of the original variable is replaced by
8496 // the address of the new private variable in the CodeGen. This new variable
8497 // is not added to IdResolver, so the code in the OpenMP region uses
8498 // original variable for proper diagnostics and variable capturing.
8499 Expr *VDInitRefExpr = nullptr;
8500 // For arrays generate initializer for single element and replace it by the
8501 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008502 if (Type->isArrayType()) {
8503 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008504 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008505 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008506 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008507 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008508 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008509 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008510 InitializedEntity Entity =
8511 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008512 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8513
8514 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8515 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8516 if (Result.isInvalid())
8517 VDPrivate->setInvalidDecl();
8518 else
8519 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008520 // Remove temp variable declaration.
8521 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008522 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008523 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8524 ".firstprivate.temp");
8525 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8526 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008527 AddInitializerToDecl(VDPrivate,
8528 DefaultLvalueConversion(VDInitRefExpr).get(),
8529 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008530 }
8531 if (VDPrivate->isInvalidDecl()) {
8532 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008533 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008534 diag::note_omp_task_predetermined_firstprivate_here);
8535 }
8536 continue;
8537 }
8538 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008539 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008540 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8541 RefExpr->getExprLoc());
8542 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008543 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008544 if (TopDVar.CKind == OMPC_lastprivate)
8545 Ref = TopDVar.PrivateCopy;
8546 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008547 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008548 if (!IsOpenMPCapturedDecl(D))
8549 ExprCaptures.push_back(Ref->getDecl());
8550 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008551 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008552 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008553 Vars.push_back((VD || CurContext->isDependentContext())
8554 ? RefExpr->IgnoreParens()
8555 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008556 PrivateCopies.push_back(VDPrivateRefExpr);
8557 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008558 }
8559
Alexey Bataeved09d242014-05-28 05:53:51 +00008560 if (Vars.empty())
8561 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008562
8563 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008564 Vars, PrivateCopies, Inits,
8565 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008566}
8567
Alexander Musman1bb328c2014-06-04 13:06:39 +00008568OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8569 SourceLocation StartLoc,
8570 SourceLocation LParenLoc,
8571 SourceLocation EndLoc) {
8572 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008573 SmallVector<Expr *, 8> SrcExprs;
8574 SmallVector<Expr *, 8> DstExprs;
8575 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008576 SmallVector<Decl *, 4> ExprCaptures;
8577 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008578 for (auto &RefExpr : VarList) {
8579 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008580 SourceLocation ELoc;
8581 SourceRange ERange;
8582 Expr *SimpleRefExpr = RefExpr;
8583 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008584 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008585 // It will be analyzed later.
8586 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008587 SrcExprs.push_back(nullptr);
8588 DstExprs.push_back(nullptr);
8589 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008590 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008591 ValueDecl *D = Res.first;
8592 if (!D)
8593 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008594
Alexey Bataev74caaf22016-02-20 04:09:36 +00008595 QualType Type = D->getType();
8596 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008597
8598 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8599 // A variable that appears in a lastprivate clause must not have an
8600 // incomplete type or a reference type.
8601 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008602 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008603 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008604 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008605
8606 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8607 // in a Construct]
8608 // Variables with the predetermined data-sharing attributes may not be
8609 // listed in data-sharing attributes clauses, except for the cases
8610 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008611 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008612 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8613 DVar.CKind != OMPC_firstprivate &&
8614 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8615 Diag(ELoc, diag::err_omp_wrong_dsa)
8616 << getOpenMPClauseName(DVar.CKind)
8617 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008618 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008619 continue;
8620 }
8621
Alexey Bataevf29276e2014-06-18 04:14:57 +00008622 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8623 // OpenMP [2.14.3.5, Restrictions, p.2]
8624 // A list item that is private within a parallel region, or that appears in
8625 // the reduction clause of a parallel construct, must not appear in a
8626 // lastprivate clause on a worksharing construct if any of the corresponding
8627 // worksharing regions ever binds to any of the corresponding parallel
8628 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008629 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008630 if (isOpenMPWorksharingDirective(CurrDir) &&
8631 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008632 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008633 if (DVar.CKind != OMPC_shared) {
8634 Diag(ELoc, diag::err_omp_required_access)
8635 << getOpenMPClauseName(OMPC_lastprivate)
8636 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008637 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008638 continue;
8639 }
8640 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008641
8642 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8643 // A list item may appear in a firstprivate or lastprivate clause but not
8644 // both.
8645 if (CurrDir == OMPD_distribute) {
8646 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8647 if (DVar.CKind == OMPC_firstprivate) {
8648 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8649 ReportOriginalDSA(*this, DSAStack, D, DVar);
8650 continue;
8651 }
8652 }
8653
Alexander Musman1bb328c2014-06-04 13:06:39 +00008654 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008655 // A variable of class type (or array thereof) that appears in a
8656 // lastprivate clause requires an accessible, unambiguous default
8657 // constructor for the class type, unless the list item is also specified
8658 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008659 // A variable of class type (or array thereof) that appears in a
8660 // lastprivate clause requires an accessible, unambiguous copy assignment
8661 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008662 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008663 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008664 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008665 D->hasAttrs() ? &D->getAttrs() : nullptr);
8666 auto *PseudoSrcExpr =
8667 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008668 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008669 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008670 D->hasAttrs() ? &D->getAttrs() : nullptr);
8671 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008672 // For arrays generate assignment operation for single element and replace
8673 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008674 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008675 PseudoDstExpr, PseudoSrcExpr);
8676 if (AssignmentOp.isInvalid())
8677 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008678 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008679 /*DiscardedValue=*/true);
8680 if (AssignmentOp.isInvalid())
8681 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008682
Alexey Bataev74caaf22016-02-20 04:09:36 +00008683 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008684 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008685 if (TopDVar.CKind == OMPC_firstprivate)
8686 Ref = TopDVar.PrivateCopy;
8687 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008688 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008689 if (!IsOpenMPCapturedDecl(D))
8690 ExprCaptures.push_back(Ref->getDecl());
8691 }
8692 if (TopDVar.CKind == OMPC_firstprivate ||
8693 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008694 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008695 ExprResult RefRes = DefaultLvalueConversion(Ref);
8696 if (!RefRes.isUsable())
8697 continue;
8698 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008699 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8700 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008701 if (!PostUpdateRes.isUsable())
8702 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008703 ExprPostUpdates.push_back(
8704 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008705 }
8706 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008707 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008708 Vars.push_back((VD || CurContext->isDependentContext())
8709 ? RefExpr->IgnoreParens()
8710 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008711 SrcExprs.push_back(PseudoSrcExpr);
8712 DstExprs.push_back(PseudoDstExpr);
8713 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008714 }
8715
8716 if (Vars.empty())
8717 return nullptr;
8718
8719 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008720 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008721 buildPreInits(Context, ExprCaptures),
8722 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008723}
8724
Alexey Bataev758e55e2013-09-06 18:03:48 +00008725OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8726 SourceLocation StartLoc,
8727 SourceLocation LParenLoc,
8728 SourceLocation EndLoc) {
8729 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008730 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008731 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008732 SourceLocation ELoc;
8733 SourceRange ERange;
8734 Expr *SimpleRefExpr = RefExpr;
8735 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008736 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008737 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008738 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008739 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008740 ValueDecl *D = Res.first;
8741 if (!D)
8742 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008743
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008744 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008745 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8746 // in a Construct]
8747 // Variables with the predetermined data-sharing attributes may not be
8748 // listed in data-sharing attributes clauses, except for the cases
8749 // listed below. For these exceptions only, listing a predetermined
8750 // variable in a data-sharing attribute clause is allowed and overrides
8751 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008752 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008753 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8754 DVar.RefExpr) {
8755 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8756 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008757 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008758 continue;
8759 }
8760
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008761 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008762 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008763 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008764 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008765 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8766 ? RefExpr->IgnoreParens()
8767 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008768 }
8769
Alexey Bataeved09d242014-05-28 05:53:51 +00008770 if (Vars.empty())
8771 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008772
8773 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8774}
8775
Alexey Bataevc5e02582014-06-16 07:08:35 +00008776namespace {
8777class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8778 DSAStackTy *Stack;
8779
8780public:
8781 bool VisitDeclRefExpr(DeclRefExpr *E) {
8782 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008783 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008784 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8785 return false;
8786 if (DVar.CKind != OMPC_unknown)
8787 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008788 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8789 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8790 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008791 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008792 return true;
8793 return false;
8794 }
8795 return false;
8796 }
8797 bool VisitStmt(Stmt *S) {
8798 for (auto Child : S->children()) {
8799 if (Child && Visit(Child))
8800 return true;
8801 }
8802 return false;
8803 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008804 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008805};
Alexey Bataev23b69422014-06-18 07:08:49 +00008806} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008807
Alexey Bataev60da77e2016-02-29 05:54:20 +00008808namespace {
8809// Transform MemberExpression for specified FieldDecl of current class to
8810// DeclRefExpr to specified OMPCapturedExprDecl.
8811class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8812 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8813 ValueDecl *Field;
8814 DeclRefExpr *CapturedExpr;
8815
8816public:
8817 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8818 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8819
8820 ExprResult TransformMemberExpr(MemberExpr *E) {
8821 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8822 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008823 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008824 return CapturedExpr;
8825 }
8826 return BaseTransform::TransformMemberExpr(E);
8827 }
8828 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8829};
8830} // namespace
8831
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008832template <typename T>
8833static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8834 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8835 for (auto &Set : Lookups) {
8836 for (auto *D : Set) {
8837 if (auto Res = Gen(cast<ValueDecl>(D)))
8838 return Res;
8839 }
8840 }
8841 return T();
8842}
8843
8844static ExprResult
8845buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8846 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8847 const DeclarationNameInfo &ReductionId, QualType Ty,
8848 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8849 if (ReductionIdScopeSpec.isInvalid())
8850 return ExprError();
8851 SmallVector<UnresolvedSet<8>, 4> Lookups;
8852 if (S) {
8853 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8854 Lookup.suppressDiagnostics();
8855 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8856 auto *D = Lookup.getRepresentativeDecl();
8857 do {
8858 S = S->getParent();
8859 } while (S && !S->isDeclScope(D));
8860 if (S)
8861 S = S->getParent();
8862 Lookups.push_back(UnresolvedSet<8>());
8863 Lookups.back().append(Lookup.begin(), Lookup.end());
8864 Lookup.clear();
8865 }
8866 } else if (auto *ULE =
8867 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8868 Lookups.push_back(UnresolvedSet<8>());
8869 Decl *PrevD = nullptr;
8870 for(auto *D : ULE->decls()) {
8871 if (D == PrevD)
8872 Lookups.push_back(UnresolvedSet<8>());
8873 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8874 Lookups.back().addDecl(DRD);
8875 PrevD = D;
8876 }
8877 }
8878 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8879 Ty->containsUnexpandedParameterPack() ||
8880 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8881 return !D->isInvalidDecl() &&
8882 (D->getType()->isDependentType() ||
8883 D->getType()->isInstantiationDependentType() ||
8884 D->getType()->containsUnexpandedParameterPack());
8885 })) {
8886 UnresolvedSet<8> ResSet;
8887 for (auto &Set : Lookups) {
8888 ResSet.append(Set.begin(), Set.end());
8889 // The last item marks the end of all declarations at the specified scope.
8890 ResSet.addDecl(Set[Set.size() - 1]);
8891 }
8892 return UnresolvedLookupExpr::Create(
8893 SemaRef.Context, /*NamingClass=*/nullptr,
8894 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8895 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8896 }
8897 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8898 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8899 if (!D->isInvalidDecl() &&
8900 SemaRef.Context.hasSameType(D->getType(), Ty))
8901 return D;
8902 return nullptr;
8903 }))
8904 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8905 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8906 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8907 if (!D->isInvalidDecl() &&
8908 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8909 !Ty.isMoreQualifiedThan(D->getType()))
8910 return D;
8911 return nullptr;
8912 })) {
8913 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8914 /*DetectVirtual=*/false);
8915 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8916 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8917 VD->getType().getUnqualifiedType()))) {
8918 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8919 /*DiagID=*/0) !=
8920 Sema::AR_inaccessible) {
8921 SemaRef.BuildBasePathArray(Paths, BasePath);
8922 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8923 }
8924 }
8925 }
8926 }
8927 if (ReductionIdScopeSpec.isSet()) {
8928 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8929 return ExprError();
8930 }
8931 return ExprEmpty();
8932}
8933
Alexey Bataevc5e02582014-06-16 07:08:35 +00008934OMPClause *Sema::ActOnOpenMPReductionClause(
8935 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8936 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008937 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8938 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008939 auto DN = ReductionId.getName();
8940 auto OOK = DN.getCXXOverloadedOperator();
8941 BinaryOperatorKind BOK = BO_Comma;
8942
8943 // OpenMP [2.14.3.6, reduction clause]
8944 // C
8945 // reduction-identifier is either an identifier or one of the following
8946 // operators: +, -, *, &, |, ^, && and ||
8947 // C++
8948 // reduction-identifier is either an id-expression or one of the following
8949 // operators: +, -, *, &, |, ^, && and ||
8950 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8951 switch (OOK) {
8952 case OO_Plus:
8953 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008954 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008955 break;
8956 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008957 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008958 break;
8959 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008960 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008961 break;
8962 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008963 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008964 break;
8965 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008966 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008967 break;
8968 case OO_AmpAmp:
8969 BOK = BO_LAnd;
8970 break;
8971 case OO_PipePipe:
8972 BOK = BO_LOr;
8973 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008974 case OO_New:
8975 case OO_Delete:
8976 case OO_Array_New:
8977 case OO_Array_Delete:
8978 case OO_Slash:
8979 case OO_Percent:
8980 case OO_Tilde:
8981 case OO_Exclaim:
8982 case OO_Equal:
8983 case OO_Less:
8984 case OO_Greater:
8985 case OO_LessEqual:
8986 case OO_GreaterEqual:
8987 case OO_PlusEqual:
8988 case OO_MinusEqual:
8989 case OO_StarEqual:
8990 case OO_SlashEqual:
8991 case OO_PercentEqual:
8992 case OO_CaretEqual:
8993 case OO_AmpEqual:
8994 case OO_PipeEqual:
8995 case OO_LessLess:
8996 case OO_GreaterGreater:
8997 case OO_LessLessEqual:
8998 case OO_GreaterGreaterEqual:
8999 case OO_EqualEqual:
9000 case OO_ExclaimEqual:
9001 case OO_PlusPlus:
9002 case OO_MinusMinus:
9003 case OO_Comma:
9004 case OO_ArrowStar:
9005 case OO_Arrow:
9006 case OO_Call:
9007 case OO_Subscript:
9008 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00009009 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009010 case NUM_OVERLOADED_OPERATORS:
9011 llvm_unreachable("Unexpected reduction identifier");
9012 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009013 if (auto II = DN.getAsIdentifierInfo()) {
9014 if (II->isStr("max"))
9015 BOK = BO_GT;
9016 else if (II->isStr("min"))
9017 BOK = BO_LT;
9018 }
9019 break;
9020 }
9021 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009022 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009023 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009024 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009025
9026 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009027 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009028 SmallVector<Expr *, 8> LHSs;
9029 SmallVector<Expr *, 8> RHSs;
9030 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009031 SmallVector<Decl *, 4> ExprCaptures;
9032 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009033 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9034 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009035 for (auto RefExpr : VarList) {
9036 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009037 // OpenMP [2.1, C/C++]
9038 // A list item is a variable or array section, subject to the restrictions
9039 // specified in Section 2.4 on page 42 and in each of the sections
9040 // describing clauses and directives for which a list appears.
9041 // OpenMP [2.14.3.3, Restrictions, p.1]
9042 // A variable that is part of another variable (as an array or
9043 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009044 if (!FirstIter && IR != ER)
9045 ++IR;
9046 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009047 SourceLocation ELoc;
9048 SourceRange ERange;
9049 Expr *SimpleRefExpr = RefExpr;
9050 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9051 /*AllowArraySection=*/true);
9052 if (Res.second) {
9053 // It will be analyzed later.
9054 Vars.push_back(RefExpr);
9055 Privates.push_back(nullptr);
9056 LHSs.push_back(nullptr);
9057 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009058 // Try to find 'declare reduction' corresponding construct before using
9059 // builtin/overloaded operators.
9060 QualType Type = Context.DependentTy;
9061 CXXCastPath BasePath;
9062 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9063 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9064 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9065 if (CurContext->isDependentContext() &&
9066 (DeclareReductionRef.isUnset() ||
9067 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9068 ReductionOps.push_back(DeclareReductionRef.get());
9069 else
9070 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009071 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009072 ValueDecl *D = Res.first;
9073 if (!D)
9074 continue;
9075
Alexey Bataeva1764212015-09-30 09:22:36 +00009076 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009077 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9078 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9079 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009080 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009081 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009082 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9083 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9084 Type = ATy->getElementType();
9085 else
9086 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009087 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009088 } else
9089 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9090 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009091
Alexey Bataevc5e02582014-06-16 07:08:35 +00009092 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9093 // A variable that appears in a private clause must not have an incomplete
9094 // type or a reference type.
9095 if (RequireCompleteType(ELoc, Type,
9096 diag::err_omp_reduction_incomplete_type))
9097 continue;
9098 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009099 // A list item that appears in a reduction clause must not be
9100 // const-qualified.
9101 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009102 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009103 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009104 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009105 bool IsDecl = !VD ||
9106 VD->isThisDeclarationADefinition(Context) ==
9107 VarDecl::DeclarationOnly;
9108 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009109 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009110 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009111 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009112 continue;
9113 }
9114 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9115 // If a list-item is a reference type then it must bind to the same object
9116 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009117 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009118 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009119 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009120 DSARefChecker Check(DSAStack);
9121 if (Check.Visit(VDDef->getInit())) {
9122 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9123 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9124 continue;
9125 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009126 }
9127 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009128
Alexey Bataevc5e02582014-06-16 07:08:35 +00009129 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9130 // in a Construct]
9131 // Variables with the predetermined data-sharing attributes may not be
9132 // listed in data-sharing attributes clauses, except for the cases
9133 // listed below. For these exceptions only, listing a predetermined
9134 // variable in a data-sharing attribute clause is allowed and overrides
9135 // the variable's predetermined data-sharing attributes.
9136 // OpenMP [2.14.3.6, Restrictions, p.3]
9137 // Any number of reduction clauses can be specified on the directive,
9138 // but a list item can appear only once in the reduction clauses for that
9139 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009140 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009141 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009142 if (DVar.CKind == OMPC_reduction) {
9143 Diag(ELoc, diag::err_omp_once_referenced)
9144 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009145 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009146 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009147 } else if (DVar.CKind != OMPC_unknown) {
9148 Diag(ELoc, diag::err_omp_wrong_dsa)
9149 << getOpenMPClauseName(DVar.CKind)
9150 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009151 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009152 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009153 }
9154
9155 // OpenMP [2.14.3.6, Restrictions, p.1]
9156 // A list item that appears in a reduction clause of a worksharing
9157 // construct must be shared in the parallel regions to which any of the
9158 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009159 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9160 if (isOpenMPWorksharingDirective(CurrDir) &&
9161 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009162 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009163 if (DVar.CKind != OMPC_shared) {
9164 Diag(ELoc, diag::err_omp_required_access)
9165 << getOpenMPClauseName(OMPC_reduction)
9166 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009167 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009168 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009169 }
9170 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009171
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009172 // Try to find 'declare reduction' corresponding construct before using
9173 // builtin/overloaded operators.
9174 CXXCastPath BasePath;
9175 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9176 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9177 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9178 if (DeclareReductionRef.isInvalid())
9179 continue;
9180 if (CurContext->isDependentContext() &&
9181 (DeclareReductionRef.isUnset() ||
9182 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9183 Vars.push_back(RefExpr);
9184 Privates.push_back(nullptr);
9185 LHSs.push_back(nullptr);
9186 RHSs.push_back(nullptr);
9187 ReductionOps.push_back(DeclareReductionRef.get());
9188 continue;
9189 }
9190 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9191 // Not allowed reduction identifier is found.
9192 Diag(ReductionId.getLocStart(),
9193 diag::err_omp_unknown_reduction_identifier)
9194 << Type << ReductionIdRange;
9195 continue;
9196 }
9197
9198 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9199 // The type of a list item that appears in a reduction clause must be valid
9200 // for the reduction-identifier. For a max or min reduction in C, the type
9201 // of the list item must be an allowed arithmetic data type: char, int,
9202 // float, double, or _Bool, possibly modified with long, short, signed, or
9203 // unsigned. For a max or min reduction in C++, the type of the list item
9204 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9205 // double, or bool, possibly modified with long, short, signed, or unsigned.
9206 if (DeclareReductionRef.isUnset()) {
9207 if ((BOK == BO_GT || BOK == BO_LT) &&
9208 !(Type->isScalarType() ||
9209 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9210 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9211 << getLangOpts().CPlusPlus;
9212 if (!ASE && !OASE) {
9213 bool IsDecl = !VD ||
9214 VD->isThisDeclarationADefinition(Context) ==
9215 VarDecl::DeclarationOnly;
9216 Diag(D->getLocation(),
9217 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9218 << D;
9219 }
9220 continue;
9221 }
9222 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9223 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9224 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9225 if (!ASE && !OASE) {
9226 bool IsDecl = !VD ||
9227 VD->isThisDeclarationADefinition(Context) ==
9228 VarDecl::DeclarationOnly;
9229 Diag(D->getLocation(),
9230 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9231 << D;
9232 }
9233 continue;
9234 }
9235 }
9236
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009237 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009238 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009239 D->hasAttrs() ? &D->getAttrs() : nullptr);
9240 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9241 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009242 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009243 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009244 (!ASE &&
9245 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009246 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009247 // Create pseudo array type for private copy. The size for this array will
9248 // be generated during codegen.
9249 // For array subscripts or single variables Private Ty is the same as Type
9250 // (type of the variable or single array element).
9251 PrivateTy = Context.getVariableArrayType(
9252 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9253 Context.getSizeType(), VK_RValue),
9254 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009255 } else if (!ASE && !OASE &&
9256 Context.getAsArrayType(D->getType().getNonReferenceType()))
9257 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009258 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009259 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9260 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009261 // Add initializer for private variable.
9262 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009263 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9264 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9265 if (DeclareReductionRef.isUsable()) {
9266 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9267 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9268 if (DRD->getInitializer()) {
9269 Init = DRDRef;
9270 RHSVD->setInit(DRDRef);
9271 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009272 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009273 } else {
9274 switch (BOK) {
9275 case BO_Add:
9276 case BO_Xor:
9277 case BO_Or:
9278 case BO_LOr:
9279 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9280 if (Type->isScalarType() || Type->isAnyComplexType())
9281 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9282 break;
9283 case BO_Mul:
9284 case BO_LAnd:
9285 if (Type->isScalarType() || Type->isAnyComplexType()) {
9286 // '*' and '&&' reduction ops - initializer is '1'.
9287 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009288 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009289 break;
9290 case BO_And: {
9291 // '&' reduction op - initializer is '~0'.
9292 QualType OrigType = Type;
9293 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9294 Type = ComplexTy->getElementType();
9295 if (Type->isRealFloatingType()) {
9296 llvm::APFloat InitValue =
9297 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9298 /*isIEEE=*/true);
9299 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9300 Type, ELoc);
9301 } else if (Type->isScalarType()) {
9302 auto Size = Context.getTypeSize(Type);
9303 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9304 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9305 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9306 }
9307 if (Init && OrigType->isAnyComplexType()) {
9308 // Init = 0xFFFF + 0xFFFFi;
9309 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9310 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9311 }
9312 Type = OrigType;
9313 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009314 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009315 case BO_LT:
9316 case BO_GT: {
9317 // 'min' reduction op - initializer is 'Largest representable number in
9318 // the reduction list item type'.
9319 // 'max' reduction op - initializer is 'Least representable number in
9320 // the reduction list item type'.
9321 if (Type->isIntegerType() || Type->isPointerType()) {
9322 bool IsSigned = Type->hasSignedIntegerRepresentation();
9323 auto Size = Context.getTypeSize(Type);
9324 QualType IntTy =
9325 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9326 llvm::APInt InitValue =
9327 (BOK != BO_LT)
9328 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9329 : llvm::APInt::getMinValue(Size)
9330 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9331 : llvm::APInt::getMaxValue(Size);
9332 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9333 if (Type->isPointerType()) {
9334 // Cast to pointer type.
9335 auto CastExpr = BuildCStyleCastExpr(
9336 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9337 SourceLocation(), Init);
9338 if (CastExpr.isInvalid())
9339 continue;
9340 Init = CastExpr.get();
9341 }
9342 } else if (Type->isRealFloatingType()) {
9343 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9344 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9345 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9346 Type, ELoc);
9347 }
9348 break;
9349 }
9350 case BO_PtrMemD:
9351 case BO_PtrMemI:
9352 case BO_MulAssign:
9353 case BO_Div:
9354 case BO_Rem:
9355 case BO_Sub:
9356 case BO_Shl:
9357 case BO_Shr:
9358 case BO_LE:
9359 case BO_GE:
9360 case BO_EQ:
9361 case BO_NE:
9362 case BO_AndAssign:
9363 case BO_XorAssign:
9364 case BO_OrAssign:
9365 case BO_Assign:
9366 case BO_AddAssign:
9367 case BO_SubAssign:
9368 case BO_DivAssign:
9369 case BO_RemAssign:
9370 case BO_ShlAssign:
9371 case BO_ShrAssign:
9372 case BO_Comma:
9373 llvm_unreachable("Unexpected reduction operation");
9374 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009375 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009376 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009377 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9378 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009379 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009380 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009381 if (RHSVD->isInvalidDecl())
9382 continue;
9383 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009384 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9385 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009386 bool IsDecl =
9387 !VD ||
9388 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9389 Diag(D->getLocation(),
9390 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9391 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009392 continue;
9393 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009394 // Store initializer for single element in private copy. Will be used during
9395 // codegen.
9396 PrivateVD->setInit(RHSVD->getInit());
9397 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009398 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009399 ExprResult ReductionOp;
9400 if (DeclareReductionRef.isUsable()) {
9401 QualType RedTy = DeclareReductionRef.get()->getType();
9402 QualType PtrRedTy = Context.getPointerType(RedTy);
9403 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9404 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9405 if (!BasePath.empty()) {
9406 LHS = DefaultLvalueConversion(LHS.get());
9407 RHS = DefaultLvalueConversion(RHS.get());
9408 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9409 CK_UncheckedDerivedToBase, LHS.get(),
9410 &BasePath, LHS.get()->getValueKind());
9411 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9412 CK_UncheckedDerivedToBase, RHS.get(),
9413 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009414 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009415 FunctionProtoType::ExtProtoInfo EPI;
9416 QualType Params[] = {PtrRedTy, PtrRedTy};
9417 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9418 auto *OVE = new (Context) OpaqueValueExpr(
9419 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9420 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9421 Expr *Args[] = {LHS.get(), RHS.get()};
9422 ReductionOp = new (Context)
9423 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9424 } else {
9425 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9426 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9427 if (ReductionOp.isUsable()) {
9428 if (BOK != BO_LT && BOK != BO_GT) {
9429 ReductionOp =
9430 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9431 BO_Assign, LHSDRE, ReductionOp.get());
9432 } else {
9433 auto *ConditionalOp = new (Context) ConditionalOperator(
9434 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9435 RHSDRE, Type, VK_LValue, OK_Ordinary);
9436 ReductionOp =
9437 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9438 BO_Assign, LHSDRE, ConditionalOp);
9439 }
9440 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9441 }
9442 if (ReductionOp.isInvalid())
9443 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009444 }
9445
Alexey Bataev60da77e2016-02-29 05:54:20 +00009446 DeclRefExpr *Ref = nullptr;
9447 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009448 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009449 if (ASE || OASE) {
9450 TransformExprToCaptures RebuildToCapture(*this, D);
9451 VarsExpr =
9452 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9453 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009454 } else {
9455 VarsExpr = Ref =
9456 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009457 }
9458 if (!IsOpenMPCapturedDecl(D)) {
9459 ExprCaptures.push_back(Ref->getDecl());
9460 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9461 ExprResult RefRes = DefaultLvalueConversion(Ref);
9462 if (!RefRes.isUsable())
9463 continue;
9464 ExprResult PostUpdateRes =
9465 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9466 SimpleRefExpr, RefRes.get());
9467 if (!PostUpdateRes.isUsable())
9468 continue;
9469 ExprPostUpdates.push_back(
9470 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009471 }
9472 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009473 }
9474 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9475 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009476 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009477 LHSs.push_back(LHSDRE);
9478 RHSs.push_back(RHSDRE);
9479 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009480 }
9481
9482 if (Vars.empty())
9483 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009484
Alexey Bataevc5e02582014-06-16 07:08:35 +00009485 return OMPReductionClause::Create(
9486 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009487 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009488 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9489 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009490}
9491
Alexey Bataevecba70f2016-04-12 11:02:11 +00009492bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9493 SourceLocation LinLoc) {
9494 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9495 LinKind == OMPC_LINEAR_unknown) {
9496 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9497 return true;
9498 }
9499 return false;
9500}
9501
9502bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9503 OpenMPLinearClauseKind LinKind,
9504 QualType Type) {
9505 auto *VD = dyn_cast_or_null<VarDecl>(D);
9506 // A variable must not have an incomplete type or a reference type.
9507 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9508 return true;
9509 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9510 !Type->isReferenceType()) {
9511 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9512 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9513 return true;
9514 }
9515 Type = Type.getNonReferenceType();
9516
9517 // A list item must not be const-qualified.
9518 if (Type.isConstant(Context)) {
9519 Diag(ELoc, diag::err_omp_const_variable)
9520 << getOpenMPClauseName(OMPC_linear);
9521 if (D) {
9522 bool IsDecl =
9523 !VD ||
9524 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9525 Diag(D->getLocation(),
9526 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9527 << D;
9528 }
9529 return true;
9530 }
9531
9532 // A list item must be of integral or pointer type.
9533 Type = Type.getUnqualifiedType().getCanonicalType();
9534 const auto *Ty = Type.getTypePtrOrNull();
9535 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9536 !Ty->isPointerType())) {
9537 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9538 if (D) {
9539 bool IsDecl =
9540 !VD ||
9541 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9542 Diag(D->getLocation(),
9543 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9544 << D;
9545 }
9546 return true;
9547 }
9548 return false;
9549}
9550
Alexey Bataev182227b2015-08-20 10:54:39 +00009551OMPClause *Sema::ActOnOpenMPLinearClause(
9552 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9553 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9554 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009555 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009556 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009557 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009558 SmallVector<Decl *, 4> ExprCaptures;
9559 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009560 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009561 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009562 for (auto &RefExpr : VarList) {
9563 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009564 SourceLocation ELoc;
9565 SourceRange ERange;
9566 Expr *SimpleRefExpr = RefExpr;
9567 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9568 /*AllowArraySection=*/false);
9569 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009570 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009571 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009572 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009573 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009574 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009575 ValueDecl *D = Res.first;
9576 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009577 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009578
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009579 QualType Type = D->getType();
9580 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009581
9582 // OpenMP [2.14.3.7, linear clause]
9583 // A list-item cannot appear in more than one linear clause.
9584 // A list-item that appears in a linear clause cannot appear in any
9585 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009586 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009587 if (DVar.RefExpr) {
9588 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9589 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009590 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009591 continue;
9592 }
9593
Alexey Bataevecba70f2016-04-12 11:02:11 +00009594 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009595 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009596 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009597
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009598 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009599 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9600 D->hasAttrs() ? &D->getAttrs() : nullptr);
9601 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009602 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009603 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009604 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009605 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009606 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009607 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9608 if (!IsOpenMPCapturedDecl(D)) {
9609 ExprCaptures.push_back(Ref->getDecl());
9610 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9611 ExprResult RefRes = DefaultLvalueConversion(Ref);
9612 if (!RefRes.isUsable())
9613 continue;
9614 ExprResult PostUpdateRes =
9615 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9616 SimpleRefExpr, RefRes.get());
9617 if (!PostUpdateRes.isUsable())
9618 continue;
9619 ExprPostUpdates.push_back(
9620 IgnoredValueConversions(PostUpdateRes.get()).get());
9621 }
9622 }
9623 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009624 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009625 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009626 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009627 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009628 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009629 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9630 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9631
9632 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009633 Vars.push_back((VD || CurContext->isDependentContext())
9634 ? RefExpr->IgnoreParens()
9635 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009636 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009637 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009638 }
9639
9640 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009641 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009642
9643 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009644 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009645 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9646 !Step->isInstantiationDependent() &&
9647 !Step->containsUnexpandedParameterPack()) {
9648 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009649 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009650 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009651 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009652 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009653
Alexander Musman3276a272015-03-21 10:12:56 +00009654 // Build var to save the step value.
9655 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009656 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009657 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009658 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009659 ExprResult CalcStep =
9660 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009661 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009662
Alexander Musman8dba6642014-04-22 13:09:42 +00009663 // Warn about zero linear step (it would be probably better specified as
9664 // making corresponding variables 'const').
9665 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009666 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9667 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009668 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9669 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009670 if (!IsConstant && CalcStep.isUsable()) {
9671 // Calculate the step beforehand instead of doing this on each iteration.
9672 // (This is not used if the number of iterations may be kfold-ed).
9673 CalcStepExpr = CalcStep.get();
9674 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009675 }
9676
Alexey Bataev182227b2015-08-20 10:54:39 +00009677 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9678 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009679 StepExpr, CalcStepExpr,
9680 buildPreInits(Context, ExprCaptures),
9681 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009682}
9683
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009684static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9685 Expr *NumIterations, Sema &SemaRef,
9686 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009687 // Walk the vars and build update/final expressions for the CodeGen.
9688 SmallVector<Expr *, 8> Updates;
9689 SmallVector<Expr *, 8> Finals;
9690 Expr *Step = Clause.getStep();
9691 Expr *CalcStep = Clause.getCalcStep();
9692 // OpenMP [2.14.3.7, linear clause]
9693 // If linear-step is not specified it is assumed to be 1.
9694 if (Step == nullptr)
9695 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009696 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009697 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009698 }
Alexander Musman3276a272015-03-21 10:12:56 +00009699 bool HasErrors = false;
9700 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009701 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009702 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009703 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009704 SourceLocation ELoc;
9705 SourceRange ERange;
9706 Expr *SimpleRefExpr = RefExpr;
9707 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9708 /*AllowArraySection=*/false);
9709 ValueDecl *D = Res.first;
9710 if (Res.second || !D) {
9711 Updates.push_back(nullptr);
9712 Finals.push_back(nullptr);
9713 HasErrors = true;
9714 continue;
9715 }
9716 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9717 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9718 ->getMemberDecl();
9719 }
9720 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009721 Expr *InitExpr = *CurInit;
9722
9723 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009724 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009725 Expr *CapturedRef;
9726 if (LinKind == OMPC_LINEAR_uval)
9727 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9728 else
9729 CapturedRef =
9730 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9731 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9732 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009733
9734 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009735 ExprResult Update;
9736 if (!Info.first) {
9737 Update =
9738 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9739 InitExpr, IV, Step, /* Subtract */ false);
9740 } else
9741 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009742 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9743 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009744
9745 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009746 ExprResult Final;
9747 if (!Info.first) {
9748 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9749 InitExpr, NumIterations, Step,
9750 /* Subtract */ false);
9751 } else
9752 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009753 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9754 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009755
Alexander Musman3276a272015-03-21 10:12:56 +00009756 if (!Update.isUsable() || !Final.isUsable()) {
9757 Updates.push_back(nullptr);
9758 Finals.push_back(nullptr);
9759 HasErrors = true;
9760 } else {
9761 Updates.push_back(Update.get());
9762 Finals.push_back(Final.get());
9763 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009764 ++CurInit;
9765 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009766 }
9767 Clause.setUpdates(Updates);
9768 Clause.setFinals(Finals);
9769 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009770}
9771
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009772OMPClause *Sema::ActOnOpenMPAlignedClause(
9773 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9774 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9775
9776 SmallVector<Expr *, 8> Vars;
9777 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009778 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9779 SourceLocation ELoc;
9780 SourceRange ERange;
9781 Expr *SimpleRefExpr = RefExpr;
9782 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9783 /*AllowArraySection=*/false);
9784 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009785 // It will be analyzed later.
9786 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009787 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009788 ValueDecl *D = Res.first;
9789 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009790 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009791
Alexey Bataev1efd1662016-03-29 10:59:56 +00009792 QualType QType = D->getType();
9793 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009794
9795 // OpenMP [2.8.1, simd construct, Restrictions]
9796 // The type of list items appearing in the aligned clause must be
9797 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009798 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009799 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009800 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009801 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009802 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009803 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009804 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009805 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009806 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009808 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009809 continue;
9810 }
9811
9812 // OpenMP [2.8.1, simd construct, Restrictions]
9813 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009814 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009815 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009816 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9817 << getOpenMPClauseName(OMPC_aligned);
9818 continue;
9819 }
9820
Alexey Bataev1efd1662016-03-29 10:59:56 +00009821 DeclRefExpr *Ref = nullptr;
9822 if (!VD && IsOpenMPCapturedDecl(D))
9823 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9824 Vars.push_back(DefaultFunctionArrayConversion(
9825 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9826 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009827 }
9828
9829 // OpenMP [2.8.1, simd construct, Description]
9830 // The parameter of the aligned clause, alignment, must be a constant
9831 // positive integer expression.
9832 // If no optional parameter is specified, implementation-defined default
9833 // alignments for SIMD instructions on the target platforms are assumed.
9834 if (Alignment != nullptr) {
9835 ExprResult AlignResult =
9836 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9837 if (AlignResult.isInvalid())
9838 return nullptr;
9839 Alignment = AlignResult.get();
9840 }
9841 if (Vars.empty())
9842 return nullptr;
9843
9844 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9845 EndLoc, Vars, Alignment);
9846}
9847
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009848OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9849 SourceLocation StartLoc,
9850 SourceLocation LParenLoc,
9851 SourceLocation EndLoc) {
9852 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009853 SmallVector<Expr *, 8> SrcExprs;
9854 SmallVector<Expr *, 8> DstExprs;
9855 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009856 for (auto &RefExpr : VarList) {
9857 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9858 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009859 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009860 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009861 SrcExprs.push_back(nullptr);
9862 DstExprs.push_back(nullptr);
9863 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009864 continue;
9865 }
9866
Alexey Bataeved09d242014-05-28 05:53:51 +00009867 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009868 // OpenMP [2.1, C/C++]
9869 // A list item is a variable name.
9870 // OpenMP [2.14.4.1, Restrictions, p.1]
9871 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009872 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009873 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009874 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9875 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009876 continue;
9877 }
9878
9879 Decl *D = DE->getDecl();
9880 VarDecl *VD = cast<VarDecl>(D);
9881
9882 QualType Type = VD->getType();
9883 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9884 // It will be analyzed later.
9885 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009886 SrcExprs.push_back(nullptr);
9887 DstExprs.push_back(nullptr);
9888 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009889 continue;
9890 }
9891
9892 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9893 // A list item that appears in a copyin clause must be threadprivate.
9894 if (!DSAStack->isThreadPrivate(VD)) {
9895 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009896 << getOpenMPClauseName(OMPC_copyin)
9897 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009898 continue;
9899 }
9900
9901 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9902 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009903 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009904 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009905 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009906 auto *SrcVD =
9907 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9908 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009909 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009910 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9911 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009912 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9913 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009914 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009915 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009916 // For arrays generate assignment operation for single element and replace
9917 // it by the original array element in CodeGen.
9918 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9919 PseudoDstExpr, PseudoSrcExpr);
9920 if (AssignmentOp.isInvalid())
9921 continue;
9922 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9923 /*DiscardedValue=*/true);
9924 if (AssignmentOp.isInvalid())
9925 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009926
9927 DSAStack->addDSA(VD, DE, OMPC_copyin);
9928 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009929 SrcExprs.push_back(PseudoSrcExpr);
9930 DstExprs.push_back(PseudoDstExpr);
9931 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009932 }
9933
Alexey Bataeved09d242014-05-28 05:53:51 +00009934 if (Vars.empty())
9935 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009936
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009937 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9938 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009939}
9940
Alexey Bataevbae9a792014-06-27 10:37:06 +00009941OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9942 SourceLocation StartLoc,
9943 SourceLocation LParenLoc,
9944 SourceLocation EndLoc) {
9945 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009946 SmallVector<Expr *, 8> SrcExprs;
9947 SmallVector<Expr *, 8> DstExprs;
9948 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009949 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009950 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9951 SourceLocation ELoc;
9952 SourceRange ERange;
9953 Expr *SimpleRefExpr = RefExpr;
9954 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9955 /*AllowArraySection=*/false);
9956 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009957 // It will be analyzed later.
9958 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009959 SrcExprs.push_back(nullptr);
9960 DstExprs.push_back(nullptr);
9961 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009962 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009963 ValueDecl *D = Res.first;
9964 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009965 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009966
Alexey Bataeve122da12016-03-17 10:50:17 +00009967 QualType Type = D->getType();
9968 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009969
9970 // OpenMP [2.14.4.2, Restrictions, p.2]
9971 // A list item that appears in a copyprivate clause may not appear in a
9972 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009973 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9974 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009975 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9976 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009977 Diag(ELoc, diag::err_omp_wrong_dsa)
9978 << getOpenMPClauseName(DVar.CKind)
9979 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009980 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009981 continue;
9982 }
9983
9984 // OpenMP [2.11.4.2, Restrictions, p.1]
9985 // All list items that appear in a copyprivate clause must be either
9986 // threadprivate or private in the enclosing context.
9987 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009988 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009989 if (DVar.CKind == OMPC_shared) {
9990 Diag(ELoc, diag::err_omp_required_access)
9991 << getOpenMPClauseName(OMPC_copyprivate)
9992 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009993 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009994 continue;
9995 }
9996 }
9997 }
9998
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009999 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010000 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010001 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010002 << getOpenMPClauseName(OMPC_copyprivate) << Type
10003 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010004 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000010005 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010006 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000010007 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010008 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000010009 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000010010 continue;
10011 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010012
Alexey Bataevbae9a792014-06-27 10:37:06 +000010013 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10014 // A variable of class type (or array thereof) that appears in a
10015 // copyin clause requires an accessible, unambiguous copy assignment
10016 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010017 Type = Context.getBaseElementType(Type.getNonReferenceType())
10018 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010019 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010020 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10021 D->hasAttrs() ? &D->getAttrs() : nullptr);
10022 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010023 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010024 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10025 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010026 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010027 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10028 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010029 PseudoDstExpr, PseudoSrcExpr);
10030 if (AssignmentOp.isInvalid())
10031 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010032 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010033 /*DiscardedValue=*/true);
10034 if (AssignmentOp.isInvalid())
10035 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010036
10037 // No need to mark vars as copyprivate, they are already threadprivate or
10038 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010039 assert(VD || IsOpenMPCapturedDecl(D));
10040 Vars.push_back(
10041 VD ? RefExpr->IgnoreParens()
10042 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010043 SrcExprs.push_back(PseudoSrcExpr);
10044 DstExprs.push_back(PseudoDstExpr);
10045 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010046 }
10047
10048 if (Vars.empty())
10049 return nullptr;
10050
Alexey Bataeva63048e2015-03-23 06:18:07 +000010051 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10052 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010053}
10054
Alexey Bataev6125da92014-07-21 11:26:11 +000010055OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10056 SourceLocation StartLoc,
10057 SourceLocation LParenLoc,
10058 SourceLocation EndLoc) {
10059 if (VarList.empty())
10060 return nullptr;
10061
10062 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10063}
Alexey Bataevdea47612014-07-23 07:46:59 +000010064
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010065OMPClause *
10066Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10067 SourceLocation DepLoc, SourceLocation ColonLoc,
10068 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10069 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010070 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010071 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010072 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010073 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010074 return nullptr;
10075 }
10076 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010077 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10078 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010079 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010080 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010081 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10082 /*Last=*/OMPC_DEPEND_unknown, Except)
10083 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010084 return nullptr;
10085 }
10086 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010087 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010088 llvm::APSInt DepCounter(/*BitWidth=*/32);
10089 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10090 if (DepKind == OMPC_DEPEND_sink) {
10091 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10092 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10093 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010094 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010095 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010096 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10097 DSAStack->getParentOrderedRegionParam()) {
10098 for (auto &RefExpr : VarList) {
10099 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010100 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010101 // It will be analyzed later.
10102 Vars.push_back(RefExpr);
10103 continue;
10104 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010105
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010106 SourceLocation ELoc = RefExpr->getExprLoc();
10107 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10108 if (DepKind == OMPC_DEPEND_sink) {
10109 if (DepCounter >= TotalDepCount) {
10110 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10111 continue;
10112 }
10113 ++DepCounter;
10114 // OpenMP [2.13.9, Summary]
10115 // depend(dependence-type : vec), where dependence-type is:
10116 // 'sink' and where vec is the iteration vector, which has the form:
10117 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10118 // where n is the value specified by the ordered clause in the loop
10119 // directive, xi denotes the loop iteration variable of the i-th nested
10120 // loop associated with the loop directive, and di is a constant
10121 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010122 if (CurContext->isDependentContext()) {
10123 // It will be analyzed later.
10124 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010125 continue;
10126 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010127 SimpleExpr = SimpleExpr->IgnoreImplicit();
10128 OverloadedOperatorKind OOK = OO_None;
10129 SourceLocation OOLoc;
10130 Expr *LHS = SimpleExpr;
10131 Expr *RHS = nullptr;
10132 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10133 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10134 OOLoc = BO->getOperatorLoc();
10135 LHS = BO->getLHS()->IgnoreParenImpCasts();
10136 RHS = BO->getRHS()->IgnoreParenImpCasts();
10137 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10138 OOK = OCE->getOperator();
10139 OOLoc = OCE->getOperatorLoc();
10140 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10141 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10142 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10143 OOK = MCE->getMethodDecl()
10144 ->getNameInfo()
10145 .getName()
10146 .getCXXOverloadedOperator();
10147 OOLoc = MCE->getCallee()->getExprLoc();
10148 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10149 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10150 }
10151 SourceLocation ELoc;
10152 SourceRange ERange;
10153 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10154 /*AllowArraySection=*/false);
10155 if (Res.second) {
10156 // It will be analyzed later.
10157 Vars.push_back(RefExpr);
10158 }
10159 ValueDecl *D = Res.first;
10160 if (!D)
10161 continue;
10162
10163 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10164 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10165 continue;
10166 }
10167 if (RHS) {
10168 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10169 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10170 if (RHSRes.isInvalid())
10171 continue;
10172 }
10173 if (!CurContext->isDependentContext() &&
10174 DSAStack->getParentOrderedRegionParam() &&
10175 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10176 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10177 << DSAStack->getParentLoopControlVariable(
10178 DepCounter.getZExtValue());
10179 continue;
10180 }
10181 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010182 } else {
10183 // OpenMP [2.11.1.1, Restrictions, p.3]
10184 // A variable that is part of another variable (such as a field of a
10185 // structure) but is not an array element or an array section cannot
10186 // appear in a depend clause.
10187 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10188 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10189 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10190 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10191 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010192 (ASE &&
10193 !ASE->getBase()
10194 ->getType()
10195 .getNonReferenceType()
10196 ->isPointerType() &&
10197 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010198 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10199 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010200 continue;
10201 }
10202 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010203 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10204 }
10205
10206 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10207 TotalDepCount > VarList.size() &&
10208 DSAStack->getParentOrderedRegionParam()) {
10209 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10210 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10211 }
10212 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10213 Vars.empty())
10214 return nullptr;
10215 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010216 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10217 DepKind, DepLoc, ColonLoc, Vars);
10218 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10219 DSAStack->addDoacrossDependClause(C, OpsOffs);
10220 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010221}
Michael Wonge710d542015-08-07 16:16:36 +000010222
10223OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10224 SourceLocation LParenLoc,
10225 SourceLocation EndLoc) {
10226 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010227
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010228 // OpenMP [2.9.1, Restrictions]
10229 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010230 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10231 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010232 return nullptr;
10233
Michael Wonge710d542015-08-07 16:16:36 +000010234 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10235}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010236
10237static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10238 DSAStackTy *Stack, CXXRecordDecl *RD) {
10239 if (!RD || RD->isInvalidDecl())
10240 return true;
10241
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010242 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10243 if (auto *CTD = CTSD->getSpecializedTemplate())
10244 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010245 auto QTy = SemaRef.Context.getRecordType(RD);
10246 if (RD->isDynamicClass()) {
10247 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10248 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10249 return false;
10250 }
10251 auto *DC = RD;
10252 bool IsCorrect = true;
10253 for (auto *I : DC->decls()) {
10254 if (I) {
10255 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10256 if (MD->isStatic()) {
10257 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10258 SemaRef.Diag(MD->getLocation(),
10259 diag::note_omp_static_member_in_target);
10260 IsCorrect = false;
10261 }
10262 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10263 if (VD->isStaticDataMember()) {
10264 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10265 SemaRef.Diag(VD->getLocation(),
10266 diag::note_omp_static_member_in_target);
10267 IsCorrect = false;
10268 }
10269 }
10270 }
10271 }
10272
10273 for (auto &I : RD->bases()) {
10274 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10275 I.getType()->getAsCXXRecordDecl()))
10276 IsCorrect = false;
10277 }
10278 return IsCorrect;
10279}
10280
10281static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10282 DSAStackTy *Stack, QualType QTy) {
10283 NamedDecl *ND;
10284 if (QTy->isIncompleteType(&ND)) {
10285 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10286 return false;
10287 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10288 if (!RD->isInvalidDecl() &&
10289 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10290 return false;
10291 }
10292 return true;
10293}
10294
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010295/// \brief Return true if it can be proven that the provided array expression
10296/// (array section or array subscript) does NOT specify the whole size of the
10297/// array whose base type is \a BaseQTy.
10298static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10299 const Expr *E,
10300 QualType BaseQTy) {
10301 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10302
10303 // If this is an array subscript, it refers to the whole size if the size of
10304 // the dimension is constant and equals 1. Also, an array section assumes the
10305 // format of an array subscript if no colon is used.
10306 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10307 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10308 return ATy->getSize().getSExtValue() != 1;
10309 // Size can't be evaluated statically.
10310 return false;
10311 }
10312
10313 assert(OASE && "Expecting array section if not an array subscript.");
10314 auto *LowerBound = OASE->getLowerBound();
10315 auto *Length = OASE->getLength();
10316
10317 // If there is a lower bound that does not evaluates to zero, we are not
10318 // convering the whole dimension.
10319 if (LowerBound) {
10320 llvm::APSInt ConstLowerBound;
10321 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10322 return false; // Can't get the integer value as a constant.
10323 if (ConstLowerBound.getSExtValue())
10324 return true;
10325 }
10326
10327 // If we don't have a length we covering the whole dimension.
10328 if (!Length)
10329 return false;
10330
10331 // If the base is a pointer, we don't have a way to get the size of the
10332 // pointee.
10333 if (BaseQTy->isPointerType())
10334 return false;
10335
10336 // We can only check if the length is the same as the size of the dimension
10337 // if we have a constant array.
10338 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10339 if (!CATy)
10340 return false;
10341
10342 llvm::APSInt ConstLength;
10343 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10344 return false; // Can't get the integer value as a constant.
10345
10346 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10347}
10348
10349// Return true if it can be proven that the provided array expression (array
10350// section or array subscript) does NOT specify a single element of the array
10351// whose base type is \a BaseQTy.
10352static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10353 const Expr *E,
10354 QualType BaseQTy) {
10355 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10356
10357 // An array subscript always refer to a single element. Also, an array section
10358 // assumes the format of an array subscript if no colon is used.
10359 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10360 return false;
10361
10362 assert(OASE && "Expecting array section if not an array subscript.");
10363 auto *Length = OASE->getLength();
10364
10365 // If we don't have a length we have to check if the array has unitary size
10366 // for this dimension. Also, we should always expect a length if the base type
10367 // is pointer.
10368 if (!Length) {
10369 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10370 return ATy->getSize().getSExtValue() != 1;
10371 // We cannot assume anything.
10372 return false;
10373 }
10374
10375 // Check if the length evaluates to 1.
10376 llvm::APSInt ConstLength;
10377 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10378 return false; // Can't get the integer value as a constant.
10379
10380 return ConstLength.getSExtValue() != 1;
10381}
10382
Samuel Antao661c0902016-05-26 17:39:58 +000010383// Return the expression of the base of the mappable expression or null if it
10384// cannot be determined and do all the necessary checks to see if the expression
10385// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010386// components of the expression.
10387static Expr *CheckMapClauseExpressionBase(
10388 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010389 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10390 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010391 SourceLocation ELoc = E->getExprLoc();
10392 SourceRange ERange = E->getSourceRange();
10393
10394 // The base of elements of list in a map clause have to be either:
10395 // - a reference to variable or field.
10396 // - a member expression.
10397 // - an array expression.
10398 //
10399 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10400 // reference to 'r'.
10401 //
10402 // If we have:
10403 //
10404 // struct SS {
10405 // Bla S;
10406 // foo() {
10407 // #pragma omp target map (S.Arr[:12]);
10408 // }
10409 // }
10410 //
10411 // We want to retrieve the member expression 'this->S';
10412
10413 Expr *RelevantExpr = nullptr;
10414
Samuel Antao5de996e2016-01-22 20:21:36 +000010415 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10416 // If a list item is an array section, it must specify contiguous storage.
10417 //
10418 // For this restriction it is sufficient that we make sure only references
10419 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010420 // exist except in the rightmost expression (unless they cover the whole
10421 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010422 //
10423 // r.ArrS[3:5].Arr[6:7]
10424 //
10425 // r.ArrS[3:5].x
10426 //
10427 // but these would be valid:
10428 // r.ArrS[3].Arr[6:7]
10429 //
10430 // r.ArrS[3].x
10431
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010432 bool AllowUnitySizeArraySection = true;
10433 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010434
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010435 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010436 E = E->IgnoreParenImpCasts();
10437
10438 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10439 if (!isa<VarDecl>(CurE->getDecl()))
10440 break;
10441
10442 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010443
10444 // If we got a reference to a declaration, we should not expect any array
10445 // section before that.
10446 AllowUnitySizeArraySection = false;
10447 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010448
10449 // Record the component.
10450 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10451 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010452 continue;
10453 }
10454
10455 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10456 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10457
10458 if (isa<CXXThisExpr>(BaseE))
10459 // We found a base expression: this->Val.
10460 RelevantExpr = CurE;
10461 else
10462 E = BaseE;
10463
10464 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10465 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10466 << CurE->getSourceRange();
10467 break;
10468 }
10469
10470 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10471
10472 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10473 // A bit-field cannot appear in a map clause.
10474 //
10475 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010476 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10477 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010478 break;
10479 }
10480
10481 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10482 // If the type of a list item is a reference to a type T then the type
10483 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010484 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010485
10486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10487 // A list item cannot be a variable that is a member of a structure with
10488 // a union type.
10489 //
10490 if (auto *RT = CurType->getAs<RecordType>())
10491 if (RT->isUnionType()) {
10492 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10493 << CurE->getSourceRange();
10494 break;
10495 }
10496
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010497 // If we got a member expression, we should not expect any array section
10498 // before that:
10499 //
10500 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10501 // If a list item is an element of a structure, only the rightmost symbol
10502 // of the variable reference can be an array section.
10503 //
10504 AllowUnitySizeArraySection = false;
10505 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010506
10507 // Record the component.
10508 CurComponents.push_back(
10509 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010510 continue;
10511 }
10512
10513 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10514 E = CurE->getBase()->IgnoreParenImpCasts();
10515
10516 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10517 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10518 << 0 << CurE->getSourceRange();
10519 break;
10520 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010521
10522 // If we got an array subscript that express the whole dimension we
10523 // can have any array expressions before. If it only expressing part of
10524 // the dimension, we can only have unitary-size array expressions.
10525 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10526 E->getType()))
10527 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010528
10529 // Record the component - we don't have any declaration associated.
10530 CurComponents.push_back(
10531 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010532 continue;
10533 }
10534
10535 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010536 E = CurE->getBase()->IgnoreParenImpCasts();
10537
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010538 auto CurType =
10539 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10540
Samuel Antao5de996e2016-01-22 20:21:36 +000010541 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10542 // If the type of a list item is a reference to a type T then the type
10543 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010544 if (CurType->isReferenceType())
10545 CurType = CurType->getPointeeType();
10546
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010547 bool IsPointer = CurType->isAnyPointerType();
10548
10549 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010550 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10551 << 0 << CurE->getSourceRange();
10552 break;
10553 }
10554
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010555 bool NotWhole =
10556 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10557 bool NotUnity =
10558 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10559
10560 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10561 // Any array section is currently allowed.
10562 //
10563 // If this array section refers to the whole dimension we can still
10564 // accept other array sections before this one, except if the base is a
10565 // pointer. Otherwise, only unitary sections are accepted.
10566 if (NotWhole || IsPointer)
10567 AllowWholeSizeArraySection = false;
10568 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10569 (AllowWholeSizeArraySection && NotWhole)) {
10570 // A unity or whole array section is not allowed and that is not
10571 // compatible with the properties of the current array section.
10572 SemaRef.Diag(
10573 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10574 << CurE->getSourceRange();
10575 break;
10576 }
Samuel Antao90927002016-04-26 14:54:23 +000010577
10578 // Record the component - we don't have any declaration associated.
10579 CurComponents.push_back(
10580 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010581 continue;
10582 }
10583
10584 // If nothing else worked, this is not a valid map clause expression.
10585 SemaRef.Diag(ELoc,
10586 diag::err_omp_expected_named_var_member_or_array_expression)
10587 << ERange;
10588 break;
10589 }
10590
10591 return RelevantExpr;
10592}
10593
10594// Return true if expression E associated with value VD has conflicts with other
10595// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010596static bool CheckMapConflicts(
10597 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10598 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010599 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10600 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010601 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010602 SourceLocation ELoc = E->getExprLoc();
10603 SourceRange ERange = E->getSourceRange();
10604
10605 // In order to easily check the conflicts we need to match each component of
10606 // the expression under test with the components of the expressions that are
10607 // already in the stack.
10608
Samuel Antao5de996e2016-01-22 20:21:36 +000010609 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010610 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010611 "Map clause expression with unexpected base!");
10612
10613 // Variables to help detecting enclosing problems in data environment nests.
10614 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010615 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010616
Samuel Antao90927002016-04-26 14:54:23 +000010617 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10618 VD, CurrentRegionOnly,
10619 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10620 StackComponents) -> bool {
10621
Samuel Antao5de996e2016-01-22 20:21:36 +000010622 assert(!StackComponents.empty() &&
10623 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010624 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010625 "Map clause expression with unexpected base!");
10626
Samuel Antao90927002016-04-26 14:54:23 +000010627 // The whole expression in the stack.
10628 auto *RE = StackComponents.front().getAssociatedExpression();
10629
Samuel Antao5de996e2016-01-22 20:21:36 +000010630 // Expressions must start from the same base. Here we detect at which
10631 // point both expressions diverge from each other and see if we can
10632 // detect if the memory referred to both expressions is contiguous and
10633 // do not overlap.
10634 auto CI = CurComponents.rbegin();
10635 auto CE = CurComponents.rend();
10636 auto SI = StackComponents.rbegin();
10637 auto SE = StackComponents.rend();
10638 for (; CI != CE && SI != SE; ++CI, ++SI) {
10639
10640 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10641 // At most one list item can be an array item derived from a given
10642 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010643 if (CurrentRegionOnly &&
10644 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10645 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10646 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10647 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10648 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010649 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010650 << CI->getAssociatedExpression()->getSourceRange();
10651 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10652 diag::note_used_here)
10653 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010654 return true;
10655 }
10656
10657 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010658 if (CI->getAssociatedExpression()->getStmtClass() !=
10659 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010660 break;
10661
10662 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010663 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010664 break;
10665 }
10666
10667 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10668 // List items of map clauses in the same construct must not share
10669 // original storage.
10670 //
10671 // If the expressions are exactly the same or one is a subset of the
10672 // other, it means they are sharing storage.
10673 if (CI == CE && SI == SE) {
10674 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010675 if (CKind == OMPC_map)
10676 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10677 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010678 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010679 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10680 << ERange;
10681 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010682 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10683 << RE->getSourceRange();
10684 return true;
10685 } else {
10686 // If we find the same expression in the enclosing data environment,
10687 // that is legal.
10688 IsEnclosedByDataEnvironmentExpr = true;
10689 return false;
10690 }
10691 }
10692
Samuel Antao90927002016-04-26 14:54:23 +000010693 QualType DerivedType =
10694 std::prev(CI)->getAssociatedDeclaration()->getType();
10695 SourceLocation DerivedLoc =
10696 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010697
10698 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10699 // If the type of a list item is a reference to a type T then the type
10700 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010701 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010702
10703 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10704 // A variable for which the type is pointer and an array section
10705 // derived from that variable must not appear as list items of map
10706 // clauses of the same construct.
10707 //
10708 // Also, cover one of the cases in:
10709 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10710 // If any part of the original storage of a list item has corresponding
10711 // storage in the device data environment, all of the original storage
10712 // must have corresponding storage in the device data environment.
10713 //
10714 if (DerivedType->isAnyPointerType()) {
10715 if (CI == CE || SI == SE) {
10716 SemaRef.Diag(
10717 DerivedLoc,
10718 diag::err_omp_pointer_mapped_along_with_derived_section)
10719 << DerivedLoc;
10720 } else {
10721 assert(CI != CE && SI != SE);
10722 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10723 << DerivedLoc;
10724 }
10725 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10726 << RE->getSourceRange();
10727 return true;
10728 }
10729
10730 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10731 // List items of map clauses in the same construct must not share
10732 // original storage.
10733 //
10734 // An expression is a subset of the other.
10735 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010736 if (CKind == OMPC_map)
10737 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10738 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010739 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010740 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10741 << ERange;
10742 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010743 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10744 << RE->getSourceRange();
10745 return true;
10746 }
10747
10748 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010749 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010750 if (!CurrentRegionOnly && SI != SE)
10751 EnclosingExpr = RE;
10752
10753 // The current expression is a subset of the expression in the data
10754 // environment.
10755 IsEnclosedByDataEnvironmentExpr |=
10756 (!CurrentRegionOnly && CI != CE && SI == SE);
10757
10758 return false;
10759 });
10760
10761 if (CurrentRegionOnly)
10762 return FoundError;
10763
10764 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10765 // If any part of the original storage of a list item has corresponding
10766 // storage in the device data environment, all of the original storage must
10767 // have corresponding storage in the device data environment.
10768 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10769 // If a list item is an element of a structure, and a different element of
10770 // the structure has a corresponding list item in the device data environment
10771 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010772 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010773 // data environment prior to the task encountering the construct.
10774 //
10775 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10776 SemaRef.Diag(ELoc,
10777 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10778 << ERange;
10779 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10780 << EnclosingExpr->getSourceRange();
10781 return true;
10782 }
10783
10784 return FoundError;
10785}
10786
Samuel Antao661c0902016-05-26 17:39:58 +000010787namespace {
10788// Utility struct that gathers all the related lists associated with a mappable
10789// expression.
10790struct MappableVarListInfo final {
10791 // The list of expressions.
10792 ArrayRef<Expr *> VarList;
10793 // The list of processed expressions.
10794 SmallVector<Expr *, 16> ProcessedVarList;
10795 // The mappble components for each expression.
10796 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10797 // The base declaration of the variable.
10798 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10799
10800 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10801 // We have a list of components and base declarations for each entry in the
10802 // variable list.
10803 VarComponents.reserve(VarList.size());
10804 VarBaseDeclarations.reserve(VarList.size());
10805 }
10806};
10807}
10808
10809// Check the validity of the provided variable list for the provided clause kind
10810// \a CKind. In the check process the valid expressions, and mappable expression
10811// components and variables are extracted and used to fill \a Vars,
10812// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10813// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10814static void
10815checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10816 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10817 SourceLocation StartLoc,
10818 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10819 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010820 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10821 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010822 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010823
Samuel Antao90927002016-04-26 14:54:23 +000010824 // Keep track of the mappable components and base declarations in this clause.
10825 // Each entry in the list is going to have a list of components associated. We
10826 // record each set of the components so that we can build the clause later on.
10827 // In the end we should have the same amount of declarations and component
10828 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010829
Samuel Antao661c0902016-05-26 17:39:58 +000010830 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010831 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010832 SourceLocation ELoc = RE->getExprLoc();
10833
Kelvin Li0bff7af2015-11-23 05:32:03 +000010834 auto *VE = RE->IgnoreParenLValueCasts();
10835
10836 if (VE->isValueDependent() || VE->isTypeDependent() ||
10837 VE->isInstantiationDependent() ||
10838 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010839 // We can only analyze this information once the missing information is
10840 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010841 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010842 continue;
10843 }
10844
10845 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010846
Samuel Antao5de996e2016-01-22 20:21:36 +000010847 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010848 SemaRef.Diag(ELoc,
10849 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010850 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010851 continue;
10852 }
10853
Samuel Antao90927002016-04-26 14:54:23 +000010854 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10855 ValueDecl *CurDeclaration = nullptr;
10856
10857 // Obtain the array or member expression bases if required. Also, fill the
10858 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010859 auto *BE =
10860 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010861 if (!BE)
10862 continue;
10863
Samuel Antao90927002016-04-26 14:54:23 +000010864 assert(!CurComponents.empty() &&
10865 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010866
Samuel Antao90927002016-04-26 14:54:23 +000010867 // For the following checks, we rely on the base declaration which is
10868 // expected to be associated with the last component. The declaration is
10869 // expected to be a variable or a field (if 'this' is being mapped).
10870 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10871 assert(CurDeclaration && "Null decl on map clause.");
10872 assert(
10873 CurDeclaration->isCanonicalDecl() &&
10874 "Expecting components to have associated only canonical declarations.");
10875
10876 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10877 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010878
10879 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010880 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010881
10882 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010883 // threadprivate variables cannot appear in a map clause.
10884 // OpenMP 4.5 [2.10.5, target update Construct]
10885 // threadprivate variables cannot appear in a from clause.
10886 if (VD && DSAS->isThreadPrivate(VD)) {
10887 auto DVar = DSAS->getTopDSA(VD, false);
10888 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10889 << getOpenMPClauseName(CKind);
10890 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010891 continue;
10892 }
10893
Samuel Antao5de996e2016-01-22 20:21:36 +000010894 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10895 // A list item cannot appear in both a map clause and a data-sharing
10896 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010897
Samuel Antao5de996e2016-01-22 20:21:36 +000010898 // Check conflicts with other map clause expressions. We check the conflicts
10899 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010900 // environment, because the restrictions are different. We only have to
10901 // check conflicts across regions for the map clauses.
10902 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10903 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010904 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010905 if (CKind == OMPC_map &&
10906 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10907 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010908 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010909
Samuel Antao661c0902016-05-26 17:39:58 +000010910 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010911 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10912 // If the type of a list item is a reference to a type T then the type will
10913 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010914 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010915
Samuel Antao661c0902016-05-26 17:39:58 +000010916 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10917 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010918 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010919 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010920 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10921 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010922 continue;
10923
Samuel Antao661c0902016-05-26 17:39:58 +000010924 if (CKind == OMPC_map) {
10925 // target enter data
10926 // OpenMP [2.10.2, Restrictions, p. 99]
10927 // A map-type must be specified in all map clauses and must be either
10928 // to or alloc.
10929 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10930 if (DKind == OMPD_target_enter_data &&
10931 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10932 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10933 << (IsMapTypeImplicit ? 1 : 0)
10934 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10935 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010936 continue;
10937 }
Samuel Antao661c0902016-05-26 17:39:58 +000010938
10939 // target exit_data
10940 // OpenMP [2.10.3, Restrictions, p. 102]
10941 // A map-type must be specified in all map clauses and must be either
10942 // from, release, or delete.
10943 if (DKind == OMPD_target_exit_data &&
10944 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10945 MapType == OMPC_MAP_delete)) {
10946 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10947 << (IsMapTypeImplicit ? 1 : 0)
10948 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10949 << getOpenMPDirectiveName(DKind);
10950 continue;
10951 }
10952
10953 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10954 // A list item cannot appear in both a map clause and a data-sharing
10955 // attribute clause on the same construct
10956 if (DKind == OMPD_target && VD) {
10957 auto DVar = DSAS->getTopDSA(VD, false);
10958 if (isOpenMPPrivate(DVar.CKind)) {
10959 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10960 << getOpenMPClauseName(DVar.CKind)
10961 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10962 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10963 continue;
10964 }
10965 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010966 }
10967
Samuel Antao90927002016-04-26 14:54:23 +000010968 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010969 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010970
10971 // Store the components in the stack so that they can be used to check
10972 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000010973 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000010974
10975 // Save the components and declaration to create the clause. For purposes of
10976 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010977 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010978 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10979 MVLI.VarComponents.back().append(CurComponents.begin(),
10980 CurComponents.end());
10981 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10982 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010983 }
Samuel Antao661c0902016-05-26 17:39:58 +000010984}
10985
10986OMPClause *
10987Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10988 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10989 SourceLocation MapLoc, SourceLocation ColonLoc,
10990 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10991 SourceLocation LParenLoc, SourceLocation EndLoc) {
10992 MappableVarListInfo MVLI(VarList);
10993 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10994 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010995
Samuel Antao5de996e2016-01-22 20:21:36 +000010996 // We need to produce a map clause even if we don't have variables so that
10997 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010998 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10999 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11000 MVLI.VarComponents, MapTypeModifier, MapType,
11001 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000011002}
Kelvin Li099bb8c2015-11-24 20:50:12 +000011003
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011004QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11005 TypeResult ParsedType) {
11006 assert(ParsedType.isUsable());
11007
11008 QualType ReductionType = GetTypeFromParser(ParsedType.get());
11009 if (ReductionType.isNull())
11010 return QualType();
11011
11012 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11013 // A type name in a declare reduction directive cannot be a function type, an
11014 // array type, a reference type, or a type qualified with const, volatile or
11015 // restrict.
11016 if (ReductionType.hasQualifiers()) {
11017 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11018 return QualType();
11019 }
11020
11021 if (ReductionType->isFunctionType()) {
11022 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11023 return QualType();
11024 }
11025 if (ReductionType->isReferenceType()) {
11026 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11027 return QualType();
11028 }
11029 if (ReductionType->isArrayType()) {
11030 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11031 return QualType();
11032 }
11033 return ReductionType;
11034}
11035
11036Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11037 Scope *S, DeclContext *DC, DeclarationName Name,
11038 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11039 AccessSpecifier AS, Decl *PrevDeclInScope) {
11040 SmallVector<Decl *, 8> Decls;
11041 Decls.reserve(ReductionTypes.size());
11042
11043 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11044 ForRedeclaration);
11045 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11046 // A reduction-identifier may not be re-declared in the current scope for the
11047 // same type or for a type that is compatible according to the base language
11048 // rules.
11049 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11050 OMPDeclareReductionDecl *PrevDRD = nullptr;
11051 bool InCompoundScope = true;
11052 if (S != nullptr) {
11053 // Find previous declaration with the same name not referenced in other
11054 // declarations.
11055 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11056 InCompoundScope =
11057 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11058 LookupName(Lookup, S);
11059 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11060 /*AllowInlineNamespace=*/false);
11061 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11062 auto Filter = Lookup.makeFilter();
11063 while (Filter.hasNext()) {
11064 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11065 if (InCompoundScope) {
11066 auto I = UsedAsPrevious.find(PrevDecl);
11067 if (I == UsedAsPrevious.end())
11068 UsedAsPrevious[PrevDecl] = false;
11069 if (auto *D = PrevDecl->getPrevDeclInScope())
11070 UsedAsPrevious[D] = true;
11071 }
11072 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11073 PrevDecl->getLocation();
11074 }
11075 Filter.done();
11076 if (InCompoundScope) {
11077 for (auto &PrevData : UsedAsPrevious) {
11078 if (!PrevData.second) {
11079 PrevDRD = PrevData.first;
11080 break;
11081 }
11082 }
11083 }
11084 } else if (PrevDeclInScope != nullptr) {
11085 auto *PrevDRDInScope = PrevDRD =
11086 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11087 do {
11088 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11089 PrevDRDInScope->getLocation();
11090 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11091 } while (PrevDRDInScope != nullptr);
11092 }
11093 for (auto &TyData : ReductionTypes) {
11094 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11095 bool Invalid = false;
11096 if (I != PreviousRedeclTypes.end()) {
11097 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11098 << TyData.first;
11099 Diag(I->second, diag::note_previous_definition);
11100 Invalid = true;
11101 }
11102 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11103 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11104 Name, TyData.first, PrevDRD);
11105 DC->addDecl(DRD);
11106 DRD->setAccess(AS);
11107 Decls.push_back(DRD);
11108 if (Invalid)
11109 DRD->setInvalidDecl();
11110 else
11111 PrevDRD = DRD;
11112 }
11113
11114 return DeclGroupPtrTy::make(
11115 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11116}
11117
11118void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11119 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11120
11121 // Enter new function scope.
11122 PushFunctionScope();
11123 getCurFunction()->setHasBranchProtectedScope();
11124 getCurFunction()->setHasOMPDeclareReductionCombiner();
11125
11126 if (S != nullptr)
11127 PushDeclContext(S, DRD);
11128 else
11129 CurContext = DRD;
11130
11131 PushExpressionEvaluationContext(PotentiallyEvaluated);
11132
11133 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011134 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11135 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11136 // uses semantics of argument handles by value, but it should be passed by
11137 // reference. C lang does not support references, so pass all parameters as
11138 // pointers.
11139 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011140 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011141 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011142 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11143 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11144 // uses semantics of argument handles by value, but it should be passed by
11145 // reference. C lang does not support references, so pass all parameters as
11146 // pointers.
11147 // Create 'T omp_out;' variable.
11148 auto *OmpOutParm =
11149 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11150 if (S != nullptr) {
11151 PushOnScopeChains(OmpInParm, S);
11152 PushOnScopeChains(OmpOutParm, S);
11153 } else {
11154 DRD->addDecl(OmpInParm);
11155 DRD->addDecl(OmpOutParm);
11156 }
11157}
11158
11159void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11160 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11161 DiscardCleanupsInEvaluationContext();
11162 PopExpressionEvaluationContext();
11163
11164 PopDeclContext();
11165 PopFunctionScopeInfo();
11166
11167 if (Combiner != nullptr)
11168 DRD->setCombiner(Combiner);
11169 else
11170 DRD->setInvalidDecl();
11171}
11172
11173void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11174 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11175
11176 // Enter new function scope.
11177 PushFunctionScope();
11178 getCurFunction()->setHasBranchProtectedScope();
11179
11180 if (S != nullptr)
11181 PushDeclContext(S, DRD);
11182 else
11183 CurContext = DRD;
11184
11185 PushExpressionEvaluationContext(PotentiallyEvaluated);
11186
11187 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011188 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11189 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11190 // uses semantics of argument handles by value, but it should be passed by
11191 // reference. C lang does not support references, so pass all parameters as
11192 // pointers.
11193 // Create 'T omp_priv;' variable.
11194 auto *OmpPrivParm =
11195 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011196 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11197 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11198 // uses semantics of argument handles by value, but it should be passed by
11199 // reference. C lang does not support references, so pass all parameters as
11200 // pointers.
11201 // Create 'T omp_orig;' variable.
11202 auto *OmpOrigParm =
11203 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011204 if (S != nullptr) {
11205 PushOnScopeChains(OmpPrivParm, S);
11206 PushOnScopeChains(OmpOrigParm, S);
11207 } else {
11208 DRD->addDecl(OmpPrivParm);
11209 DRD->addDecl(OmpOrigParm);
11210 }
11211}
11212
11213void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11214 Expr *Initializer) {
11215 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11216 DiscardCleanupsInEvaluationContext();
11217 PopExpressionEvaluationContext();
11218
11219 PopDeclContext();
11220 PopFunctionScopeInfo();
11221
11222 if (Initializer != nullptr)
11223 DRD->setInitializer(Initializer);
11224 else
11225 DRD->setInvalidDecl();
11226}
11227
11228Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11229 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11230 for (auto *D : DeclReductions.get()) {
11231 if (IsValid) {
11232 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11233 if (S != nullptr)
11234 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11235 } else
11236 D->setInvalidDecl();
11237 }
11238 return DeclReductions;
11239}
11240
Kelvin Li099bb8c2015-11-24 20:50:12 +000011241OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11242 SourceLocation StartLoc,
11243 SourceLocation LParenLoc,
11244 SourceLocation EndLoc) {
11245 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011246
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011247 // OpenMP [teams Constrcut, Restrictions]
11248 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011249 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11250 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011251 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011252
11253 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11254}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011255
11256OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11257 SourceLocation StartLoc,
11258 SourceLocation LParenLoc,
11259 SourceLocation EndLoc) {
11260 Expr *ValExpr = ThreadLimit;
11261
11262 // OpenMP [teams Constrcut, Restrictions]
11263 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011264 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11265 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011266 return nullptr;
11267
11268 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11269 EndLoc);
11270}
Alexey Bataeva0569352015-12-01 10:17:31 +000011271
11272OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11273 SourceLocation StartLoc,
11274 SourceLocation LParenLoc,
11275 SourceLocation EndLoc) {
11276 Expr *ValExpr = Priority;
11277
11278 // OpenMP [2.9.1, task Constrcut]
11279 // The priority-value is a non-negative numerical scalar expression.
11280 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11281 /*StrictlyPositive=*/false))
11282 return nullptr;
11283
11284 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11285}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011286
11287OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11288 SourceLocation StartLoc,
11289 SourceLocation LParenLoc,
11290 SourceLocation EndLoc) {
11291 Expr *ValExpr = Grainsize;
11292
11293 // OpenMP [2.9.2, taskloop Constrcut]
11294 // The parameter of the grainsize clause must be a positive integer
11295 // expression.
11296 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11297 /*StrictlyPositive=*/true))
11298 return nullptr;
11299
11300 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11301}
Alexey Bataev382967a2015-12-08 12:06:20 +000011302
11303OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11304 SourceLocation StartLoc,
11305 SourceLocation LParenLoc,
11306 SourceLocation EndLoc) {
11307 Expr *ValExpr = NumTasks;
11308
11309 // OpenMP [2.9.2, taskloop Constrcut]
11310 // The parameter of the num_tasks clause must be a positive integer
11311 // expression.
11312 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11313 /*StrictlyPositive=*/true))
11314 return nullptr;
11315
11316 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11317}
11318
Alexey Bataev28c75412015-12-15 08:19:24 +000011319OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11320 SourceLocation LParenLoc,
11321 SourceLocation EndLoc) {
11322 // OpenMP [2.13.2, critical construct, Description]
11323 // ... where hint-expression is an integer constant expression that evaluates
11324 // to a valid lock hint.
11325 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11326 if (HintExpr.isInvalid())
11327 return nullptr;
11328 return new (Context)
11329 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11330}
11331
Carlo Bertollib4adf552016-01-15 18:50:31 +000011332OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11333 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11334 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11335 SourceLocation EndLoc) {
11336 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11337 std::string Values;
11338 Values += "'";
11339 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11340 Values += "'";
11341 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11342 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11343 return nullptr;
11344 }
11345 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011346 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011347 if (ChunkSize) {
11348 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11349 !ChunkSize->isInstantiationDependent() &&
11350 !ChunkSize->containsUnexpandedParameterPack()) {
11351 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11352 ExprResult Val =
11353 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11354 if (Val.isInvalid())
11355 return nullptr;
11356
11357 ValExpr = Val.get();
11358
11359 // OpenMP [2.7.1, Restrictions]
11360 // chunk_size must be a loop invariant integer expression with a positive
11361 // value.
11362 llvm::APSInt Result;
11363 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11364 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11365 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11366 << "dist_schedule" << ChunkSize->getSourceRange();
11367 return nullptr;
11368 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011369 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11370 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011371 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11372 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11373 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011374 }
11375 }
11376 }
11377
11378 return new (Context)
11379 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011380 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011381}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011382
11383OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11384 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11385 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11386 SourceLocation KindLoc, SourceLocation EndLoc) {
11387 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11388 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11389 Kind != OMPC_DEFAULTMAP_scalar) {
11390 std::string Value;
11391 SourceLocation Loc;
11392 Value += "'";
11393 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11394 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11395 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11396 Loc = MLoc;
11397 } else {
11398 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11399 OMPC_DEFAULTMAP_scalar);
11400 Loc = KindLoc;
11401 }
11402 Value += "'";
11403 Diag(Loc, diag::err_omp_unexpected_clause_value)
11404 << Value << getOpenMPClauseName(OMPC_defaultmap);
11405 return nullptr;
11406 }
11407
11408 return new (Context)
11409 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11410}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011411
11412bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11413 DeclContext *CurLexicalContext = getCurLexicalContext();
11414 if (!CurLexicalContext->isFileContext() &&
11415 !CurLexicalContext->isExternCContext() &&
11416 !CurLexicalContext->isExternCXXContext()) {
11417 Diag(Loc, diag::err_omp_region_not_file_context);
11418 return false;
11419 }
11420 if (IsInOpenMPDeclareTargetContext) {
11421 Diag(Loc, diag::err_omp_enclosed_declare_target);
11422 return false;
11423 }
11424
11425 IsInOpenMPDeclareTargetContext = true;
11426 return true;
11427}
11428
11429void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11430 assert(IsInOpenMPDeclareTargetContext &&
11431 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11432
11433 IsInOpenMPDeclareTargetContext = false;
11434}
11435
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011436void
11437Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11438 const DeclarationNameInfo &Id,
11439 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11440 NamedDeclSetType &SameDirectiveDecls) {
11441 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11442 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11443
11444 if (Lookup.isAmbiguous())
11445 return;
11446 Lookup.suppressDiagnostics();
11447
11448 if (!Lookup.isSingleResult()) {
11449 if (TypoCorrection Corrected =
11450 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11451 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11452 CTK_ErrorRecovery)) {
11453 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11454 << Id.getName());
11455 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11456 return;
11457 }
11458
11459 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11460 return;
11461 }
11462
11463 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11464 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11465 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11466 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11467
11468 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11469 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11470 ND->addAttr(A);
11471 if (ASTMutationListener *ML = Context.getASTMutationListener())
11472 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11473 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11474 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11475 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11476 << Id.getName();
11477 }
11478 } else
11479 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11480}
11481
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011482static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11483 Sema &SemaRef, Decl *D) {
11484 if (!D)
11485 return;
11486 Decl *LD = nullptr;
11487 if (isa<TagDecl>(D)) {
11488 LD = cast<TagDecl>(D)->getDefinition();
11489 } else if (isa<VarDecl>(D)) {
11490 LD = cast<VarDecl>(D)->getDefinition();
11491
11492 // If this is an implicit variable that is legal and we do not need to do
11493 // anything.
11494 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011495 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11496 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11497 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011498 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011499 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011500 return;
11501 }
11502
11503 } else if (isa<FunctionDecl>(D)) {
11504 const FunctionDecl *FD = nullptr;
11505 if (cast<FunctionDecl>(D)->hasBody(FD))
11506 LD = const_cast<FunctionDecl *>(FD);
11507
11508 // If the definition is associated with the current declaration in the
11509 // target region (it can be e.g. a lambda) that is legal and we do not need
11510 // to do anything else.
11511 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011512 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11513 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11514 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011515 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011516 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011517 return;
11518 }
11519 }
11520 if (!LD)
11521 LD = D;
11522 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11523 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11524 // Outlined declaration is not declared target.
11525 if (LD->isOutOfLine()) {
11526 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11527 SemaRef.Diag(SL, diag::note_used_here) << SR;
11528 } else {
11529 DeclContext *DC = LD->getDeclContext();
11530 while (DC) {
11531 if (isa<FunctionDecl>(DC) &&
11532 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11533 break;
11534 DC = DC->getParent();
11535 }
11536 if (DC)
11537 return;
11538
11539 // Is not declared in target context.
11540 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11541 SemaRef.Diag(SL, diag::note_used_here) << SR;
11542 }
11543 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011544 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11545 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11546 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011547 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011548 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011549 }
11550}
11551
11552static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11553 Sema &SemaRef, DSAStackTy *Stack,
11554 ValueDecl *VD) {
11555 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11556 return true;
11557 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11558 return false;
11559 return true;
11560}
11561
11562void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11563 if (!D || D->isInvalidDecl())
11564 return;
11565 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11566 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11567 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11568 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11569 if (DSAStack->isThreadPrivate(VD)) {
11570 Diag(SL, diag::err_omp_threadprivate_in_target);
11571 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11572 return;
11573 }
11574 }
11575 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11576 // Problem if any with var declared with incomplete type will be reported
11577 // as normal, so no need to check it here.
11578 if ((E || !VD->getType()->isIncompleteType()) &&
11579 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11580 // Mark decl as declared target to prevent further diagnostic.
11581 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011582 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11583 Context, OMPDeclareTargetDeclAttr::MT_To);
11584 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011585 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011586 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011587 }
11588 return;
11589 }
11590 }
11591 if (!E) {
11592 // Checking declaration inside declare target region.
11593 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11594 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011595 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11596 Context, OMPDeclareTargetDeclAttr::MT_To);
11597 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011598 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011599 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011600 }
11601 return;
11602 }
11603 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11604}
Samuel Antao661c0902016-05-26 17:39:58 +000011605
11606OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11607 SourceLocation StartLoc,
11608 SourceLocation LParenLoc,
11609 SourceLocation EndLoc) {
11610 MappableVarListInfo MVLI(VarList);
11611 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11612 if (MVLI.ProcessedVarList.empty())
11613 return nullptr;
11614
11615 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11616 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11617 MVLI.VarComponents);
11618}
Samuel Antaoec172c62016-05-26 17:49:04 +000011619
11620OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11621 SourceLocation StartLoc,
11622 SourceLocation LParenLoc,
11623 SourceLocation EndLoc) {
11624 MappableVarListInfo MVLI(VarList);
11625 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11626 if (MVLI.ProcessedVarList.empty())
11627 return nullptr;
11628
11629 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11630 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11631 MVLI.VarComponents);
11632}
Carlo Bertolli2404b172016-07-13 15:37:16 +000011633
11634OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11635 SourceLocation StartLoc,
11636 SourceLocation LParenLoc,
11637 SourceLocation EndLoc) {
11638 SmallVector<Expr *, 8> Vars;
11639 for (auto &RefExpr : VarList) {
11640 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11641 SourceLocation ELoc;
11642 SourceRange ERange;
11643 Expr *SimpleRefExpr = RefExpr;
11644 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11645 if (Res.second) {
11646 // It will be analyzed later.
11647 Vars.push_back(RefExpr);
11648 }
11649 ValueDecl *D = Res.first;
11650 if (!D)
11651 continue;
11652
11653 QualType Type = D->getType();
11654 // item should be a pointer or reference to pointer
11655 if (!Type.getNonReferenceType()->isPointerType()) {
11656 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11657 << 0 << RefExpr->getSourceRange();
11658 continue;
11659 }
11660 Vars.push_back(RefExpr->IgnoreParens());
11661 }
11662
11663 if (Vars.empty())
11664 return nullptr;
11665
11666 return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11667 Vars);
11668}
Carlo Bertolli70594e92016-07-13 17:16:49 +000011669
11670OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11671 SourceLocation StartLoc,
11672 SourceLocation LParenLoc,
11673 SourceLocation EndLoc) {
11674 SmallVector<Expr *, 8> Vars;
11675 for (auto &RefExpr : VarList) {
11676 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11677 SourceLocation ELoc;
11678 SourceRange ERange;
11679 Expr *SimpleRefExpr = RefExpr;
11680 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11681 if (Res.second) {
11682 // It will be analyzed later.
11683 Vars.push_back(RefExpr);
11684 }
11685 ValueDecl *D = Res.first;
11686 if (!D)
11687 continue;
11688
11689 QualType Type = D->getType();
11690 // item should be a pointer or array or reference to pointer or array
11691 if (!Type.getNonReferenceType()->isPointerType() &&
11692 !Type.getNonReferenceType()->isArrayType()) {
11693 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11694 << 0 << RefExpr->getSourceRange();
11695 continue;
11696 }
11697 Vars.push_back(RefExpr->IgnoreParens());
11698 }
11699
11700 if (Vars.empty())
11701 return nullptr;
11702
11703 return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11704 Vars);
11705}