blob: 59c2f8bfc008c0c2cd89518493ce09a23f6b2440 [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:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007272 llvm_unreachable("Clause is not allowed.");
7273 }
7274 return Res;
7275}
7276
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007277OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7278 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007279 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007280 SourceLocation NameModifierLoc,
7281 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007282 SourceLocation EndLoc) {
7283 Expr *ValExpr = Condition;
7284 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7285 !Condition->isInstantiationDependent() &&
7286 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007287 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007288 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007289 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007290
Richard Smith03a4aa32016-06-23 19:02:52 +00007291 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007292 }
7293
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007294 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7295 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007296}
7297
Alexey Bataev3778b602014-07-17 07:32:53 +00007298OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7299 SourceLocation StartLoc,
7300 SourceLocation LParenLoc,
7301 SourceLocation EndLoc) {
7302 Expr *ValExpr = Condition;
7303 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7304 !Condition->isInstantiationDependent() &&
7305 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00007306 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00007307 if (Val.isInvalid())
7308 return nullptr;
7309
Richard Smith03a4aa32016-06-23 19:02:52 +00007310 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00007311 }
7312
7313 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7314}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007315ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7316 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00007317 if (!Op)
7318 return ExprError();
7319
7320 class IntConvertDiagnoser : public ICEConvertDiagnoser {
7321 public:
7322 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00007323 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00007324 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7325 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007326 return S.Diag(Loc, diag::err_omp_not_integral) << T;
7327 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007328 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7329 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007330 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7331 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007332 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7333 QualType T,
7334 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007335 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7336 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007337 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7338 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007339 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007340 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007341 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007342 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7343 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007344 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7345 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007346 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7347 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007348 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00007349 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00007350 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007351 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7352 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00007353 llvm_unreachable("conversion functions are permitted");
7354 }
7355 } ConvertDiagnoser;
7356 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7357}
7358
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007359static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00007360 OpenMPClauseKind CKind,
7361 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007362 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7363 !ValExpr->isInstantiationDependent()) {
7364 SourceLocation Loc = ValExpr->getExprLoc();
7365 ExprResult Value =
7366 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7367 if (Value.isInvalid())
7368 return false;
7369
7370 ValExpr = Value.get();
7371 // The expression must evaluate to a non-negative integer value.
7372 llvm::APSInt Result;
7373 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00007374 Result.isSigned() &&
7375 !((!StrictlyPositive && Result.isNonNegative()) ||
7376 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007377 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007378 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7379 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007380 return false;
7381 }
7382 }
7383 return true;
7384}
7385
Alexey Bataev568a8332014-03-06 06:15:19 +00007386OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7387 SourceLocation StartLoc,
7388 SourceLocation LParenLoc,
7389 SourceLocation EndLoc) {
7390 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00007391
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007392 // OpenMP [2.5, Restrictions]
7393 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007394 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7395 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007396 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00007397
Alexey Bataeved09d242014-05-28 05:53:51 +00007398 return new (Context)
7399 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00007400}
7401
Alexey Bataev62c87d22014-03-21 04:51:18 +00007402ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007403 OpenMPClauseKind CKind,
7404 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007405 if (!E)
7406 return ExprError();
7407 if (E->isValueDependent() || E->isTypeDependent() ||
7408 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00007409 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007410 llvm::APSInt Result;
7411 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7412 if (ICE.isInvalid())
7413 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007414 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7415 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00007416 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007417 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7418 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00007419 return ExprError();
7420 }
Alexander Musman09184fe2014-09-30 05:29:28 +00007421 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7422 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7423 << E->getSourceRange();
7424 return ExprError();
7425 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007426 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7427 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00007428 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00007429 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00007430 return ICE;
7431}
7432
7433OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7434 SourceLocation LParenLoc,
7435 SourceLocation EndLoc) {
7436 // OpenMP [2.8.1, simd construct, Description]
7437 // The parameter of the safelen clause must be a constant
7438 // positive integer expression.
7439 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7440 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007441 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007442 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007443 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00007444}
7445
Alexey Bataev66b15b52015-08-21 11:14:16 +00007446OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7447 SourceLocation LParenLoc,
7448 SourceLocation EndLoc) {
7449 // OpenMP [2.8.1, simd construct, Description]
7450 // The parameter of the simdlen clause must be a constant
7451 // positive integer expression.
7452 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7453 if (Simdlen.isInvalid())
7454 return nullptr;
7455 return new (Context)
7456 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7457}
7458
Alexander Musman64d33f12014-06-04 07:53:32 +00007459OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7460 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00007461 SourceLocation LParenLoc,
7462 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00007463 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007464 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00007465 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00007466 // The parameter of the collapse clause must be a constant
7467 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00007468 ExprResult NumForLoopsResult =
7469 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7470 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00007471 return nullptr;
7472 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00007473 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00007474}
7475
Alexey Bataev10e775f2015-07-30 11:36:16 +00007476OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7477 SourceLocation EndLoc,
7478 SourceLocation LParenLoc,
7479 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00007480 // OpenMP [2.7.1, loop construct, Description]
7481 // OpenMP [2.8.1, simd construct, Description]
7482 // OpenMP [2.9.6, distribute construct, Description]
7483 // The parameter of the ordered clause must be a constant
7484 // positive integer expression if any.
7485 if (NumForLoops && LParenLoc.isValid()) {
7486 ExprResult NumForLoopsResult =
7487 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7488 if (NumForLoopsResult.isInvalid())
7489 return nullptr;
7490 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00007491 } else
7492 NumForLoops = nullptr;
7493 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00007494 return new (Context)
7495 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7496}
7497
Alexey Bataeved09d242014-05-28 05:53:51 +00007498OMPClause *Sema::ActOnOpenMPSimpleClause(
7499 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7500 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007501 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007502 switch (Kind) {
7503 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007504 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00007505 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7506 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007507 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007508 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00007509 Res = ActOnOpenMPProcBindClause(
7510 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7511 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007512 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007513 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007514 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007515 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007516 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007517 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007518 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007519 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007520 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007521 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00007522 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00007523 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007524 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00007525 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007526 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007527 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007528 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007529 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007530 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007531 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007532 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007533 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007534 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007535 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007536 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007537 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007538 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007539 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007540 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007541 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007542 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007543 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007544 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007545 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007546 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007547 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007548 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007549 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007550 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007551 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007552 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007553 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007554 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007555 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007556 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007557 case OMPC_from:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007558 llvm_unreachable("Clause is not allowed.");
7559 }
7560 return Res;
7561}
7562
Alexey Bataev6402bca2015-12-28 07:25:51 +00007563static std::string
7564getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7565 ArrayRef<unsigned> Exclude = llvm::None) {
7566 std::string Values;
7567 unsigned Bound = Last >= 2 ? Last - 2 : 0;
7568 unsigned Skipped = Exclude.size();
7569 auto S = Exclude.begin(), E = Exclude.end();
7570 for (unsigned i = First; i < Last; ++i) {
7571 if (std::find(S, E, i) != E) {
7572 --Skipped;
7573 continue;
7574 }
7575 Values += "'";
7576 Values += getOpenMPSimpleClauseTypeName(K, i);
7577 Values += "'";
7578 if (i == Bound - Skipped)
7579 Values += " or ";
7580 else if (i != Bound + 1 - Skipped)
7581 Values += ", ";
7582 }
7583 return Values;
7584}
7585
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007586OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7587 SourceLocation KindKwLoc,
7588 SourceLocation StartLoc,
7589 SourceLocation LParenLoc,
7590 SourceLocation EndLoc) {
7591 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007592 static_assert(OMPC_DEFAULT_unknown > 0,
7593 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007594 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007595 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7596 /*Last=*/OMPC_DEFAULT_unknown)
7597 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007598 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007599 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007600 switch (Kind) {
7601 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007602 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007603 break;
7604 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007605 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007606 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007607 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007608 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007609 break;
7610 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007611 return new (Context)
7612 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007613}
7614
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007615OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7616 SourceLocation KindKwLoc,
7617 SourceLocation StartLoc,
7618 SourceLocation LParenLoc,
7619 SourceLocation EndLoc) {
7620 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007621 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007622 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7623 /*Last=*/OMPC_PROC_BIND_unknown)
7624 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007625 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007626 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007627 return new (Context)
7628 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007629}
7630
Alexey Bataev56dafe82014-06-20 07:16:17 +00007631OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007632 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007633 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007634 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007635 SourceLocation EndLoc) {
7636 OMPClause *Res = nullptr;
7637 switch (Kind) {
7638 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007639 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7640 assert(Argument.size() == NumberOfElements &&
7641 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007642 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007643 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7644 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7645 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7646 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7647 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007648 break;
7649 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007650 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7651 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7652 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7653 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007654 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007655 case OMPC_dist_schedule:
7656 Res = ActOnOpenMPDistScheduleClause(
7657 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7658 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7659 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007660 case OMPC_defaultmap:
7661 enum { Modifier, DefaultmapKind };
7662 Res = ActOnOpenMPDefaultmapClause(
7663 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7664 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7665 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7666 ArgumentLoc[DefaultmapKind], EndLoc);
7667 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007668 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007669 case OMPC_num_threads:
7670 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007671 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007672 case OMPC_collapse:
7673 case OMPC_default:
7674 case OMPC_proc_bind:
7675 case OMPC_private:
7676 case OMPC_firstprivate:
7677 case OMPC_lastprivate:
7678 case OMPC_shared:
7679 case OMPC_reduction:
7680 case OMPC_linear:
7681 case OMPC_aligned:
7682 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007683 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007684 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007685 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007686 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007687 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007688 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007689 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007690 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007691 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007692 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007693 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007694 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007695 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007696 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007697 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007698 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007699 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007700 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007701 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007702 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007703 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007704 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007705 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007706 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007707 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007708 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007709 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007710 case OMPC_from:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007711 llvm_unreachable("Clause is not allowed.");
7712 }
7713 return Res;
7714}
7715
Alexey Bataev6402bca2015-12-28 07:25:51 +00007716static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7717 OpenMPScheduleClauseModifier M2,
7718 SourceLocation M1Loc, SourceLocation M2Loc) {
7719 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7720 SmallVector<unsigned, 2> Excluded;
7721 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7722 Excluded.push_back(M2);
7723 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7724 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7725 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7726 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7727 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7728 << getListOfPossibleValues(OMPC_schedule,
7729 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7730 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7731 Excluded)
7732 << getOpenMPClauseName(OMPC_schedule);
7733 return true;
7734 }
7735 return false;
7736}
7737
Alexey Bataev56dafe82014-06-20 07:16:17 +00007738OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007739 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007740 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007741 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7742 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7743 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7744 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7745 return nullptr;
7746 // OpenMP, 2.7.1, Loop Construct, Restrictions
7747 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7748 // but not both.
7749 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7750 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7751 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7752 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7753 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7754 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7755 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7756 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7757 return nullptr;
7758 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007759 if (Kind == OMPC_SCHEDULE_unknown) {
7760 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007761 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7762 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7763 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7764 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7765 Exclude);
7766 } else {
7767 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7768 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007769 }
7770 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7771 << Values << getOpenMPClauseName(OMPC_schedule);
7772 return nullptr;
7773 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007774 // OpenMP, 2.7.1, Loop Construct, Restrictions
7775 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7776 // schedule(guided).
7777 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7778 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7779 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7780 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7781 diag::err_omp_schedule_nonmonotonic_static);
7782 return nullptr;
7783 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007784 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007785 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007786 if (ChunkSize) {
7787 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7788 !ChunkSize->isInstantiationDependent() &&
7789 !ChunkSize->containsUnexpandedParameterPack()) {
7790 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7791 ExprResult Val =
7792 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7793 if (Val.isInvalid())
7794 return nullptr;
7795
7796 ValExpr = Val.get();
7797
7798 // OpenMP [2.7.1, Restrictions]
7799 // chunk_size must be a loop invariant integer expression with a positive
7800 // value.
7801 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007802 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7803 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7804 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007805 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007806 return nullptr;
7807 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +00007808 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7809 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007810 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7811 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7812 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007813 }
7814 }
7815 }
7816
Alexey Bataev6402bca2015-12-28 07:25:51 +00007817 return new (Context)
7818 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007819 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007820}
7821
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007822OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7823 SourceLocation StartLoc,
7824 SourceLocation EndLoc) {
7825 OMPClause *Res = nullptr;
7826 switch (Kind) {
7827 case OMPC_ordered:
7828 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7829 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007830 case OMPC_nowait:
7831 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7832 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007833 case OMPC_untied:
7834 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7835 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007836 case OMPC_mergeable:
7837 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7838 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007839 case OMPC_read:
7840 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7841 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007842 case OMPC_write:
7843 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7844 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007845 case OMPC_update:
7846 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7847 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007848 case OMPC_capture:
7849 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7850 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007851 case OMPC_seq_cst:
7852 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7853 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007854 case OMPC_threads:
7855 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7856 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007857 case OMPC_simd:
7858 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7859 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007860 case OMPC_nogroup:
7861 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7862 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007863 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007864 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007865 case OMPC_num_threads:
7866 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007867 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007868 case OMPC_collapse:
7869 case OMPC_schedule:
7870 case OMPC_private:
7871 case OMPC_firstprivate:
7872 case OMPC_lastprivate:
7873 case OMPC_shared:
7874 case OMPC_reduction:
7875 case OMPC_linear:
7876 case OMPC_aligned:
7877 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007878 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007879 case OMPC_default:
7880 case OMPC_proc_bind:
7881 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007882 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007883 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007884 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007885 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007886 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007887 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007888 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007889 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007890 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007891 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007892 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007893 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007894 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007895 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00007896 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00007897 case OMPC_from:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007898 llvm_unreachable("Clause is not allowed.");
7899 }
7900 return Res;
7901}
7902
Alexey Bataev236070f2014-06-20 11:19:47 +00007903OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7904 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007905 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007906 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7907}
7908
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007909OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7910 SourceLocation EndLoc) {
7911 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7912}
7913
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007914OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7915 SourceLocation EndLoc) {
7916 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7917}
7918
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007919OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7920 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007921 return new (Context) OMPReadClause(StartLoc, EndLoc);
7922}
7923
Alexey Bataevdea47612014-07-23 07:46:59 +00007924OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7925 SourceLocation EndLoc) {
7926 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7927}
7928
Alexey Bataev67a4f222014-07-23 10:25:33 +00007929OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7930 SourceLocation EndLoc) {
7931 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7932}
7933
Alexey Bataev459dec02014-07-24 06:46:57 +00007934OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7935 SourceLocation EndLoc) {
7936 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7937}
7938
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007939OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7940 SourceLocation EndLoc) {
7941 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7942}
7943
Alexey Bataev346265e2015-09-25 10:37:12 +00007944OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7945 SourceLocation EndLoc) {
7946 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7947}
7948
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007949OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7950 SourceLocation EndLoc) {
7951 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7952}
7953
Alexey Bataevb825de12015-12-07 10:51:44 +00007954OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7955 SourceLocation EndLoc) {
7956 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7957}
7958
Alexey Bataevc5e02582014-06-16 07:08:35 +00007959OMPClause *Sema::ActOnOpenMPVarListClause(
7960 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7961 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7962 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007963 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007964 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7965 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7966 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007967 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007968 switch (Kind) {
7969 case OMPC_private:
7970 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7971 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007972 case OMPC_firstprivate:
7973 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7974 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007975 case OMPC_lastprivate:
7976 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7977 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007978 case OMPC_shared:
7979 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7980 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007981 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007982 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7983 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007984 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007985 case OMPC_linear:
7986 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007987 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007988 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007989 case OMPC_aligned:
7990 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7991 ColonLoc, EndLoc);
7992 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007993 case OMPC_copyin:
7994 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7995 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007996 case OMPC_copyprivate:
7997 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7998 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007999 case OMPC_flush:
8000 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8001 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008002 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008003 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8004 StartLoc, LParenLoc, EndLoc);
8005 break;
8006 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00008007 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8008 DepLinMapLoc, ColonLoc, VarList, StartLoc,
8009 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008010 break;
Samuel Antao661c0902016-05-26 17:39:58 +00008011 case OMPC_to:
8012 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8013 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00008014 case OMPC_from:
8015 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8016 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008017 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008018 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008019 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008020 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008021 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008022 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008023 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008024 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008025 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008026 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008027 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008028 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008029 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008030 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008031 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008032 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008033 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008034 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008035 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00008036 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008037 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008038 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008039 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008040 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008041 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008042 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008043 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008044 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008045 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008046 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008047 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008048 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008049 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008050 llvm_unreachable("Clause is not allowed.");
8051 }
8052 return Res;
8053}
8054
Alexey Bataev90c228f2016-02-08 09:29:13 +00008055ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00008056 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00008057 ExprResult Res = BuildDeclRefExpr(
8058 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8059 if (!Res.isUsable())
8060 return ExprError();
8061 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8062 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8063 if (!Res.isUsable())
8064 return ExprError();
8065 }
8066 if (VK != VK_LValue && Res.get()->isGLValue()) {
8067 Res = DefaultLvalueConversion(Res.get());
8068 if (!Res.isUsable())
8069 return ExprError();
8070 }
8071 return Res;
8072}
8073
Alexey Bataev60da77e2016-02-29 05:54:20 +00008074static std::pair<ValueDecl *, bool>
8075getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8076 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008077 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8078 RefExpr->containsUnexpandedParameterPack())
8079 return std::make_pair(nullptr, true);
8080
Alexey Bataevd985eda2016-02-10 11:29:16 +00008081 // OpenMP [3.1, C/C++]
8082 // A list item is a variable name.
8083 // OpenMP [2.9.3.3, Restrictions, p.1]
8084 // A variable that is part of another variable (as an array or
8085 // structure element) cannot appear in a private clause.
8086 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008087 enum {
8088 NoArrayExpr = -1,
8089 ArraySubscript = 0,
8090 OMPArraySection = 1
8091 } IsArrayExpr = NoArrayExpr;
8092 if (AllowArraySection) {
8093 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8094 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8095 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8096 Base = TempASE->getBase()->IgnoreParenImpCasts();
8097 RefExpr = Base;
8098 IsArrayExpr = ArraySubscript;
8099 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8100 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8101 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8102 Base = TempOASE->getBase()->IgnoreParenImpCasts();
8103 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8104 Base = TempASE->getBase()->IgnoreParenImpCasts();
8105 RefExpr = Base;
8106 IsArrayExpr = OMPArraySection;
8107 }
8108 }
8109 ELoc = RefExpr->getExprLoc();
8110 ERange = RefExpr->getSourceRange();
8111 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008112 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8113 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8114 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8115 (S.getCurrentThisType().isNull() || !ME ||
8116 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8117 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008118 if (IsArrayExpr != NoArrayExpr)
8119 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8120 << ERange;
8121 else {
8122 S.Diag(ELoc,
8123 AllowArraySection
8124 ? diag::err_omp_expected_var_name_member_expr_or_array_item
8125 : diag::err_omp_expected_var_name_member_expr)
8126 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8127 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008128 return std::make_pair(nullptr, false);
8129 }
8130 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8131}
8132
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008133OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8134 SourceLocation StartLoc,
8135 SourceLocation LParenLoc,
8136 SourceLocation EndLoc) {
8137 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00008138 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00008139 for (auto &RefExpr : VarList) {
8140 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008141 SourceLocation ELoc;
8142 SourceRange ERange;
8143 Expr *SimpleRefExpr = RefExpr;
8144 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008145 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008146 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008147 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008148 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008149 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008150 ValueDecl *D = Res.first;
8151 if (!D)
8152 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008153
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008154 QualType Type = D->getType();
8155 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008156
8157 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8158 // A variable that appears in a private clause must not have an incomplete
8159 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008160 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008161 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008162 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008163
Alexey Bataev758e55e2013-09-06 18:03:48 +00008164 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8165 // in a Construct]
8166 // Variables with the predetermined data-sharing attributes may not be
8167 // listed in data-sharing attributes clauses, except for the cases
8168 // listed below. For these exceptions only, listing a predetermined
8169 // variable in a data-sharing attribute clause is allowed and overrides
8170 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008171 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008172 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00008173 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8174 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008175 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008176 continue;
8177 }
8178
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008179 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008180 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008181 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008182 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8183 << getOpenMPClauseName(OMPC_private) << Type
8184 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8185 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008186 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008187 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008188 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008189 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008190 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008191 continue;
8192 }
8193
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008194 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8195 // A list item cannot appear in both a map clause and a data-sharing
8196 // attribute clause on the same construct
8197 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008198 if (DSAStack->checkMappableExprComponentListsForDecl(
8199 VD, /* CurrentRegionOnly = */ true,
8200 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8201 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008202 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8203 << getOpenMPClauseName(OMPC_private)
8204 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8205 ReportOriginalDSA(*this, DSAStack, D, DVar);
8206 continue;
8207 }
8208 }
8209
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008210 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8211 // A variable of class type (or array thereof) that appears in a private
8212 // clause requires an accessible, unambiguous default constructor for the
8213 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00008214 // Generate helper private variable and initialize it with the default
8215 // value. The address of the original variable is replaced by the address of
8216 // the new private variable in CodeGen. This new variable is not added to
8217 // IdResolver, so the code in the OpenMP region uses original variable for
8218 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008219 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008220 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8221 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008222 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008223 if (VDPrivate->isInvalidDecl())
8224 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008225 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008226 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008227
Alexey Bataev90c228f2016-02-08 09:29:13 +00008228 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008229 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008230 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00008231 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008232 Vars.push_back((VD || CurContext->isDependentContext())
8233 ? RefExpr->IgnoreParens()
8234 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00008235 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008236 }
8237
Alexey Bataeved09d242014-05-28 05:53:51 +00008238 if (Vars.empty())
8239 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008240
Alexey Bataev03b340a2014-10-21 03:16:40 +00008241 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8242 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008243}
8244
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008245namespace {
8246class DiagsUninitializedSeveretyRAII {
8247private:
8248 DiagnosticsEngine &Diags;
8249 SourceLocation SavedLoc;
8250 bool IsIgnored;
8251
8252public:
8253 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8254 bool IsIgnored)
8255 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8256 if (!IsIgnored) {
8257 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8258 /*Map*/ diag::Severity::Ignored, Loc);
8259 }
8260 }
8261 ~DiagsUninitializedSeveretyRAII() {
8262 if (!IsIgnored)
8263 Diags.popMappings(SavedLoc);
8264 }
8265};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00008266}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008267
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008268OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8269 SourceLocation StartLoc,
8270 SourceLocation LParenLoc,
8271 SourceLocation EndLoc) {
8272 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008273 SmallVector<Expr *, 8> PrivateCopies;
8274 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00008275 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008276 bool IsImplicitClause =
8277 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8278 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8279
Alexey Bataeved09d242014-05-28 05:53:51 +00008280 for (auto &RefExpr : VarList) {
8281 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008282 SourceLocation ELoc;
8283 SourceRange ERange;
8284 Expr *SimpleRefExpr = RefExpr;
8285 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008286 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008287 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008288 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008289 PrivateCopies.push_back(nullptr);
8290 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008291 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008292 ValueDecl *D = Res.first;
8293 if (!D)
8294 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008295
Alexey Bataev60da77e2016-02-29 05:54:20 +00008296 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008297 QualType Type = D->getType();
8298 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008299
8300 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8301 // A variable that appears in a private clause must not have an incomplete
8302 // type or a reference type.
8303 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00008304 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008305 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008306 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008307
8308 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8309 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00008310 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008311 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008312 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008313
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008314 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00008315 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008316 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008317 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008318 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008319 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008320 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8321 // A list item that specifies a given variable may not appear in more
8322 // than one clause on the same directive, except that a variable may be
8323 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008324 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00008325 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008326 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008327 << getOpenMPClauseName(DVar.CKind)
8328 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008329 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008330 continue;
8331 }
8332
8333 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8334 // in a Construct]
8335 // Variables with the predetermined data-sharing attributes may not be
8336 // listed in data-sharing attributes clauses, except for the cases
8337 // listed below. For these exceptions only, listing a predetermined
8338 // variable in a data-sharing attribute clause is allowed and overrides
8339 // the variable's predetermined data-sharing attributes.
8340 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8341 // in a Construct, C/C++, p.2]
8342 // Variables with const-qualified type having no mutable member may be
8343 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00008344 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008345 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8346 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00008347 << getOpenMPClauseName(DVar.CKind)
8348 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008349 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008350 continue;
8351 }
8352
Alexey Bataevf29276e2014-06-18 04:14:57 +00008353 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008354 // OpenMP [2.9.3.4, Restrictions, p.2]
8355 // A list item that is private within a parallel region must not appear
8356 // in a firstprivate clause on a worksharing construct if any of the
8357 // worksharing regions arising from the worksharing construct ever bind
8358 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00008359 if (isOpenMPWorksharingDirective(CurrDir) &&
8360 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008361 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008362 if (DVar.CKind != OMPC_shared &&
8363 (isOpenMPParallelDirective(DVar.DKind) ||
8364 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00008365 Diag(ELoc, diag::err_omp_required_access)
8366 << getOpenMPClauseName(OMPC_firstprivate)
8367 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008368 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008369 continue;
8370 }
8371 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008372 // OpenMP [2.9.3.4, Restrictions, p.3]
8373 // A list item that appears in a reduction clause of a parallel construct
8374 // must not appear in a firstprivate clause on a worksharing or task
8375 // construct if any of the worksharing or task regions arising from the
8376 // worksharing or task construct ever bind to any of the parallel regions
8377 // arising from the parallel construct.
8378 // OpenMP [2.9.3.4, Restrictions, p.4]
8379 // A list item that appears in a reduction clause in worksharing
8380 // construct must not appear in a firstprivate clause in a task construct
8381 // encountered during execution of any of the worksharing regions arising
8382 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00008383 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008384 DVar = DSAStack->hasInnermostDSA(
8385 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8386 [](OpenMPDirectiveKind K) -> bool {
8387 return isOpenMPParallelDirective(K) ||
8388 isOpenMPWorksharingDirective(K);
8389 },
8390 false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008391 if (DVar.CKind == OMPC_reduction &&
8392 (isOpenMPParallelDirective(DVar.DKind) ||
8393 isOpenMPWorksharingDirective(DVar.DKind))) {
8394 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8395 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008396 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008397 continue;
8398 }
8399 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008400
8401 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8402 // A list item that is private within a teams region must not appear in a
8403 // firstprivate clause on a distribute construct if any of the distribute
8404 // regions arising from the distribute construct ever bind to any of the
8405 // teams regions arising from the teams construct.
8406 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8407 // A list item that appears in a reduction clause of a teams construct
8408 // must not appear in a firstprivate clause on a distribute construct if
8409 // any of the distribute regions arising from the distribute construct
8410 // ever bind to any of the teams regions arising from the teams construct.
8411 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8412 // A list item may appear in a firstprivate or lastprivate clause but not
8413 // both.
8414 if (CurrDir == OMPD_distribute) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008415 DVar = DSAStack->hasInnermostDSA(
8416 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8417 [](OpenMPDirectiveKind K) -> bool {
8418 return isOpenMPTeamsDirective(K);
8419 },
8420 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008421 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8422 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008423 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008424 continue;
8425 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008426 DVar = DSAStack->hasInnermostDSA(
8427 D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8428 [](OpenMPDirectiveKind K) -> bool {
8429 return isOpenMPTeamsDirective(K);
8430 },
8431 false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008432 if (DVar.CKind == OMPC_reduction &&
8433 isOpenMPTeamsDirective(DVar.DKind)) {
8434 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008435 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008436 continue;
8437 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008438 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008439 if (DVar.CKind == OMPC_lastprivate) {
8440 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00008441 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00008442 continue;
8443 }
8444 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008445 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8446 // A list item cannot appear in both a map clause and a data-sharing
8447 // attribute clause on the same construct
8448 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00008449 if (DSAStack->checkMappableExprComponentListsForDecl(
8450 VD, /* CurrentRegionOnly = */ true,
8451 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8452 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00008453 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8454 << getOpenMPClauseName(OMPC_firstprivate)
8455 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8456 ReportOriginalDSA(*this, DSAStack, D, DVar);
8457 continue;
8458 }
8459 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008460 }
8461
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008462 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00008463 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00008464 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008465 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8466 << getOpenMPClauseName(OMPC_firstprivate) << Type
8467 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8468 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008469 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008470 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00008471 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008472 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00008473 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00008474 continue;
8475 }
8476
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008477 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008478 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8479 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008480 // Generate helper private variable and initialize it with the value of the
8481 // original variable. The address of the original variable is replaced by
8482 // the address of the new private variable in the CodeGen. This new variable
8483 // is not added to IdResolver, so the code in the OpenMP region uses
8484 // original variable for proper diagnostics and variable capturing.
8485 Expr *VDInitRefExpr = nullptr;
8486 // For arrays generate initializer for single element and replace it by the
8487 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008488 if (Type->isArrayType()) {
8489 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00008490 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008491 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008492 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008493 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00008494 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008495 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00008496 InitializedEntity Entity =
8497 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008498 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8499
8500 InitializationSequence InitSeq(*this, Entity, Kind, Init);
8501 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8502 if (Result.isInvalid())
8503 VDPrivate->setInvalidDecl();
8504 else
8505 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008506 // Remove temp variable declaration.
8507 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008508 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008509 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8510 ".firstprivate.temp");
8511 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8512 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00008513 AddInitializerToDecl(VDPrivate,
8514 DefaultLvalueConversion(VDInitRefExpr).get(),
8515 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008516 }
8517 if (VDPrivate->isInvalidDecl()) {
8518 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00008519 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008520 diag::note_omp_task_predetermined_firstprivate_here);
8521 }
8522 continue;
8523 }
8524 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008525 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00008526 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8527 RefExpr->getExprLoc());
8528 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008529 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008530 if (TopDVar.CKind == OMPC_lastprivate)
8531 Ref = TopDVar.PrivateCopy;
8532 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008533 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00008534 if (!IsOpenMPCapturedDecl(D))
8535 ExprCaptures.push_back(Ref->getDecl());
8536 }
Alexey Bataev417089f2016-02-17 13:19:37 +00008537 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00008538 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008539 Vars.push_back((VD || CurContext->isDependentContext())
8540 ? RefExpr->IgnoreParens()
8541 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00008542 PrivateCopies.push_back(VDPrivateRefExpr);
8543 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008544 }
8545
Alexey Bataeved09d242014-05-28 05:53:51 +00008546 if (Vars.empty())
8547 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008548
8549 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008550 Vars, PrivateCopies, Inits,
8551 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008552}
8553
Alexander Musman1bb328c2014-06-04 13:06:39 +00008554OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8555 SourceLocation StartLoc,
8556 SourceLocation LParenLoc,
8557 SourceLocation EndLoc) {
8558 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00008559 SmallVector<Expr *, 8> SrcExprs;
8560 SmallVector<Expr *, 8> DstExprs;
8561 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00008562 SmallVector<Decl *, 4> ExprCaptures;
8563 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008564 for (auto &RefExpr : VarList) {
8565 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008566 SourceLocation ELoc;
8567 SourceRange ERange;
8568 Expr *SimpleRefExpr = RefExpr;
8569 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008570 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00008571 // It will be analyzed later.
8572 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00008573 SrcExprs.push_back(nullptr);
8574 DstExprs.push_back(nullptr);
8575 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008576 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008577 ValueDecl *D = Res.first;
8578 if (!D)
8579 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008580
Alexey Bataev74caaf22016-02-20 04:09:36 +00008581 QualType Type = D->getType();
8582 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008583
8584 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8585 // A variable that appears in a lastprivate clause must not have an
8586 // incomplete type or a reference type.
8587 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00008588 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00008589 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008590 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00008591
8592 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8593 // in a Construct]
8594 // Variables with the predetermined data-sharing attributes may not be
8595 // listed in data-sharing attributes clauses, except for the cases
8596 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008597 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008598 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8599 DVar.CKind != OMPC_firstprivate &&
8600 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8601 Diag(ELoc, diag::err_omp_wrong_dsa)
8602 << getOpenMPClauseName(DVar.CKind)
8603 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008604 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008605 continue;
8606 }
8607
Alexey Bataevf29276e2014-06-18 04:14:57 +00008608 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8609 // OpenMP [2.14.3.5, Restrictions, p.2]
8610 // A list item that is private within a parallel region, or that appears in
8611 // the reduction clause of a parallel construct, must not appear in a
8612 // lastprivate clause on a worksharing construct if any of the corresponding
8613 // worksharing regions ever binds to any of the corresponding parallel
8614 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008615 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008616 if (isOpenMPWorksharingDirective(CurrDir) &&
8617 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008618 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008619 if (DVar.CKind != OMPC_shared) {
8620 Diag(ELoc, diag::err_omp_required_access)
8621 << getOpenMPClauseName(OMPC_lastprivate)
8622 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008623 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008624 continue;
8625 }
8626 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008627
8628 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8629 // A list item may appear in a firstprivate or lastprivate clause but not
8630 // both.
8631 if (CurrDir == OMPD_distribute) {
8632 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8633 if (DVar.CKind == OMPC_firstprivate) {
8634 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8635 ReportOriginalDSA(*this, DSAStack, D, DVar);
8636 continue;
8637 }
8638 }
8639
Alexander Musman1bb328c2014-06-04 13:06:39 +00008640 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008641 // A variable of class type (or array thereof) that appears in a
8642 // lastprivate clause requires an accessible, unambiguous default
8643 // constructor for the class type, unless the list item is also specified
8644 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008645 // A variable of class type (or array thereof) that appears in a
8646 // lastprivate clause requires an accessible, unambiguous copy assignment
8647 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008648 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008649 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008650 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008651 D->hasAttrs() ? &D->getAttrs() : nullptr);
8652 auto *PseudoSrcExpr =
8653 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008654 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008655 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008656 D->hasAttrs() ? &D->getAttrs() : nullptr);
8657 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008658 // For arrays generate assignment operation for single element and replace
8659 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008660 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008661 PseudoDstExpr, PseudoSrcExpr);
8662 if (AssignmentOp.isInvalid())
8663 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008664 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008665 /*DiscardedValue=*/true);
8666 if (AssignmentOp.isInvalid())
8667 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008668
Alexey Bataev74caaf22016-02-20 04:09:36 +00008669 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008670 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008671 if (TopDVar.CKind == OMPC_firstprivate)
8672 Ref = TopDVar.PrivateCopy;
8673 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008674 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008675 if (!IsOpenMPCapturedDecl(D))
8676 ExprCaptures.push_back(Ref->getDecl());
8677 }
8678 if (TopDVar.CKind == OMPC_firstprivate ||
8679 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008680 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008681 ExprResult RefRes = DefaultLvalueConversion(Ref);
8682 if (!RefRes.isUsable())
8683 continue;
8684 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008685 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8686 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008687 if (!PostUpdateRes.isUsable())
8688 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008689 ExprPostUpdates.push_back(
8690 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008691 }
8692 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008693 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008694 Vars.push_back((VD || CurContext->isDependentContext())
8695 ? RefExpr->IgnoreParens()
8696 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008697 SrcExprs.push_back(PseudoSrcExpr);
8698 DstExprs.push_back(PseudoDstExpr);
8699 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008700 }
8701
8702 if (Vars.empty())
8703 return nullptr;
8704
8705 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008706 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008707 buildPreInits(Context, ExprCaptures),
8708 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008709}
8710
Alexey Bataev758e55e2013-09-06 18:03:48 +00008711OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8712 SourceLocation StartLoc,
8713 SourceLocation LParenLoc,
8714 SourceLocation EndLoc) {
8715 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008716 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008717 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008718 SourceLocation ELoc;
8719 SourceRange ERange;
8720 Expr *SimpleRefExpr = RefExpr;
8721 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008722 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008723 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008724 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008725 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008726 ValueDecl *D = Res.first;
8727 if (!D)
8728 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008729
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008730 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008731 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8732 // in a Construct]
8733 // Variables with the predetermined data-sharing attributes may not be
8734 // listed in data-sharing attributes clauses, except for the cases
8735 // listed below. For these exceptions only, listing a predetermined
8736 // variable in a data-sharing attribute clause is allowed and overrides
8737 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008738 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008739 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8740 DVar.RefExpr) {
8741 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8742 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008743 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008744 continue;
8745 }
8746
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008747 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008748 if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00008749 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008750 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00008751 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8752 ? RefExpr->IgnoreParens()
8753 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008754 }
8755
Alexey Bataeved09d242014-05-28 05:53:51 +00008756 if (Vars.empty())
8757 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008758
8759 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8760}
8761
Alexey Bataevc5e02582014-06-16 07:08:35 +00008762namespace {
8763class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8764 DSAStackTy *Stack;
8765
8766public:
8767 bool VisitDeclRefExpr(DeclRefExpr *E) {
8768 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008769 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008770 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8771 return false;
8772 if (DVar.CKind != OMPC_unknown)
8773 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00008774 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8775 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8776 false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008777 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008778 return true;
8779 return false;
8780 }
8781 return false;
8782 }
8783 bool VisitStmt(Stmt *S) {
8784 for (auto Child : S->children()) {
8785 if (Child && Visit(Child))
8786 return true;
8787 }
8788 return false;
8789 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008790 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008791};
Alexey Bataev23b69422014-06-18 07:08:49 +00008792} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008793
Alexey Bataev60da77e2016-02-29 05:54:20 +00008794namespace {
8795// Transform MemberExpression for specified FieldDecl of current class to
8796// DeclRefExpr to specified OMPCapturedExprDecl.
8797class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8798 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8799 ValueDecl *Field;
8800 DeclRefExpr *CapturedExpr;
8801
8802public:
8803 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8804 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8805
8806 ExprResult TransformMemberExpr(MemberExpr *E) {
8807 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8808 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008809 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008810 return CapturedExpr;
8811 }
8812 return BaseTransform::TransformMemberExpr(E);
8813 }
8814 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8815};
8816} // namespace
8817
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008818template <typename T>
8819static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8820 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8821 for (auto &Set : Lookups) {
8822 for (auto *D : Set) {
8823 if (auto Res = Gen(cast<ValueDecl>(D)))
8824 return Res;
8825 }
8826 }
8827 return T();
8828}
8829
8830static ExprResult
8831buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8832 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8833 const DeclarationNameInfo &ReductionId, QualType Ty,
8834 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8835 if (ReductionIdScopeSpec.isInvalid())
8836 return ExprError();
8837 SmallVector<UnresolvedSet<8>, 4> Lookups;
8838 if (S) {
8839 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8840 Lookup.suppressDiagnostics();
8841 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8842 auto *D = Lookup.getRepresentativeDecl();
8843 do {
8844 S = S->getParent();
8845 } while (S && !S->isDeclScope(D));
8846 if (S)
8847 S = S->getParent();
8848 Lookups.push_back(UnresolvedSet<8>());
8849 Lookups.back().append(Lookup.begin(), Lookup.end());
8850 Lookup.clear();
8851 }
8852 } else if (auto *ULE =
8853 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8854 Lookups.push_back(UnresolvedSet<8>());
8855 Decl *PrevD = nullptr;
8856 for(auto *D : ULE->decls()) {
8857 if (D == PrevD)
8858 Lookups.push_back(UnresolvedSet<8>());
8859 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8860 Lookups.back().addDecl(DRD);
8861 PrevD = D;
8862 }
8863 }
8864 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8865 Ty->containsUnexpandedParameterPack() ||
8866 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8867 return !D->isInvalidDecl() &&
8868 (D->getType()->isDependentType() ||
8869 D->getType()->isInstantiationDependentType() ||
8870 D->getType()->containsUnexpandedParameterPack());
8871 })) {
8872 UnresolvedSet<8> ResSet;
8873 for (auto &Set : Lookups) {
8874 ResSet.append(Set.begin(), Set.end());
8875 // The last item marks the end of all declarations at the specified scope.
8876 ResSet.addDecl(Set[Set.size() - 1]);
8877 }
8878 return UnresolvedLookupExpr::Create(
8879 SemaRef.Context, /*NamingClass=*/nullptr,
8880 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8881 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8882 }
8883 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8884 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8885 if (!D->isInvalidDecl() &&
8886 SemaRef.Context.hasSameType(D->getType(), Ty))
8887 return D;
8888 return nullptr;
8889 }))
8890 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8891 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8892 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8893 if (!D->isInvalidDecl() &&
8894 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8895 !Ty.isMoreQualifiedThan(D->getType()))
8896 return D;
8897 return nullptr;
8898 })) {
8899 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8900 /*DetectVirtual=*/false);
8901 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8902 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8903 VD->getType().getUnqualifiedType()))) {
8904 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8905 /*DiagID=*/0) !=
8906 Sema::AR_inaccessible) {
8907 SemaRef.BuildBasePathArray(Paths, BasePath);
8908 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8909 }
8910 }
8911 }
8912 }
8913 if (ReductionIdScopeSpec.isSet()) {
8914 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8915 return ExprError();
8916 }
8917 return ExprEmpty();
8918}
8919
Alexey Bataevc5e02582014-06-16 07:08:35 +00008920OMPClause *Sema::ActOnOpenMPReductionClause(
8921 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8922 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008923 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8924 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008925 auto DN = ReductionId.getName();
8926 auto OOK = DN.getCXXOverloadedOperator();
8927 BinaryOperatorKind BOK = BO_Comma;
8928
8929 // OpenMP [2.14.3.6, reduction clause]
8930 // C
8931 // reduction-identifier is either an identifier or one of the following
8932 // operators: +, -, *, &, |, ^, && and ||
8933 // C++
8934 // reduction-identifier is either an id-expression or one of the following
8935 // operators: +, -, *, &, |, ^, && and ||
8936 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8937 switch (OOK) {
8938 case OO_Plus:
8939 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008940 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008941 break;
8942 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008943 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008944 break;
8945 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008946 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008947 break;
8948 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008949 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008950 break;
8951 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008952 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008953 break;
8954 case OO_AmpAmp:
8955 BOK = BO_LAnd;
8956 break;
8957 case OO_PipePipe:
8958 BOK = BO_LOr;
8959 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008960 case OO_New:
8961 case OO_Delete:
8962 case OO_Array_New:
8963 case OO_Array_Delete:
8964 case OO_Slash:
8965 case OO_Percent:
8966 case OO_Tilde:
8967 case OO_Exclaim:
8968 case OO_Equal:
8969 case OO_Less:
8970 case OO_Greater:
8971 case OO_LessEqual:
8972 case OO_GreaterEqual:
8973 case OO_PlusEqual:
8974 case OO_MinusEqual:
8975 case OO_StarEqual:
8976 case OO_SlashEqual:
8977 case OO_PercentEqual:
8978 case OO_CaretEqual:
8979 case OO_AmpEqual:
8980 case OO_PipeEqual:
8981 case OO_LessLess:
8982 case OO_GreaterGreater:
8983 case OO_LessLessEqual:
8984 case OO_GreaterGreaterEqual:
8985 case OO_EqualEqual:
8986 case OO_ExclaimEqual:
8987 case OO_PlusPlus:
8988 case OO_MinusMinus:
8989 case OO_Comma:
8990 case OO_ArrowStar:
8991 case OO_Arrow:
8992 case OO_Call:
8993 case OO_Subscript:
8994 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008995 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008996 case NUM_OVERLOADED_OPERATORS:
8997 llvm_unreachable("Unexpected reduction identifier");
8998 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008999 if (auto II = DN.getAsIdentifierInfo()) {
9000 if (II->isStr("max"))
9001 BOK = BO_GT;
9002 else if (II->isStr("min"))
9003 BOK = BO_LT;
9004 }
9005 break;
9006 }
9007 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009008 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00009009 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009010 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009011
9012 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009013 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009014 SmallVector<Expr *, 8> LHSs;
9015 SmallVector<Expr *, 8> RHSs;
9016 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00009017 SmallVector<Decl *, 4> ExprCaptures;
9018 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009019 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9020 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009021 for (auto RefExpr : VarList) {
9022 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00009023 // OpenMP [2.1, C/C++]
9024 // A list item is a variable or array section, subject to the restrictions
9025 // specified in Section 2.4 on page 42 and in each of the sections
9026 // describing clauses and directives for which a list appears.
9027 // OpenMP [2.14.3.3, Restrictions, p.1]
9028 // A variable that is part of another variable (as an array or
9029 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009030 if (!FirstIter && IR != ER)
9031 ++IR;
9032 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009033 SourceLocation ELoc;
9034 SourceRange ERange;
9035 Expr *SimpleRefExpr = RefExpr;
9036 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9037 /*AllowArraySection=*/true);
9038 if (Res.second) {
9039 // It will be analyzed later.
9040 Vars.push_back(RefExpr);
9041 Privates.push_back(nullptr);
9042 LHSs.push_back(nullptr);
9043 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009044 // Try to find 'declare reduction' corresponding construct before using
9045 // builtin/overloaded operators.
9046 QualType Type = Context.DependentTy;
9047 CXXCastPath BasePath;
9048 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9049 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9050 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9051 if (CurContext->isDependentContext() &&
9052 (DeclareReductionRef.isUnset() ||
9053 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9054 ReductionOps.push_back(DeclareReductionRef.get());
9055 else
9056 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009057 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009058 ValueDecl *D = Res.first;
9059 if (!D)
9060 continue;
9061
Alexey Bataeva1764212015-09-30 09:22:36 +00009062 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009063 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9064 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9065 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00009066 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009067 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009068 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9069 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9070 Type = ATy->getElementType();
9071 else
9072 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009073 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009074 } else
9075 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9076 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00009077
Alexey Bataevc5e02582014-06-16 07:08:35 +00009078 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9079 // A variable that appears in a private clause must not have an incomplete
9080 // type or a reference type.
9081 if (RequireCompleteType(ELoc, Type,
9082 diag::err_omp_reduction_incomplete_type))
9083 continue;
9084 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00009085 // A list item that appears in a reduction clause must not be
9086 // const-qualified.
9087 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009088 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00009089 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009090 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009091 bool IsDecl = !VD ||
9092 VD->isThisDeclarationADefinition(Context) ==
9093 VarDecl::DeclarationOnly;
9094 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00009095 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00009096 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00009097 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009098 continue;
9099 }
9100 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9101 // If a list-item is a reference type then it must bind to the same object
9102 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009103 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009104 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00009105 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00009106 DSARefChecker Check(DSAStack);
9107 if (Check.Visit(VDDef->getInit())) {
9108 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9109 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9110 continue;
9111 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00009112 }
9113 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009114
Alexey Bataevc5e02582014-06-16 07:08:35 +00009115 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9116 // in a Construct]
9117 // Variables with the predetermined data-sharing attributes may not be
9118 // listed in data-sharing attributes clauses, except for the cases
9119 // listed below. For these exceptions only, listing a predetermined
9120 // variable in a data-sharing attribute clause is allowed and overrides
9121 // the variable's predetermined data-sharing attributes.
9122 // OpenMP [2.14.3.6, Restrictions, p.3]
9123 // Any number of reduction clauses can be specified on the directive,
9124 // but a list item can appear only once in the reduction clauses for that
9125 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00009126 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009127 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009128 if (DVar.CKind == OMPC_reduction) {
9129 Diag(ELoc, diag::err_omp_once_referenced)
9130 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009131 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009132 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009133 } else if (DVar.CKind != OMPC_unknown) {
9134 Diag(ELoc, diag::err_omp_wrong_dsa)
9135 << getOpenMPClauseName(DVar.CKind)
9136 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009137 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009138 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009139 }
9140
9141 // OpenMP [2.14.3.6, Restrictions, p.1]
9142 // A list item that appears in a reduction clause of a worksharing
9143 // construct must be shared in the parallel regions to which any of the
9144 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009145 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9146 if (isOpenMPWorksharingDirective(CurrDir) &&
9147 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009148 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009149 if (DVar.CKind != OMPC_shared) {
9150 Diag(ELoc, diag::err_omp_required_access)
9151 << getOpenMPClauseName(OMPC_reduction)
9152 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00009153 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009154 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00009155 }
9156 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009157
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009158 // Try to find 'declare reduction' corresponding construct before using
9159 // builtin/overloaded operators.
9160 CXXCastPath BasePath;
9161 ExprResult DeclareReductionRef = buildDeclareReductionRef(
9162 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9163 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9164 if (DeclareReductionRef.isInvalid())
9165 continue;
9166 if (CurContext->isDependentContext() &&
9167 (DeclareReductionRef.isUnset() ||
9168 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9169 Vars.push_back(RefExpr);
9170 Privates.push_back(nullptr);
9171 LHSs.push_back(nullptr);
9172 RHSs.push_back(nullptr);
9173 ReductionOps.push_back(DeclareReductionRef.get());
9174 continue;
9175 }
9176 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9177 // Not allowed reduction identifier is found.
9178 Diag(ReductionId.getLocStart(),
9179 diag::err_omp_unknown_reduction_identifier)
9180 << Type << ReductionIdRange;
9181 continue;
9182 }
9183
9184 // OpenMP [2.14.3.6, reduction clause, Restrictions]
9185 // The type of a list item that appears in a reduction clause must be valid
9186 // for the reduction-identifier. For a max or min reduction in C, the type
9187 // of the list item must be an allowed arithmetic data type: char, int,
9188 // float, double, or _Bool, possibly modified with long, short, signed, or
9189 // unsigned. For a max or min reduction in C++, the type of the list item
9190 // must be an allowed arithmetic data type: char, wchar_t, int, float,
9191 // double, or bool, possibly modified with long, short, signed, or unsigned.
9192 if (DeclareReductionRef.isUnset()) {
9193 if ((BOK == BO_GT || BOK == BO_LT) &&
9194 !(Type->isScalarType() ||
9195 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9196 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9197 << getLangOpts().CPlusPlus;
9198 if (!ASE && !OASE) {
9199 bool IsDecl = !VD ||
9200 VD->isThisDeclarationADefinition(Context) ==
9201 VarDecl::DeclarationOnly;
9202 Diag(D->getLocation(),
9203 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9204 << D;
9205 }
9206 continue;
9207 }
9208 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9209 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9210 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9211 if (!ASE && !OASE) {
9212 bool IsDecl = !VD ||
9213 VD->isThisDeclarationADefinition(Context) ==
9214 VarDecl::DeclarationOnly;
9215 Diag(D->getLocation(),
9216 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9217 << D;
9218 }
9219 continue;
9220 }
9221 }
9222
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009223 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009224 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00009225 D->hasAttrs() ? &D->getAttrs() : nullptr);
9226 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9227 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009228 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00009229 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00009230 (!ASE &&
9231 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00009232 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009233 // Create pseudo array type for private copy. The size for this array will
9234 // be generated during codegen.
9235 // For array subscripts or single variables Private Ty is the same as Type
9236 // (type of the variable or single array element).
9237 PrivateTy = Context.getVariableArrayType(
9238 Type, new (Context) OpaqueValueExpr(SourceLocation(),
9239 Context.getSizeType(), VK_RValue),
9240 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00009241 } else if (!ASE && !OASE &&
9242 Context.getAsArrayType(D->getType().getNonReferenceType()))
9243 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009244 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00009245 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9246 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009247 // Add initializer for private variable.
9248 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009249 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9250 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9251 if (DeclareReductionRef.isUsable()) {
9252 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9253 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9254 if (DRD->getInitializer()) {
9255 Init = DRDRef;
9256 RHSVD->setInit(DRDRef);
9257 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009258 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009259 } else {
9260 switch (BOK) {
9261 case BO_Add:
9262 case BO_Xor:
9263 case BO_Or:
9264 case BO_LOr:
9265 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9266 if (Type->isScalarType() || Type->isAnyComplexType())
9267 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9268 break;
9269 case BO_Mul:
9270 case BO_LAnd:
9271 if (Type->isScalarType() || Type->isAnyComplexType()) {
9272 // '*' and '&&' reduction ops - initializer is '1'.
9273 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00009274 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009275 break;
9276 case BO_And: {
9277 // '&' reduction op - initializer is '~0'.
9278 QualType OrigType = Type;
9279 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9280 Type = ComplexTy->getElementType();
9281 if (Type->isRealFloatingType()) {
9282 llvm::APFloat InitValue =
9283 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9284 /*isIEEE=*/true);
9285 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9286 Type, ELoc);
9287 } else if (Type->isScalarType()) {
9288 auto Size = Context.getTypeSize(Type);
9289 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9290 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9291 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9292 }
9293 if (Init && OrigType->isAnyComplexType()) {
9294 // Init = 0xFFFF + 0xFFFFi;
9295 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9296 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9297 }
9298 Type = OrigType;
9299 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009300 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009301 case BO_LT:
9302 case BO_GT: {
9303 // 'min' reduction op - initializer is 'Largest representable number in
9304 // the reduction list item type'.
9305 // 'max' reduction op - initializer is 'Least representable number in
9306 // the reduction list item type'.
9307 if (Type->isIntegerType() || Type->isPointerType()) {
9308 bool IsSigned = Type->hasSignedIntegerRepresentation();
9309 auto Size = Context.getTypeSize(Type);
9310 QualType IntTy =
9311 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9312 llvm::APInt InitValue =
9313 (BOK != BO_LT)
9314 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9315 : llvm::APInt::getMinValue(Size)
9316 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9317 : llvm::APInt::getMaxValue(Size);
9318 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9319 if (Type->isPointerType()) {
9320 // Cast to pointer type.
9321 auto CastExpr = BuildCStyleCastExpr(
9322 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9323 SourceLocation(), Init);
9324 if (CastExpr.isInvalid())
9325 continue;
9326 Init = CastExpr.get();
9327 }
9328 } else if (Type->isRealFloatingType()) {
9329 llvm::APFloat InitValue = llvm::APFloat::getLargest(
9330 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9331 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9332 Type, ELoc);
9333 }
9334 break;
9335 }
9336 case BO_PtrMemD:
9337 case BO_PtrMemI:
9338 case BO_MulAssign:
9339 case BO_Div:
9340 case BO_Rem:
9341 case BO_Sub:
9342 case BO_Shl:
9343 case BO_Shr:
9344 case BO_LE:
9345 case BO_GE:
9346 case BO_EQ:
9347 case BO_NE:
9348 case BO_AndAssign:
9349 case BO_XorAssign:
9350 case BO_OrAssign:
9351 case BO_Assign:
9352 case BO_AddAssign:
9353 case BO_SubAssign:
9354 case BO_DivAssign:
9355 case BO_RemAssign:
9356 case BO_ShlAssign:
9357 case BO_ShrAssign:
9358 case BO_Comma:
9359 llvm_unreachable("Unexpected reduction operation");
9360 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009361 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009362 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009363 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9364 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009365 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009366 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009367 if (RHSVD->isInvalidDecl())
9368 continue;
9369 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009370 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9371 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00009372 bool IsDecl =
9373 !VD ||
9374 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9375 Diag(D->getLocation(),
9376 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9377 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009378 continue;
9379 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009380 // Store initializer for single element in private copy. Will be used during
9381 // codegen.
9382 PrivateVD->setInit(RHSVD->getInit());
9383 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009384 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009385 ExprResult ReductionOp;
9386 if (DeclareReductionRef.isUsable()) {
9387 QualType RedTy = DeclareReductionRef.get()->getType();
9388 QualType PtrRedTy = Context.getPointerType(RedTy);
9389 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9390 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9391 if (!BasePath.empty()) {
9392 LHS = DefaultLvalueConversion(LHS.get());
9393 RHS = DefaultLvalueConversion(RHS.get());
9394 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9395 CK_UncheckedDerivedToBase, LHS.get(),
9396 &BasePath, LHS.get()->getValueKind());
9397 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9398 CK_UncheckedDerivedToBase, RHS.get(),
9399 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009400 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00009401 FunctionProtoType::ExtProtoInfo EPI;
9402 QualType Params[] = {PtrRedTy, PtrRedTy};
9403 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9404 auto *OVE = new (Context) OpaqueValueExpr(
9405 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9406 DefaultLvalueConversion(DeclareReductionRef.get()).get());
9407 Expr *Args[] = {LHS.get(), RHS.get()};
9408 ReductionOp = new (Context)
9409 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9410 } else {
9411 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9412 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9413 if (ReductionOp.isUsable()) {
9414 if (BOK != BO_LT && BOK != BO_GT) {
9415 ReductionOp =
9416 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9417 BO_Assign, LHSDRE, ReductionOp.get());
9418 } else {
9419 auto *ConditionalOp = new (Context) ConditionalOperator(
9420 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9421 RHSDRE, Type, VK_LValue, OK_Ordinary);
9422 ReductionOp =
9423 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9424 BO_Assign, LHSDRE, ConditionalOp);
9425 }
9426 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9427 }
9428 if (ReductionOp.isInvalid())
9429 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009430 }
9431
Alexey Bataev60da77e2016-02-29 05:54:20 +00009432 DeclRefExpr *Ref = nullptr;
9433 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009434 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009435 if (ASE || OASE) {
9436 TransformExprToCaptures RebuildToCapture(*this, D);
9437 VarsExpr =
9438 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9439 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00009440 } else {
9441 VarsExpr = Ref =
9442 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00009443 }
9444 if (!IsOpenMPCapturedDecl(D)) {
9445 ExprCaptures.push_back(Ref->getDecl());
9446 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9447 ExprResult RefRes = DefaultLvalueConversion(Ref);
9448 if (!RefRes.isUsable())
9449 continue;
9450 ExprResult PostUpdateRes =
9451 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9452 SimpleRefExpr, RefRes.get());
9453 if (!PostUpdateRes.isUsable())
9454 continue;
9455 ExprPostUpdates.push_back(
9456 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00009457 }
9458 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00009459 }
9460 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9461 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009462 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00009463 LHSs.push_back(LHSDRE);
9464 RHSs.push_back(RHSDRE);
9465 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00009466 }
9467
9468 if (Vars.empty())
9469 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00009470
Alexey Bataevc5e02582014-06-16 07:08:35 +00009471 return OMPReductionClause::Create(
9472 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009473 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009474 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9475 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00009476}
9477
Alexey Bataevecba70f2016-04-12 11:02:11 +00009478bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9479 SourceLocation LinLoc) {
9480 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9481 LinKind == OMPC_LINEAR_unknown) {
9482 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9483 return true;
9484 }
9485 return false;
9486}
9487
9488bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9489 OpenMPLinearClauseKind LinKind,
9490 QualType Type) {
9491 auto *VD = dyn_cast_or_null<VarDecl>(D);
9492 // A variable must not have an incomplete type or a reference type.
9493 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9494 return true;
9495 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9496 !Type->isReferenceType()) {
9497 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9498 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9499 return true;
9500 }
9501 Type = Type.getNonReferenceType();
9502
9503 // A list item must not be const-qualified.
9504 if (Type.isConstant(Context)) {
9505 Diag(ELoc, diag::err_omp_const_variable)
9506 << getOpenMPClauseName(OMPC_linear);
9507 if (D) {
9508 bool IsDecl =
9509 !VD ||
9510 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9511 Diag(D->getLocation(),
9512 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9513 << D;
9514 }
9515 return true;
9516 }
9517
9518 // A list item must be of integral or pointer type.
9519 Type = Type.getUnqualifiedType().getCanonicalType();
9520 const auto *Ty = Type.getTypePtrOrNull();
9521 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9522 !Ty->isPointerType())) {
9523 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9524 if (D) {
9525 bool IsDecl =
9526 !VD ||
9527 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9528 Diag(D->getLocation(),
9529 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9530 << D;
9531 }
9532 return true;
9533 }
9534 return false;
9535}
9536
Alexey Bataev182227b2015-08-20 10:54:39 +00009537OMPClause *Sema::ActOnOpenMPLinearClause(
9538 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9539 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9540 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009541 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009542 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00009543 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00009544 SmallVector<Decl *, 4> ExprCaptures;
9545 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009546 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00009547 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00009548 for (auto &RefExpr : VarList) {
9549 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009550 SourceLocation ELoc;
9551 SourceRange ERange;
9552 Expr *SimpleRefExpr = RefExpr;
9553 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9554 /*AllowArraySection=*/false);
9555 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00009556 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009557 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009558 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00009559 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00009560 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009561 ValueDecl *D = Res.first;
9562 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00009563 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00009564
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009565 QualType Type = D->getType();
9566 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00009567
9568 // OpenMP [2.14.3.7, linear clause]
9569 // A list-item cannot appear in more than one linear clause.
9570 // A list-item that appears in a linear clause cannot appear in any
9571 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009572 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00009573 if (DVar.RefExpr) {
9574 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9575 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009576 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00009577 continue;
9578 }
9579
Alexey Bataevecba70f2016-04-12 11:02:11 +00009580 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00009581 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00009582 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00009583
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009584 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009585 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9586 D->hasAttrs() ? &D->getAttrs() : nullptr);
9587 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009588 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009589 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009590 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009591 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009592 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +00009593 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9594 if (!IsOpenMPCapturedDecl(D)) {
9595 ExprCaptures.push_back(Ref->getDecl());
9596 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9597 ExprResult RefRes = DefaultLvalueConversion(Ref);
9598 if (!RefRes.isUsable())
9599 continue;
9600 ExprResult PostUpdateRes =
9601 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9602 SimpleRefExpr, RefRes.get());
9603 if (!PostUpdateRes.isUsable())
9604 continue;
9605 ExprPostUpdates.push_back(
9606 IgnoredValueConversions(PostUpdateRes.get()).get());
9607 }
9608 }
9609 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009610 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009611 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009612 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009613 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009614 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009615 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9616 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9617
9618 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009619 Vars.push_back((VD || CurContext->isDependentContext())
9620 ? RefExpr->IgnoreParens()
9621 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009622 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009623 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009624 }
9625
9626 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009627 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009628
9629 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009630 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009631 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9632 !Step->isInstantiationDependent() &&
9633 !Step->containsUnexpandedParameterPack()) {
9634 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009635 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009636 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009637 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009638 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009639
Alexander Musman3276a272015-03-21 10:12:56 +00009640 // Build var to save the step value.
9641 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009642 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009643 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009644 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009645 ExprResult CalcStep =
9646 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009647 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009648
Alexander Musman8dba6642014-04-22 13:09:42 +00009649 // Warn about zero linear step (it would be probably better specified as
9650 // making corresponding variables 'const').
9651 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009652 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9653 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009654 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9655 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009656 if (!IsConstant && CalcStep.isUsable()) {
9657 // Calculate the step beforehand instead of doing this on each iteration.
9658 // (This is not used if the number of iterations may be kfold-ed).
9659 CalcStepExpr = CalcStep.get();
9660 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009661 }
9662
Alexey Bataev182227b2015-08-20 10:54:39 +00009663 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9664 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009665 StepExpr, CalcStepExpr,
9666 buildPreInits(Context, ExprCaptures),
9667 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009668}
9669
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009670static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9671 Expr *NumIterations, Sema &SemaRef,
9672 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009673 // Walk the vars and build update/final expressions for the CodeGen.
9674 SmallVector<Expr *, 8> Updates;
9675 SmallVector<Expr *, 8> Finals;
9676 Expr *Step = Clause.getStep();
9677 Expr *CalcStep = Clause.getCalcStep();
9678 // OpenMP [2.14.3.7, linear clause]
9679 // If linear-step is not specified it is assumed to be 1.
9680 if (Step == nullptr)
9681 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009682 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009683 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009684 }
Alexander Musman3276a272015-03-21 10:12:56 +00009685 bool HasErrors = false;
9686 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009687 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009688 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009689 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009690 SourceLocation ELoc;
9691 SourceRange ERange;
9692 Expr *SimpleRefExpr = RefExpr;
9693 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9694 /*AllowArraySection=*/false);
9695 ValueDecl *D = Res.first;
9696 if (Res.second || !D) {
9697 Updates.push_back(nullptr);
9698 Finals.push_back(nullptr);
9699 HasErrors = true;
9700 continue;
9701 }
9702 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9703 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9704 ->getMemberDecl();
9705 }
9706 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009707 Expr *InitExpr = *CurInit;
9708
9709 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009710 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009711 Expr *CapturedRef;
9712 if (LinKind == OMPC_LINEAR_uval)
9713 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9714 else
9715 CapturedRef =
9716 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9717 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9718 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009719
9720 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009721 ExprResult Update;
9722 if (!Info.first) {
9723 Update =
9724 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9725 InitExpr, IV, Step, /* Subtract */ false);
9726 } else
9727 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009728 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9729 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009730
9731 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009732 ExprResult Final;
9733 if (!Info.first) {
9734 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9735 InitExpr, NumIterations, Step,
9736 /* Subtract */ false);
9737 } else
9738 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009739 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9740 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009741
Alexander Musman3276a272015-03-21 10:12:56 +00009742 if (!Update.isUsable() || !Final.isUsable()) {
9743 Updates.push_back(nullptr);
9744 Finals.push_back(nullptr);
9745 HasErrors = true;
9746 } else {
9747 Updates.push_back(Update.get());
9748 Finals.push_back(Final.get());
9749 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009750 ++CurInit;
9751 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009752 }
9753 Clause.setUpdates(Updates);
9754 Clause.setFinals(Finals);
9755 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009756}
9757
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009758OMPClause *Sema::ActOnOpenMPAlignedClause(
9759 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9760 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9761
9762 SmallVector<Expr *, 8> Vars;
9763 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009764 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9765 SourceLocation ELoc;
9766 SourceRange ERange;
9767 Expr *SimpleRefExpr = RefExpr;
9768 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9769 /*AllowArraySection=*/false);
9770 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009771 // It will be analyzed later.
9772 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009773 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009774 ValueDecl *D = Res.first;
9775 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009776 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009777
Alexey Bataev1efd1662016-03-29 10:59:56 +00009778 QualType QType = D->getType();
9779 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009780
9781 // OpenMP [2.8.1, simd construct, Restrictions]
9782 // The type of list items appearing in the aligned clause must be
9783 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009784 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009785 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009786 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009787 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009788 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009789 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009790 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009791 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009792 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009793 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009794 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009795 continue;
9796 }
9797
9798 // OpenMP [2.8.1, simd construct, Restrictions]
9799 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009800 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009801 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009802 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9803 << getOpenMPClauseName(OMPC_aligned);
9804 continue;
9805 }
9806
Alexey Bataev1efd1662016-03-29 10:59:56 +00009807 DeclRefExpr *Ref = nullptr;
9808 if (!VD && IsOpenMPCapturedDecl(D))
9809 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9810 Vars.push_back(DefaultFunctionArrayConversion(
9811 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9812 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009813 }
9814
9815 // OpenMP [2.8.1, simd construct, Description]
9816 // The parameter of the aligned clause, alignment, must be a constant
9817 // positive integer expression.
9818 // If no optional parameter is specified, implementation-defined default
9819 // alignments for SIMD instructions on the target platforms are assumed.
9820 if (Alignment != nullptr) {
9821 ExprResult AlignResult =
9822 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9823 if (AlignResult.isInvalid())
9824 return nullptr;
9825 Alignment = AlignResult.get();
9826 }
9827 if (Vars.empty())
9828 return nullptr;
9829
9830 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9831 EndLoc, Vars, Alignment);
9832}
9833
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009834OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9835 SourceLocation StartLoc,
9836 SourceLocation LParenLoc,
9837 SourceLocation EndLoc) {
9838 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009839 SmallVector<Expr *, 8> SrcExprs;
9840 SmallVector<Expr *, 8> DstExprs;
9841 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009842 for (auto &RefExpr : VarList) {
9843 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9844 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009845 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009846 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009847 SrcExprs.push_back(nullptr);
9848 DstExprs.push_back(nullptr);
9849 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009850 continue;
9851 }
9852
Alexey Bataeved09d242014-05-28 05:53:51 +00009853 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009854 // OpenMP [2.1, C/C++]
9855 // A list item is a variable name.
9856 // OpenMP [2.14.4.1, Restrictions, p.1]
9857 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009858 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009859 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009860 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9861 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009862 continue;
9863 }
9864
9865 Decl *D = DE->getDecl();
9866 VarDecl *VD = cast<VarDecl>(D);
9867
9868 QualType Type = VD->getType();
9869 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9870 // It will be analyzed later.
9871 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009872 SrcExprs.push_back(nullptr);
9873 DstExprs.push_back(nullptr);
9874 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009875 continue;
9876 }
9877
9878 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9879 // A list item that appears in a copyin clause must be threadprivate.
9880 if (!DSAStack->isThreadPrivate(VD)) {
9881 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009882 << getOpenMPClauseName(OMPC_copyin)
9883 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009884 continue;
9885 }
9886
9887 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9888 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009889 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009890 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009891 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009892 auto *SrcVD =
9893 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9894 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009895 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009896 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9897 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009898 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9899 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009900 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009901 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009902 // For arrays generate assignment operation for single element and replace
9903 // it by the original array element in CodeGen.
9904 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9905 PseudoDstExpr, PseudoSrcExpr);
9906 if (AssignmentOp.isInvalid())
9907 continue;
9908 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9909 /*DiscardedValue=*/true);
9910 if (AssignmentOp.isInvalid())
9911 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009912
9913 DSAStack->addDSA(VD, DE, OMPC_copyin);
9914 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009915 SrcExprs.push_back(PseudoSrcExpr);
9916 DstExprs.push_back(PseudoDstExpr);
9917 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009918 }
9919
Alexey Bataeved09d242014-05-28 05:53:51 +00009920 if (Vars.empty())
9921 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009922
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009923 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9924 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009925}
9926
Alexey Bataevbae9a792014-06-27 10:37:06 +00009927OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9928 SourceLocation StartLoc,
9929 SourceLocation LParenLoc,
9930 SourceLocation EndLoc) {
9931 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009932 SmallVector<Expr *, 8> SrcExprs;
9933 SmallVector<Expr *, 8> DstExprs;
9934 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009935 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009936 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9937 SourceLocation ELoc;
9938 SourceRange ERange;
9939 Expr *SimpleRefExpr = RefExpr;
9940 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9941 /*AllowArraySection=*/false);
9942 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009943 // It will be analyzed later.
9944 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009945 SrcExprs.push_back(nullptr);
9946 DstExprs.push_back(nullptr);
9947 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009948 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009949 ValueDecl *D = Res.first;
9950 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009951 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009952
Alexey Bataeve122da12016-03-17 10:50:17 +00009953 QualType Type = D->getType();
9954 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009955
9956 // OpenMP [2.14.4.2, Restrictions, p.2]
9957 // A list item that appears in a copyprivate clause may not appear in a
9958 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009959 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9960 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009961 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9962 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009963 Diag(ELoc, diag::err_omp_wrong_dsa)
9964 << getOpenMPClauseName(DVar.CKind)
9965 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009966 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009967 continue;
9968 }
9969
9970 // OpenMP [2.11.4.2, Restrictions, p.1]
9971 // All list items that appear in a copyprivate clause must be either
9972 // threadprivate or private in the enclosing context.
9973 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009974 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009975 if (DVar.CKind == OMPC_shared) {
9976 Diag(ELoc, diag::err_omp_required_access)
9977 << getOpenMPClauseName(OMPC_copyprivate)
9978 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009979 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009980 continue;
9981 }
9982 }
9983 }
9984
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009985 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009986 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009987 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009988 << getOpenMPClauseName(OMPC_copyprivate) << Type
9989 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009990 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009991 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009992 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009993 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009994 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009995 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009996 continue;
9997 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009998
Alexey Bataevbae9a792014-06-27 10:37:06 +00009999 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10000 // A variable of class type (or array thereof) that appears in a
10001 // copyin clause requires an accessible, unambiguous copy assignment
10002 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010003 Type = Context.getBaseElementType(Type.getNonReferenceType())
10004 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +000010005 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010006 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10007 D->hasAttrs() ? &D->getAttrs() : nullptr);
10008 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010009 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +000010010 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10011 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +000010012 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +000010013 buildDeclRefExpr(*this, DstVD, Type, ELoc);
10014 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010015 PseudoDstExpr, PseudoSrcExpr);
10016 if (AssignmentOp.isInvalid())
10017 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000010018 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000010019 /*DiscardedValue=*/true);
10020 if (AssignmentOp.isInvalid())
10021 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000010022
10023 // No need to mark vars as copyprivate, they are already threadprivate or
10024 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +000010025 assert(VD || IsOpenMPCapturedDecl(D));
10026 Vars.push_back(
10027 VD ? RefExpr->IgnoreParens()
10028 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000010029 SrcExprs.push_back(PseudoSrcExpr);
10030 DstExprs.push_back(PseudoDstExpr);
10031 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000010032 }
10033
10034 if (Vars.empty())
10035 return nullptr;
10036
Alexey Bataeva63048e2015-03-23 06:18:07 +000010037 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10038 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000010039}
10040
Alexey Bataev6125da92014-07-21 11:26:11 +000010041OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10042 SourceLocation StartLoc,
10043 SourceLocation LParenLoc,
10044 SourceLocation EndLoc) {
10045 if (VarList.empty())
10046 return nullptr;
10047
10048 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10049}
Alexey Bataevdea47612014-07-23 07:46:59 +000010050
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010051OMPClause *
10052Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10053 SourceLocation DepLoc, SourceLocation ColonLoc,
10054 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10055 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010056 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010057 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000010058 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010059 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000010060 return nullptr;
10061 }
10062 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010063 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10064 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000010065 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010066 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000010067 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10068 /*Last=*/OMPC_DEPEND_unknown, Except)
10069 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010070 return nullptr;
10071 }
10072 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000010073 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010074 llvm::APSInt DepCounter(/*BitWidth=*/32);
10075 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10076 if (DepKind == OMPC_DEPEND_sink) {
10077 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10078 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10079 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010080 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010081 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010082 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10083 DSAStack->getParentOrderedRegionParam()) {
10084 for (auto &RefExpr : VarList) {
10085 assert(RefExpr && "NULL expr in OpenMP shared clause.");
Alexey Bataev8b427062016-05-25 12:36:08 +000010086 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010087 // It will be analyzed later.
10088 Vars.push_back(RefExpr);
10089 continue;
10090 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010091
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010092 SourceLocation ELoc = RefExpr->getExprLoc();
10093 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10094 if (DepKind == OMPC_DEPEND_sink) {
10095 if (DepCounter >= TotalDepCount) {
10096 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10097 continue;
10098 }
10099 ++DepCounter;
10100 // OpenMP [2.13.9, Summary]
10101 // depend(dependence-type : vec), where dependence-type is:
10102 // 'sink' and where vec is the iteration vector, which has the form:
10103 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10104 // where n is the value specified by the ordered clause in the loop
10105 // directive, xi denotes the loop iteration variable of the i-th nested
10106 // loop associated with the loop directive, and di is a constant
10107 // non-negative integer.
Alexey Bataev8b427062016-05-25 12:36:08 +000010108 if (CurContext->isDependentContext()) {
10109 // It will be analyzed later.
10110 Vars.push_back(RefExpr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010111 continue;
10112 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010113 SimpleExpr = SimpleExpr->IgnoreImplicit();
10114 OverloadedOperatorKind OOK = OO_None;
10115 SourceLocation OOLoc;
10116 Expr *LHS = SimpleExpr;
10117 Expr *RHS = nullptr;
10118 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10119 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10120 OOLoc = BO->getOperatorLoc();
10121 LHS = BO->getLHS()->IgnoreParenImpCasts();
10122 RHS = BO->getRHS()->IgnoreParenImpCasts();
10123 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10124 OOK = OCE->getOperator();
10125 OOLoc = OCE->getOperatorLoc();
10126 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10127 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10128 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10129 OOK = MCE->getMethodDecl()
10130 ->getNameInfo()
10131 .getName()
10132 .getCXXOverloadedOperator();
10133 OOLoc = MCE->getCallee()->getExprLoc();
10134 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10135 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10136 }
10137 SourceLocation ELoc;
10138 SourceRange ERange;
10139 auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10140 /*AllowArraySection=*/false);
10141 if (Res.second) {
10142 // It will be analyzed later.
10143 Vars.push_back(RefExpr);
10144 }
10145 ValueDecl *D = Res.first;
10146 if (!D)
10147 continue;
10148
10149 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10150 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10151 continue;
10152 }
10153 if (RHS) {
10154 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10155 RHS, OMPC_depend, /*StrictlyPositive=*/false);
10156 if (RHSRes.isInvalid())
10157 continue;
10158 }
10159 if (!CurContext->isDependentContext() &&
10160 DSAStack->getParentOrderedRegionParam() &&
10161 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10162 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10163 << DSAStack->getParentLoopControlVariable(
10164 DepCounter.getZExtValue());
10165 continue;
10166 }
10167 OpsOffs.push_back({RHS, OOK});
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010168 } else {
10169 // OpenMP [2.11.1.1, Restrictions, p.3]
10170 // A variable that is part of another variable (such as a field of a
10171 // structure) but is not an array element or an array section cannot
10172 // appear in a depend clause.
10173 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10174 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10175 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10176 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10177 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +000010178 (ASE &&
10179 !ASE->getBase()
10180 ->getType()
10181 .getNonReferenceType()
10182 ->isPointerType() &&
10183 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010184 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10185 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010186 continue;
10187 }
10188 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000010189 Vars.push_back(RefExpr->IgnoreParenImpCasts());
10190 }
10191
10192 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10193 TotalDepCount > VarList.size() &&
10194 DSAStack->getParentOrderedRegionParam()) {
10195 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10196 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10197 }
10198 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10199 Vars.empty())
10200 return nullptr;
10201 }
Alexey Bataev8b427062016-05-25 12:36:08 +000010202 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10203 DepKind, DepLoc, ColonLoc, Vars);
10204 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10205 DSAStack->addDoacrossDependClause(C, OpsOffs);
10206 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000010207}
Michael Wonge710d542015-08-07 16:16:36 +000010208
10209OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10210 SourceLocation LParenLoc,
10211 SourceLocation EndLoc) {
10212 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +000010213
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010214 // OpenMP [2.9.1, Restrictions]
10215 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010216 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10217 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010218 return nullptr;
10219
Michael Wonge710d542015-08-07 16:16:36 +000010220 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10221}
Kelvin Li0bff7af2015-11-23 05:32:03 +000010222
10223static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10224 DSAStackTy *Stack, CXXRecordDecl *RD) {
10225 if (!RD || RD->isInvalidDecl())
10226 return true;
10227
Alexey Bataevc9bd03d2015-12-17 06:55:08 +000010228 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10229 if (auto *CTD = CTSD->getSpecializedTemplate())
10230 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010231 auto QTy = SemaRef.Context.getRecordType(RD);
10232 if (RD->isDynamicClass()) {
10233 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10234 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10235 return false;
10236 }
10237 auto *DC = RD;
10238 bool IsCorrect = true;
10239 for (auto *I : DC->decls()) {
10240 if (I) {
10241 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10242 if (MD->isStatic()) {
10243 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10244 SemaRef.Diag(MD->getLocation(),
10245 diag::note_omp_static_member_in_target);
10246 IsCorrect = false;
10247 }
10248 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10249 if (VD->isStaticDataMember()) {
10250 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10251 SemaRef.Diag(VD->getLocation(),
10252 diag::note_omp_static_member_in_target);
10253 IsCorrect = false;
10254 }
10255 }
10256 }
10257 }
10258
10259 for (auto &I : RD->bases()) {
10260 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10261 I.getType()->getAsCXXRecordDecl()))
10262 IsCorrect = false;
10263 }
10264 return IsCorrect;
10265}
10266
10267static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10268 DSAStackTy *Stack, QualType QTy) {
10269 NamedDecl *ND;
10270 if (QTy->isIncompleteType(&ND)) {
10271 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10272 return false;
10273 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10274 if (!RD->isInvalidDecl() &&
10275 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10276 return false;
10277 }
10278 return true;
10279}
10280
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010281/// \brief Return true if it can be proven that the provided array expression
10282/// (array section or array subscript) does NOT specify the whole size of the
10283/// array whose base type is \a BaseQTy.
10284static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10285 const Expr *E,
10286 QualType BaseQTy) {
10287 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10288
10289 // If this is an array subscript, it refers to the whole size if the size of
10290 // the dimension is constant and equals 1. Also, an array section assumes the
10291 // format of an array subscript if no colon is used.
10292 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10293 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10294 return ATy->getSize().getSExtValue() != 1;
10295 // Size can't be evaluated statically.
10296 return false;
10297 }
10298
10299 assert(OASE && "Expecting array section if not an array subscript.");
10300 auto *LowerBound = OASE->getLowerBound();
10301 auto *Length = OASE->getLength();
10302
10303 // If there is a lower bound that does not evaluates to zero, we are not
10304 // convering the whole dimension.
10305 if (LowerBound) {
10306 llvm::APSInt ConstLowerBound;
10307 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10308 return false; // Can't get the integer value as a constant.
10309 if (ConstLowerBound.getSExtValue())
10310 return true;
10311 }
10312
10313 // If we don't have a length we covering the whole dimension.
10314 if (!Length)
10315 return false;
10316
10317 // If the base is a pointer, we don't have a way to get the size of the
10318 // pointee.
10319 if (BaseQTy->isPointerType())
10320 return false;
10321
10322 // We can only check if the length is the same as the size of the dimension
10323 // if we have a constant array.
10324 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10325 if (!CATy)
10326 return false;
10327
10328 llvm::APSInt ConstLength;
10329 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10330 return false; // Can't get the integer value as a constant.
10331
10332 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10333}
10334
10335// Return true if it can be proven that the provided array expression (array
10336// section or array subscript) does NOT specify a single element of the array
10337// whose base type is \a BaseQTy.
10338static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10339 const Expr *E,
10340 QualType BaseQTy) {
10341 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10342
10343 // An array subscript always refer to a single element. Also, an array section
10344 // assumes the format of an array subscript if no colon is used.
10345 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10346 return false;
10347
10348 assert(OASE && "Expecting array section if not an array subscript.");
10349 auto *Length = OASE->getLength();
10350
10351 // If we don't have a length we have to check if the array has unitary size
10352 // for this dimension. Also, we should always expect a length if the base type
10353 // is pointer.
10354 if (!Length) {
10355 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10356 return ATy->getSize().getSExtValue() != 1;
10357 // We cannot assume anything.
10358 return false;
10359 }
10360
10361 // Check if the length evaluates to 1.
10362 llvm::APSInt ConstLength;
10363 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10364 return false; // Can't get the integer value as a constant.
10365
10366 return ConstLength.getSExtValue() != 1;
10367}
10368
Samuel Antao661c0902016-05-26 17:39:58 +000010369// Return the expression of the base of the mappable expression or null if it
10370// cannot be determined and do all the necessary checks to see if the expression
10371// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000010372// components of the expression.
10373static Expr *CheckMapClauseExpressionBase(
10374 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000010375 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10376 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010377 SourceLocation ELoc = E->getExprLoc();
10378 SourceRange ERange = E->getSourceRange();
10379
10380 // The base of elements of list in a map clause have to be either:
10381 // - a reference to variable or field.
10382 // - a member expression.
10383 // - an array expression.
10384 //
10385 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10386 // reference to 'r'.
10387 //
10388 // If we have:
10389 //
10390 // struct SS {
10391 // Bla S;
10392 // foo() {
10393 // #pragma omp target map (S.Arr[:12]);
10394 // }
10395 // }
10396 //
10397 // We want to retrieve the member expression 'this->S';
10398
10399 Expr *RelevantExpr = nullptr;
10400
Samuel Antao5de996e2016-01-22 20:21:36 +000010401 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10402 // If a list item is an array section, it must specify contiguous storage.
10403 //
10404 // For this restriction it is sufficient that we make sure only references
10405 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010406 // exist except in the rightmost expression (unless they cover the whole
10407 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000010408 //
10409 // r.ArrS[3:5].Arr[6:7]
10410 //
10411 // r.ArrS[3:5].x
10412 //
10413 // but these would be valid:
10414 // r.ArrS[3].Arr[6:7]
10415 //
10416 // r.ArrS[3].x
10417
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010418 bool AllowUnitySizeArraySection = true;
10419 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000010420
Dmitry Polukhin644a9252016-03-11 07:58:34 +000010421 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010422 E = E->IgnoreParenImpCasts();
10423
10424 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10425 if (!isa<VarDecl>(CurE->getDecl()))
10426 break;
10427
10428 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010429
10430 // If we got a reference to a declaration, we should not expect any array
10431 // section before that.
10432 AllowUnitySizeArraySection = false;
10433 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010434
10435 // Record the component.
10436 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10437 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +000010438 continue;
10439 }
10440
10441 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10442 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10443
10444 if (isa<CXXThisExpr>(BaseE))
10445 // We found a base expression: this->Val.
10446 RelevantExpr = CurE;
10447 else
10448 E = BaseE;
10449
10450 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10451 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10452 << CurE->getSourceRange();
10453 break;
10454 }
10455
10456 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10457
10458 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10459 // A bit-field cannot appear in a map clause.
10460 //
10461 if (FD->isBitField()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010462 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10463 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010464 break;
10465 }
10466
10467 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10468 // If the type of a list item is a reference to a type T then the type
10469 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010470 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010471
10472 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10473 // A list item cannot be a variable that is a member of a structure with
10474 // a union type.
10475 //
10476 if (auto *RT = CurType->getAs<RecordType>())
10477 if (RT->isUnionType()) {
10478 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10479 << CurE->getSourceRange();
10480 break;
10481 }
10482
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010483 // If we got a member expression, we should not expect any array section
10484 // before that:
10485 //
10486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10487 // If a list item is an element of a structure, only the rightmost symbol
10488 // of the variable reference can be an array section.
10489 //
10490 AllowUnitySizeArraySection = false;
10491 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010492
10493 // Record the component.
10494 CurComponents.push_back(
10495 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +000010496 continue;
10497 }
10498
10499 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10500 E = CurE->getBase()->IgnoreParenImpCasts();
10501
10502 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10503 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10504 << 0 << CurE->getSourceRange();
10505 break;
10506 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010507
10508 // If we got an array subscript that express the whole dimension we
10509 // can have any array expressions before. If it only expressing part of
10510 // the dimension, we can only have unitary-size array expressions.
10511 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10512 E->getType()))
10513 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000010514
10515 // Record the component - we don't have any declaration associated.
10516 CurComponents.push_back(
10517 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010518 continue;
10519 }
10520
10521 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010522 E = CurE->getBase()->IgnoreParenImpCasts();
10523
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010524 auto CurType =
10525 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10526
Samuel Antao5de996e2016-01-22 20:21:36 +000010527 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10528 // If the type of a list item is a reference to a type T then the type
10529 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000010530 if (CurType->isReferenceType())
10531 CurType = CurType->getPointeeType();
10532
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010533 bool IsPointer = CurType->isAnyPointerType();
10534
10535 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010536 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10537 << 0 << CurE->getSourceRange();
10538 break;
10539 }
10540
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000010541 bool NotWhole =
10542 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10543 bool NotUnity =
10544 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10545
10546 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10547 // Any array section is currently allowed.
10548 //
10549 // If this array section refers to the whole dimension we can still
10550 // accept other array sections before this one, except if the base is a
10551 // pointer. Otherwise, only unitary sections are accepted.
10552 if (NotWhole || IsPointer)
10553 AllowWholeSizeArraySection = false;
10554 } else if ((AllowUnitySizeArraySection && NotUnity) ||
10555 (AllowWholeSizeArraySection && NotWhole)) {
10556 // A unity or whole array section is not allowed and that is not
10557 // compatible with the properties of the current array section.
10558 SemaRef.Diag(
10559 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10560 << CurE->getSourceRange();
10561 break;
10562 }
Samuel Antao90927002016-04-26 14:54:23 +000010563
10564 // Record the component - we don't have any declaration associated.
10565 CurComponents.push_back(
10566 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +000010567 continue;
10568 }
10569
10570 // If nothing else worked, this is not a valid map clause expression.
10571 SemaRef.Diag(ELoc,
10572 diag::err_omp_expected_named_var_member_or_array_expression)
10573 << ERange;
10574 break;
10575 }
10576
10577 return RelevantExpr;
10578}
10579
10580// Return true if expression E associated with value VD has conflicts with other
10581// map information.
Samuel Antao90927002016-04-26 14:54:23 +000010582static bool CheckMapConflicts(
10583 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10584 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000010585 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10586 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010587 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000010588 SourceLocation ELoc = E->getExprLoc();
10589 SourceRange ERange = E->getSourceRange();
10590
10591 // In order to easily check the conflicts we need to match each component of
10592 // the expression under test with the components of the expressions that are
10593 // already in the stack.
10594
Samuel Antao5de996e2016-01-22 20:21:36 +000010595 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010596 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010597 "Map clause expression with unexpected base!");
10598
10599 // Variables to help detecting enclosing problems in data environment nests.
10600 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000010601 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000010602
Samuel Antao90927002016-04-26 14:54:23 +000010603 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10604 VD, CurrentRegionOnly,
10605 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10606 StackComponents) -> bool {
10607
Samuel Antao5de996e2016-01-22 20:21:36 +000010608 assert(!StackComponents.empty() &&
10609 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000010610 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000010611 "Map clause expression with unexpected base!");
10612
Samuel Antao90927002016-04-26 14:54:23 +000010613 // The whole expression in the stack.
10614 auto *RE = StackComponents.front().getAssociatedExpression();
10615
Samuel Antao5de996e2016-01-22 20:21:36 +000010616 // Expressions must start from the same base. Here we detect at which
10617 // point both expressions diverge from each other and see if we can
10618 // detect if the memory referred to both expressions is contiguous and
10619 // do not overlap.
10620 auto CI = CurComponents.rbegin();
10621 auto CE = CurComponents.rend();
10622 auto SI = StackComponents.rbegin();
10623 auto SE = StackComponents.rend();
10624 for (; CI != CE && SI != SE; ++CI, ++SI) {
10625
10626 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10627 // At most one list item can be an array item derived from a given
10628 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010629 if (CurrentRegionOnly &&
10630 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10631 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10632 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10633 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10634 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010635 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010636 << CI->getAssociatedExpression()->getSourceRange();
10637 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10638 diag::note_used_here)
10639 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010640 return true;
10641 }
10642
10643 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010644 if (CI->getAssociatedExpression()->getStmtClass() !=
10645 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010646 break;
10647
10648 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010649 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010650 break;
10651 }
10652
10653 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10654 // List items of map clauses in the same construct must not share
10655 // original storage.
10656 //
10657 // If the expressions are exactly the same or one is a subset of the
10658 // other, it means they are sharing storage.
10659 if (CI == CE && SI == SE) {
10660 if (CurrentRegionOnly) {
Samuel Antao661c0902016-05-26 17:39:58 +000010661 if (CKind == OMPC_map)
10662 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10663 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010664 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010665 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10666 << ERange;
10667 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010668 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10669 << RE->getSourceRange();
10670 return true;
10671 } else {
10672 // If we find the same expression in the enclosing data environment,
10673 // that is legal.
10674 IsEnclosedByDataEnvironmentExpr = true;
10675 return false;
10676 }
10677 }
10678
Samuel Antao90927002016-04-26 14:54:23 +000010679 QualType DerivedType =
10680 std::prev(CI)->getAssociatedDeclaration()->getType();
10681 SourceLocation DerivedLoc =
10682 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010683
10684 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10685 // If the type of a list item is a reference to a type T then the type
10686 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010687 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010688
10689 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10690 // A variable for which the type is pointer and an array section
10691 // derived from that variable must not appear as list items of map
10692 // clauses of the same construct.
10693 //
10694 // Also, cover one of the cases in:
10695 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10696 // If any part of the original storage of a list item has corresponding
10697 // storage in the device data environment, all of the original storage
10698 // must have corresponding storage in the device data environment.
10699 //
10700 if (DerivedType->isAnyPointerType()) {
10701 if (CI == CE || SI == SE) {
10702 SemaRef.Diag(
10703 DerivedLoc,
10704 diag::err_omp_pointer_mapped_along_with_derived_section)
10705 << DerivedLoc;
10706 } else {
10707 assert(CI != CE && SI != SE);
10708 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10709 << DerivedLoc;
10710 }
10711 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10712 << RE->getSourceRange();
10713 return true;
10714 }
10715
10716 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10717 // List items of map clauses in the same construct must not share
10718 // original storage.
10719 //
10720 // An expression is a subset of the other.
10721 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Samuel Antao661c0902016-05-26 17:39:58 +000010722 if (CKind == OMPC_map)
10723 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10724 else {
Samuel Antaoec172c62016-05-26 17:49:04 +000010725 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000010726 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10727 << ERange;
10728 }
Samuel Antao5de996e2016-01-22 20:21:36 +000010729 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10730 << RE->getSourceRange();
10731 return true;
10732 }
10733
10734 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010735 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010736 if (!CurrentRegionOnly && SI != SE)
10737 EnclosingExpr = RE;
10738
10739 // The current expression is a subset of the expression in the data
10740 // environment.
10741 IsEnclosedByDataEnvironmentExpr |=
10742 (!CurrentRegionOnly && CI != CE && SI == SE);
10743
10744 return false;
10745 });
10746
10747 if (CurrentRegionOnly)
10748 return FoundError;
10749
10750 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10751 // If any part of the original storage of a list item has corresponding
10752 // storage in the device data environment, all of the original storage must
10753 // have corresponding storage in the device data environment.
10754 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10755 // If a list item is an element of a structure, and a different element of
10756 // the structure has a corresponding list item in the device data environment
10757 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010758 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010759 // data environment prior to the task encountering the construct.
10760 //
10761 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10762 SemaRef.Diag(ELoc,
10763 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10764 << ERange;
10765 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10766 << EnclosingExpr->getSourceRange();
10767 return true;
10768 }
10769
10770 return FoundError;
10771}
10772
Samuel Antao661c0902016-05-26 17:39:58 +000010773namespace {
10774// Utility struct that gathers all the related lists associated with a mappable
10775// expression.
10776struct MappableVarListInfo final {
10777 // The list of expressions.
10778 ArrayRef<Expr *> VarList;
10779 // The list of processed expressions.
10780 SmallVector<Expr *, 16> ProcessedVarList;
10781 // The mappble components for each expression.
10782 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10783 // The base declaration of the variable.
10784 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10785
10786 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10787 // We have a list of components and base declarations for each entry in the
10788 // variable list.
10789 VarComponents.reserve(VarList.size());
10790 VarBaseDeclarations.reserve(VarList.size());
10791 }
10792};
10793}
10794
10795// Check the validity of the provided variable list for the provided clause kind
10796// \a CKind. In the check process the valid expressions, and mappable expression
10797// components and variables are extracted and used to fill \a Vars,
10798// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10799// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10800static void
10801checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10802 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10803 SourceLocation StartLoc,
10804 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10805 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010806 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10807 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000010808 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010809
Samuel Antao90927002016-04-26 14:54:23 +000010810 // Keep track of the mappable components and base declarations in this clause.
10811 // Each entry in the list is going to have a list of components associated. We
10812 // record each set of the components so that we can build the clause later on.
10813 // In the end we should have the same amount of declarations and component
10814 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000010815
Samuel Antao661c0902016-05-26 17:39:58 +000010816 for (auto &RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000010817 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010818 SourceLocation ELoc = RE->getExprLoc();
10819
Kelvin Li0bff7af2015-11-23 05:32:03 +000010820 auto *VE = RE->IgnoreParenLValueCasts();
10821
10822 if (VE->isValueDependent() || VE->isTypeDependent() ||
10823 VE->isInstantiationDependent() ||
10824 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010825 // We can only analyze this information once the missing information is
10826 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000010827 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010828 continue;
10829 }
10830
10831 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010832
Samuel Antao5de996e2016-01-22 20:21:36 +000010833 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000010834 SemaRef.Diag(ELoc,
10835 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000010836 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010837 continue;
10838 }
10839
Samuel Antao90927002016-04-26 14:54:23 +000010840 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10841 ValueDecl *CurDeclaration = nullptr;
10842
10843 // Obtain the array or member expression bases if required. Also, fill the
10844 // components array with all the components identified in the process.
Samuel Antao661c0902016-05-26 17:39:58 +000010845 auto *BE =
10846 CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010847 if (!BE)
10848 continue;
10849
Samuel Antao90927002016-04-26 14:54:23 +000010850 assert(!CurComponents.empty() &&
10851 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010852
Samuel Antao90927002016-04-26 14:54:23 +000010853 // For the following checks, we rely on the base declaration which is
10854 // expected to be associated with the last component. The declaration is
10855 // expected to be a variable or a field (if 'this' is being mapped).
10856 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10857 assert(CurDeclaration && "Null decl on map clause.");
10858 assert(
10859 CurDeclaration->isCanonicalDecl() &&
10860 "Expecting components to have associated only canonical declarations.");
10861
10862 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10863 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010864
10865 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010866 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010867
10868 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000010869 // threadprivate variables cannot appear in a map clause.
10870 // OpenMP 4.5 [2.10.5, target update Construct]
10871 // threadprivate variables cannot appear in a from clause.
10872 if (VD && DSAS->isThreadPrivate(VD)) {
10873 auto DVar = DSAS->getTopDSA(VD, false);
10874 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10875 << getOpenMPClauseName(CKind);
10876 ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010877 continue;
10878 }
10879
Samuel Antao5de996e2016-01-22 20:21:36 +000010880 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10881 // A list item cannot appear in both a map clause and a data-sharing
10882 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010883
Samuel Antao5de996e2016-01-22 20:21:36 +000010884 // Check conflicts with other map clause expressions. We check the conflicts
10885 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000010886 // environment, because the restrictions are different. We only have to
10887 // check conflicts across regions for the map clauses.
10888 if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10889 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010890 break;
Samuel Antao661c0902016-05-26 17:39:58 +000010891 if (CKind == OMPC_map &&
10892 CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10893 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000010894 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010895
Samuel Antao661c0902016-05-26 17:39:58 +000010896 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000010897 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10898 // If the type of a list item is a reference to a type T then the type will
10899 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010900 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010901
Samuel Antao661c0902016-05-26 17:39:58 +000010902 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10903 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000010904 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010905 // A list item must have a mappable type.
Samuel Antao661c0902016-05-26 17:39:58 +000010906 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10907 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000010908 continue;
10909
Samuel Antao661c0902016-05-26 17:39:58 +000010910 if (CKind == OMPC_map) {
10911 // target enter data
10912 // OpenMP [2.10.2, Restrictions, p. 99]
10913 // A map-type must be specified in all map clauses and must be either
10914 // to or alloc.
10915 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10916 if (DKind == OMPD_target_enter_data &&
10917 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10918 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10919 << (IsMapTypeImplicit ? 1 : 0)
10920 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10921 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010922 continue;
10923 }
Samuel Antao661c0902016-05-26 17:39:58 +000010924
10925 // target exit_data
10926 // OpenMP [2.10.3, Restrictions, p. 102]
10927 // A map-type must be specified in all map clauses and must be either
10928 // from, release, or delete.
10929 if (DKind == OMPD_target_exit_data &&
10930 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10931 MapType == OMPC_MAP_delete)) {
10932 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10933 << (IsMapTypeImplicit ? 1 : 0)
10934 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10935 << getOpenMPDirectiveName(DKind);
10936 continue;
10937 }
10938
10939 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10940 // A list item cannot appear in both a map clause and a data-sharing
10941 // attribute clause on the same construct
10942 if (DKind == OMPD_target && VD) {
10943 auto DVar = DSAS->getTopDSA(VD, false);
10944 if (isOpenMPPrivate(DVar.CKind)) {
10945 SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10946 << getOpenMPClauseName(DVar.CKind)
10947 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
10948 ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
10949 continue;
10950 }
10951 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010952 }
10953
Samuel Antao90927002016-04-26 14:54:23 +000010954 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000010955 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010956
10957 // Store the components in the stack so that they can be used to check
10958 // against other clauses later on.
Samuel Antao661c0902016-05-26 17:39:58 +000010959 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
Samuel Antao90927002016-04-26 14:54:23 +000010960
10961 // Save the components and declaration to create the clause. For purposes of
10962 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000010963 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000010964 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
10965 MVLI.VarComponents.back().append(CurComponents.begin(),
10966 CurComponents.end());
10967 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10968 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010969 }
Samuel Antao661c0902016-05-26 17:39:58 +000010970}
10971
10972OMPClause *
10973Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10974 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10975 SourceLocation MapLoc, SourceLocation ColonLoc,
10976 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10977 SourceLocation LParenLoc, SourceLocation EndLoc) {
10978 MappableVarListInfo MVLI(VarList);
10979 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
10980 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010981
Samuel Antao5de996e2016-01-22 20:21:36 +000010982 // We need to produce a map clause even if we don't have variables so that
10983 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000010984 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10985 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
10986 MVLI.VarComponents, MapTypeModifier, MapType,
10987 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010988}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010989
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010990QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10991 TypeResult ParsedType) {
10992 assert(ParsedType.isUsable());
10993
10994 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10995 if (ReductionType.isNull())
10996 return QualType();
10997
10998 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10999 // A type name in a declare reduction directive cannot be a function type, an
11000 // array type, a reference type, or a type qualified with const, volatile or
11001 // restrict.
11002 if (ReductionType.hasQualifiers()) {
11003 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11004 return QualType();
11005 }
11006
11007 if (ReductionType->isFunctionType()) {
11008 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11009 return QualType();
11010 }
11011 if (ReductionType->isReferenceType()) {
11012 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11013 return QualType();
11014 }
11015 if (ReductionType->isArrayType()) {
11016 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11017 return QualType();
11018 }
11019 return ReductionType;
11020}
11021
11022Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11023 Scope *S, DeclContext *DC, DeclarationName Name,
11024 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11025 AccessSpecifier AS, Decl *PrevDeclInScope) {
11026 SmallVector<Decl *, 8> Decls;
11027 Decls.reserve(ReductionTypes.size());
11028
11029 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11030 ForRedeclaration);
11031 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11032 // A reduction-identifier may not be re-declared in the current scope for the
11033 // same type or for a type that is compatible according to the base language
11034 // rules.
11035 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11036 OMPDeclareReductionDecl *PrevDRD = nullptr;
11037 bool InCompoundScope = true;
11038 if (S != nullptr) {
11039 // Find previous declaration with the same name not referenced in other
11040 // declarations.
11041 FunctionScopeInfo *ParentFn = getEnclosingFunction();
11042 InCompoundScope =
11043 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11044 LookupName(Lookup, S);
11045 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11046 /*AllowInlineNamespace=*/false);
11047 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11048 auto Filter = Lookup.makeFilter();
11049 while (Filter.hasNext()) {
11050 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11051 if (InCompoundScope) {
11052 auto I = UsedAsPrevious.find(PrevDecl);
11053 if (I == UsedAsPrevious.end())
11054 UsedAsPrevious[PrevDecl] = false;
11055 if (auto *D = PrevDecl->getPrevDeclInScope())
11056 UsedAsPrevious[D] = true;
11057 }
11058 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11059 PrevDecl->getLocation();
11060 }
11061 Filter.done();
11062 if (InCompoundScope) {
11063 for (auto &PrevData : UsedAsPrevious) {
11064 if (!PrevData.second) {
11065 PrevDRD = PrevData.first;
11066 break;
11067 }
11068 }
11069 }
11070 } else if (PrevDeclInScope != nullptr) {
11071 auto *PrevDRDInScope = PrevDRD =
11072 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11073 do {
11074 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11075 PrevDRDInScope->getLocation();
11076 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11077 } while (PrevDRDInScope != nullptr);
11078 }
11079 for (auto &TyData : ReductionTypes) {
11080 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11081 bool Invalid = false;
11082 if (I != PreviousRedeclTypes.end()) {
11083 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11084 << TyData.first;
11085 Diag(I->second, diag::note_previous_definition);
11086 Invalid = true;
11087 }
11088 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11089 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11090 Name, TyData.first, PrevDRD);
11091 DC->addDecl(DRD);
11092 DRD->setAccess(AS);
11093 Decls.push_back(DRD);
11094 if (Invalid)
11095 DRD->setInvalidDecl();
11096 else
11097 PrevDRD = DRD;
11098 }
11099
11100 return DeclGroupPtrTy::make(
11101 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11102}
11103
11104void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11105 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11106
11107 // Enter new function scope.
11108 PushFunctionScope();
11109 getCurFunction()->setHasBranchProtectedScope();
11110 getCurFunction()->setHasOMPDeclareReductionCombiner();
11111
11112 if (S != nullptr)
11113 PushDeclContext(S, DRD);
11114 else
11115 CurContext = DRD;
11116
11117 PushExpressionEvaluationContext(PotentiallyEvaluated);
11118
11119 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011120 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11121 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11122 // uses semantics of argument handles by value, but it should be passed by
11123 // reference. C lang does not support references, so pass all parameters as
11124 // pointers.
11125 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011126 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011127 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011128 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11129 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11130 // uses semantics of argument handles by value, but it should be passed by
11131 // reference. C lang does not support references, so pass all parameters as
11132 // pointers.
11133 // Create 'T omp_out;' variable.
11134 auto *OmpOutParm =
11135 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11136 if (S != nullptr) {
11137 PushOnScopeChains(OmpInParm, S);
11138 PushOnScopeChains(OmpOutParm, S);
11139 } else {
11140 DRD->addDecl(OmpInParm);
11141 DRD->addDecl(OmpOutParm);
11142 }
11143}
11144
11145void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11146 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11147 DiscardCleanupsInEvaluationContext();
11148 PopExpressionEvaluationContext();
11149
11150 PopDeclContext();
11151 PopFunctionScopeInfo();
11152
11153 if (Combiner != nullptr)
11154 DRD->setCombiner(Combiner);
11155 else
11156 DRD->setInvalidDecl();
11157}
11158
11159void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11160 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11161
11162 // Enter new function scope.
11163 PushFunctionScope();
11164 getCurFunction()->setHasBranchProtectedScope();
11165
11166 if (S != nullptr)
11167 PushDeclContext(S, DRD);
11168 else
11169 CurContext = DRD;
11170
11171 PushExpressionEvaluationContext(PotentiallyEvaluated);
11172
11173 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011174 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11175 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11176 // uses semantics of argument handles by value, but it should be passed by
11177 // reference. C lang does not support references, so pass all parameters as
11178 // pointers.
11179 // Create 'T omp_priv;' variable.
11180 auto *OmpPrivParm =
11181 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011182 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11183 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11184 // uses semantics of argument handles by value, but it should be passed by
11185 // reference. C lang does not support references, so pass all parameters as
11186 // pointers.
11187 // Create 'T omp_orig;' variable.
11188 auto *OmpOrigParm =
11189 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000011190 if (S != nullptr) {
11191 PushOnScopeChains(OmpPrivParm, S);
11192 PushOnScopeChains(OmpOrigParm, S);
11193 } else {
11194 DRD->addDecl(OmpPrivParm);
11195 DRD->addDecl(OmpOrigParm);
11196 }
11197}
11198
11199void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11200 Expr *Initializer) {
11201 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11202 DiscardCleanupsInEvaluationContext();
11203 PopExpressionEvaluationContext();
11204
11205 PopDeclContext();
11206 PopFunctionScopeInfo();
11207
11208 if (Initializer != nullptr)
11209 DRD->setInitializer(Initializer);
11210 else
11211 DRD->setInvalidDecl();
11212}
11213
11214Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11215 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11216 for (auto *D : DeclReductions.get()) {
11217 if (IsValid) {
11218 auto *DRD = cast<OMPDeclareReductionDecl>(D);
11219 if (S != nullptr)
11220 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11221 } else
11222 D->setInvalidDecl();
11223 }
11224 return DeclReductions;
11225}
11226
Kelvin Li099bb8c2015-11-24 20:50:12 +000011227OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11228 SourceLocation StartLoc,
11229 SourceLocation LParenLoc,
11230 SourceLocation EndLoc) {
11231 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011232
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011233 // OpenMP [teams Constrcut, Restrictions]
11234 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011235 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11236 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011237 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000011238
11239 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11240}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011241
11242OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11243 SourceLocation StartLoc,
11244 SourceLocation LParenLoc,
11245 SourceLocation EndLoc) {
11246 Expr *ValExpr = ThreadLimit;
11247
11248 // OpenMP [teams Constrcut, Restrictions]
11249 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000011250 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11251 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011252 return nullptr;
11253
11254 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11255 EndLoc);
11256}
Alexey Bataeva0569352015-12-01 10:17:31 +000011257
11258OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11259 SourceLocation StartLoc,
11260 SourceLocation LParenLoc,
11261 SourceLocation EndLoc) {
11262 Expr *ValExpr = Priority;
11263
11264 // OpenMP [2.9.1, task Constrcut]
11265 // The priority-value is a non-negative numerical scalar expression.
11266 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11267 /*StrictlyPositive=*/false))
11268 return nullptr;
11269
11270 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11271}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000011272
11273OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11274 SourceLocation StartLoc,
11275 SourceLocation LParenLoc,
11276 SourceLocation EndLoc) {
11277 Expr *ValExpr = Grainsize;
11278
11279 // OpenMP [2.9.2, taskloop Constrcut]
11280 // The parameter of the grainsize clause must be a positive integer
11281 // expression.
11282 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11283 /*StrictlyPositive=*/true))
11284 return nullptr;
11285
11286 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11287}
Alexey Bataev382967a2015-12-08 12:06:20 +000011288
11289OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11290 SourceLocation StartLoc,
11291 SourceLocation LParenLoc,
11292 SourceLocation EndLoc) {
11293 Expr *ValExpr = NumTasks;
11294
11295 // OpenMP [2.9.2, taskloop Constrcut]
11296 // The parameter of the num_tasks clause must be a positive integer
11297 // expression.
11298 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11299 /*StrictlyPositive=*/true))
11300 return nullptr;
11301
11302 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11303}
11304
Alexey Bataev28c75412015-12-15 08:19:24 +000011305OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11306 SourceLocation LParenLoc,
11307 SourceLocation EndLoc) {
11308 // OpenMP [2.13.2, critical construct, Description]
11309 // ... where hint-expression is an integer constant expression that evaluates
11310 // to a valid lock hint.
11311 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11312 if (HintExpr.isInvalid())
11313 return nullptr;
11314 return new (Context)
11315 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11316}
11317
Carlo Bertollib4adf552016-01-15 18:50:31 +000011318OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11319 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11320 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11321 SourceLocation EndLoc) {
11322 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11323 std::string Values;
11324 Values += "'";
11325 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11326 Values += "'";
11327 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11328 << Values << getOpenMPClauseName(OMPC_dist_schedule);
11329 return nullptr;
11330 }
11331 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000011332 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000011333 if (ChunkSize) {
11334 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11335 !ChunkSize->isInstantiationDependent() &&
11336 !ChunkSize->containsUnexpandedParameterPack()) {
11337 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11338 ExprResult Val =
11339 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11340 if (Val.isInvalid())
11341 return nullptr;
11342
11343 ValExpr = Val.get();
11344
11345 // OpenMP [2.7.1, Restrictions]
11346 // chunk_size must be a loop invariant integer expression with a positive
11347 // value.
11348 llvm::APSInt Result;
11349 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11350 if (Result.isSigned() && !Result.isStrictlyPositive()) {
11351 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11352 << "dist_schedule" << ChunkSize->getSourceRange();
11353 return nullptr;
11354 }
Alexey Bataevb46cdea2016-06-15 11:20:48 +000011355 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11356 !CurContext->isDependentContext()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000011357 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11358 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11359 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011360 }
11361 }
11362 }
11363
11364 return new (Context)
11365 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000011366 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000011367}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000011368
11369OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11370 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11371 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11372 SourceLocation KindLoc, SourceLocation EndLoc) {
11373 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11374 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11375 Kind != OMPC_DEFAULTMAP_scalar) {
11376 std::string Value;
11377 SourceLocation Loc;
11378 Value += "'";
11379 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11380 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11381 OMPC_DEFAULTMAP_MODIFIER_tofrom);
11382 Loc = MLoc;
11383 } else {
11384 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11385 OMPC_DEFAULTMAP_scalar);
11386 Loc = KindLoc;
11387 }
11388 Value += "'";
11389 Diag(Loc, diag::err_omp_unexpected_clause_value)
11390 << Value << getOpenMPClauseName(OMPC_defaultmap);
11391 return nullptr;
11392 }
11393
11394 return new (Context)
11395 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11396}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011397
11398bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11399 DeclContext *CurLexicalContext = getCurLexicalContext();
11400 if (!CurLexicalContext->isFileContext() &&
11401 !CurLexicalContext->isExternCContext() &&
11402 !CurLexicalContext->isExternCXXContext()) {
11403 Diag(Loc, diag::err_omp_region_not_file_context);
11404 return false;
11405 }
11406 if (IsInOpenMPDeclareTargetContext) {
11407 Diag(Loc, diag::err_omp_enclosed_declare_target);
11408 return false;
11409 }
11410
11411 IsInOpenMPDeclareTargetContext = true;
11412 return true;
11413}
11414
11415void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11416 assert(IsInOpenMPDeclareTargetContext &&
11417 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11418
11419 IsInOpenMPDeclareTargetContext = false;
11420}
11421
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011422void
11423Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11424 const DeclarationNameInfo &Id,
11425 OMPDeclareTargetDeclAttr::MapTypeTy MT,
11426 NamedDeclSetType &SameDirectiveDecls) {
11427 LookupResult Lookup(*this, Id, LookupOrdinaryName);
11428 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11429
11430 if (Lookup.isAmbiguous())
11431 return;
11432 Lookup.suppressDiagnostics();
11433
11434 if (!Lookup.isSingleResult()) {
11435 if (TypoCorrection Corrected =
11436 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11437 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11438 CTK_ErrorRecovery)) {
11439 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11440 << Id.getName());
11441 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11442 return;
11443 }
11444
11445 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11446 return;
11447 }
11448
11449 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11450 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11451 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11452 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11453
11454 if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11455 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11456 ND->addAttr(A);
11457 if (ASTMutationListener *ML = Context.getASTMutationListener())
11458 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11459 checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11460 } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11461 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11462 << Id.getName();
11463 }
11464 } else
11465 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11466}
11467
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011468static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11469 Sema &SemaRef, Decl *D) {
11470 if (!D)
11471 return;
11472 Decl *LD = nullptr;
11473 if (isa<TagDecl>(D)) {
11474 LD = cast<TagDecl>(D)->getDefinition();
11475 } else if (isa<VarDecl>(D)) {
11476 LD = cast<VarDecl>(D)->getDefinition();
11477
11478 // If this is an implicit variable that is legal and we do not need to do
11479 // anything.
11480 if (cast<VarDecl>(D)->isImplicit()) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011481 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11482 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11483 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011484 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011485 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011486 return;
11487 }
11488
11489 } else if (isa<FunctionDecl>(D)) {
11490 const FunctionDecl *FD = nullptr;
11491 if (cast<FunctionDecl>(D)->hasBody(FD))
11492 LD = const_cast<FunctionDecl *>(FD);
11493
11494 // If the definition is associated with the current declaration in the
11495 // target region (it can be e.g. a lambda) that is legal and we do not need
11496 // to do anything else.
11497 if (LD == D) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011498 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11499 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11500 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011501 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011502 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011503 return;
11504 }
11505 }
11506 if (!LD)
11507 LD = D;
11508 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11509 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11510 // Outlined declaration is not declared target.
11511 if (LD->isOutOfLine()) {
11512 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11513 SemaRef.Diag(SL, diag::note_used_here) << SR;
11514 } else {
11515 DeclContext *DC = LD->getDeclContext();
11516 while (DC) {
11517 if (isa<FunctionDecl>(DC) &&
11518 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11519 break;
11520 DC = DC->getParent();
11521 }
11522 if (DC)
11523 return;
11524
11525 // Is not declared in target context.
11526 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11527 SemaRef.Diag(SL, diag::note_used_here) << SR;
11528 }
11529 // Mark decl as declared target to prevent further diagnostic.
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011530 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11531 SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11532 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011533 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011534 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011535 }
11536}
11537
11538static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11539 Sema &SemaRef, DSAStackTy *Stack,
11540 ValueDecl *VD) {
11541 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11542 return true;
11543 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11544 return false;
11545 return true;
11546}
11547
11548void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11549 if (!D || D->isInvalidDecl())
11550 return;
11551 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11552 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11553 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11554 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11555 if (DSAStack->isThreadPrivate(VD)) {
11556 Diag(SL, diag::err_omp_threadprivate_in_target);
11557 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11558 return;
11559 }
11560 }
11561 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11562 // Problem if any with var declared with incomplete type will be reported
11563 // as normal, so no need to check it here.
11564 if ((E || !VD->getType()->isIncompleteType()) &&
11565 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11566 // Mark decl as declared target to prevent further diagnostic.
11567 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011568 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11569 Context, OMPDeclareTargetDeclAttr::MT_To);
11570 VD->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011571 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011572 ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011573 }
11574 return;
11575 }
11576 }
11577 if (!E) {
11578 // Checking declaration inside declare target region.
11579 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11580 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011581 Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11582 Context, OMPDeclareTargetDeclAttr::MT_To);
11583 D->addAttr(A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011584 if (ASTMutationListener *ML = Context.getASTMutationListener())
Dmitry Polukhind69b5052016-05-09 14:59:13 +000011585 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000011586 }
11587 return;
11588 }
11589 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11590}
Samuel Antao661c0902016-05-26 17:39:58 +000011591
11592OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11593 SourceLocation StartLoc,
11594 SourceLocation LParenLoc,
11595 SourceLocation EndLoc) {
11596 MappableVarListInfo MVLI(VarList);
11597 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11598 if (MVLI.ProcessedVarList.empty())
11599 return nullptr;
11600
11601 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11602 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11603 MVLI.VarComponents);
11604}
Samuel Antaoec172c62016-05-26 17:49:04 +000011605
11606OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11607 SourceLocation StartLoc,
11608 SourceLocation LParenLoc,
11609 SourceLocation EndLoc) {
11610 MappableVarListInfo MVLI(VarList);
11611 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11612 if (MVLI.ProcessedVarList.empty())
11613 return nullptr;
11614
11615 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11616 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11617 MVLI.VarComponents);
11618}