blob: d58f91104fef1acc2b1d96b2a157b7cefade84b0 [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 Bataevf29276e2014-06-18 04:14:57 +000048template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000049 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000050 bool operator()(T Kind) {
51 for (auto KindEl : Arr)
52 if (KindEl == Kind)
53 return true;
54 return false;
55 }
56
57private:
58 ArrayRef<T> Arr;
59};
Alexey Bataev23b69422014-06-18 07:08:49 +000060struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000061 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000062 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000063};
64
65typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
66typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000067
68/// \brief Stack for tracking declarations used in OpenMP directives and
69/// clauses and their data-sharing attributes.
70class DSAStackTy {
71public:
72 struct DSAVarData {
73 OpenMPDirectiveKind DKind;
74 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000075 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000076 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000077 SourceLocation ImplicitDSALoc;
78 DSAVarData()
79 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000080 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000081 };
Alexey Bataeved09d242014-05-28 05:53:51 +000082
Alexey Bataev758e55e2013-09-06 18:03:48 +000083private:
Samuel Antao5de996e2016-01-22 20:21:36 +000084 typedef SmallVector<Expr *, 4> MapInfo;
85
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 struct DSAInfo {
87 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000088 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000091 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
92 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000093 typedef llvm::DenseMap<ValueDecl *, unsigned> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000094 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000095 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
96 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000097
98 struct SharingMapTy {
99 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000100 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000101 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000102 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000105 OpenMPDirectiveKind Directive;
106 DeclarationNameInfo DirectiveName;
107 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000108 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000109 /// \brief first argument (Expr *) contains optional argument of the
110 /// 'ordered' clause, the second one is true if the regions has 'ordered'
111 /// clause, false otherwise.
112 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000113 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000114 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000115 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000116 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000117 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000118 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000119 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000120 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000121 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000122 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000125 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000126 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000127 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128 };
129
Axel Naumann323862e2016-02-03 10:45:22 +0000130 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131
132 /// \brief Stack of used declaration and their data-sharing attributes.
133 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000134 /// \brief true, if check for DSA must be from parent directive, false, if
135 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000136 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000137 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000138 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000139 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140
141 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
142
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000143 DSAVarData getDSA(StackTy::reverse_iterator Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000144
145 /// \brief Checks if the variable is a local for OpenMP region.
146 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000147
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000150 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
151 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000152
Alexey Bataevaac108a2015-06-23 04:51:00 +0000153 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
154 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000156 bool isForceVarCapturing() const { return ForceCapturing; }
157 void setForceVarCapturing(bool V) { ForceCapturing = V; }
158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000160 Scope *CurScope, SourceLocation Loc) {
161 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
162 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 }
164
165 void pop() {
166 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
167 Stack.pop_back();
168 }
169
Alexey Bataev28c75412015-12-15 08:19:24 +0000170 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
171 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
172 }
173 const std::pair<OMPCriticalDirective *, llvm::APSInt>
174 getCriticalWithHint(const DeclarationNameInfo &Name) const {
175 auto I = Criticals.find(Name.getAsString());
176 if (I != Criticals.end())
177 return I->second;
178 return std::make_pair(nullptr, llvm::APSInt());
179 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000180 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000181 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000182 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000183 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000184
Alexey Bataev9c821032015-04-30 04:23:23 +0000185 /// \brief Register specified variable as loop control variable.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000186 void addLoopControlVariable(ValueDecl *D);
Alexey Bataev9c821032015-04-30 04:23:23 +0000187 /// \brief Check if the specified variable is a loop control variable for
188 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000189 /// \return The index of the loop control variable in the list of associated
190 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000191 unsigned isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000192 /// \brief Check if the specified variable is a loop control variable for
193 /// parent region.
194 /// \return The index of the loop control variable in the list of associated
195 /// for-loops (from outer to inner).
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000196 unsigned isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000197 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
198 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000199 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000200
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000202 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
203 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000204
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205 /// \brief Returns data sharing attributes from top of the stack for the
206 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000207 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000208 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000209 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000210 /// \brief Checks if the specified variables has data-sharing attributes which
211 /// match specified \a CPred predicate in any directive which matches \a DPred
212 /// predicate.
213 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000214 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000215 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000216 /// \brief Checks if the specified variables has data-sharing attributes which
217 /// match specified \a CPred predicate in any innermost directive which
218 /// matches \a DPred predicate.
219 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000220 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
221 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000222 /// \brief Checks if the specified variables has explicit data-sharing
223 /// attributes which match specified \a CPred predicate at the specified
224 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000225 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000226 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
227 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000228
229 /// \brief Returns true if the directive at level \Level matches in the
230 /// specified \a DPred predicate.
231 bool hasExplicitDirective(
232 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
233 unsigned Level);
234
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000235 /// \brief Finds a directive which matches specified \a DPred predicate.
236 template <class NamedDirectivesPredicate>
237 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000238
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 /// \brief Returns currently analyzed directive.
240 OpenMPDirectiveKind getCurrentDirective() const {
241 return Stack.back().Directive;
242 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000243 /// \brief Returns parent directive.
244 OpenMPDirectiveKind getParentDirective() const {
245 if (Stack.size() > 2)
246 return Stack[Stack.size() - 2].Directive;
247 return OMPD_unknown;
248 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000249 /// \brief Return the directive associated with the provided scope.
250 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000251
252 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 void setDefaultDSANone(SourceLocation Loc) {
254 Stack.back().DefaultAttr = DSA_none;
255 Stack.back().DefaultAttrLoc = Loc;
256 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000258 void setDefaultDSAShared(SourceLocation Loc) {
259 Stack.back().DefaultAttr = DSA_shared;
260 Stack.back().DefaultAttrLoc = Loc;
261 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000262
263 DefaultDataSharingAttributes getDefaultDSA() const {
264 return Stack.back().DefaultAttr;
265 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000266 SourceLocation getDefaultDSALocation() const {
267 return Stack.back().DefaultAttrLoc;
268 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000269
Alexey Bataevf29276e2014-06-18 04:14:57 +0000270 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000271 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000272 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000273 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000274 }
275
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000276 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000277 void setOrderedRegion(bool IsOrdered, Expr *Param) {
278 Stack.back().OrderedRegion.setInt(IsOrdered);
279 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000280 }
281 /// \brief Returns true, if parent region is ordered (has associated
282 /// 'ordered' clause), false - otherwise.
283 bool isParentOrderedRegion() const {
284 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000285 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000286 return false;
287 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000288 /// \brief Returns optional parameter for the ordered region.
289 Expr *getParentOrderedRegionParam() const {
290 if (Stack.size() > 2)
291 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
292 return nullptr;
293 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000294 /// \brief Marks current region as nowait (it has a 'nowait' clause).
295 void setNowaitRegion(bool IsNowait = true) {
296 Stack.back().NowaitRegion = IsNowait;
297 }
298 /// \brief Returns true, if parent region is nowait (has associated
299 /// 'nowait' clause), false - otherwise.
300 bool isParentNowaitRegion() const {
301 if (Stack.size() > 2)
302 return Stack[Stack.size() - 2].NowaitRegion;
303 return false;
304 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000305 /// \brief Marks parent region as cancel region.
306 void setParentCancelRegion(bool Cancel = true) {
307 if (Stack.size() > 2)
308 Stack[Stack.size() - 2].CancelRegion =
309 Stack[Stack.size() - 2].CancelRegion || Cancel;
310 }
311 /// \brief Return true if current region has inner cancel construct.
312 bool isCancelRegion() const {
313 return Stack.back().CancelRegion;
314 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000315
Alexey Bataev9c821032015-04-30 04:23:23 +0000316 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000317 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000318 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000319 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000320
Alexey Bataev13314bf2014-10-09 04:18:56 +0000321 /// \brief Marks current target region as one with closely nested teams
322 /// region.
323 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
324 if (Stack.size() > 2)
325 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
326 }
327 /// \brief Returns true, if current region has closely nested teams region.
328 bool hasInnerTeamsRegion() const {
329 return getInnerTeamsRegionLoc().isValid();
330 }
331 /// \brief Returns location of the nested teams region (if any).
332 SourceLocation getInnerTeamsRegionLoc() const {
333 if (Stack.size() > 1)
334 return Stack.back().InnerTeamsRegionLoc;
335 return SourceLocation();
336 }
337
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000338 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000340 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000341
Samuel Antao5de996e2016-01-22 20:21:36 +0000342 // Do the check specified in MapInfoCheck and return true if any issue is
343 // found.
344 template <class MapInfoCheck>
345 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
346 MapInfoCheck Check) {
347 auto SI = Stack.rbegin();
348 auto SE = Stack.rend();
349
350 if (SI == SE)
351 return false;
352
353 if (CurrentRegionOnly) {
354 SE = std::next(SI);
355 } else {
356 ++SI;
357 }
358
359 for (; SI != SE; ++SI) {
360 auto MI = SI->MappedDecls.find(VD);
361 if (MI != SI->MappedDecls.end()) {
362 for (Expr *E : MI->second) {
363 if (Check(E))
364 return true;
365 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000366 }
367 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000368 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000369 }
370
Samuel Antao5de996e2016-01-22 20:21:36 +0000371 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000372 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000373 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000374 }
375 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000377bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
378 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000379 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000380 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000381}
Alexey Bataeved09d242014-05-28 05:53:51 +0000382} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000384static ValueDecl *getCanonicalDecl(ValueDecl *D) {
385 auto *VD = dyn_cast<VarDecl>(D);
386 auto *FD = dyn_cast<FieldDecl>(D);
387 if (VD != nullptr) {
388 VD = VD->getCanonicalDecl();
389 D = VD;
390 } else {
391 assert(FD);
392 FD = FD->getCanonicalDecl();
393 D = FD;
394 }
395 return D;
396}
397
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000399 ValueDecl *D) {
400 D = getCanonicalDecl(D);
401 auto *VD = dyn_cast<VarDecl>(D);
402 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000404 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406 // in a region but not in construct]
407 // File-scope or namespace-scope variables referenced in called routines
408 // in the region are shared unless they appear in a threadprivate
409 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000410 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000411 DVar.CKind = OMPC_shared;
412
413 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
414 // in a region but not in construct]
415 // Variables with static storage duration that are declared in called
416 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000417 if (VD && VD->hasGlobalStorage())
418 DVar.CKind = OMPC_shared;
419
420 // Non-static data members are shared by default.
421 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000422 DVar.CKind = OMPC_shared;
423
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 return DVar;
425 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000426
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
429 // in a Construct, C/C++, predetermined, p.1]
430 // Variables with automatic storage duration that are declared in a scope
431 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000432 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
433 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000434 DVar.CKind = OMPC_private;
435 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000436 }
437
Alexey Bataev758e55e2013-09-06 18:03:48 +0000438 // Explicitly specified attributes and local variables with predetermined
439 // attributes.
440 if (Iter->SharingMap.count(D)) {
441 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000442 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000444 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 return DVar;
446 }
447
448 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
449 // in a Construct, C/C++, implicitly determined, p.1]
450 // In a parallel or task construct, the data-sharing attributes of these
451 // variables are determined by the default clause, if present.
452 switch (Iter->DefaultAttr) {
453 case DSA_shared:
454 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000455 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456 return DVar;
457 case DSA_none:
458 return DVar;
459 case DSA_unspecified:
460 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
461 // in a Construct, implicitly determined, p.2]
462 // In a parallel construct, if no default clause is present, these
463 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000465 if (isOpenMPParallelDirective(DVar.DKind) ||
466 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000467 DVar.CKind = OMPC_shared;
468 return DVar;
469 }
470
471 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
472 // in a Construct, implicitly determined, p.4]
473 // In a task construct, if no default clause is present, a variable that in
474 // the enclosing context is determined to be shared by all implicit tasks
475 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 if (DVar.DKind == OMPD_task) {
477 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000478 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000479 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
481 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 // in a Construct, implicitly determined, p.6]
483 // In a task construct, if no default clause is present, a variable
484 // whose data-sharing attribute is not determined by the rules above is
485 // firstprivate.
486 DVarTemp = getDSA(I, D);
487 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000488 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000490 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 return DVar;
492 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000493 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000494 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 }
496 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000497 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000498 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000499 return DVar;
500 }
501 }
502 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
503 // in a Construct, implicitly determined, p.3]
504 // For constructs other than task, if no default clause is present, these
505 // variables inherit their data-sharing attributes from the enclosing
506 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000507 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508}
509
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000510Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000511 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000512 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000513 auto It = Stack.back().AlignedMap.find(D);
514 if (It == Stack.back().AlignedMap.end()) {
515 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
516 Stack.back().AlignedMap[D] = NewDE;
517 return nullptr;
518 } else {
519 assert(It->second && "Unexpected nullptr expr in the aligned map");
520 return It->second;
521 }
522 return nullptr;
523}
524
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000525void DSAStackTy::addLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000526 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000527 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000528 Stack.back().LCVMap.insert(std::make_pair(D, Stack.back().LCVMap.size() + 1));
Alexey Bataev9c821032015-04-30 04:23:23 +0000529}
530
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531unsigned DSAStackTy::isLoopControlVariable(ValueDecl *D) {
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 Bataeva636c7f2015-12-23 10:27:45 +0000534 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D] : 0;
535}
536
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537unsigned DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000538 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000539 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000540 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
541 ? Stack[Stack.size() - 2].LCVMap[D]
542 : 0;
543}
544
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000545ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000546 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
547 if (Stack[Stack.size() - 2].LCVMap.size() < I)
548 return nullptr;
549 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
550 if (Pair.second == I)
551 return Pair.first;
552 }
553 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000554}
555
Alexey Bataev90c228f2016-02-08 09:29:13 +0000556void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
557 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000558 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 if (A == OMPC_threadprivate) {
560 Stack[0].SharingMap[D].Attributes = A;
561 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000562 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000563 } else {
564 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
565 Stack.back().SharingMap[D].Attributes = A;
566 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000567 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
568 if (PrivateCopy)
569 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000570 }
571}
572
Alexey Bataeved09d242014-05-28 05:53:51 +0000573bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000574 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000575 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000576 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000577 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000578 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000579 ++I;
580 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000581 if (I == E)
582 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000583 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000584 Scope *CurScope = getCurScope();
585 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000587 }
588 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000589 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000590 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000591}
592
Alexey Bataev39f915b82015-05-08 10:41:21 +0000593/// \brief Build a variable declaration for OpenMP loop iteration variable.
594static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000595 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000596 DeclContext *DC = SemaRef.CurContext;
597 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
598 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
599 VarDecl *Decl =
600 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000601 if (Attrs) {
602 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
603 I != E; ++I)
604 Decl->addAttr(*I);
605 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000606 Decl->setImplicit();
607 return Decl;
608}
609
610static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
611 SourceLocation Loc,
612 bool RefersToCapture = false) {
613 D->setReferenced();
614 D->markUsed(S.Context);
615 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
616 SourceLocation(), D, RefersToCapture, Loc, Ty,
617 VK_LValue);
618}
619
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000620DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
621 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000622 DSAVarData DVar;
623
624 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
625 // in a Construct, C/C++, predetermined, p.1]
626 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000627 auto *VD = dyn_cast<VarDecl>(D);
628 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
629 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000630 SemaRef.getLangOpts().OpenMPUseTLS &&
631 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000632 (VD && VD->getStorageClass() == SC_Register &&
633 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
634 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000635 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000636 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000637 }
638 if (Stack[0].SharingMap.count(D)) {
639 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
640 DVar.CKind = OMPC_threadprivate;
641 return DVar;
642 }
643
644 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000645 // in a Construct, C/C++, predetermined, p.4]
646 // Static data members are shared.
647 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
648 // in a Construct, C/C++, predetermined, p.7]
649 // Variables with static storage duration that are declared in a scope
650 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000651 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000652 DSAVarData DVarTemp =
653 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
654 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000655 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000656
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000657 DVar.CKind = OMPC_shared;
658 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000659 }
660
661 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000662 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
663 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000664 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
665 // in a Construct, C/C++, predetermined, p.6]
666 // Variables with const qualified type having no mutable member are
667 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000668 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000669 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000670 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
671 if (auto *CTD = CTSD->getSpecializedTemplate())
672 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000674 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
675 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000676 // Variables with const-qualified type having no mutable member may be
677 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000678 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
679 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000680 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
681 return DVar;
682
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 DVar.CKind = OMPC_shared;
684 return DVar;
685 }
686
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 // Explicitly specified attributes and local variables with predetermined
688 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000689 auto StartI = std::next(Stack.rbegin());
690 auto EndI = std::prev(Stack.rend());
691 if (FromParent && StartI != EndI) {
692 StartI = std::next(StartI);
693 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000694 auto I = std::prev(StartI);
695 if (I->SharingMap.count(D)) {
696 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000697 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000698 DVar.CKind = I->SharingMap[D].Attributes;
699 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000700 }
701
702 return DVar;
703}
704
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000705DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
706 bool FromParent) {
707 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 auto StartI = Stack.rbegin();
709 auto EndI = std::prev(Stack.rend());
710 if (FromParent && StartI != EndI) {
711 StartI = std::next(StartI);
712 }
713 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000714}
715
Alexey Bataevf29276e2014-06-18 04:14:57 +0000716template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000717DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718 DirectivesPredicate DPred,
719 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000720 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000721 auto StartI = std::next(Stack.rbegin());
722 auto EndI = std::prev(Stack.rend());
723 if (FromParent && StartI != EndI) {
724 StartI = std::next(StartI);
725 }
726 for (auto I = StartI, EE = EndI; I != EE; ++I) {
727 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000728 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000729 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000730 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000731 return DVar;
732 }
733 return DSAVarData();
734}
735
Alexey Bataevf29276e2014-06-18 04:14:57 +0000736template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000737DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000738DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000739 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000740 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000741 auto StartI = std::next(Stack.rbegin());
742 auto EndI = std::prev(Stack.rend());
743 if (FromParent && StartI != EndI) {
744 StartI = std::next(StartI);
745 }
746 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000748 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000749 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000750 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000751 return DVar;
752 return DSAVarData();
753 }
754 return DSAVarData();
755}
756
Alexey Bataevaac108a2015-06-23 04:51:00 +0000757bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000758 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000759 unsigned Level) {
760 if (CPred(ClauseKindMode))
761 return true;
762 if (isClauseParsingMode())
763 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000764 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000765 auto StartI = Stack.rbegin();
766 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000767 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000768 return false;
769 std::advance(StartI, Level);
770 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
771 CPred(StartI->SharingMap[D].Attributes);
772}
773
Samuel Antao4be30e92015-10-02 17:14:03 +0000774bool DSAStackTy::hasExplicitDirective(
775 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
776 unsigned Level) {
777 if (isClauseParsingMode())
778 ++Level;
779 auto StartI = Stack.rbegin();
780 auto EndI = std::prev(Stack.rend());
781 if (std::distance(StartI, EndI) <= (int)Level)
782 return false;
783 std::advance(StartI, Level);
784 return DPred(StartI->Directive);
785}
786
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000787template <class NamedDirectivesPredicate>
788bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
789 auto StartI = std::next(Stack.rbegin());
790 auto EndI = std::prev(Stack.rend());
791 if (FromParent && StartI != EndI) {
792 StartI = std::next(StartI);
793 }
794 for (auto I = StartI, EE = EndI; I != EE; ++I) {
795 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
796 return true;
797 }
798 return false;
799}
800
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000801OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
802 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
803 if (I->CurScope == S)
804 return I->Directive;
805 return OMPD_unknown;
806}
807
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808void Sema::InitDataSharingAttributesStack() {
809 VarDataSharingAttributesStack = new DSAStackTy(*this);
810}
811
812#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
813
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000814bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000815 const CapturedRegionScopeInfo *RSI) {
816 assert(LangOpts.OpenMP && "OpenMP is not allowed");
817
818 auto &Ctx = getASTContext();
819 bool IsByRef = true;
820
821 // Find the directive that is associated with the provided scope.
822 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000823 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000824
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000825 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000826 // This table summarizes how a given variable should be passed to the device
827 // given its type and the clauses where it appears. This table is based on
828 // the description in OpenMP 4.5 [2.10.4, target Construct] and
829 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
830 //
831 // =========================================================================
832 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
833 // | |(tofrom:scalar)| | pvt | | | |
834 // =========================================================================
835 // | scl | | | | - | | bycopy|
836 // | scl | | - | x | - | - | bycopy|
837 // | scl | | x | - | - | - | null |
838 // | scl | x | | | - | | byref |
839 // | scl | x | - | x | - | - | bycopy|
840 // | scl | x | x | - | - | - | null |
841 // | scl | | - | - | - | x | byref |
842 // | scl | x | - | - | - | x | byref |
843 //
844 // | agg | n.a. | | | - | | byref |
845 // | agg | n.a. | - | x | - | - | byref |
846 // | agg | n.a. | x | - | - | - | null |
847 // | agg | n.a. | - | - | - | x | byref |
848 // | agg | n.a. | - | - | - | x[] | byref |
849 //
850 // | ptr | n.a. | | | - | | bycopy|
851 // | ptr | n.a. | - | x | - | - | bycopy|
852 // | ptr | n.a. | x | - | - | - | null |
853 // | ptr | n.a. | - | - | - | x | byref |
854 // | ptr | n.a. | - | - | - | x[] | bycopy|
855 // | ptr | n.a. | - | - | x | | bycopy|
856 // | ptr | n.a. | - | - | x | x | bycopy|
857 // | ptr | n.a. | - | - | x | x[] | bycopy|
858 // =========================================================================
859 // Legend:
860 // scl - scalar
861 // ptr - pointer
862 // agg - aggregate
863 // x - applies
864 // - - invalid in this combination
865 // [] - mapped with an array section
866 // byref - should be mapped by reference
867 // byval - should be mapped by value
868 // null - initialize a local variable to null on the device
869 //
870 // Observations:
871 // - All scalar declarations that show up in a map clause have to be passed
872 // by reference, because they may have been mapped in the enclosing data
873 // environment.
874 // - If the scalar value does not fit the size of uintptr, it has to be
875 // passed by reference, regardless the result in the table above.
876 // - For pointers mapped by value that have either an implicit map or an
877 // array section, the runtime library may pass the NULL value to the
878 // device instead of the value passed to it by the compiler.
879
880 // FIXME: Right now, only implicit maps are implemented. Properly mapping
881 // values requires having the map, private, and firstprivate clauses SEMA
882 // and parsing in place, which we don't yet.
883
884 if (Ty->isReferenceType())
885 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
886 IsByRef = !Ty->isScalarType();
887 }
888
889 // When passing data by value, we need to make sure it fits the uintptr size
890 // and alignment, because the runtime library only deals with uintptr types.
891 // If it does not fit the uintptr size, we need to pass the data by reference
892 // instead.
893 if (!IsByRef &&
894 (Ctx.getTypeSizeInChars(Ty) >
895 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000896 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000897 IsByRef = true;
898
899 return IsByRef;
900}
901
Alexey Bataev90c228f2016-02-08 09:29:13 +0000902VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000903 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000904 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000905
906 // If we are attempting to capture a global variable in a directive with
907 // 'target' we return true so that this global is also mapped to the device.
908 //
909 // FIXME: If the declaration is enclosed in a 'declare target' directive,
910 // then it should not be captured. Therefore, an extra check has to be
911 // inserted here once support for 'declare target' is added.
912 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000913 auto *VD = dyn_cast<VarDecl>(D);
914 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000915 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000916 !DSAStack->isClauseParsingMode())
917 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000918 if (DSAStack->getCurScope() &&
919 DSAStack->hasDirective(
920 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
921 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000922 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000923 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000924 false))
925 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000926 }
927
Alexey Bataev48977c32015-08-04 08:10:48 +0000928 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
929 (!DSAStack->isClauseParsingMode() ||
930 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000931 if (DSAStack->isLoopControlVariable(D) ||
932 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000933 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000934 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000935 return VD;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000936 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000937 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000938 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000939 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000940 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000941 if (DVarPrivate.CKind != OMPC_unknown)
942 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000943 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000944 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000945}
946
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000947bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000948 assert(LangOpts.OpenMP && "OpenMP is not allowed");
949 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000950 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000951}
952
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000953bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000954 assert(LangOpts.OpenMP && "OpenMP is not allowed");
955 // Return true if the current level is no longer enclosed in a target region.
956
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000957 auto *VD = dyn_cast<VarDecl>(D);
958 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000959 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
960 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000961}
962
Alexey Bataeved09d242014-05-28 05:53:51 +0000963void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000964
965void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
966 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000967 Scope *CurScope, SourceLocation Loc) {
968 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969 PushExpressionEvaluationContext(PotentiallyEvaluated);
970}
971
Alexey Bataevaac108a2015-06-23 04:51:00 +0000972void Sema::StartOpenMPClause(OpenMPClauseKind K) {
973 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000974}
975
Alexey Bataevaac108a2015-06-23 04:51:00 +0000976void Sema::EndOpenMPClause() {
977 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000978}
979
Alexey Bataev758e55e2013-09-06 18:03:48 +0000980void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000981 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
982 // A variable of class type (or array thereof) that appears in a lastprivate
983 // clause requires an accessible, unambiguous default constructor for the
984 // class type, unless the list item is also specified in a firstprivate
985 // clause.
986 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000987 for (auto *C : D->clauses()) {
988 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
989 SmallVector<Expr *, 8> PrivateCopies;
990 for (auto *DE : Clause->varlists()) {
991 if (DE->isValueDependent() || DE->isTypeDependent()) {
992 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000993 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000994 }
Alexey Bataev74caaf22016-02-20 04:09:36 +0000995 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +0000996 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
997 QualType Type = VD->getType().getNonReferenceType();
998 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000999 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001000 // Generate helper private variable and initialize it with the
1001 // default value. The address of the original variable is replaced
1002 // by the address of the new private variable in CodeGen. This new
1003 // variable is not added to IdResolver, so the code in the OpenMP
1004 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001005 auto *VDPrivate = buildVarDecl(
1006 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001007 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001008 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1009 if (VDPrivate->isInvalidDecl())
1010 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001011 PrivateCopies.push_back(buildDeclRefExpr(
1012 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001013 } else {
1014 // The variable is also a firstprivate, so initialization sequence
1015 // for private copy is generated already.
1016 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001017 }
1018 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001020 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001021 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001022 }
1023 }
1024 }
1025
Alexey Bataev758e55e2013-09-06 18:03:48 +00001026 DSAStack->pop();
1027 DiscardCleanupsInEvaluationContext();
1028 PopExpressionEvaluationContext();
1029}
1030
Alexander Musman3276a272015-03-21 10:12:56 +00001031static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1032 Expr *NumIterations, Sema &SemaRef,
1033 Scope *S);
1034
Alexey Bataeva769e072013-03-22 06:34:35 +00001035namespace {
1036
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001037class VarDeclFilterCCC : public CorrectionCandidateCallback {
1038private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001039 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001040
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001041public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001042 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001043 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001044 NamedDecl *ND = Candidate.getCorrectionDecl();
1045 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1046 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1048 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001051 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052};
Alexey Bataeved09d242014-05-28 05:53:51 +00001053} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001054
1055ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1056 CXXScopeSpec &ScopeSpec,
1057 const DeclarationNameInfo &Id) {
1058 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1059 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1060
1061 if (Lookup.isAmbiguous())
1062 return ExprError();
1063
1064 VarDecl *VD;
1065 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001066 if (TypoCorrection Corrected = CorrectTypo(
1067 Id, LookupOrdinaryName, CurScope, nullptr,
1068 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001069 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001070 PDiag(Lookup.empty()
1071 ? diag::err_undeclared_var_use_suggest
1072 : diag::err_omp_expected_var_arg_suggest)
1073 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001074 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001075 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001076 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1077 : diag::err_omp_expected_var_arg)
1078 << Id.getName();
1079 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 } else {
1082 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001083 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1085 return ExprError();
1086 }
1087 }
1088 Lookup.suppressDiagnostics();
1089
1090 // OpenMP [2.9.2, Syntax, C/C++]
1091 // Variables must be file-scope, namespace-scope, or static block-scope.
1092 if (!VD->hasGlobalStorage()) {
1093 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001094 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1095 bool IsDecl =
1096 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1099 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100 return ExprError();
1101 }
1102
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001103 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1104 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1106 // A threadprivate directive for file-scope variables must appear outside
1107 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001108 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1109 !getCurLexicalContext()->isTranslationUnit()) {
1110 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001111 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1112 bool IsDecl =
1113 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1114 Diag(VD->getLocation(),
1115 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1116 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001117 return ExprError();
1118 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001119 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1120 // A threadprivate directive for static class member variables must appear
1121 // in the class definition, in the same scope in which the member
1122 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001123 if (CanonicalVD->isStaticDataMember() &&
1124 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1125 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001126 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1127 bool IsDecl =
1128 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1129 Diag(VD->getLocation(),
1130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1131 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001132 return ExprError();
1133 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001134 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1135 // A threadprivate directive for namespace-scope variables must appear
1136 // outside any definition or declaration other than the namespace
1137 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001138 if (CanonicalVD->getDeclContext()->isNamespace() &&
1139 (!getCurLexicalContext()->isFileContext() ||
1140 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1141 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001142 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1143 bool IsDecl =
1144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1145 Diag(VD->getLocation(),
1146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1147 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001148 return ExprError();
1149 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001150 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1151 // A threadprivate directive for static block-scope variables must appear
1152 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001153 if (CanonicalVD->isStaticLocal() && CurScope &&
1154 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001156 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1157 bool IsDecl =
1158 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1159 Diag(VD->getLocation(),
1160 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1161 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001162 return ExprError();
1163 }
1164
1165 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1166 // A threadprivate directive must lexically precede all references to any
1167 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001168 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001169 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001170 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001171 return ExprError();
1172 }
1173
1174 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001175 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1176 SourceLocation(), VD,
1177 /*RefersToEnclosingVariableOrCapture=*/false,
1178 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001179}
1180
Alexey Bataeved09d242014-05-28 05:53:51 +00001181Sema::DeclGroupPtrTy
1182Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1183 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001185 CurContext->addDecl(D);
1186 return DeclGroupPtrTy::make(DeclGroupRef(D));
1187 }
David Blaikie0403cb12016-01-15 23:43:25 +00001188 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001189}
1190
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001191namespace {
1192class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1193 Sema &SemaRef;
1194
1195public:
1196 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1197 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1198 if (VD->hasLocalStorage()) {
1199 SemaRef.Diag(E->getLocStart(),
1200 diag::err_omp_local_var_in_threadprivate_init)
1201 << E->getSourceRange();
1202 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1203 << VD << VD->getSourceRange();
1204 return true;
1205 }
1206 }
1207 return false;
1208 }
1209 bool VisitStmt(const Stmt *S) {
1210 for (auto Child : S->children()) {
1211 if (Child && Visit(Child))
1212 return true;
1213 }
1214 return false;
1215 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001216 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001217};
1218} // namespace
1219
Alexey Bataeved09d242014-05-28 05:53:51 +00001220OMPThreadPrivateDecl *
1221Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001222 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001223 for (auto &RefExpr : VarList) {
1224 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1226 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001227
Alexey Bataev376b4a42016-02-09 09:41:09 +00001228 // Mark variable as used.
1229 VD->setReferenced();
1230 VD->markUsed(Context);
1231
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001232 QualType QType = VD->getType();
1233 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1234 // It will be analyzed later.
1235 Vars.push_back(DE);
1236 continue;
1237 }
1238
Alexey Bataeva769e072013-03-22 06:34:35 +00001239 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1240 // A threadprivate variable must not have an incomplete type.
1241 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001242 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001243 continue;
1244 }
1245
1246 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1247 // A threadprivate variable must not have a reference type.
1248 if (VD->getType()->isReferenceType()) {
1249 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001250 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1251 bool IsDecl =
1252 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1253 Diag(VD->getLocation(),
1254 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1255 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001256 continue;
1257 }
1258
Samuel Antaof8b50122015-07-13 22:54:53 +00001259 // Check if this is a TLS variable. If TLS is not being supported, produce
1260 // the corresponding diagnostic.
1261 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1262 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1263 getLangOpts().OpenMPUseTLS &&
1264 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001265 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1266 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001267 Diag(ILoc, diag::err_omp_var_thread_local)
1268 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001269 bool IsDecl =
1270 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1271 Diag(VD->getLocation(),
1272 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1273 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001274 continue;
1275 }
1276
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001277 // Check if initial value of threadprivate variable reference variable with
1278 // local storage (it is not supported by runtime).
1279 if (auto Init = VD->getAnyInitializer()) {
1280 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001281 if (Checker.Visit(Init))
1282 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001283 }
1284
Alexey Bataeved09d242014-05-28 05:53:51 +00001285 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001286 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001287 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1288 Context, SourceRange(Loc, Loc)));
1289 if (auto *ML = Context.getASTMutationListener())
1290 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001291 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001292 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001293 if (!Vars.empty()) {
1294 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1295 Vars);
1296 D->setAccess(AS_public);
1297 }
1298 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001299}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001300
Alexey Bataev7ff55242014-06-19 09:13:45 +00001301static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001302 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001303 bool IsLoopIterVar = false) {
1304 if (DVar.RefExpr) {
1305 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1306 << getOpenMPClauseName(DVar.CKind);
1307 return;
1308 }
1309 enum {
1310 PDSA_StaticMemberShared,
1311 PDSA_StaticLocalVarShared,
1312 PDSA_LoopIterVarPrivate,
1313 PDSA_LoopIterVarLinear,
1314 PDSA_LoopIterVarLastprivate,
1315 PDSA_ConstVarShared,
1316 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001317 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001318 PDSA_LocalVarPrivate,
1319 PDSA_Implicit
1320 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001321 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001322 auto ReportLoc = D->getLocation();
1323 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001324 if (IsLoopIterVar) {
1325 if (DVar.CKind == OMPC_private)
1326 Reason = PDSA_LoopIterVarPrivate;
1327 else if (DVar.CKind == OMPC_lastprivate)
1328 Reason = PDSA_LoopIterVarLastprivate;
1329 else
1330 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001331 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1332 Reason = PDSA_TaskVarFirstprivate;
1333 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001334 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001336 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001337 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001338 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001339 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 ReportHint = true;
1344 Reason = PDSA_LocalVarPrivate;
1345 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001346 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001347 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001348 << Reason << ReportHint
1349 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1350 } else if (DVar.ImplicitDSALoc.isValid()) {
1351 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1352 << getOpenMPClauseName(DVar.CKind);
1353 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001354}
1355
Alexey Bataev758e55e2013-09-06 18:03:48 +00001356namespace {
1357class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1358 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001359 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360 bool ErrorFound;
1361 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001362 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001363 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001364
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365public:
1366 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001367 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001369 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1370 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001372 auto DVar = Stack->getTopDSA(VD, false);
1373 // Check if the variable has explicit DSA set and stop analysis if it so.
1374 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001375
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 auto ELoc = E->getExprLoc();
1377 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001378 // The default(none) clause requires that each variable that is referenced
1379 // in the construct, and does not have a predetermined data-sharing
1380 // attribute, must have its data-sharing attribute explicitly determined
1381 // by being listed in a data-sharing attribute clause.
1382 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001383 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 VarsWithInheritedDSA.count(VD) == 0) {
1385 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001386 return;
1387 }
1388
1389 // OpenMP [2.9.3.6, Restrictions, p.2]
1390 // A list item that appears in a reduction clause of the innermost
1391 // enclosing worksharing or parallel construct may not be accessed in an
1392 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001393 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001394 [](OpenMPDirectiveKind K) -> bool {
1395 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001396 isOpenMPWorksharingDirective(K) ||
1397 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001398 },
1399 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001400 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1401 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001402 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1403 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001404 return;
1405 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001406
1407 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001408 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001409 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001411 }
1412 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001413 void VisitMemberExpr(MemberExpr *E) {
1414 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1415 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1416 auto DVar = Stack->getTopDSA(FD, false);
1417 // Check if the variable has explicit DSA set and stop analysis if it
1418 // so.
1419 if (DVar.RefExpr)
1420 return;
1421
1422 auto ELoc = E->getExprLoc();
1423 auto DKind = Stack->getCurrentDirective();
1424 // OpenMP [2.9.3.6, Restrictions, p.2]
1425 // A list item that appears in a reduction clause of the innermost
1426 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001427 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001428 DVar =
1429 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1430 [](OpenMPDirectiveKind K) -> bool {
1431 return isOpenMPParallelDirective(K) ||
1432 isOpenMPWorksharingDirective(K) ||
1433 isOpenMPTeamsDirective(K);
1434 },
1435 false);
1436 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1437 ErrorFound = true;
1438 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1439 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1440 return;
1441 }
1442
1443 // Define implicit data-sharing attributes for task.
1444 DVar = Stack->getImplicitDSA(FD, false);
1445 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1446 ImplicitFirstprivate.push_back(E);
1447 }
1448 }
1449 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001450 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001451 for (auto *C : S->clauses()) {
1452 // Skip analysis of arguments of implicitly defined firstprivate clause
1453 // for task directives.
1454 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1455 for (auto *CC : C->children()) {
1456 if (CC)
1457 Visit(CC);
1458 }
1459 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001460 }
1461 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001462 for (auto *C : S->children()) {
1463 if (C && !isa<OMPExecutableDirective>(C))
1464 Visit(C);
1465 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001466 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001467
1468 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001469 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001470 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001471 return VarsWithInheritedDSA;
1472 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001473
Alexey Bataev7ff55242014-06-19 09:13:45 +00001474 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1475 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001476};
Alexey Bataeved09d242014-05-28 05:53:51 +00001477} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001478
Alexey Bataevbae9a792014-06-27 10:37:06 +00001479void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001480 switch (DKind) {
1481 case OMPD_parallel: {
1482 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001483 QualType KmpInt32PtrTy =
1484 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001485 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001486 std::make_pair(".global_tid.", KmpInt32PtrTy),
1487 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1488 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001489 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001490 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1491 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001492 break;
1493 }
1494 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001495 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001496 std::make_pair(StringRef(), QualType()) // __context with shared vars
1497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001500 break;
1501 }
1502 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001503 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001508 break;
1509 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001510 case OMPD_for_simd: {
1511 Sema::CapturedParamNameType Params[] = {
1512 std::make_pair(StringRef(), QualType()) // __context with shared vars
1513 };
1514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
1516 break;
1517 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001518 case OMPD_sections: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001524 break;
1525 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001526 case OMPD_section: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001532 break;
1533 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001534 case OMPD_single: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001540 break;
1541 }
Alexander Musman80c22892014-07-17 08:54:58 +00001542 case OMPD_master: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
1546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
1548 break;
1549 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001550 case OMPD_critical: {
1551 Sema::CapturedParamNameType Params[] = {
1552 std::make_pair(StringRef(), QualType()) // __context with shared vars
1553 };
1554 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1555 Params);
1556 break;
1557 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001558 case OMPD_parallel_for: {
1559 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001560 QualType KmpInt32PtrTy =
1561 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001562 Sema::CapturedParamNameType Params[] = {
1563 std::make_pair(".global_tid.", KmpInt32PtrTy),
1564 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1565 std::make_pair(StringRef(), QualType()) // __context with shared vars
1566 };
1567 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1568 Params);
1569 break;
1570 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001571 case OMPD_parallel_for_simd: {
1572 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001573 QualType KmpInt32PtrTy =
1574 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 Sema::CapturedParamNameType Params[] = {
1576 std::make_pair(".global_tid.", KmpInt32PtrTy),
1577 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1578 std::make_pair(StringRef(), QualType()) // __context with shared vars
1579 };
1580 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1581 Params);
1582 break;
1583 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001584 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001585 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001586 QualType KmpInt32PtrTy =
1587 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001588 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001589 std::make_pair(".global_tid.", KmpInt32PtrTy),
1590 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001591 std::make_pair(StringRef(), QualType()) // __context with shared vars
1592 };
1593 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1594 Params);
1595 break;
1596 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001597 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001598 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001599 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1600 FunctionProtoType::ExtProtoInfo EPI;
1601 EPI.Variadic = true;
1602 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001603 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001604 std::make_pair(".global_tid.", KmpInt32Ty),
1605 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001606 std::make_pair(".privates.",
1607 Context.VoidPtrTy.withConst().withRestrict()),
1608 std::make_pair(
1609 ".copy_fn.",
1610 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 std::make_pair(StringRef(), QualType()) // __context with shared vars
1612 };
1613 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1614 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001615 // Mark this captured region as inlined, because we don't use outlined
1616 // function directly.
1617 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1618 AlwaysInlineAttr::CreateImplicit(
1619 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 break;
1621 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001622 case OMPD_ordered: {
1623 Sema::CapturedParamNameType Params[] = {
1624 std::make_pair(StringRef(), QualType()) // __context with shared vars
1625 };
1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1627 Params);
1628 break;
1629 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001630 case OMPD_atomic: {
1631 Sema::CapturedParamNameType Params[] = {
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Michael Wong65f367f2015-07-21 13:44:28 +00001638 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001639 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001640 case OMPD_target_parallel:
1641 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001642 Sema::CapturedParamNameType Params[] = {
1643 std::make_pair(StringRef(), QualType()) // __context with shared vars
1644 };
1645 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1646 Params);
1647 break;
1648 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001649 case OMPD_teams: {
1650 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001651 QualType KmpInt32PtrTy =
1652 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001653 Sema::CapturedParamNameType Params[] = {
1654 std::make_pair(".global_tid.", KmpInt32PtrTy),
1655 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1656 std::make_pair(StringRef(), QualType()) // __context with shared vars
1657 };
1658 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1659 Params);
1660 break;
1661 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001662 case OMPD_taskgroup: {
1663 Sema::CapturedParamNameType Params[] = {
1664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
1668 break;
1669 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001670 case OMPD_taskloop: {
1671 Sema::CapturedParamNameType Params[] = {
1672 std::make_pair(StringRef(), QualType()) // __context with shared vars
1673 };
1674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1675 Params);
1676 break;
1677 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001678 case OMPD_taskloop_simd: {
1679 Sema::CapturedParamNameType Params[] = {
1680 std::make_pair(StringRef(), QualType()) // __context with shared vars
1681 };
1682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1683 Params);
1684 break;
1685 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001686 case OMPD_distribute: {
1687 Sema::CapturedParamNameType Params[] = {
1688 std::make_pair(StringRef(), QualType()) // __context with shared vars
1689 };
1690 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1691 Params);
1692 break;
1693 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001694 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001695 case OMPD_taskyield:
1696 case OMPD_barrier:
1697 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001698 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001699 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001700 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001701 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001702 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001703 case OMPD_declare_reduction:
Alexey Bataev9959db52014-05-06 10:08:46 +00001704 llvm_unreachable("OpenMP Directive is not allowed");
1705 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001706 llvm_unreachable("Unknown OpenMP directive");
1707 }
1708}
1709
Alexey Bataev3392d762016-02-16 11:18:12 +00001710static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev61205072016-03-02 04:57:40 +00001711 Expr *CaptureExpr, bool WithInit) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001712 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001713 ASTContext &C = S.getASTContext();
1714 Expr *Init = CaptureExpr->IgnoreImpCasts();
1715 QualType Ty = Init->getType();
1716 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1717 if (S.getLangOpts().CPlusPlus)
1718 Ty = C.getLValueReferenceType(Ty);
1719 else {
1720 Ty = C.getPointerType(Ty);
1721 ExprResult Res =
1722 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1723 if (!Res.isUsable())
1724 return nullptr;
1725 Init = Res.get();
1726 }
Alexey Bataev61205072016-03-02 04:57:40 +00001727 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001728 }
1729 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001730 if (!WithInit)
1731 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001732 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001733 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1734 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001735 return CED;
1736}
1737
Alexey Bataev61205072016-03-02 04:57:40 +00001738static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1739 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001740 OMPCapturedExprDecl *CD;
1741 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1742 CD = cast<OMPCapturedExprDecl>(VD);
1743 else
Alexey Bataev61205072016-03-02 04:57:40 +00001744 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit);
Alexey Bataev3392d762016-02-16 11:18:12 +00001745 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1746 SourceLocation());
1747}
1748
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001749static DeclRefExpr *buildCapture(Sema &S, Expr *CaptureExpr) {
Alexey Bataev61205072016-03-02 04:57:40 +00001750 auto *CD =
1751 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1752 CaptureExpr, /*WithInit=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001753 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1754 SourceLocation());
Alexey Bataev4244be22016-02-11 05:35:55 +00001755}
1756
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001757StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1758 ArrayRef<OMPClause *> Clauses) {
1759 if (!S.isUsable()) {
1760 ActOnCapturedRegionError();
1761 return StmtError();
1762 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001763
1764 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001765 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001766 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001767 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001768 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001769 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001770 Clause->getClauseKind() == OMPC_copyprivate ||
1771 (getLangOpts().OpenMPUseTLS &&
1772 getASTContext().getTargetInfo().isTLSSupported() &&
1773 Clause->getClauseKind() == OMPC_copyin)) {
1774 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001775 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001776 for (auto *VarRef : Clause->children()) {
1777 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001778 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001779 }
1780 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001781 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001782 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001783 // Mark all variables in private list clauses as used in inner region.
1784 // Required for proper codegen of combined directives.
1785 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001786 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001787 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1788 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001789 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1790 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001791 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001792 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1793 if (auto *E = C->getPostUpdateExpr())
1794 MarkDeclarationsReferencedInExpr(E);
1795 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001796 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001797 if (Clause->getClauseKind() == OMPC_schedule)
1798 SC = cast<OMPScheduleClause>(Clause);
1799 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001800 OC = cast<OMPOrderedClause>(Clause);
1801 else if (Clause->getClauseKind() == OMPC_linear)
1802 LCs.push_back(cast<OMPLinearClause>(Clause));
1803 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001804 bool ErrorFound = false;
1805 // OpenMP, 2.7.1 Loop Construct, Restrictions
1806 // The nonmonotonic modifier cannot be specified if an ordered clause is
1807 // specified.
1808 if (SC &&
1809 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1810 SC->getSecondScheduleModifier() ==
1811 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1812 OC) {
1813 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1814 ? SC->getFirstScheduleModifierLoc()
1815 : SC->getSecondScheduleModifierLoc(),
1816 diag::err_omp_schedule_nonmonotonic_ordered)
1817 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1818 ErrorFound = true;
1819 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001820 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1821 for (auto *C : LCs) {
1822 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1823 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1824 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001825 ErrorFound = true;
1826 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001827 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1828 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1829 OC->getNumForLoops()) {
1830 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1831 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1832 ErrorFound = true;
1833 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001834 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001835 ActOnCapturedRegionError();
1836 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001837 }
1838 return ActOnCapturedRegionEnd(S.get());
1839}
1840
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001841static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1842 OpenMPDirectiveKind CurrentRegion,
1843 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001844 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001845 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001846 // Allowed nesting of constructs
1847 // +------------------+-----------------+------------------------------------+
1848 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1849 // +------------------+-----------------+------------------------------------+
1850 // | parallel | parallel | * |
1851 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001852 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001853 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001854 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001855 // | parallel | simd | * |
1856 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001857 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001858 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001859 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001860 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001861 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001862 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001863 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001864 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001865 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001866 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001867 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001868 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001869 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001870 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001871 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001872 // | parallel | target parallel | * |
1873 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001874 // | parallel | target enter | * |
1875 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001876 // | parallel | target exit | * |
1877 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001878 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001879 // | parallel | cancellation | |
1880 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001881 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001882 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001883 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001884 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001885 // +------------------+-----------------+------------------------------------+
1886 // | for | parallel | * |
1887 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001888 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001889 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001890 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001891 // | for | simd | * |
1892 // | for | sections | + |
1893 // | for | section | + |
1894 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001895 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001896 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001897 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001898 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001899 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001900 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001901 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001902 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001903 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001904 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001905 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001906 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001907 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001908 // | for | target parallel | * |
1909 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001910 // | for | target enter | * |
1911 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001912 // | for | target exit | * |
1913 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001914 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001915 // | for | cancellation | |
1916 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001917 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001918 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001919 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001920 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001921 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001922 // | master | parallel | * |
1923 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001924 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001925 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001926 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001927 // | master | simd | * |
1928 // | master | sections | + |
1929 // | master | section | + |
1930 // | master | single | + |
1931 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001932 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001933 // | master |parallel sections| * |
1934 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001935 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001936 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001937 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001938 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001939 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001940 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001941 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001942 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001943 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001944 // | master | target parallel | * |
1945 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001946 // | master | target enter | * |
1947 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001948 // | master | target exit | * |
1949 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001950 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001951 // | master | cancellation | |
1952 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001953 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001954 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001955 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001956 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001957 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 // | critical | parallel | * |
1959 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001960 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001961 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001962 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001963 // | critical | simd | * |
1964 // | critical | sections | + |
1965 // | critical | section | + |
1966 // | critical | single | + |
1967 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001968 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001969 // | critical |parallel sections| * |
1970 // | critical | task | * |
1971 // | critical | taskyield | * |
1972 // | critical | barrier | + |
1973 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001974 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001975 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001976 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001977 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001978 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001979 // | critical | target parallel | * |
1980 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001981 // | critical | target enter | * |
1982 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001983 // | critical | target exit | * |
1984 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001985 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001986 // | critical | cancellation | |
1987 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001988 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001989 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001990 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001991 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001992 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001993 // | simd | parallel | |
1994 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001995 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001996 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001997 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00001998 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001999 // | simd | sections | |
2000 // | simd | section | |
2001 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002002 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002003 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002004 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002005 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002006 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002007 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002008 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002009 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002010 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002011 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002012 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002013 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002014 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002015 // | simd | target parallel | |
2016 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002017 // | simd | target enter | |
2018 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002019 // | simd | target exit | |
2020 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002021 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002022 // | simd | cancellation | |
2023 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002024 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002025 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002026 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002027 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002028 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002029 // | for simd | parallel | |
2030 // | for simd | for | |
2031 // | for simd | for simd | |
2032 // | for simd | master | |
2033 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002034 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002035 // | for simd | sections | |
2036 // | for simd | section | |
2037 // | for simd | single | |
2038 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002039 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002040 // | for simd |parallel sections| |
2041 // | for simd | task | |
2042 // | for simd | taskyield | |
2043 // | for simd | barrier | |
2044 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002045 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002046 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002047 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002048 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002049 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002050 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002051 // | for simd | target parallel | |
2052 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002053 // | for simd | target enter | |
2054 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002055 // | for simd | target exit | |
2056 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002057 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002058 // | for simd | cancellation | |
2059 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002060 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002061 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002062 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002063 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002064 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002065 // | parallel for simd| parallel | |
2066 // | parallel for simd| for | |
2067 // | parallel for simd| for simd | |
2068 // | parallel for simd| master | |
2069 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002070 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002071 // | parallel for simd| sections | |
2072 // | parallel for simd| section | |
2073 // | parallel for simd| single | |
2074 // | parallel for simd| parallel for | |
2075 // | parallel for simd|parallel for simd| |
2076 // | parallel for simd|parallel sections| |
2077 // | parallel for simd| task | |
2078 // | parallel for simd| taskyield | |
2079 // | parallel for simd| barrier | |
2080 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002081 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002082 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002083 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002084 // | parallel for simd| atomic | |
2085 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002086 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002087 // | parallel for simd| target parallel | |
2088 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002089 // | parallel for simd| target enter | |
2090 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002091 // | parallel for simd| target exit | |
2092 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002093 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002094 // | parallel for simd| cancellation | |
2095 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002096 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002097 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002098 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002099 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002100 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002101 // | sections | parallel | * |
2102 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002103 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002104 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002105 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002106 // | sections | simd | * |
2107 // | sections | sections | + |
2108 // | sections | section | * |
2109 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002110 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002111 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002112 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002113 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002114 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002115 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002116 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002117 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002118 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002119 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002120 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002121 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002122 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002123 // | sections | target parallel | * |
2124 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002125 // | sections | target enter | * |
2126 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002127 // | sections | target exit | * |
2128 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002129 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002130 // | sections | cancellation | |
2131 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002132 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002133 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002134 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002135 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002136 // +------------------+-----------------+------------------------------------+
2137 // | section | parallel | * |
2138 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002139 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002140 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002141 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002142 // | section | simd | * |
2143 // | section | sections | + |
2144 // | section | section | + |
2145 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002146 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002147 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002148 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002149 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002150 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002151 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002152 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002153 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002154 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002155 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002156 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002157 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002158 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002159 // | section | target parallel | * |
2160 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002161 // | section | target enter | * |
2162 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002163 // | section | target exit | * |
2164 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002165 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002166 // | section | cancellation | |
2167 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002168 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002169 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002170 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002171 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002172 // +------------------+-----------------+------------------------------------+
2173 // | single | parallel | * |
2174 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002175 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002176 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002177 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002178 // | single | simd | * |
2179 // | single | sections | + |
2180 // | single | section | + |
2181 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002182 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002183 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002184 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002185 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002186 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002187 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002188 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002189 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002190 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002191 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002192 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002193 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002194 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002195 // | single | target parallel | * |
2196 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002197 // | single | target enter | * |
2198 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002199 // | single | target exit | * |
2200 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002201 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002202 // | single | cancellation | |
2203 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002204 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002205 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002206 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002207 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002208 // +------------------+-----------------+------------------------------------+
2209 // | parallel for | parallel | * |
2210 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002211 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002212 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002213 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002214 // | parallel for | simd | * |
2215 // | parallel for | sections | + |
2216 // | parallel for | section | + |
2217 // | parallel for | single | + |
2218 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002219 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002220 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002221 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002222 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002223 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002224 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002225 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002226 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002227 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002228 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002229 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002230 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002231 // | parallel for | target parallel | * |
2232 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002233 // | parallel for | target enter | * |
2234 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002235 // | parallel for | target exit | * |
2236 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002237 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002238 // | parallel for | cancellation | |
2239 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002240 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002241 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002242 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002243 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002244 // +------------------+-----------------+------------------------------------+
2245 // | parallel sections| parallel | * |
2246 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002247 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002248 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002249 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002250 // | parallel sections| simd | * |
2251 // | parallel sections| sections | + |
2252 // | parallel sections| section | * |
2253 // | parallel sections| single | + |
2254 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002255 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002256 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002257 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002258 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002259 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002260 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002261 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002262 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002263 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002264 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002265 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002266 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002267 // | parallel sections| target parallel | * |
2268 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002269 // | parallel sections| target enter | * |
2270 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002271 // | parallel sections| target exit | * |
2272 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002273 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002274 // | parallel sections| cancellation | |
2275 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002276 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002277 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002278 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002279 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002280 // +------------------+-----------------+------------------------------------+
2281 // | task | parallel | * |
2282 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002283 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002284 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002285 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002286 // | task | simd | * |
2287 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002288 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002289 // | task | single | + |
2290 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002291 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002292 // | task |parallel sections| * |
2293 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002294 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002295 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002296 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002297 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002298 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002299 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002300 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002301 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002302 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002303 // | task | target parallel | * |
2304 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002305 // | task | target enter | * |
2306 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002307 // | task | target exit | * |
2308 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002309 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002310 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002311 // | | point | ! |
2312 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002313 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002314 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002315 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002316 // +------------------+-----------------+------------------------------------+
2317 // | ordered | parallel | * |
2318 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002319 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002320 // | ordered | master | * |
2321 // | ordered | critical | * |
2322 // | ordered | simd | * |
2323 // | ordered | sections | + |
2324 // | ordered | section | + |
2325 // | ordered | single | + |
2326 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002327 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 // | ordered |parallel sections| * |
2329 // | ordered | task | * |
2330 // | ordered | taskyield | * |
2331 // | ordered | barrier | + |
2332 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002333 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002334 // | ordered | flush | * |
2335 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002336 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002337 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002338 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002339 // | ordered | target parallel | * |
2340 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002341 // | ordered | target enter | * |
2342 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002343 // | ordered | target exit | * |
2344 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002345 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002346 // | ordered | cancellation | |
2347 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002348 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002349 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002350 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002351 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002352 // +------------------+-----------------+------------------------------------+
2353 // | atomic | parallel | |
2354 // | atomic | for | |
2355 // | atomic | for simd | |
2356 // | atomic | master | |
2357 // | atomic | critical | |
2358 // | atomic | simd | |
2359 // | atomic | sections | |
2360 // | atomic | section | |
2361 // | atomic | single | |
2362 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002363 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002364 // | atomic |parallel sections| |
2365 // | atomic | task | |
2366 // | atomic | taskyield | |
2367 // | atomic | barrier | |
2368 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002369 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002370 // | atomic | flush | |
2371 // | atomic | ordered | |
2372 // | atomic | atomic | |
2373 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002374 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002375 // | atomic | target parallel | |
2376 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002377 // | atomic | target enter | |
2378 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002379 // | atomic | target exit | |
2380 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002381 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002382 // | atomic | cancellation | |
2383 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002384 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002385 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002386 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002387 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002388 // +------------------+-----------------+------------------------------------+
2389 // | target | parallel | * |
2390 // | target | for | * |
2391 // | target | for simd | * |
2392 // | target | master | * |
2393 // | target | critical | * |
2394 // | target | simd | * |
2395 // | target | sections | * |
2396 // | target | section | * |
2397 // | target | single | * |
2398 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002399 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002400 // | target |parallel sections| * |
2401 // | target | task | * |
2402 // | target | taskyield | * |
2403 // | target | barrier | * |
2404 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002405 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002406 // | target | flush | * |
2407 // | target | ordered | * |
2408 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002409 // | target | target | |
2410 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002411 // | target | target parallel | |
2412 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002413 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002414 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002415 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002416 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002417 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002418 // | target | cancellation | |
2419 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002420 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002421 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002422 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002423 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002424 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002425 // | target parallel | parallel | * |
2426 // | target parallel | for | * |
2427 // | target parallel | for simd | * |
2428 // | target parallel | master | * |
2429 // | target parallel | critical | * |
2430 // | target parallel | simd | * |
2431 // | target parallel | sections | * |
2432 // | target parallel | section | * |
2433 // | target parallel | single | * |
2434 // | target parallel | parallel for | * |
2435 // | target parallel |parallel for simd| * |
2436 // | target parallel |parallel sections| * |
2437 // | target parallel | task | * |
2438 // | target parallel | taskyield | * |
2439 // | target parallel | barrier | * |
2440 // | target parallel | taskwait | * |
2441 // | target parallel | taskgroup | * |
2442 // | target parallel | flush | * |
2443 // | target parallel | ordered | * |
2444 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002445 // | target parallel | target | |
2446 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002447 // | target parallel | target parallel | |
2448 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002449 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002450 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002451 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002452 // | | data | |
2453 // | target parallel | teams | |
2454 // | target parallel | cancellation | |
2455 // | | point | ! |
2456 // | target parallel | cancel | ! |
2457 // | target parallel | taskloop | * |
2458 // | target parallel | taskloop simd | * |
2459 // | target parallel | distribute | |
2460 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002461 // | target parallel | parallel | * |
2462 // | for | | |
2463 // | target parallel | for | * |
2464 // | for | | |
2465 // | target parallel | for simd | * |
2466 // | for | | |
2467 // | target parallel | master | * |
2468 // | for | | |
2469 // | target parallel | critical | * |
2470 // | for | | |
2471 // | target parallel | simd | * |
2472 // | for | | |
2473 // | target parallel | sections | * |
2474 // | for | | |
2475 // | target parallel | section | * |
2476 // | for | | |
2477 // | target parallel | single | * |
2478 // | for | | |
2479 // | target parallel | parallel for | * |
2480 // | for | | |
2481 // | target parallel |parallel for simd| * |
2482 // | for | | |
2483 // | target parallel |parallel sections| * |
2484 // | for | | |
2485 // | target parallel | task | * |
2486 // | for | | |
2487 // | target parallel | taskyield | * |
2488 // | for | | |
2489 // | target parallel | barrier | * |
2490 // | for | | |
2491 // | target parallel | taskwait | * |
2492 // | for | | |
2493 // | target parallel | taskgroup | * |
2494 // | for | | |
2495 // | target parallel | flush | * |
2496 // | for | | |
2497 // | target parallel | ordered | * |
2498 // | for | | |
2499 // | target parallel | atomic | * |
2500 // | for | | |
2501 // | target parallel | target | |
2502 // | for | | |
2503 // | target parallel | target parallel | |
2504 // | for | | |
2505 // | target parallel | target parallel | |
2506 // | for | for | |
2507 // | target parallel | target enter | |
2508 // | for | data | |
2509 // | target parallel | target exit | |
2510 // | for | data | |
2511 // | target parallel | teams | |
2512 // | for | | |
2513 // | target parallel | cancellation | |
2514 // | for | point | ! |
2515 // | target parallel | cancel | ! |
2516 // | for | | |
2517 // | target parallel | taskloop | * |
2518 // | for | | |
2519 // | target parallel | taskloop simd | * |
2520 // | for | | |
2521 // | target parallel | distribute | |
2522 // | for | | |
2523 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002524 // | teams | parallel | * |
2525 // | teams | for | + |
2526 // | teams | for simd | + |
2527 // | teams | master | + |
2528 // | teams | critical | + |
2529 // | teams | simd | + |
2530 // | teams | sections | + |
2531 // | teams | section | + |
2532 // | teams | single | + |
2533 // | teams | parallel for | * |
2534 // | teams |parallel for simd| * |
2535 // | teams |parallel sections| * |
2536 // | teams | task | + |
2537 // | teams | taskyield | + |
2538 // | teams | barrier | + |
2539 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002540 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002541 // | teams | flush | + |
2542 // | teams | ordered | + |
2543 // | teams | atomic | + |
2544 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002545 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002546 // | teams | target parallel | + |
2547 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002548 // | teams | target enter | + |
2549 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002550 // | teams | target exit | + |
2551 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002552 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002553 // | teams | cancellation | |
2554 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002555 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002556 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002557 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002558 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002559 // +------------------+-----------------+------------------------------------+
2560 // | taskloop | parallel | * |
2561 // | taskloop | for | + |
2562 // | taskloop | for simd | + |
2563 // | taskloop | master | + |
2564 // | taskloop | critical | * |
2565 // | taskloop | simd | * |
2566 // | taskloop | sections | + |
2567 // | taskloop | section | + |
2568 // | taskloop | single | + |
2569 // | taskloop | parallel for | * |
2570 // | taskloop |parallel for simd| * |
2571 // | taskloop |parallel sections| * |
2572 // | taskloop | task | * |
2573 // | taskloop | taskyield | * |
2574 // | taskloop | barrier | + |
2575 // | taskloop | taskwait | * |
2576 // | taskloop | taskgroup | * |
2577 // | taskloop | flush | * |
2578 // | taskloop | ordered | + |
2579 // | taskloop | atomic | * |
2580 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002581 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002582 // | taskloop | target parallel | * |
2583 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002584 // | taskloop | target enter | * |
2585 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002586 // | taskloop | target exit | * |
2587 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002588 // | taskloop | teams | + |
2589 // | taskloop | cancellation | |
2590 // | | point | |
2591 // | taskloop | cancel | |
2592 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002593 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002594 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002595 // | taskloop simd | parallel | |
2596 // | taskloop simd | for | |
2597 // | taskloop simd | for simd | |
2598 // | taskloop simd | master | |
2599 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002600 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002601 // | taskloop simd | sections | |
2602 // | taskloop simd | section | |
2603 // | taskloop simd | single | |
2604 // | taskloop simd | parallel for | |
2605 // | taskloop simd |parallel for simd| |
2606 // | taskloop simd |parallel sections| |
2607 // | taskloop simd | task | |
2608 // | taskloop simd | taskyield | |
2609 // | taskloop simd | barrier | |
2610 // | taskloop simd | taskwait | |
2611 // | taskloop simd | taskgroup | |
2612 // | taskloop simd | flush | |
2613 // | taskloop simd | ordered | + (with simd clause) |
2614 // | taskloop simd | atomic | |
2615 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002616 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002617 // | taskloop simd | target parallel | |
2618 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002619 // | taskloop simd | target enter | |
2620 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002621 // | taskloop simd | target exit | |
2622 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002623 // | taskloop simd | teams | |
2624 // | taskloop simd | cancellation | |
2625 // | | point | |
2626 // | taskloop simd | cancel | |
2627 // | taskloop simd | taskloop | |
2628 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002629 // | taskloop simd | distribute | |
2630 // +------------------+-----------------+------------------------------------+
2631 // | distribute | parallel | * |
2632 // | distribute | for | * |
2633 // | distribute | for simd | * |
2634 // | distribute | master | * |
2635 // | distribute | critical | * |
2636 // | distribute | simd | * |
2637 // | distribute | sections | * |
2638 // | distribute | section | * |
2639 // | distribute | single | * |
2640 // | distribute | parallel for | * |
2641 // | distribute |parallel for simd| * |
2642 // | distribute |parallel sections| * |
2643 // | distribute | task | * |
2644 // | distribute | taskyield | * |
2645 // | distribute | barrier | * |
2646 // | distribute | taskwait | * |
2647 // | distribute | taskgroup | * |
2648 // | distribute | flush | * |
2649 // | distribute | ordered | + |
2650 // | distribute | atomic | * |
2651 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002652 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002653 // | distribute | target parallel | |
2654 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002655 // | distribute | target enter | |
2656 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002657 // | distribute | target exit | |
2658 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002659 // | distribute | teams | |
2660 // | distribute | cancellation | + |
2661 // | | point | |
2662 // | distribute | cancel | + |
2663 // | distribute | taskloop | * |
2664 // | distribute | taskloop simd | * |
2665 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002666 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002667 if (Stack->getCurScope()) {
2668 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002669 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002670 bool NestingProhibited = false;
2671 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002672 enum {
2673 NoRecommend,
2674 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002675 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002676 ShouldBeInTargetRegion,
2677 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002678 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002679 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2680 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002681 // OpenMP [2.16, Nesting of Regions]
2682 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002683 // OpenMP [2.8.1,simd Construct, Restrictions]
2684 // An ordered construct with the simd clause is the only OpenMP construct
2685 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002686 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2687 return true;
2688 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002689 if (ParentRegion == OMPD_atomic) {
2690 // OpenMP [2.16, Nesting of Regions]
2691 // OpenMP constructs may not be nested inside an atomic region.
2692 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2693 return true;
2694 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002695 if (CurrentRegion == OMPD_section) {
2696 // OpenMP [2.7.2, sections Construct, Restrictions]
2697 // Orphaned section directives are prohibited. That is, the section
2698 // directives must appear within the sections construct and must not be
2699 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002700 if (ParentRegion != OMPD_sections &&
2701 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002702 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2703 << (ParentRegion != OMPD_unknown)
2704 << getOpenMPDirectiveName(ParentRegion);
2705 return true;
2706 }
2707 return false;
2708 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002709 // Allow some constructs to be orphaned (they could be used in functions,
2710 // called from OpenMP regions with the required preconditions).
2711 if (ParentRegion == OMPD_unknown)
2712 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002713 if (CurrentRegion == OMPD_cancellation_point ||
2714 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002715 // OpenMP [2.16, Nesting of Regions]
2716 // A cancellation point construct for which construct-type-clause is
2717 // taskgroup must be nested inside a task construct. A cancellation
2718 // point construct for which construct-type-clause is not taskgroup must
2719 // be closely nested inside an OpenMP construct that matches the type
2720 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002721 // A cancel construct for which construct-type-clause is taskgroup must be
2722 // nested inside a task construct. A cancel construct for which
2723 // construct-type-clause is not taskgroup must be closely nested inside an
2724 // OpenMP construct that matches the type specified in
2725 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002726 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002727 !((CancelRegion == OMPD_parallel &&
2728 (ParentRegion == OMPD_parallel ||
2729 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002730 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002731 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2732 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002733 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2734 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002735 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2736 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002737 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002738 // OpenMP [2.16, Nesting of Regions]
2739 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002740 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002741 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002742 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002743 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002744 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2745 // OpenMP [2.16, Nesting of Regions]
2746 // A critical region may not be nested (closely or otherwise) inside a
2747 // critical region with the same name. Note that this restriction is not
2748 // sufficient to prevent deadlock.
2749 SourceLocation PreviousCriticalLoc;
2750 bool DeadLock =
2751 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2752 OpenMPDirectiveKind K,
2753 const DeclarationNameInfo &DNI,
2754 SourceLocation Loc)
2755 ->bool {
2756 if (K == OMPD_critical &&
2757 DNI.getName() == CurrentName.getName()) {
2758 PreviousCriticalLoc = Loc;
2759 return true;
2760 } else
2761 return false;
2762 },
2763 false /* skip top directive */);
2764 if (DeadLock) {
2765 SemaRef.Diag(StartLoc,
2766 diag::err_omp_prohibited_region_critical_same_name)
2767 << CurrentName.getName();
2768 if (PreviousCriticalLoc.isValid())
2769 SemaRef.Diag(PreviousCriticalLoc,
2770 diag::note_omp_previous_critical_region);
2771 return true;
2772 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002773 } else if (CurrentRegion == OMPD_barrier) {
2774 // OpenMP [2.16, Nesting of Regions]
2775 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002776 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002777 NestingProhibited =
2778 isOpenMPWorksharingDirective(ParentRegion) ||
2779 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002780 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002781 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002782 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002783 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002784 // OpenMP [2.16, Nesting of Regions]
2785 // A worksharing region may not be closely nested inside a worksharing,
2786 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002787 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002788 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002789 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002790 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002791 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002792 Recommend = ShouldBeInParallelRegion;
2793 } else if (CurrentRegion == OMPD_ordered) {
2794 // OpenMP [2.16, Nesting of Regions]
2795 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002796 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002797 // An ordered region must be closely nested inside a loop region (or
2798 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002799 // OpenMP [2.8.1,simd Construct, Restrictions]
2800 // An ordered construct with the simd clause is the only OpenMP construct
2801 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002802 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002803 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002804 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002805 !(isOpenMPSimdDirective(ParentRegion) ||
2806 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002807 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002808 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2809 // OpenMP [2.16, Nesting of Regions]
2810 // If specified, a teams construct must be contained within a target
2811 // construct.
2812 NestingProhibited = ParentRegion != OMPD_target;
2813 Recommend = ShouldBeInTargetRegion;
2814 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2815 }
2816 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2817 // OpenMP [2.16, Nesting of Regions]
2818 // distribute, parallel, parallel sections, parallel workshare, and the
2819 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2820 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002821 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2822 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002823 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002824 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002825 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2826 // OpenMP 4.5 [2.17 Nesting of Regions]
2827 // The region associated with the distribute construct must be strictly
2828 // nested inside a teams region
2829 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2830 Recommend = ShouldBeInTeamsRegion;
2831 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002832 if (!NestingProhibited &&
2833 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2834 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2835 // OpenMP 4.5 [2.17 Nesting of Regions]
2836 // If a target, target update, target data, target enter data, or
2837 // target exit data construct is encountered during execution of a
2838 // target region, the behavior is unspecified.
2839 NestingProhibited = Stack->hasDirective(
2840 [&OffendingRegion](OpenMPDirectiveKind K,
2841 const DeclarationNameInfo &DNI,
2842 SourceLocation Loc) -> bool {
2843 if (isOpenMPTargetExecutionDirective(K)) {
2844 OffendingRegion = K;
2845 return true;
2846 } else
2847 return false;
2848 },
2849 false /* don't skip top directive */);
2850 CloseNesting = false;
2851 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002852 if (NestingProhibited) {
2853 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002854 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2855 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002856 return true;
2857 }
2858 }
2859 return false;
2860}
2861
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002862static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2863 ArrayRef<OMPClause *> Clauses,
2864 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2865 bool ErrorFound = false;
2866 unsigned NamedModifiersNumber = 0;
2867 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2868 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002869 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002870 for (const auto *C : Clauses) {
2871 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2872 // At most one if clause without a directive-name-modifier can appear on
2873 // the directive.
2874 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2875 if (FoundNameModifiers[CurNM]) {
2876 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2877 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2878 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2879 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002880 } else if (CurNM != OMPD_unknown) {
2881 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002882 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002883 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002884 FoundNameModifiers[CurNM] = IC;
2885 if (CurNM == OMPD_unknown)
2886 continue;
2887 // Check if the specified name modifier is allowed for the current
2888 // directive.
2889 // At most one if clause with the particular directive-name-modifier can
2890 // appear on the directive.
2891 bool MatchFound = false;
2892 for (auto NM : AllowedNameModifiers) {
2893 if (CurNM == NM) {
2894 MatchFound = true;
2895 break;
2896 }
2897 }
2898 if (!MatchFound) {
2899 S.Diag(IC->getNameModifierLoc(),
2900 diag::err_omp_wrong_if_directive_name_modifier)
2901 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2902 ErrorFound = true;
2903 }
2904 }
2905 }
2906 // If any if clause on the directive includes a directive-name-modifier then
2907 // all if clauses on the directive must include a directive-name-modifier.
2908 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2909 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2910 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2911 diag::err_omp_no_more_if_clause);
2912 } else {
2913 std::string Values;
2914 std::string Sep(", ");
2915 unsigned AllowedCnt = 0;
2916 unsigned TotalAllowedNum =
2917 AllowedNameModifiers.size() - NamedModifiersNumber;
2918 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2919 ++Cnt) {
2920 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2921 if (!FoundNameModifiers[NM]) {
2922 Values += "'";
2923 Values += getOpenMPDirectiveName(NM);
2924 Values += "'";
2925 if (AllowedCnt + 2 == TotalAllowedNum)
2926 Values += " or ";
2927 else if (AllowedCnt + 1 != TotalAllowedNum)
2928 Values += Sep;
2929 ++AllowedCnt;
2930 }
2931 }
2932 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2933 diag::err_omp_unnamed_if_clause)
2934 << (TotalAllowedNum > 1) << Values;
2935 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002936 for (auto Loc : NameModifierLoc) {
2937 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2938 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002939 ErrorFound = true;
2940 }
2941 return ErrorFound;
2942}
2943
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002944StmtResult Sema::ActOnOpenMPExecutableDirective(
2945 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2946 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2947 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002948 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002949 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2950 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002951 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002952
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002953 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002954 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002955 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002956 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002957 if (AStmt) {
2958 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2959
2960 // Check default data sharing attributes for referenced variables.
2961 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2962 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2963 if (DSAChecker.isErrorFound())
2964 return StmtError();
2965 // Generate list of implicitly defined firstprivate variables.
2966 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002967
2968 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2969 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2970 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2971 SourceLocation(), SourceLocation())) {
2972 ClausesWithImplicit.push_back(Implicit);
2973 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2974 DSAChecker.getImplicitFirstprivate().size();
2975 } else
2976 ErrorFound = true;
2977 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002978 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002979
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002980 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002981 switch (Kind) {
2982 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002983 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2984 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002985 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002986 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002987 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002988 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2989 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002990 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002991 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002992 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2993 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002994 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002995 case OMPD_for_simd:
2996 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2997 EndLoc, VarsWithInheritedDSA);
2998 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002999 case OMPD_sections:
3000 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3001 EndLoc);
3002 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003003 case OMPD_section:
3004 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003005 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003006 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3007 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003008 case OMPD_single:
3009 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3010 EndLoc);
3011 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003012 case OMPD_master:
3013 assert(ClausesWithImplicit.empty() &&
3014 "No clauses are allowed for 'omp master' directive");
3015 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3016 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003017 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003018 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3019 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003020 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003021 case OMPD_parallel_for:
3022 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3023 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003024 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003025 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003026 case OMPD_parallel_for_simd:
3027 Res = ActOnOpenMPParallelForSimdDirective(
3028 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003029 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003030 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003031 case OMPD_parallel_sections:
3032 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3033 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003034 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003035 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003036 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003037 Res =
3038 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003039 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003040 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003041 case OMPD_taskyield:
3042 assert(ClausesWithImplicit.empty() &&
3043 "No clauses are allowed for 'omp taskyield' directive");
3044 assert(AStmt == nullptr &&
3045 "No associated statement allowed for 'omp taskyield' directive");
3046 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3047 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003048 case OMPD_barrier:
3049 assert(ClausesWithImplicit.empty() &&
3050 "No clauses are allowed for 'omp barrier' directive");
3051 assert(AStmt == nullptr &&
3052 "No associated statement allowed for 'omp barrier' directive");
3053 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3054 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003055 case OMPD_taskwait:
3056 assert(ClausesWithImplicit.empty() &&
3057 "No clauses are allowed for 'omp taskwait' directive");
3058 assert(AStmt == nullptr &&
3059 "No associated statement allowed for 'omp taskwait' directive");
3060 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3061 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003062 case OMPD_taskgroup:
3063 assert(ClausesWithImplicit.empty() &&
3064 "No clauses are allowed for 'omp taskgroup' directive");
3065 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3066 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003067 case OMPD_flush:
3068 assert(AStmt == nullptr &&
3069 "No associated statement allowed for 'omp flush' directive");
3070 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3071 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003072 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003073 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3074 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003075 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003076 case OMPD_atomic:
3077 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3078 EndLoc);
3079 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003080 case OMPD_teams:
3081 Res =
3082 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3083 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003084 case OMPD_target:
3085 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3086 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003087 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003088 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003089 case OMPD_target_parallel:
3090 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3091 StartLoc, EndLoc);
3092 AllowedNameModifiers.push_back(OMPD_target);
3093 AllowedNameModifiers.push_back(OMPD_parallel);
3094 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003095 case OMPD_target_parallel_for:
3096 Res = ActOnOpenMPTargetParallelForDirective(
3097 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3098 AllowedNameModifiers.push_back(OMPD_target);
3099 AllowedNameModifiers.push_back(OMPD_parallel);
3100 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003101 case OMPD_cancellation_point:
3102 assert(ClausesWithImplicit.empty() &&
3103 "No clauses are allowed for 'omp cancellation point' directive");
3104 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3105 "cancellation point' directive");
3106 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3107 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003108 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003109 assert(AStmt == nullptr &&
3110 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003111 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3112 CancelRegion);
3113 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003114 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003115 case OMPD_target_data:
3116 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3117 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003118 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003119 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003120 case OMPD_target_enter_data:
3121 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3122 EndLoc);
3123 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3124 break;
Samuel Antao72590762016-01-19 20:04:50 +00003125 case OMPD_target_exit_data:
3126 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3127 EndLoc);
3128 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3129 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003130 case OMPD_taskloop:
3131 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3132 EndLoc, VarsWithInheritedDSA);
3133 AllowedNameModifiers.push_back(OMPD_taskloop);
3134 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003135 case OMPD_taskloop_simd:
3136 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3137 EndLoc, VarsWithInheritedDSA);
3138 AllowedNameModifiers.push_back(OMPD_taskloop);
3139 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003140 case OMPD_distribute:
3141 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3142 EndLoc, VarsWithInheritedDSA);
3143 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003144 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003145 case OMPD_declare_reduction:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003146 llvm_unreachable("OpenMP Directive is not allowed");
3147 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003148 llvm_unreachable("Unknown OpenMP directive");
3149 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003150
Alexey Bataev4acb8592014-07-07 13:01:15 +00003151 for (auto P : VarsWithInheritedDSA) {
3152 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3153 << P.first << P.second->getSourceRange();
3154 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003155 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3156
3157 if (!AllowedNameModifiers.empty())
3158 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3159 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003160
Alexey Bataeved09d242014-05-28 05:53:51 +00003161 if (ErrorFound)
3162 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003163 return Res;
3164}
3165
3166StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3167 Stmt *AStmt,
3168 SourceLocation StartLoc,
3169 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003170 if (!AStmt)
3171 return StmtError();
3172
Alexey Bataev9959db52014-05-06 10:08:46 +00003173 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3174 // 1.2.2 OpenMP Language Terminology
3175 // Structured block - An executable statement with a single entry at the
3176 // top and a single exit at the bottom.
3177 // The point of exit cannot be a branch out of the structured block.
3178 // longjmp() and throw() must not violate the entry/exit criteria.
3179 CS->getCapturedDecl()->setNothrow();
3180
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003181 getCurFunction()->setHasBranchProtectedScope();
3182
Alexey Bataev25e5b442015-09-15 12:52:43 +00003183 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3184 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003185}
3186
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003187namespace {
3188/// \brief Helper class for checking canonical form of the OpenMP loops and
3189/// extracting iteration space of each loop in the loop nest, that will be used
3190/// for IR generation.
3191class OpenMPIterationSpaceChecker {
3192 /// \brief Reference to Sema.
3193 Sema &SemaRef;
3194 /// \brief A location for diagnostics (when there is no some better location).
3195 SourceLocation DefaultLoc;
3196 /// \brief A location for diagnostics (when increment is not compatible).
3197 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003198 /// \brief A source location for referring to loop init later.
3199 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003200 /// \brief A source location for referring to condition later.
3201 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003202 /// \brief A source location for referring to increment later.
3203 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003204 /// \brief Loop variable.
3205 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003206 /// \brief Reference to loop variable.
3207 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003208 /// \brief Lower bound (initializer for the var).
3209 Expr *LB;
3210 /// \brief Upper bound.
3211 Expr *UB;
3212 /// \brief Loop step (increment).
3213 Expr *Step;
3214 /// \brief This flag is true when condition is one of:
3215 /// Var < UB
3216 /// Var <= UB
3217 /// UB > Var
3218 /// UB >= Var
3219 bool TestIsLessOp;
3220 /// \brief This flag is true when condition is strict ( < or > ).
3221 bool TestIsStrictOp;
3222 /// \brief This flag is true when step is subtracted on each iteration.
3223 bool SubtractStep;
3224
3225public:
3226 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3227 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003228 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
3229 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003230 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
3231 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003232 /// \brief Check init-expr for canonical loop form and save loop counter
3233 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003234 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003235 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3236 /// for less/greater and for strict/non-strict comparison.
3237 bool CheckCond(Expr *S);
3238 /// \brief Check incr-expr for canonical loop form and return true if it
3239 /// does not conform, otherwise save loop step (#Step).
3240 bool CheckInc(Expr *S);
3241 /// \brief Return the loop counter variable.
3242 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003243 /// \brief Return the reference expression to loop counter variable.
3244 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003245 /// \brief Source range of the loop init.
3246 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3247 /// \brief Source range of the loop condition.
3248 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3249 /// \brief Source range of the loop increment.
3250 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3251 /// \brief True if the step should be subtracted.
3252 bool ShouldSubtractStep() const { return SubtractStep; }
3253 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003254 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003255 /// \brief Build the precondition expression for the loops.
3256 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003257 /// \brief Build reference expression to the counter be used for codegen.
3258 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003259 /// \brief Build reference expression to the private counter be used for
3260 /// codegen.
3261 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003262 /// \brief Build initization of the counter be used for codegen.
3263 Expr *BuildCounterInit() const;
3264 /// \brief Build step of the counter be used for codegen.
3265 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003266 /// \brief Return true if any expression is dependent.
3267 bool Dependent() const;
3268
3269private:
3270 /// \brief Check the right-hand side of an assignment in the increment
3271 /// expression.
3272 bool CheckIncRHS(Expr *RHS);
3273 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003274 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003275 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003276 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003277 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003278 /// \brief Helper to set loop increment.
3279 bool SetStep(Expr *NewStep, bool Subtract);
3280};
3281
3282bool OpenMPIterationSpaceChecker::Dependent() const {
3283 if (!Var) {
3284 assert(!LB && !UB && !Step);
3285 return false;
3286 }
3287 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
3288 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
3289}
3290
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003291template <typename T>
3292static T *getExprAsWritten(T *E) {
3293 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3294 E = ExprTemp->getSubExpr();
3295
3296 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3297 E = MTE->GetTemporaryExpr();
3298
3299 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3300 E = Binder->getSubExpr();
3301
3302 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3303 E = ICE->getSubExprAsWritten();
3304 return E->IgnoreParens();
3305}
3306
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003307bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
3308 DeclRefExpr *NewVarRefExpr,
3309 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003310 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003311 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
3312 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003313 if (!NewVar || !NewLB)
3314 return true;
3315 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003316 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003317 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3318 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003319 if ((Ctor->isCopyOrMoveConstructor() ||
3320 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3321 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003322 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003323 LB = NewLB;
3324 return false;
3325}
3326
3327bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003328 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003329 // State consistency checking to ensure correct usage.
3330 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
3331 !TestIsLessOp && !TestIsStrictOp);
3332 if (!NewUB)
3333 return true;
3334 UB = NewUB;
3335 TestIsLessOp = LessOp;
3336 TestIsStrictOp = StrictOp;
3337 ConditionSrcRange = SR;
3338 ConditionLoc = SL;
3339 return false;
3340}
3341
3342bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3343 // State consistency checking to ensure correct usage.
3344 assert(Var != nullptr && LB != nullptr && Step == nullptr);
3345 if (!NewStep)
3346 return true;
3347 if (!NewStep->isValueDependent()) {
3348 // Check that the step is integer expression.
3349 SourceLocation StepLoc = NewStep->getLocStart();
3350 ExprResult Val =
3351 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3352 if (Val.isInvalid())
3353 return true;
3354 NewStep = Val.get();
3355
3356 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3357 // If test-expr is of form var relational-op b and relational-op is < or
3358 // <= then incr-expr must cause var to increase on each iteration of the
3359 // loop. If test-expr is of form var relational-op b and relational-op is
3360 // > or >= then incr-expr must cause var to decrease on each iteration of
3361 // the loop.
3362 // If test-expr is of form b relational-op var and relational-op is < or
3363 // <= then incr-expr must cause var to decrease on each iteration of the
3364 // loop. If test-expr is of form b relational-op var and relational-op is
3365 // > or >= then incr-expr must cause var to increase on each iteration of
3366 // the loop.
3367 llvm::APSInt Result;
3368 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3369 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3370 bool IsConstNeg =
3371 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003372 bool IsConstPos =
3373 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003374 bool IsConstZero = IsConstant && !Result.getBoolValue();
3375 if (UB && (IsConstZero ||
3376 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003377 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003378 SemaRef.Diag(NewStep->getExprLoc(),
3379 diag::err_omp_loop_incr_not_compatible)
3380 << Var << TestIsLessOp << NewStep->getSourceRange();
3381 SemaRef.Diag(ConditionLoc,
3382 diag::note_omp_loop_cond_requres_compatible_incr)
3383 << TestIsLessOp << ConditionSrcRange;
3384 return true;
3385 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003386 if (TestIsLessOp == Subtract) {
3387 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3388 NewStep).get();
3389 Subtract = !Subtract;
3390 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391 }
3392
3393 Step = NewStep;
3394 SubtractStep = Subtract;
3395 return false;
3396}
3397
Alexey Bataev9c821032015-04-30 04:23:23 +00003398bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003399 // Check init-expr for canonical loop form and save loop counter
3400 // variable - #Var and its initialization value - #LB.
3401 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3402 // var = lb
3403 // integer-type var = lb
3404 // random-access-iterator-type var = lb
3405 // pointer-type var = lb
3406 //
3407 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003408 if (EmitDiags) {
3409 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3410 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003411 return true;
3412 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003413 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414 if (Expr *E = dyn_cast<Expr>(S))
3415 S = E->IgnoreParens();
3416 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3417 if (BO->getOpcode() == BO_Assign)
3418 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003419 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003420 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3422 if (DS->isSingleDecl()) {
3423 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003424 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003425 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003426 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003427 SemaRef.Diag(S->getLocStart(),
3428 diag::ext_omp_loop_not_canonical_init)
3429 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003430 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003431 }
3432 }
3433 }
3434 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
3435 if (CE->getOperator() == OO_Equal)
3436 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003437 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
3438 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003439
Alexey Bataev9c821032015-04-30 04:23:23 +00003440 if (EmitDiags) {
3441 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3442 << S->getSourceRange();
3443 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003444 return true;
3445}
3446
Alexey Bataev23b69422014-06-18 07:08:49 +00003447/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003448/// variable (which may be the loop variable) if possible.
3449static const VarDecl *GetInitVarDecl(const Expr *E) {
3450 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003451 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003452 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003453 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3454 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003455 if ((Ctor->isCopyOrMoveConstructor() ||
3456 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3457 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003458 E = CE->getArg(0)->IgnoreParenImpCasts();
3459 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
3460 if (!DRE)
3461 return nullptr;
3462 return dyn_cast<VarDecl>(DRE->getDecl());
3463}
3464
3465bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3466 // Check test-expr for canonical form, save upper-bound UB, flags for
3467 // less/greater and for strict/non-strict comparison.
3468 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3469 // var relational-op b
3470 // b relational-op var
3471 //
3472 if (!S) {
3473 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
3474 return true;
3475 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003476 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003477 SourceLocation CondLoc = S->getLocStart();
3478 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3479 if (BO->isRelationalOp()) {
3480 if (GetInitVarDecl(BO->getLHS()) == Var)
3481 return SetUB(BO->getRHS(),
3482 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3483 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3484 BO->getSourceRange(), BO->getOperatorLoc());
3485 if (GetInitVarDecl(BO->getRHS()) == Var)
3486 return SetUB(BO->getLHS(),
3487 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3488 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3489 BO->getSourceRange(), BO->getOperatorLoc());
3490 }
3491 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3492 if (CE->getNumArgs() == 2) {
3493 auto Op = CE->getOperator();
3494 switch (Op) {
3495 case OO_Greater:
3496 case OO_GreaterEqual:
3497 case OO_Less:
3498 case OO_LessEqual:
3499 if (GetInitVarDecl(CE->getArg(0)) == Var)
3500 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3501 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3502 CE->getOperatorLoc());
3503 if (GetInitVarDecl(CE->getArg(1)) == Var)
3504 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3505 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3506 CE->getOperatorLoc());
3507 break;
3508 default:
3509 break;
3510 }
3511 }
3512 }
3513 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3514 << S->getSourceRange() << Var;
3515 return true;
3516}
3517
3518bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3519 // RHS of canonical loop form increment can be:
3520 // var + incr
3521 // incr + var
3522 // var - incr
3523 //
3524 RHS = RHS->IgnoreParenImpCasts();
3525 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3526 if (BO->isAdditiveOp()) {
3527 bool IsAdd = BO->getOpcode() == BO_Add;
3528 if (GetInitVarDecl(BO->getLHS()) == Var)
3529 return SetStep(BO->getRHS(), !IsAdd);
3530 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3531 return SetStep(BO->getLHS(), false);
3532 }
3533 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3534 bool IsAdd = CE->getOperator() == OO_Plus;
3535 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3536 if (GetInitVarDecl(CE->getArg(0)) == Var)
3537 return SetStep(CE->getArg(1), !IsAdd);
3538 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3539 return SetStep(CE->getArg(0), false);
3540 }
3541 }
3542 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3543 << RHS->getSourceRange() << Var;
3544 return true;
3545}
3546
3547bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3548 // Check incr-expr for canonical loop form and return true if it
3549 // does not conform.
3550 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3551 // ++var
3552 // var++
3553 // --var
3554 // var--
3555 // var += incr
3556 // var -= incr
3557 // var = var + incr
3558 // var = incr + var
3559 // var = var - incr
3560 //
3561 if (!S) {
3562 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3563 return true;
3564 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003565 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003566 S = S->IgnoreParens();
3567 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3568 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3569 return SetStep(
3570 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3571 (UO->isDecrementOp() ? -1 : 1)).get(),
3572 false);
3573 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3574 switch (BO->getOpcode()) {
3575 case BO_AddAssign:
3576 case BO_SubAssign:
3577 if (GetInitVarDecl(BO->getLHS()) == Var)
3578 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3579 break;
3580 case BO_Assign:
3581 if (GetInitVarDecl(BO->getLHS()) == Var)
3582 return CheckIncRHS(BO->getRHS());
3583 break;
3584 default:
3585 break;
3586 }
3587 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3588 switch (CE->getOperator()) {
3589 case OO_PlusPlus:
3590 case OO_MinusMinus:
3591 if (GetInitVarDecl(CE->getArg(0)) == Var)
3592 return SetStep(
3593 SemaRef.ActOnIntegerConstant(
3594 CE->getLocStart(),
3595 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3596 false);
3597 break;
3598 case OO_PlusEqual:
3599 case OO_MinusEqual:
3600 if (GetInitVarDecl(CE->getArg(0)) == Var)
3601 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3602 break;
3603 case OO_Equal:
3604 if (GetInitVarDecl(CE->getArg(0)) == Var)
3605 return CheckIncRHS(CE->getArg(1));
3606 break;
3607 default:
3608 break;
3609 }
3610 }
3611 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3612 << S->getSourceRange() << Var;
3613 return true;
3614}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003615
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003616namespace {
3617// Transform variables declared in GNU statement expressions to new ones to
3618// avoid crash on codegen.
3619class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3620 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3621
3622public:
3623 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3624
3625 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3626 if (auto *VD = cast<VarDecl>(D))
3627 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3628 !isa<ImplicitParamDecl>(D)) {
3629 auto *NewVD = VarDecl::Create(
3630 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3631 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3632 VD->getTypeSourceInfo(), VD->getStorageClass());
3633 NewVD->setTSCSpec(VD->getTSCSpec());
3634 NewVD->setInit(VD->getInit());
3635 NewVD->setInitStyle(VD->getInitStyle());
3636 NewVD->setExceptionVariable(VD->isExceptionVariable());
3637 NewVD->setNRVOVariable(VD->isNRVOVariable());
Alexey Bataev11481f52016-02-17 10:29:05 +00003638 NewVD->setCXXForRangeDecl(VD->isCXXForRangeDecl());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003639 NewVD->setConstexpr(VD->isConstexpr());
3640 NewVD->setInitCapture(VD->isInitCapture());
3641 NewVD->setPreviousDeclInSameBlockScope(
3642 VD->isPreviousDeclInSameBlockScope());
3643 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003644 if (VD->hasAttrs())
3645 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003646 transformedLocalDecl(VD, NewVD);
3647 return NewVD;
3648 }
3649 return BaseTransform::TransformDefinition(Loc, D);
3650 }
3651
3652 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3653 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3654 if (E->getDecl() != NewD) {
3655 NewD->setReferenced();
3656 NewD->markUsed(SemaRef.Context);
3657 return DeclRefExpr::Create(
3658 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3659 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3660 E->getNameInfo(), E->getType(), E->getValueKind());
3661 }
3662 return BaseTransform::TransformDeclRefExpr(E);
3663 }
3664};
3665}
3666
Alexander Musmana5f070a2014-10-01 06:03:56 +00003667/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003668Expr *
3669OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3670 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003671 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003672 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003673 auto VarType = Var->getType().getNonReferenceType();
3674 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003675 SemaRef.getLangOpts().CPlusPlus) {
3676 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003677 auto *UBExpr = TestIsLessOp ? UB : LB;
3678 auto *LBExpr = TestIsLessOp ? LB : UB;
3679 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3680 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3681 if (!Upper || !Lower)
3682 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003683 if (!SemaRef.Context.hasSameType(Upper->getType(), UBExpr->getType())) {
3684 Upper = SemaRef
3685 .PerformImplicitConversion(Upper, UBExpr->getType(),
3686 Sema::AA_Converting,
3687 /*AllowExplicit=*/true)
3688 .get();
3689 }
3690 if (!SemaRef.Context.hasSameType(Lower->getType(), LBExpr->getType())) {
3691 Lower = SemaRef
3692 .PerformImplicitConversion(Lower, LBExpr->getType(),
3693 Sema::AA_Converting,
3694 /*AllowExplicit=*/true)
3695 .get();
3696 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003697 if (!Upper || !Lower)
3698 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003699
3700 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3701
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003702 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003703 // BuildBinOp already emitted error, this one is to point user to upper
3704 // and lower bound, and to tell what is passed to 'operator-'.
3705 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3706 << Upper->getSourceRange() << Lower->getSourceRange();
3707 return nullptr;
3708 }
3709 }
3710
3711 if (!Diff.isUsable())
3712 return nullptr;
3713
3714 // Upper - Lower [- 1]
3715 if (TestIsStrictOp)
3716 Diff = SemaRef.BuildBinOp(
3717 S, DefaultLoc, BO_Sub, Diff.get(),
3718 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3719 if (!Diff.isUsable())
3720 return nullptr;
3721
3722 // Upper - Lower [- 1] + Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003723 auto *StepNoImp = Step->IgnoreImplicit();
3724 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003725 if (NewStep.isInvalid())
3726 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003727 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3728 StepNoImp->getType())) {
3729 NewStep = SemaRef.PerformImplicitConversion(
3730 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3731 /*AllowExplicit=*/true);
3732 if (NewStep.isInvalid())
3733 return nullptr;
3734 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003735 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003736 if (!Diff.isUsable())
3737 return nullptr;
3738
3739 // Parentheses (for dumping/debugging purposes only).
3740 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3741 if (!Diff.isUsable())
3742 return nullptr;
3743
3744 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataev11481f52016-02-17 10:29:05 +00003745 NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003746 if (NewStep.isInvalid())
3747 return nullptr;
Alexey Bataev11481f52016-02-17 10:29:05 +00003748 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
3749 StepNoImp->getType())) {
3750 NewStep = SemaRef.PerformImplicitConversion(
3751 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
3752 /*AllowExplicit=*/true);
3753 if (NewStep.isInvalid())
3754 return nullptr;
3755 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003756 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003757 if (!Diff.isUsable())
3758 return nullptr;
3759
Alexander Musman174b3ca2014-10-06 11:16:29 +00003760 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003761 QualType Type = Diff.get()->getType();
3762 auto &C = SemaRef.Context;
3763 bool UseVarType = VarType->hasIntegerRepresentation() &&
3764 C.getTypeSize(Type) > C.getTypeSize(VarType);
3765 if (!Type->isIntegerType() || UseVarType) {
3766 unsigned NewSize =
3767 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3768 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3769 : Type->hasSignedIntegerRepresentation();
3770 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00003771 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
3772 Diff = SemaRef.PerformImplicitConversion(
3773 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3774 if (!Diff.isUsable())
3775 return nullptr;
3776 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003777 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003778 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003779 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3780 if (NewSize != C.getTypeSize(Type)) {
3781 if (NewSize < C.getTypeSize(Type)) {
3782 assert(NewSize == 64 && "incorrect loop var size");
3783 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3784 << InitSrcRange << ConditionSrcRange;
3785 }
3786 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003787 NewSize, Type->hasSignedIntegerRepresentation() ||
3788 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00003789 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
3790 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3791 Sema::AA_Converting, true);
3792 if (!Diff.isUsable())
3793 return nullptr;
3794 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003795 }
3796 }
3797
Alexander Musmana5f070a2014-10-01 06:03:56 +00003798 return Diff.get();
3799}
3800
Alexey Bataev62dbb972015-04-22 11:59:37 +00003801Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3802 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3803 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3804 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003805 TransformToNewDefs Transform(SemaRef);
3806
3807 auto NewLB = Transform.TransformExpr(LB);
3808 auto NewUB = Transform.TransformExpr(UB);
3809 if (NewLB.isInvalid() || NewUB.isInvalid())
3810 return Cond;
Alexey Bataev11481f52016-02-17 10:29:05 +00003811 if (!SemaRef.Context.hasSameType(NewLB.get()->getType(), LB->getType())) {
3812 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3813 Sema::AA_Converting,
3814 /*AllowExplicit=*/true);
3815 }
3816 if (!SemaRef.Context.hasSameType(NewUB.get()->getType(), UB->getType())) {
3817 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3818 Sema::AA_Converting,
3819 /*AllowExplicit=*/true);
3820 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003821 if (NewLB.isInvalid() || NewUB.isInvalid())
3822 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003823 auto CondExpr = SemaRef.BuildBinOp(
3824 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3825 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003826 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003827 if (CondExpr.isUsable()) {
Alexey Bataev11481f52016-02-17 10:29:05 +00003828 if (!SemaRef.Context.hasSameType(CondExpr.get()->getType(),
3829 SemaRef.Context.BoolTy))
3830 CondExpr = SemaRef.PerformImplicitConversion(
3831 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3832 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003833 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003834 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3835 // Otherwise use original loop conditon and evaluate it in runtime.
3836 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3837}
3838
Alexander Musmana5f070a2014-10-01 06:03:56 +00003839/// \brief Build reference expression to the counter be used for codegen.
3840Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003841 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3842 DefaultLoc);
3843}
3844
3845Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3846 if (Var && !Var->isInvalidDecl()) {
3847 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003848 auto *PrivateVar =
3849 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3850 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003851 if (PrivateVar->isInvalidDecl())
3852 return nullptr;
3853 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3854 }
3855 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003856}
3857
3858/// \brief Build initization of the counter be used for codegen.
3859Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3860
3861/// \brief Build step of the counter be used for codegen.
3862Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3863
3864/// \brief Iteration space of a single for loop.
3865struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003866 /// \brief Condition of the loop.
3867 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003868 /// \brief This expression calculates the number of iterations in the loop.
3869 /// It is always possible to calculate it before starting the loop.
3870 Expr *NumIterations;
3871 /// \brief The loop counter variable.
3872 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003873 /// \brief Private loop counter variable.
3874 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003875 /// \brief This is initializer for the initial value of #CounterVar.
3876 Expr *CounterInit;
3877 /// \brief This is step for the #CounterVar used to generate its update:
3878 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3879 Expr *CounterStep;
3880 /// \brief Should step be subtracted?
3881 bool Subtract;
3882 /// \brief Source range of the loop init.
3883 SourceRange InitSrcRange;
3884 /// \brief Source range of the loop condition.
3885 SourceRange CondSrcRange;
3886 /// \brief Source range of the loop increment.
3887 SourceRange IncSrcRange;
3888};
3889
Alexey Bataev23b69422014-06-18 07:08:49 +00003890} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003891
Alexey Bataev9c821032015-04-30 04:23:23 +00003892void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3893 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3894 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003895 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
3896 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00003897 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3898 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003899 if (!ISC.CheckInit(Init, /*EmitDiags=*/false))
Alexey Bataev9c821032015-04-30 04:23:23 +00003900 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
Alexey Bataeva636c7f2015-12-23 10:27:45 +00003901 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00003902 }
3903}
3904
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905/// \brief Called on a for stmt to check and extract its iteration space
3906/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003907static bool CheckOpenMPIterationSpace(
3908 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3909 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003910 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003911 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003912 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003913 // OpenMP [2.6, Canonical Loop Form]
3914 // for (init-expr; test-expr; incr-expr) structured-block
3915 auto For = dyn_cast_or_null<ForStmt>(S);
3916 if (!For) {
3917 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003918 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3919 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3920 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3921 if (NestedLoopCount > 1) {
3922 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3923 SemaRef.Diag(DSA.getConstructLoc(),
3924 diag::note_omp_collapse_ordered_expr)
3925 << 2 << CollapseLoopCountExpr->getSourceRange()
3926 << OrderedLoopCountExpr->getSourceRange();
3927 else if (CollapseLoopCountExpr)
3928 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3929 diag::note_omp_collapse_ordered_expr)
3930 << 0 << CollapseLoopCountExpr->getSourceRange();
3931 else
3932 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3933 diag::note_omp_collapse_ordered_expr)
3934 << 1 << OrderedLoopCountExpr->getSourceRange();
3935 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936 return true;
3937 }
3938 assert(For->getBody());
3939
3940 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3941
3942 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003943 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003944 if (ISC.CheckInit(Init)) {
3945 return true;
3946 }
3947
3948 bool HasErrors = false;
3949
3950 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003951 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003952
3953 // OpenMP [2.6, Canonical Loop Form]
3954 // Var is one of the following:
3955 // A variable of signed or unsigned integer type.
3956 // For C++, a variable of a random access iterator type.
3957 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003958 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3960 !VarType->isPointerType() &&
3961 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3962 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3963 << SemaRef.getLangOpts().CPlusPlus;
3964 HasErrors = true;
3965 }
3966
Alexey Bataev4acb8592014-07-07 13:01:15 +00003967 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3968 // Construct
3969 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3970 // parallel for construct is (are) private.
3971 // The loop iteration variable in the associated for-loop of a simd construct
3972 // with just one associated for-loop is linear with a constant-linear-step
3973 // that is the increment of the associated for-loop.
3974 // Exclude loop var from the list of variables with implicitly defined data
3975 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003976 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003977
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003978 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3979 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003980 // The loop iteration variable in the associated for-loop of a simd construct
3981 // with just one associated for-loop may be listed in a linear clause with a
3982 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003983 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3984 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003985 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003986 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3987 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3988 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003989 auto PredeterminedCKind =
3990 isOpenMPSimdDirective(DKind)
3991 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3992 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003993 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003994 DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003995 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
Alexey Bataeve648e802015-12-25 13:38:08 +00003996 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003997 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataeve648e802015-12-25 13:38:08 +00003998 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
3999 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004000 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00004001 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4002 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00004003 if (DVar.RefExpr == nullptr)
4004 DVar.CKind = PredeterminedCKind;
4005 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004006 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004007 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00004008 // Make the loop iteration variable private (for worksharing constructs),
4009 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00004010 // lastprivate (for simd directives with several collapsed or ordered
4011 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00004012 if (DVar.CKind == OMPC_unknown)
4013 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
4014 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00004015 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004016 }
4017
Alexey Bataev7ff55242014-06-19 09:13:45 +00004018 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00004019
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004020 // Check test-expr.
4021 HasErrors |= ISC.CheckCond(For->getCond());
4022
4023 // Check incr-expr.
4024 HasErrors |= ISC.CheckInc(For->getInc());
4025
Alexander Musmana5f070a2014-10-01 06:03:56 +00004026 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004027 return HasErrors;
4028
Alexander Musmana5f070a2014-10-01 06:03:56 +00004029 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004030 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00004031 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004032 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004033 isOpenMPTaskLoopDirective(DKind) ||
4034 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004035 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00004036 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004037 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4038 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4039 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4040 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4041 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4042 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4043
Alexey Bataev62dbb972015-04-22 11:59:37 +00004044 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4045 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004046 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004047 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 ResultIterSpace.CounterInit == nullptr ||
4049 ResultIterSpace.CounterStep == nullptr);
4050
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004051 return HasErrors;
4052}
4053
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004054/// \brief Build 'VarRef = Start.
4055static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
4056 ExprResult VarRef, ExprResult Start) {
4057 TransformToNewDefs Transform(SemaRef);
4058 // Build 'VarRef = Start.
Alexey Bataev11481f52016-02-17 10:29:05 +00004059 auto *StartNoImp = Start.get()->IgnoreImplicit();
4060 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004061 if (NewStart.isInvalid())
4062 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004063 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4064 StartNoImp->getType())) {
4065 NewStart = SemaRef.PerformImplicitConversion(
4066 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4067 /*AllowExplicit=*/true);
4068 if (NewStart.isInvalid())
4069 return ExprError();
4070 }
4071 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4072 VarRef.get()->getType())) {
4073 NewStart = SemaRef.PerformImplicitConversion(
4074 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4075 /*AllowExplicit=*/true);
4076 if (!NewStart.isUsable())
4077 return ExprError();
4078 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004079
4080 auto Init =
4081 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4082 return Init;
4083}
4084
Alexander Musmana5f070a2014-10-01 06:03:56 +00004085/// \brief Build 'VarRef = Start + Iter * Step'.
4086static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
4087 SourceLocation Loc, ExprResult VarRef,
4088 ExprResult Start, ExprResult Iter,
4089 ExprResult Step, bool Subtract) {
4090 // Add parentheses (for debugging purposes only).
4091 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4092 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4093 !Step.isUsable())
4094 return ExprError();
4095
Alexey Bataev11481f52016-02-17 10:29:05 +00004096 auto *StepNoImp = Step.get()->IgnoreImplicit();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004097 TransformToNewDefs Transform(SemaRef);
Alexey Bataev11481f52016-02-17 10:29:05 +00004098 auto NewStep = Transform.TransformExpr(StepNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004099 if (NewStep.isInvalid())
4100 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004101 if (!SemaRef.Context.hasSameType(NewStep.get()->getType(),
4102 StepNoImp->getType())) {
4103 NewStep = SemaRef.PerformImplicitConversion(
4104 NewStep.get(), StepNoImp->getType(), Sema::AA_Converting,
4105 /*AllowExplicit=*/true);
4106 if (NewStep.isInvalid())
4107 return ExprError();
4108 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004109 ExprResult Update =
4110 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004111 if (!Update.isUsable())
4112 return ExprError();
4113
Alexey Bataevc0214e02016-02-16 12:13:49 +00004114 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4115 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev11481f52016-02-17 10:29:05 +00004116 auto *StartNoImp = Start.get()->IgnoreImplicit();
4117 auto NewStart = Transform.TransformExpr(StartNoImp);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004118 if (NewStart.isInvalid())
4119 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004120 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4121 StartNoImp->getType())) {
4122 NewStart = SemaRef.PerformImplicitConversion(
4123 NewStart.get(), StartNoImp->getType(), Sema::AA_Converting,
4124 /*AllowExplicit=*/true);
4125 if (NewStart.isInvalid())
4126 return ExprError();
4127 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004128
Alexey Bataevc0214e02016-02-16 12:13:49 +00004129 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4130 ExprResult SavedUpdate = Update;
4131 ExprResult UpdateVal;
4132 if (VarRef.get()->getType()->isOverloadableType() ||
4133 NewStart.get()->getType()->isOverloadableType() ||
4134 Update.get()->getType()->isOverloadableType()) {
4135 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4136 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4137 Update =
4138 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4139 if (Update.isUsable()) {
4140 UpdateVal =
4141 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4142 VarRef.get(), SavedUpdate.get());
4143 if (UpdateVal.isUsable()) {
4144 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4145 UpdateVal.get());
4146 }
4147 }
4148 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4149 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004150
Alexey Bataevc0214e02016-02-16 12:13:49 +00004151 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4152 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4153 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4154 NewStart.get(), SavedUpdate.get());
4155 if (!Update.isUsable())
4156 return ExprError();
4157
Alexey Bataev11481f52016-02-17 10:29:05 +00004158 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4159 VarRef.get()->getType())) {
4160 Update = SemaRef.PerformImplicitConversion(
4161 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4162 if (!Update.isUsable())
4163 return ExprError();
4164 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004165
4166 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4167 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004168 return Update;
4169}
4170
4171/// \brief Convert integer expression \a E to make it have at least \a Bits
4172/// bits.
4173static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4174 Sema &SemaRef) {
4175 if (E == nullptr)
4176 return ExprError();
4177 auto &C = SemaRef.Context;
4178 QualType OldType = E->getType();
4179 unsigned HasBits = C.getTypeSize(OldType);
4180 if (HasBits >= Bits)
4181 return ExprResult(E);
4182 // OK to convert to signed, because new type has more bits than old.
4183 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4184 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4185 true);
4186}
4187
4188/// \brief Check if the given expression \a E is a constant integer that fits
4189/// into \a Bits bits.
4190static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4191 if (E == nullptr)
4192 return false;
4193 llvm::APSInt Result;
4194 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4195 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4196 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197}
4198
4199/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004200/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4201/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004202static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004203CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4204 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4205 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004206 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004207 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004208 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004209 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004210 // Found 'collapse' clause - calculate collapse number.
4211 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004212 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004213 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004214 }
4215 if (OrderedLoopCountExpr) {
4216 // Found 'ordered' clause - calculate collapse number.
4217 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004218 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4219 if (Result.getLimitedValue() < NestedLoopCount) {
4220 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4221 diag::err_omp_wrong_ordered_loop_count)
4222 << OrderedLoopCountExpr->getSourceRange();
4223 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4224 diag::note_collapse_loop_count)
4225 << CollapseLoopCountExpr->getSourceRange();
4226 }
4227 NestedLoopCount = Result.getLimitedValue();
4228 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004229 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 // This is helper routine for loop directives (e.g., 'for', 'simd',
4231 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00004232 SmallVector<LoopIterationSpace, 4> IterSpaces;
4233 IterSpaces.resize(NestedLoopCount);
4234 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004235 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004236 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004237 NestedLoopCount, CollapseLoopCountExpr,
4238 OrderedLoopCountExpr, VarsWithImplicitDSA,
4239 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004240 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004242 // OpenMP [2.8.1, simd construct, Restrictions]
4243 // All loops associated with the construct must be perfectly nested; that
4244 // is, there must be no intervening code nor any OpenMP directive between
4245 // any two loops.
4246 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247 }
4248
Alexander Musmana5f070a2014-10-01 06:03:56 +00004249 Built.clear(/* size */ NestedLoopCount);
4250
4251 if (SemaRef.CurContext->isDependentContext())
4252 return NestedLoopCount;
4253
4254 // An example of what is generated for the following code:
4255 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004256 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004257 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004258 // for (k = 0; k < NK; ++k)
4259 // for (j = J0; j < NJ; j+=2) {
4260 // <loop body>
4261 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004262 //
4263 // We generate the code below.
4264 // Note: the loop body may be outlined in CodeGen.
4265 // Note: some counters may be C++ classes, operator- is used to find number of
4266 // iterations and operator+= to calculate counter value.
4267 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4268 // or i64 is currently supported).
4269 //
4270 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4271 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4272 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4273 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4274 // // similar updates for vars in clauses (e.g. 'linear')
4275 // <loop body (using local i and j)>
4276 // }
4277 // i = NI; // assign final values of counters
4278 // j = NJ;
4279 //
4280
4281 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4282 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004283 // Precondition tests if there is at least one iteration (all conditions are
4284 // true).
4285 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004286 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004287 ExprResult LastIteration32 = WidenIterationCount(
4288 32 /* Bits */, SemaRef.PerformImplicitConversion(
4289 N0->IgnoreImpCasts(), N0->getType(),
4290 Sema::AA_Converting, /*AllowExplicit=*/true)
4291 .get(),
4292 SemaRef);
4293 ExprResult LastIteration64 = WidenIterationCount(
4294 64 /* Bits */, SemaRef.PerformImplicitConversion(
4295 N0->IgnoreImpCasts(), N0->getType(),
4296 Sema::AA_Converting, /*AllowExplicit=*/true)
4297 .get(),
4298 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004299
4300 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4301 return NestedLoopCount;
4302
4303 auto &C = SemaRef.Context;
4304 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4305
4306 Scope *CurScope = DSA.getCurScope();
4307 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004308 if (PreCond.isUsable()) {
4309 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4310 PreCond.get(), IterSpaces[Cnt].PreCond);
4311 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004312 auto N = IterSpaces[Cnt].NumIterations;
4313 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4314 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004315 LastIteration32 = SemaRef.BuildBinOp(
4316 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4317 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4318 Sema::AA_Converting,
4319 /*AllowExplicit=*/true)
4320 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004321 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004322 LastIteration64 = SemaRef.BuildBinOp(
4323 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4324 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4325 Sema::AA_Converting,
4326 /*AllowExplicit=*/true)
4327 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328 }
4329
4330 // Choose either the 32-bit or 64-bit version.
4331 ExprResult LastIteration = LastIteration64;
4332 if (LastIteration32.isUsable() &&
4333 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4334 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4335 FitsInto(
4336 32 /* Bits */,
4337 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4338 LastIteration64.get(), SemaRef)))
4339 LastIteration = LastIteration32;
4340
4341 if (!LastIteration.isUsable())
4342 return 0;
4343
4344 // Save the number of iterations.
4345 ExprResult NumIterations = LastIteration;
4346 {
4347 LastIteration = SemaRef.BuildBinOp(
4348 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4349 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4350 if (!LastIteration.isUsable())
4351 return 0;
4352 }
4353
4354 // Calculate the last iteration number beforehand instead of doing this on
4355 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4356 llvm::APSInt Result;
4357 bool IsConstant =
4358 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4359 ExprResult CalcLastIteration;
4360 if (!IsConstant) {
4361 SourceLocation SaveLoc;
4362 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004363 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00004365 ExprResult SaveRef = buildDeclRefExpr(
4366 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004367 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
4368 SaveRef.get(), LastIteration.get());
4369 LastIteration = SaveRef;
4370
4371 // Prepare SaveRef + 1.
4372 NumIterations = SemaRef.BuildBinOp(
4373 CurScope, SaveLoc, BO_Add, SaveRef.get(),
4374 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4375 if (!NumIterations.isUsable())
4376 return 0;
4377 }
4378
4379 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4380
Alexander Musmanc6388682014-12-15 07:07:06 +00004381 QualType VType = LastIteration.get()->getType();
4382 // Build variables passed into runtime, nesessary for worksharing directives.
4383 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004384 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4385 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004386 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004387 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4388 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004389 SemaRef.AddInitializerToDecl(
4390 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4391 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4392
4393 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004394 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4395 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004396 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4397 /*DirectInit*/ false,
4398 /*TypeMayContainAuto*/ false);
4399
4400 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4401 // This will be used to implement clause 'lastprivate'.
4402 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004403 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4404 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004405 SemaRef.AddInitializerToDecl(
4406 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4407 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4408
4409 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004410 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4411 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004412 SemaRef.AddInitializerToDecl(
4413 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4414 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4415
4416 // Build expression: UB = min(UB, LastIteration)
4417 // It is nesessary for CodeGen of directives with static scheduling.
4418 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4419 UB.get(), LastIteration.get());
4420 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4421 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4422 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4423 CondOp.get());
4424 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4425 }
4426
4427 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004428 ExprResult IV;
4429 ExprResult Init;
4430 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004431 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4432 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004433 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004434 isOpenMPTaskLoopDirective(DKind) ||
4435 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004436 ? LB.get()
4437 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4438 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4439 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004440 }
4441
Alexander Musmanc6388682014-12-15 07:07:06 +00004442 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004443 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004444 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004445 (isOpenMPWorksharingDirective(DKind) ||
4446 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004447 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4448 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4449 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004450
4451 // Loop increment (IV = IV + 1)
4452 SourceLocation IncLoc;
4453 ExprResult Inc =
4454 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4455 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4456 if (!Inc.isUsable())
4457 return 0;
4458 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004459 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4460 if (!Inc.isUsable())
4461 return 0;
4462
4463 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4464 // Used for directives with static scheduling.
4465 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004466 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4467 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004468 // LB + ST
4469 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4470 if (!NextLB.isUsable())
4471 return 0;
4472 // LB = LB + ST
4473 NextLB =
4474 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4475 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4476 if (!NextLB.isUsable())
4477 return 0;
4478 // UB + ST
4479 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4480 if (!NextUB.isUsable())
4481 return 0;
4482 // UB = UB + ST
4483 NextUB =
4484 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4485 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4486 if (!NextUB.isUsable())
4487 return 0;
4488 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004489
4490 // Build updates and final values of the loop counters.
4491 bool HasErrors = false;
4492 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004493 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004494 Built.Updates.resize(NestedLoopCount);
4495 Built.Finals.resize(NestedLoopCount);
4496 {
4497 ExprResult Div;
4498 // Go from inner nested loop to outer.
4499 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4500 LoopIterationSpace &IS = IterSpaces[Cnt];
4501 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4502 // Build: Iter = (IV / Div) % IS.NumIters
4503 // where Div is product of previous iterations' IS.NumIters.
4504 ExprResult Iter;
4505 if (Div.isUsable()) {
4506 Iter =
4507 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4508 } else {
4509 Iter = IV;
4510 assert((Cnt == (int)NestedLoopCount - 1) &&
4511 "unusable div expected on first iteration only");
4512 }
4513
4514 if (Cnt != 0 && Iter.isUsable())
4515 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4516 IS.NumIterations);
4517 if (!Iter.isUsable()) {
4518 HasErrors = true;
4519 break;
4520 }
4521
Alexey Bataev39f915b82015-05-08 10:41:21 +00004522 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4523 auto *CounterVar = buildDeclRefExpr(
4524 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4525 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4526 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004527 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4528 IS.CounterInit);
4529 if (!Init.isUsable()) {
4530 HasErrors = true;
4531 break;
4532 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004533 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00004534 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004535 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
4536 if (!Update.isUsable()) {
4537 HasErrors = true;
4538 break;
4539 }
4540
4541 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4542 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004543 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00004544 IS.NumIterations, IS.CounterStep, IS.Subtract);
4545 if (!Final.isUsable()) {
4546 HasErrors = true;
4547 break;
4548 }
4549
4550 // Build Div for the next iteration: Div <- Div * IS.NumIters
4551 if (Cnt != 0) {
4552 if (Div.isUnset())
4553 Div = IS.NumIterations;
4554 else
4555 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4556 IS.NumIterations);
4557
4558 // Add parentheses (for debugging purposes only).
4559 if (Div.isUsable())
4560 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4561 if (!Div.isUsable()) {
4562 HasErrors = true;
4563 break;
4564 }
4565 }
4566 if (!Update.isUsable() || !Final.isUsable()) {
4567 HasErrors = true;
4568 break;
4569 }
4570 // Save results
4571 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004572 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004573 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004574 Built.Updates[Cnt] = Update.get();
4575 Built.Finals[Cnt] = Final.get();
4576 }
4577 }
4578
4579 if (HasErrors)
4580 return 0;
4581
4582 // Save results
4583 Built.IterationVarRef = IV.get();
4584 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004585 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004586 Built.CalcLastIteration =
4587 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004588 Built.PreCond = PreCond.get();
4589 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004590 Built.Init = Init.get();
4591 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004592 Built.LB = LB.get();
4593 Built.UB = UB.get();
4594 Built.IL = IL.get();
4595 Built.ST = ST.get();
4596 Built.EUB = EUB.get();
4597 Built.NLB = NextLB.get();
4598 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004599
Alexey Bataevabfc0692014-06-25 06:52:00 +00004600 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004601}
4602
Alexey Bataev10e775f2015-07-30 11:36:16 +00004603static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004604 auto CollapseClauses =
4605 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4606 if (CollapseClauses.begin() != CollapseClauses.end())
4607 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004608 return nullptr;
4609}
4610
Alexey Bataev10e775f2015-07-30 11:36:16 +00004611static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004612 auto OrderedClauses =
4613 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4614 if (OrderedClauses.begin() != OrderedClauses.end())
4615 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004616 return nullptr;
4617}
4618
Alexey Bataev66b15b52015-08-21 11:14:16 +00004619static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4620 const Expr *Safelen) {
4621 llvm::APSInt SimdlenRes, SafelenRes;
4622 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4623 Simdlen->isInstantiationDependent() ||
4624 Simdlen->containsUnexpandedParameterPack())
4625 return false;
4626 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4627 Safelen->isInstantiationDependent() ||
4628 Safelen->containsUnexpandedParameterPack())
4629 return false;
4630 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4631 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4632 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4633 // If both simdlen and safelen clauses are specified, the value of the simdlen
4634 // parameter must be less than or equal to the value of the safelen parameter.
4635 if (SimdlenRes > SafelenRes) {
4636 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4637 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4638 return true;
4639 }
4640 return false;
4641}
4642
Alexey Bataev4acb8592014-07-07 13:01:15 +00004643StmtResult Sema::ActOnOpenMPSimdDirective(
4644 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4645 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004646 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004647 if (!AStmt)
4648 return StmtError();
4649
4650 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004651 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004652 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4653 // define the nested loops number.
4654 unsigned NestedLoopCount = CheckOpenMPLoop(
4655 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4656 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004657 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004658 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004659
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660 assert((CurContext->isDependentContext() || B.builtAll()) &&
4661 "omp simd loop exprs were not built");
4662
Alexander Musman3276a272015-03-21 10:12:56 +00004663 if (!CurContext->isDependentContext()) {
4664 // Finalize the clauses that need pre-built expressions for CodeGen.
4665 for (auto C : Clauses) {
4666 if (auto LC = dyn_cast<OMPLinearClause>(C))
4667 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4668 B.NumIterations, *this, CurScope))
4669 return StmtError();
4670 }
4671 }
4672
Alexey Bataev66b15b52015-08-21 11:14:16 +00004673 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4674 // If both simdlen and safelen clauses are specified, the value of the simdlen
4675 // parameter must be less than or equal to the value of the safelen parameter.
4676 OMPSafelenClause *Safelen = nullptr;
4677 OMPSimdlenClause *Simdlen = nullptr;
4678 for (auto *Clause : Clauses) {
4679 if (Clause->getClauseKind() == OMPC_safelen)
4680 Safelen = cast<OMPSafelenClause>(Clause);
4681 else if (Clause->getClauseKind() == OMPC_simdlen)
4682 Simdlen = cast<OMPSimdlenClause>(Clause);
4683 if (Safelen && Simdlen)
4684 break;
4685 }
4686 if (Simdlen && Safelen &&
4687 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4688 Safelen->getSafelen()))
4689 return StmtError();
4690
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004691 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004692 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4693 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004694}
4695
Alexey Bataev4acb8592014-07-07 13:01:15 +00004696StmtResult Sema::ActOnOpenMPForDirective(
4697 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4698 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004699 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004700 if (!AStmt)
4701 return StmtError();
4702
4703 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004704 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004705 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4706 // define the nested loops number.
4707 unsigned NestedLoopCount = CheckOpenMPLoop(
4708 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4709 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004710 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004711 return StmtError();
4712
Alexander Musmana5f070a2014-10-01 06:03:56 +00004713 assert((CurContext->isDependentContext() || B.builtAll()) &&
4714 "omp for loop exprs were not built");
4715
Alexey Bataev54acd402015-08-04 11:18:19 +00004716 if (!CurContext->isDependentContext()) {
4717 // Finalize the clauses that need pre-built expressions for CodeGen.
4718 for (auto C : Clauses) {
4719 if (auto LC = dyn_cast<OMPLinearClause>(C))
4720 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4721 B.NumIterations, *this, CurScope))
4722 return StmtError();
4723 }
4724 }
4725
Alexey Bataevf29276e2014-06-18 04:14:57 +00004726 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004727 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004728 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004729}
4730
Alexander Musmanf82886e2014-09-18 05:12:34 +00004731StmtResult Sema::ActOnOpenMPForSimdDirective(
4732 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4733 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004734 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004735 if (!AStmt)
4736 return StmtError();
4737
4738 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004739 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004740 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4741 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004742 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004743 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4744 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4745 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004746 if (NestedLoopCount == 0)
4747 return StmtError();
4748
Alexander Musmanc6388682014-12-15 07:07:06 +00004749 assert((CurContext->isDependentContext() || B.builtAll()) &&
4750 "omp for simd loop exprs were not built");
4751
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004752 if (!CurContext->isDependentContext()) {
4753 // Finalize the clauses that need pre-built expressions for CodeGen.
4754 for (auto C : Clauses) {
4755 if (auto LC = dyn_cast<OMPLinearClause>(C))
4756 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4757 B.NumIterations, *this, CurScope))
4758 return StmtError();
4759 }
4760 }
4761
Alexey Bataev66b15b52015-08-21 11:14:16 +00004762 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4763 // If both simdlen and safelen clauses are specified, the value of the simdlen
4764 // parameter must be less than or equal to the value of the safelen parameter.
4765 OMPSafelenClause *Safelen = nullptr;
4766 OMPSimdlenClause *Simdlen = nullptr;
4767 for (auto *Clause : Clauses) {
4768 if (Clause->getClauseKind() == OMPC_safelen)
4769 Safelen = cast<OMPSafelenClause>(Clause);
4770 else if (Clause->getClauseKind() == OMPC_simdlen)
4771 Simdlen = cast<OMPSimdlenClause>(Clause);
4772 if (Safelen && Simdlen)
4773 break;
4774 }
4775 if (Simdlen && Safelen &&
4776 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4777 Safelen->getSafelen()))
4778 return StmtError();
4779
Alexander Musmanf82886e2014-09-18 05:12:34 +00004780 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004781 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4782 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004783}
4784
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004785StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4786 Stmt *AStmt,
4787 SourceLocation StartLoc,
4788 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004789 if (!AStmt)
4790 return StmtError();
4791
4792 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004793 auto BaseStmt = AStmt;
4794 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4795 BaseStmt = CS->getCapturedStmt();
4796 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4797 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004798 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004799 return StmtError();
4800 // All associated statements must be '#pragma omp section' except for
4801 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004802 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004803 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4804 if (SectionStmt)
4805 Diag(SectionStmt->getLocStart(),
4806 diag::err_omp_sections_substmt_not_section);
4807 return StmtError();
4808 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004809 cast<OMPSectionDirective>(SectionStmt)
4810 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004811 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004812 } else {
4813 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4814 return StmtError();
4815 }
4816
4817 getCurFunction()->setHasBranchProtectedScope();
4818
Alexey Bataev25e5b442015-09-15 12:52:43 +00004819 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4820 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004821}
4822
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004823StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4824 SourceLocation StartLoc,
4825 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004826 if (!AStmt)
4827 return StmtError();
4828
4829 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004830
4831 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004832 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004833
Alexey Bataev25e5b442015-09-15 12:52:43 +00004834 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4835 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004836}
4837
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004838StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4839 Stmt *AStmt,
4840 SourceLocation StartLoc,
4841 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004842 if (!AStmt)
4843 return StmtError();
4844
4845 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004846
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004847 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004848
Alexey Bataev3255bf32015-01-19 05:20:46 +00004849 // OpenMP [2.7.3, single Construct, Restrictions]
4850 // The copyprivate clause must not be used with the nowait clause.
4851 OMPClause *Nowait = nullptr;
4852 OMPClause *Copyprivate = nullptr;
4853 for (auto *Clause : Clauses) {
4854 if (Clause->getClauseKind() == OMPC_nowait)
4855 Nowait = Clause;
4856 else if (Clause->getClauseKind() == OMPC_copyprivate)
4857 Copyprivate = Clause;
4858 if (Copyprivate && Nowait) {
4859 Diag(Copyprivate->getLocStart(),
4860 diag::err_omp_single_copyprivate_with_nowait);
4861 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4862 return StmtError();
4863 }
4864 }
4865
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004866 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4867}
4868
Alexander Musman80c22892014-07-17 08:54:58 +00004869StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4870 SourceLocation StartLoc,
4871 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004872 if (!AStmt)
4873 return StmtError();
4874
4875 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004876
4877 getCurFunction()->setHasBranchProtectedScope();
4878
4879 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4880}
4881
Alexey Bataev28c75412015-12-15 08:19:24 +00004882StmtResult Sema::ActOnOpenMPCriticalDirective(
4883 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4884 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004885 if (!AStmt)
4886 return StmtError();
4887
4888 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004889
Alexey Bataev28c75412015-12-15 08:19:24 +00004890 bool ErrorFound = false;
4891 llvm::APSInt Hint;
4892 SourceLocation HintLoc;
4893 bool DependentHint = false;
4894 for (auto *C : Clauses) {
4895 if (C->getClauseKind() == OMPC_hint) {
4896 if (!DirName.getName()) {
4897 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4898 ErrorFound = true;
4899 }
4900 Expr *E = cast<OMPHintClause>(C)->getHint();
4901 if (E->isTypeDependent() || E->isValueDependent() ||
4902 E->isInstantiationDependent())
4903 DependentHint = true;
4904 else {
4905 Hint = E->EvaluateKnownConstInt(Context);
4906 HintLoc = C->getLocStart();
4907 }
4908 }
4909 }
4910 if (ErrorFound)
4911 return StmtError();
4912 auto Pair = DSAStack->getCriticalWithHint(DirName);
4913 if (Pair.first && DirName.getName() && !DependentHint) {
4914 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4915 Diag(StartLoc, diag::err_omp_critical_with_hint);
4916 if (HintLoc.isValid()) {
4917 Diag(HintLoc, diag::note_omp_critical_hint_here)
4918 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4919 } else
4920 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4921 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4922 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4923 << 1
4924 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4925 /*Radix=*/10, /*Signed=*/false);
4926 } else
4927 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4928 }
4929 }
4930
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004931 getCurFunction()->setHasBranchProtectedScope();
4932
Alexey Bataev28c75412015-12-15 08:19:24 +00004933 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4934 Clauses, AStmt);
4935 if (!Pair.first && DirName.getName() && !DependentHint)
4936 DSAStack->addCriticalWithHint(Dir, Hint);
4937 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004938}
4939
Alexey Bataev4acb8592014-07-07 13:01:15 +00004940StmtResult Sema::ActOnOpenMPParallelForDirective(
4941 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4942 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004943 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004944 if (!AStmt)
4945 return StmtError();
4946
Alexey Bataev4acb8592014-07-07 13:01:15 +00004947 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4948 // 1.2.2 OpenMP Language Terminology
4949 // Structured block - An executable statement with a single entry at the
4950 // top and a single exit at the bottom.
4951 // The point of exit cannot be a branch out of the structured block.
4952 // longjmp() and throw() must not violate the entry/exit criteria.
4953 CS->getCapturedDecl()->setNothrow();
4954
Alexander Musmanc6388682014-12-15 07:07:06 +00004955 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004956 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4957 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004958 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004959 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4960 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4961 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004962 if (NestedLoopCount == 0)
4963 return StmtError();
4964
Alexander Musmana5f070a2014-10-01 06:03:56 +00004965 assert((CurContext->isDependentContext() || B.builtAll()) &&
4966 "omp parallel for loop exprs were not built");
4967
Alexey Bataev54acd402015-08-04 11:18:19 +00004968 if (!CurContext->isDependentContext()) {
4969 // Finalize the clauses that need pre-built expressions for CodeGen.
4970 for (auto C : Clauses) {
4971 if (auto LC = dyn_cast<OMPLinearClause>(C))
4972 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4973 B.NumIterations, *this, CurScope))
4974 return StmtError();
4975 }
4976 }
4977
Alexey Bataev4acb8592014-07-07 13:01:15 +00004978 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004979 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004980 NestedLoopCount, Clauses, AStmt, B,
4981 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004982}
4983
Alexander Musmane4e893b2014-09-23 09:33:00 +00004984StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4985 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4986 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004987 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004988 if (!AStmt)
4989 return StmtError();
4990
Alexander Musmane4e893b2014-09-23 09:33:00 +00004991 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4992 // 1.2.2 OpenMP Language Terminology
4993 // Structured block - An executable statement with a single entry at the
4994 // top and a single exit at the bottom.
4995 // The point of exit cannot be a branch out of the structured block.
4996 // longjmp() and throw() must not violate the entry/exit criteria.
4997 CS->getCapturedDecl()->setNothrow();
4998
Alexander Musmanc6388682014-12-15 07:07:06 +00004999 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005000 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5001 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005002 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005003 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5004 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5005 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005006 if (NestedLoopCount == 0)
5007 return StmtError();
5008
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005009 if (!CurContext->isDependentContext()) {
5010 // Finalize the clauses that need pre-built expressions for CodeGen.
5011 for (auto C : Clauses) {
5012 if (auto LC = dyn_cast<OMPLinearClause>(C))
5013 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5014 B.NumIterations, *this, CurScope))
5015 return StmtError();
5016 }
5017 }
5018
Alexey Bataev66b15b52015-08-21 11:14:16 +00005019 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5020 // If both simdlen and safelen clauses are specified, the value of the simdlen
5021 // parameter must be less than or equal to the value of the safelen parameter.
5022 OMPSafelenClause *Safelen = nullptr;
5023 OMPSimdlenClause *Simdlen = nullptr;
5024 for (auto *Clause : Clauses) {
5025 if (Clause->getClauseKind() == OMPC_safelen)
5026 Safelen = cast<OMPSafelenClause>(Clause);
5027 else if (Clause->getClauseKind() == OMPC_simdlen)
5028 Simdlen = cast<OMPSimdlenClause>(Clause);
5029 if (Safelen && Simdlen)
5030 break;
5031 }
5032 if (Simdlen && Safelen &&
5033 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5034 Safelen->getSafelen()))
5035 return StmtError();
5036
Alexander Musmane4e893b2014-09-23 09:33:00 +00005037 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005038 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005039 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005040}
5041
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005042StmtResult
5043Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5044 Stmt *AStmt, SourceLocation StartLoc,
5045 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005046 if (!AStmt)
5047 return StmtError();
5048
5049 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005050 auto BaseStmt = AStmt;
5051 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5052 BaseStmt = CS->getCapturedStmt();
5053 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5054 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005055 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005056 return StmtError();
5057 // All associated statements must be '#pragma omp section' except for
5058 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005059 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005060 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5061 if (SectionStmt)
5062 Diag(SectionStmt->getLocStart(),
5063 diag::err_omp_parallel_sections_substmt_not_section);
5064 return StmtError();
5065 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005066 cast<OMPSectionDirective>(SectionStmt)
5067 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005068 }
5069 } else {
5070 Diag(AStmt->getLocStart(),
5071 diag::err_omp_parallel_sections_not_compound_stmt);
5072 return StmtError();
5073 }
5074
5075 getCurFunction()->setHasBranchProtectedScope();
5076
Alexey Bataev25e5b442015-09-15 12:52:43 +00005077 return OMPParallelSectionsDirective::Create(
5078 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005079}
5080
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005081StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5082 Stmt *AStmt, SourceLocation StartLoc,
5083 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005084 if (!AStmt)
5085 return StmtError();
5086
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005087 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5088 // 1.2.2 OpenMP Language Terminology
5089 // Structured block - An executable statement with a single entry at the
5090 // top and a single exit at the bottom.
5091 // The point of exit cannot be a branch out of the structured block.
5092 // longjmp() and throw() must not violate the entry/exit criteria.
5093 CS->getCapturedDecl()->setNothrow();
5094
5095 getCurFunction()->setHasBranchProtectedScope();
5096
Alexey Bataev25e5b442015-09-15 12:52:43 +00005097 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5098 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005099}
5100
Alexey Bataev68446b72014-07-18 07:47:19 +00005101StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5102 SourceLocation EndLoc) {
5103 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5104}
5105
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005106StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5107 SourceLocation EndLoc) {
5108 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5109}
5110
Alexey Bataev2df347a2014-07-18 10:17:07 +00005111StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5112 SourceLocation EndLoc) {
5113 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5114}
5115
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005116StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5117 SourceLocation StartLoc,
5118 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005119 if (!AStmt)
5120 return StmtError();
5121
5122 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005123
5124 getCurFunction()->setHasBranchProtectedScope();
5125
5126 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5127}
5128
Alexey Bataev6125da92014-07-21 11:26:11 +00005129StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5130 SourceLocation StartLoc,
5131 SourceLocation EndLoc) {
5132 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5133 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5134}
5135
Alexey Bataev346265e2015-09-25 10:37:12 +00005136StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5137 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005138 SourceLocation StartLoc,
5139 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005140 OMPClause *DependFound = nullptr;
5141 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005142 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005143 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005144 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005145 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005146 for (auto *C : Clauses) {
5147 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5148 DependFound = C;
5149 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5150 if (DependSourceClause) {
5151 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5152 << getOpenMPDirectiveName(OMPD_ordered)
5153 << getOpenMPClauseName(OMPC_depend) << 2;
5154 ErrorFound = true;
5155 } else
5156 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005157 if (DependSinkClause) {
5158 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5159 << 0;
5160 ErrorFound = true;
5161 }
5162 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5163 if (DependSourceClause) {
5164 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5165 << 1;
5166 ErrorFound = true;
5167 }
5168 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005169 }
5170 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005171 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005172 else if (C->getClauseKind() == OMPC_simd)
5173 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005174 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005175 if (!ErrorFound && !SC &&
5176 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005177 // OpenMP [2.8.1,simd Construct, Restrictions]
5178 // An ordered construct with the simd clause is the only OpenMP construct
5179 // that can appear in the simd region.
5180 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005181 ErrorFound = true;
5182 } else if (DependFound && (TC || SC)) {
5183 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5184 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5185 ErrorFound = true;
5186 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5187 Diag(DependFound->getLocStart(),
5188 diag::err_omp_ordered_directive_without_param);
5189 ErrorFound = true;
5190 } else if (TC || Clauses.empty()) {
5191 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5192 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5193 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5194 << (TC != nullptr);
5195 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5196 ErrorFound = true;
5197 }
5198 }
5199 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005200 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005201
5202 if (AStmt) {
5203 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5204
5205 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005206 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005207
5208 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005209}
5210
Alexey Bataev1d160b12015-03-13 12:27:31 +00005211namespace {
5212/// \brief Helper class for checking expression in 'omp atomic [update]'
5213/// construct.
5214class OpenMPAtomicUpdateChecker {
5215 /// \brief Error results for atomic update expressions.
5216 enum ExprAnalysisErrorCode {
5217 /// \brief A statement is not an expression statement.
5218 NotAnExpression,
5219 /// \brief Expression is not builtin binary or unary operation.
5220 NotABinaryOrUnaryExpression,
5221 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5222 NotAnUnaryIncDecExpression,
5223 /// \brief An expression is not of scalar type.
5224 NotAScalarType,
5225 /// \brief A binary operation is not an assignment operation.
5226 NotAnAssignmentOp,
5227 /// \brief RHS part of the binary operation is not a binary expression.
5228 NotABinaryExpression,
5229 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5230 /// expression.
5231 NotABinaryOperator,
5232 /// \brief RHS binary operation does not have reference to the updated LHS
5233 /// part.
5234 NotAnUpdateExpression,
5235 /// \brief No errors is found.
5236 NoError
5237 };
5238 /// \brief Reference to Sema.
5239 Sema &SemaRef;
5240 /// \brief A location for note diagnostics (when error is found).
5241 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005242 /// \brief 'x' lvalue part of the source atomic expression.
5243 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005244 /// \brief 'expr' rvalue part of the source atomic expression.
5245 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005246 /// \brief Helper expression of the form
5247 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5248 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5249 Expr *UpdateExpr;
5250 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5251 /// important for non-associative operations.
5252 bool IsXLHSInRHSPart;
5253 BinaryOperatorKind Op;
5254 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005255 /// \brief true if the source expression is a postfix unary operation, false
5256 /// if it is a prefix unary operation.
5257 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005258
5259public:
5260 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005261 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005262 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005263 /// \brief Check specified statement that it is suitable for 'atomic update'
5264 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005265 /// expression. If DiagId and NoteId == 0, then only check is performed
5266 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005267 /// \param DiagId Diagnostic which should be emitted if error is found.
5268 /// \param NoteId Diagnostic note for the main error message.
5269 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005270 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005271 /// \brief Return the 'x' lvalue part of the source atomic expression.
5272 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005273 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5274 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005275 /// \brief Return the update expression used in calculation of the updated
5276 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5277 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5278 Expr *getUpdateExpr() const { return UpdateExpr; }
5279 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5280 /// false otherwise.
5281 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5282
Alexey Bataevb78ca832015-04-01 03:33:17 +00005283 /// \brief true if the source expression is a postfix unary operation, false
5284 /// if it is a prefix unary operation.
5285 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5286
Alexey Bataev1d160b12015-03-13 12:27:31 +00005287private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005288 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5289 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005290};
5291} // namespace
5292
5293bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5294 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5295 ExprAnalysisErrorCode ErrorFound = NoError;
5296 SourceLocation ErrorLoc, NoteLoc;
5297 SourceRange ErrorRange, NoteRange;
5298 // Allowed constructs are:
5299 // x = x binop expr;
5300 // x = expr binop x;
5301 if (AtomicBinOp->getOpcode() == BO_Assign) {
5302 X = AtomicBinOp->getLHS();
5303 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5304 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5305 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5306 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5307 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005308 Op = AtomicInnerBinOp->getOpcode();
5309 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005310 auto *LHS = AtomicInnerBinOp->getLHS();
5311 auto *RHS = AtomicInnerBinOp->getRHS();
5312 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5313 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5314 /*Canonical=*/true);
5315 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5316 /*Canonical=*/true);
5317 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5318 /*Canonical=*/true);
5319 if (XId == LHSId) {
5320 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005321 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005322 } else if (XId == RHSId) {
5323 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005324 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005325 } else {
5326 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5327 ErrorRange = AtomicInnerBinOp->getSourceRange();
5328 NoteLoc = X->getExprLoc();
5329 NoteRange = X->getSourceRange();
5330 ErrorFound = NotAnUpdateExpression;
5331 }
5332 } else {
5333 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5334 ErrorRange = AtomicInnerBinOp->getSourceRange();
5335 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5336 NoteRange = SourceRange(NoteLoc, NoteLoc);
5337 ErrorFound = NotABinaryOperator;
5338 }
5339 } else {
5340 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5341 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5342 ErrorFound = NotABinaryExpression;
5343 }
5344 } else {
5345 ErrorLoc = AtomicBinOp->getExprLoc();
5346 ErrorRange = AtomicBinOp->getSourceRange();
5347 NoteLoc = AtomicBinOp->getOperatorLoc();
5348 NoteRange = SourceRange(NoteLoc, NoteLoc);
5349 ErrorFound = NotAnAssignmentOp;
5350 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005351 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005352 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5353 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5354 return true;
5355 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005356 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005357 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005358}
5359
5360bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5361 unsigned NoteId) {
5362 ExprAnalysisErrorCode ErrorFound = NoError;
5363 SourceLocation ErrorLoc, NoteLoc;
5364 SourceRange ErrorRange, NoteRange;
5365 // Allowed constructs are:
5366 // x++;
5367 // x--;
5368 // ++x;
5369 // --x;
5370 // x binop= expr;
5371 // x = x binop expr;
5372 // x = expr binop x;
5373 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5374 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5375 if (AtomicBody->getType()->isScalarType() ||
5376 AtomicBody->isInstantiationDependent()) {
5377 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5378 AtomicBody->IgnoreParenImpCasts())) {
5379 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005380 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005381 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005382 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005383 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005384 X = AtomicCompAssignOp->getLHS();
5385 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005386 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5387 AtomicBody->IgnoreParenImpCasts())) {
5388 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005389 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5390 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005391 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005392 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5393 // Check for Unary Operation
5394 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005395 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005396 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5397 OpLoc = AtomicUnaryOp->getOperatorLoc();
5398 X = AtomicUnaryOp->getSubExpr();
5399 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5400 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005401 } else {
5402 ErrorFound = NotAnUnaryIncDecExpression;
5403 ErrorLoc = AtomicUnaryOp->getExprLoc();
5404 ErrorRange = AtomicUnaryOp->getSourceRange();
5405 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5406 NoteRange = SourceRange(NoteLoc, NoteLoc);
5407 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005408 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005409 ErrorFound = NotABinaryOrUnaryExpression;
5410 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5411 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5412 }
5413 } else {
5414 ErrorFound = NotAScalarType;
5415 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5416 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5417 }
5418 } else {
5419 ErrorFound = NotAnExpression;
5420 NoteLoc = ErrorLoc = S->getLocStart();
5421 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5422 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005423 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005424 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5425 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5426 return true;
5427 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005428 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005429 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005430 // Build an update expression of form 'OpaqueValueExpr(x) binop
5431 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5432 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5433 auto *OVEX = new (SemaRef.getASTContext())
5434 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5435 auto *OVEExpr = new (SemaRef.getASTContext())
5436 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5437 auto Update =
5438 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5439 IsXLHSInRHSPart ? OVEExpr : OVEX);
5440 if (Update.isInvalid())
5441 return true;
5442 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5443 Sema::AA_Casting);
5444 if (Update.isInvalid())
5445 return true;
5446 UpdateExpr = Update.get();
5447 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005448 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005449}
5450
Alexey Bataev0162e452014-07-22 10:10:35 +00005451StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5452 Stmt *AStmt,
5453 SourceLocation StartLoc,
5454 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005455 if (!AStmt)
5456 return StmtError();
5457
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005458 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005459 // 1.2.2 OpenMP Language Terminology
5460 // Structured block - An executable statement with a single entry at the
5461 // top and a single exit at the bottom.
5462 // The point of exit cannot be a branch out of the structured block.
5463 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005464 OpenMPClauseKind AtomicKind = OMPC_unknown;
5465 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005466 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005467 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005468 C->getClauseKind() == OMPC_update ||
5469 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005470 if (AtomicKind != OMPC_unknown) {
5471 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5472 << SourceRange(C->getLocStart(), C->getLocEnd());
5473 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5474 << getOpenMPClauseName(AtomicKind);
5475 } else {
5476 AtomicKind = C->getClauseKind();
5477 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005478 }
5479 }
5480 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005481
Alexey Bataev459dec02014-07-24 06:46:57 +00005482 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005483 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5484 Body = EWC->getSubExpr();
5485
Alexey Bataev62cec442014-11-18 10:14:22 +00005486 Expr *X = nullptr;
5487 Expr *V = nullptr;
5488 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005489 Expr *UE = nullptr;
5490 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005491 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005492 // OpenMP [2.12.6, atomic Construct]
5493 // In the next expressions:
5494 // * x and v (as applicable) are both l-value expressions with scalar type.
5495 // * During the execution of an atomic region, multiple syntactic
5496 // occurrences of x must designate the same storage location.
5497 // * Neither of v and expr (as applicable) may access the storage location
5498 // designated by x.
5499 // * Neither of x and expr (as applicable) may access the storage location
5500 // designated by v.
5501 // * expr is an expression with scalar type.
5502 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5503 // * binop, binop=, ++, and -- are not overloaded operators.
5504 // * The expression x binop expr must be numerically equivalent to x binop
5505 // (expr). This requirement is satisfied if the operators in expr have
5506 // precedence greater than binop, or by using parentheses around expr or
5507 // subexpressions of expr.
5508 // * The expression expr binop x must be numerically equivalent to (expr)
5509 // binop x. This requirement is satisfied if the operators in expr have
5510 // precedence equal to or greater than binop, or by using parentheses around
5511 // expr or subexpressions of expr.
5512 // * For forms that allow multiple occurrences of x, the number of times
5513 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005514 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005515 enum {
5516 NotAnExpression,
5517 NotAnAssignmentOp,
5518 NotAScalarType,
5519 NotAnLValue,
5520 NoError
5521 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005522 SourceLocation ErrorLoc, NoteLoc;
5523 SourceRange ErrorRange, NoteRange;
5524 // If clause is read:
5525 // v = x;
5526 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5527 auto AtomicBinOp =
5528 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5529 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5530 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5531 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5532 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5533 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5534 if (!X->isLValue() || !V->isLValue()) {
5535 auto NotLValueExpr = X->isLValue() ? V : X;
5536 ErrorFound = NotAnLValue;
5537 ErrorLoc = AtomicBinOp->getExprLoc();
5538 ErrorRange = AtomicBinOp->getSourceRange();
5539 NoteLoc = NotLValueExpr->getExprLoc();
5540 NoteRange = NotLValueExpr->getSourceRange();
5541 }
5542 } else if (!X->isInstantiationDependent() ||
5543 !V->isInstantiationDependent()) {
5544 auto NotScalarExpr =
5545 (X->isInstantiationDependent() || X->getType()->isScalarType())
5546 ? V
5547 : X;
5548 ErrorFound = NotAScalarType;
5549 ErrorLoc = AtomicBinOp->getExprLoc();
5550 ErrorRange = AtomicBinOp->getSourceRange();
5551 NoteLoc = NotScalarExpr->getExprLoc();
5552 NoteRange = NotScalarExpr->getSourceRange();
5553 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005554 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005555 ErrorFound = NotAnAssignmentOp;
5556 ErrorLoc = AtomicBody->getExprLoc();
5557 ErrorRange = AtomicBody->getSourceRange();
5558 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5559 : AtomicBody->getExprLoc();
5560 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5561 : AtomicBody->getSourceRange();
5562 }
5563 } else {
5564 ErrorFound = NotAnExpression;
5565 NoteLoc = ErrorLoc = Body->getLocStart();
5566 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005567 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005568 if (ErrorFound != NoError) {
5569 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5570 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005571 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5572 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005573 return StmtError();
5574 } else if (CurContext->isDependentContext())
5575 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005576 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005577 enum {
5578 NotAnExpression,
5579 NotAnAssignmentOp,
5580 NotAScalarType,
5581 NotAnLValue,
5582 NoError
5583 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005584 SourceLocation ErrorLoc, NoteLoc;
5585 SourceRange ErrorRange, NoteRange;
5586 // If clause is write:
5587 // x = expr;
5588 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5589 auto AtomicBinOp =
5590 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5591 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005592 X = AtomicBinOp->getLHS();
5593 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005594 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5595 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5596 if (!X->isLValue()) {
5597 ErrorFound = NotAnLValue;
5598 ErrorLoc = AtomicBinOp->getExprLoc();
5599 ErrorRange = AtomicBinOp->getSourceRange();
5600 NoteLoc = X->getExprLoc();
5601 NoteRange = X->getSourceRange();
5602 }
5603 } else if (!X->isInstantiationDependent() ||
5604 !E->isInstantiationDependent()) {
5605 auto NotScalarExpr =
5606 (X->isInstantiationDependent() || X->getType()->isScalarType())
5607 ? E
5608 : X;
5609 ErrorFound = NotAScalarType;
5610 ErrorLoc = AtomicBinOp->getExprLoc();
5611 ErrorRange = AtomicBinOp->getSourceRange();
5612 NoteLoc = NotScalarExpr->getExprLoc();
5613 NoteRange = NotScalarExpr->getSourceRange();
5614 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005615 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005616 ErrorFound = NotAnAssignmentOp;
5617 ErrorLoc = AtomicBody->getExprLoc();
5618 ErrorRange = AtomicBody->getSourceRange();
5619 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5620 : AtomicBody->getExprLoc();
5621 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5622 : AtomicBody->getSourceRange();
5623 }
5624 } else {
5625 ErrorFound = NotAnExpression;
5626 NoteLoc = ErrorLoc = Body->getLocStart();
5627 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005628 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005629 if (ErrorFound != NoError) {
5630 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5631 << ErrorRange;
5632 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5633 << NoteRange;
5634 return StmtError();
5635 } else if (CurContext->isDependentContext())
5636 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005637 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005638 // If clause is update:
5639 // x++;
5640 // x--;
5641 // ++x;
5642 // --x;
5643 // x binop= expr;
5644 // x = x binop expr;
5645 // x = expr binop x;
5646 OpenMPAtomicUpdateChecker Checker(*this);
5647 if (Checker.checkStatement(
5648 Body, (AtomicKind == OMPC_update)
5649 ? diag::err_omp_atomic_update_not_expression_statement
5650 : diag::err_omp_atomic_not_expression_statement,
5651 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005652 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005653 if (!CurContext->isDependentContext()) {
5654 E = Checker.getExpr();
5655 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005656 UE = Checker.getUpdateExpr();
5657 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005658 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005659 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005660 enum {
5661 NotAnAssignmentOp,
5662 NotACompoundStatement,
5663 NotTwoSubstatements,
5664 NotASpecificExpression,
5665 NoError
5666 } ErrorFound = NoError;
5667 SourceLocation ErrorLoc, NoteLoc;
5668 SourceRange ErrorRange, NoteRange;
5669 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5670 // If clause is a capture:
5671 // v = x++;
5672 // v = x--;
5673 // v = ++x;
5674 // v = --x;
5675 // v = x binop= expr;
5676 // v = x = x binop expr;
5677 // v = x = expr binop x;
5678 auto *AtomicBinOp =
5679 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5680 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5681 V = AtomicBinOp->getLHS();
5682 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5683 OpenMPAtomicUpdateChecker Checker(*this);
5684 if (Checker.checkStatement(
5685 Body, diag::err_omp_atomic_capture_not_expression_statement,
5686 diag::note_omp_atomic_update))
5687 return StmtError();
5688 E = Checker.getExpr();
5689 X = Checker.getX();
5690 UE = Checker.getUpdateExpr();
5691 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5692 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005693 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005694 ErrorLoc = AtomicBody->getExprLoc();
5695 ErrorRange = AtomicBody->getSourceRange();
5696 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5697 : AtomicBody->getExprLoc();
5698 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5699 : AtomicBody->getSourceRange();
5700 ErrorFound = NotAnAssignmentOp;
5701 }
5702 if (ErrorFound != NoError) {
5703 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5704 << ErrorRange;
5705 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5706 return StmtError();
5707 } else if (CurContext->isDependentContext()) {
5708 UE = V = E = X = nullptr;
5709 }
5710 } else {
5711 // If clause is a capture:
5712 // { v = x; x = expr; }
5713 // { v = x; x++; }
5714 // { v = x; x--; }
5715 // { v = x; ++x; }
5716 // { v = x; --x; }
5717 // { v = x; x binop= expr; }
5718 // { v = x; x = x binop expr; }
5719 // { v = x; x = expr binop x; }
5720 // { x++; v = x; }
5721 // { x--; v = x; }
5722 // { ++x; v = x; }
5723 // { --x; v = x; }
5724 // { x binop= expr; v = x; }
5725 // { x = x binop expr; v = x; }
5726 // { x = expr binop x; v = x; }
5727 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5728 // Check that this is { expr1; expr2; }
5729 if (CS->size() == 2) {
5730 auto *First = CS->body_front();
5731 auto *Second = CS->body_back();
5732 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5733 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5734 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5735 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5736 // Need to find what subexpression is 'v' and what is 'x'.
5737 OpenMPAtomicUpdateChecker Checker(*this);
5738 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5739 BinaryOperator *BinOp = nullptr;
5740 if (IsUpdateExprFound) {
5741 BinOp = dyn_cast<BinaryOperator>(First);
5742 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5743 }
5744 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5745 // { v = x; x++; }
5746 // { v = x; x--; }
5747 // { v = x; ++x; }
5748 // { v = x; --x; }
5749 // { v = x; x binop= expr; }
5750 // { v = x; x = x binop expr; }
5751 // { v = x; x = expr binop x; }
5752 // Check that the first expression has form v = x.
5753 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5754 llvm::FoldingSetNodeID XId, PossibleXId;
5755 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5756 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5757 IsUpdateExprFound = XId == PossibleXId;
5758 if (IsUpdateExprFound) {
5759 V = BinOp->getLHS();
5760 X = Checker.getX();
5761 E = Checker.getExpr();
5762 UE = Checker.getUpdateExpr();
5763 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005764 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005765 }
5766 }
5767 if (!IsUpdateExprFound) {
5768 IsUpdateExprFound = !Checker.checkStatement(First);
5769 BinOp = nullptr;
5770 if (IsUpdateExprFound) {
5771 BinOp = dyn_cast<BinaryOperator>(Second);
5772 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5773 }
5774 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5775 // { x++; v = x; }
5776 // { x--; v = x; }
5777 // { ++x; v = x; }
5778 // { --x; v = x; }
5779 // { x binop= expr; v = x; }
5780 // { x = x binop expr; v = x; }
5781 // { x = expr binop x; v = x; }
5782 // Check that the second expression has form v = x.
5783 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5784 llvm::FoldingSetNodeID XId, PossibleXId;
5785 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5786 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5787 IsUpdateExprFound = XId == PossibleXId;
5788 if (IsUpdateExprFound) {
5789 V = BinOp->getLHS();
5790 X = Checker.getX();
5791 E = Checker.getExpr();
5792 UE = Checker.getUpdateExpr();
5793 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005794 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005795 }
5796 }
5797 }
5798 if (!IsUpdateExprFound) {
5799 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005800 auto *FirstExpr = dyn_cast<Expr>(First);
5801 auto *SecondExpr = dyn_cast<Expr>(Second);
5802 if (!FirstExpr || !SecondExpr ||
5803 !(FirstExpr->isInstantiationDependent() ||
5804 SecondExpr->isInstantiationDependent())) {
5805 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5806 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005807 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005808 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5809 : First->getLocStart();
5810 NoteRange = ErrorRange = FirstBinOp
5811 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005812 : SourceRange(ErrorLoc, ErrorLoc);
5813 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005814 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5815 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5816 ErrorFound = NotAnAssignmentOp;
5817 NoteLoc = ErrorLoc = SecondBinOp
5818 ? SecondBinOp->getOperatorLoc()
5819 : Second->getLocStart();
5820 NoteRange = ErrorRange =
5821 SecondBinOp ? SecondBinOp->getSourceRange()
5822 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005823 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005824 auto *PossibleXRHSInFirst =
5825 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5826 auto *PossibleXLHSInSecond =
5827 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5828 llvm::FoldingSetNodeID X1Id, X2Id;
5829 PossibleXRHSInFirst->Profile(X1Id, Context,
5830 /*Canonical=*/true);
5831 PossibleXLHSInSecond->Profile(X2Id, Context,
5832 /*Canonical=*/true);
5833 IsUpdateExprFound = X1Id == X2Id;
5834 if (IsUpdateExprFound) {
5835 V = FirstBinOp->getLHS();
5836 X = SecondBinOp->getLHS();
5837 E = SecondBinOp->getRHS();
5838 UE = nullptr;
5839 IsXLHSInRHSPart = false;
5840 IsPostfixUpdate = true;
5841 } else {
5842 ErrorFound = NotASpecificExpression;
5843 ErrorLoc = FirstBinOp->getExprLoc();
5844 ErrorRange = FirstBinOp->getSourceRange();
5845 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5846 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5847 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005848 }
5849 }
5850 }
5851 }
5852 } else {
5853 NoteLoc = ErrorLoc = Body->getLocStart();
5854 NoteRange = ErrorRange =
5855 SourceRange(Body->getLocStart(), Body->getLocStart());
5856 ErrorFound = NotTwoSubstatements;
5857 }
5858 } else {
5859 NoteLoc = ErrorLoc = Body->getLocStart();
5860 NoteRange = ErrorRange =
5861 SourceRange(Body->getLocStart(), Body->getLocStart());
5862 ErrorFound = NotACompoundStatement;
5863 }
5864 if (ErrorFound != NoError) {
5865 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5866 << ErrorRange;
5867 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5868 return StmtError();
5869 } else if (CurContext->isDependentContext()) {
5870 UE = V = E = X = nullptr;
5871 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005872 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005873 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005874
5875 getCurFunction()->setHasBranchProtectedScope();
5876
Alexey Bataev62cec442014-11-18 10:14:22 +00005877 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005878 X, V, E, UE, IsXLHSInRHSPart,
5879 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005880}
5881
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005882StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5883 Stmt *AStmt,
5884 SourceLocation StartLoc,
5885 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005886 if (!AStmt)
5887 return StmtError();
5888
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005889 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5890 // 1.2.2 OpenMP Language Terminology
5891 // Structured block - An executable statement with a single entry at the
5892 // top and a single exit at the bottom.
5893 // The point of exit cannot be a branch out of the structured block.
5894 // longjmp() and throw() must not violate the entry/exit criteria.
5895 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005896
Alexey Bataev13314bf2014-10-09 04:18:56 +00005897 // OpenMP [2.16, Nesting of Regions]
5898 // If specified, a teams construct must be contained within a target
5899 // construct. That target construct must contain no statements or directives
5900 // outside of the teams construct.
5901 if (DSAStack->hasInnerTeamsRegion()) {
5902 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5903 bool OMPTeamsFound = true;
5904 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5905 auto I = CS->body_begin();
5906 while (I != CS->body_end()) {
5907 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5908 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5909 OMPTeamsFound = false;
5910 break;
5911 }
5912 ++I;
5913 }
5914 assert(I != CS->body_end() && "Not found statement");
5915 S = *I;
5916 }
5917 if (!OMPTeamsFound) {
5918 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5919 Diag(DSAStack->getInnerTeamsRegionLoc(),
5920 diag::note_omp_nested_teams_construct_here);
5921 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5922 << isa<OMPExecutableDirective>(S);
5923 return StmtError();
5924 }
5925 }
5926
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005927 getCurFunction()->setHasBranchProtectedScope();
5928
5929 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5930}
5931
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00005932StmtResult
5933Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
5934 Stmt *AStmt, SourceLocation StartLoc,
5935 SourceLocation EndLoc) {
5936 if (!AStmt)
5937 return StmtError();
5938
5939 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5940 // 1.2.2 OpenMP Language Terminology
5941 // Structured block - An executable statement with a single entry at the
5942 // top and a single exit at the bottom.
5943 // The point of exit cannot be a branch out of the structured block.
5944 // longjmp() and throw() must not violate the entry/exit criteria.
5945 CS->getCapturedDecl()->setNothrow();
5946
5947 getCurFunction()->setHasBranchProtectedScope();
5948
5949 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5950 AStmt);
5951}
5952
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00005953StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
5954 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5955 SourceLocation EndLoc,
5956 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5957 if (!AStmt)
5958 return StmtError();
5959
5960 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5961 // 1.2.2 OpenMP Language Terminology
5962 // Structured block - An executable statement with a single entry at the
5963 // top and a single exit at the bottom.
5964 // The point of exit cannot be a branch out of the structured block.
5965 // longjmp() and throw() must not violate the entry/exit criteria.
5966 CS->getCapturedDecl()->setNothrow();
5967
5968 OMPLoopDirective::HelperExprs B;
5969 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5970 // define the nested loops number.
5971 unsigned NestedLoopCount =
5972 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
5973 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5974 VarsWithImplicitDSA, B);
5975 if (NestedLoopCount == 0)
5976 return StmtError();
5977
5978 assert((CurContext->isDependentContext() || B.builtAll()) &&
5979 "omp target parallel for loop exprs were not built");
5980
5981 if (!CurContext->isDependentContext()) {
5982 // Finalize the clauses that need pre-built expressions for CodeGen.
5983 for (auto C : Clauses) {
5984 if (auto LC = dyn_cast<OMPLinearClause>(C))
5985 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5986 B.NumIterations, *this, CurScope))
5987 return StmtError();
5988 }
5989 }
5990
5991 getCurFunction()->setHasBranchProtectedScope();
5992 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
5993 NestedLoopCount, Clauses, AStmt,
5994 B, DSAStack->isCancelRegion());
5995}
5996
Samuel Antaodf67fc42016-01-19 19:15:56 +00005997/// \brief Check for existence of a map clause in the list of clauses.
5998static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
5999 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6000 I != E; ++I) {
6001 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6002 return true;
6003 }
6004 }
6005
6006 return false;
6007}
6008
Michael Wong65f367f2015-07-21 13:44:28 +00006009StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6010 Stmt *AStmt,
6011 SourceLocation StartLoc,
6012 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006013 if (!AStmt)
6014 return StmtError();
6015
6016 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6017
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006018 // OpenMP [2.10.1, Restrictions, p. 97]
6019 // At least one map clause must appear on the directive.
6020 if (!HasMapClause(Clauses)) {
6021 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6022 getOpenMPDirectiveName(OMPD_target_data);
6023 return StmtError();
6024 }
6025
Michael Wong65f367f2015-07-21 13:44:28 +00006026 getCurFunction()->setHasBranchProtectedScope();
6027
6028 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6029 AStmt);
6030}
6031
Samuel Antaodf67fc42016-01-19 19:15:56 +00006032StmtResult
6033Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6034 SourceLocation StartLoc,
6035 SourceLocation EndLoc) {
6036 // OpenMP [2.10.2, Restrictions, p. 99]
6037 // At least one map clause must appear on the directive.
6038 if (!HasMapClause(Clauses)) {
6039 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6040 << getOpenMPDirectiveName(OMPD_target_enter_data);
6041 return StmtError();
6042 }
6043
6044 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6045 Clauses);
6046}
6047
Samuel Antao72590762016-01-19 20:04:50 +00006048StmtResult
6049Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6050 SourceLocation StartLoc,
6051 SourceLocation EndLoc) {
6052 // OpenMP [2.10.3, Restrictions, p. 102]
6053 // At least one map clause must appear on the directive.
6054 if (!HasMapClause(Clauses)) {
6055 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6056 << getOpenMPDirectiveName(OMPD_target_exit_data);
6057 return StmtError();
6058 }
6059
6060 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6061}
6062
Alexey Bataev13314bf2014-10-09 04:18:56 +00006063StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6064 Stmt *AStmt, SourceLocation StartLoc,
6065 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006066 if (!AStmt)
6067 return StmtError();
6068
Alexey Bataev13314bf2014-10-09 04:18:56 +00006069 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6070 // 1.2.2 OpenMP Language Terminology
6071 // Structured block - An executable statement with a single entry at the
6072 // top and a single exit at the bottom.
6073 // The point of exit cannot be a branch out of the structured block.
6074 // longjmp() and throw() must not violate the entry/exit criteria.
6075 CS->getCapturedDecl()->setNothrow();
6076
6077 getCurFunction()->setHasBranchProtectedScope();
6078
6079 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6080}
6081
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006082StmtResult
6083Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6084 SourceLocation EndLoc,
6085 OpenMPDirectiveKind CancelRegion) {
6086 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6087 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6088 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6089 << getOpenMPDirectiveName(CancelRegion);
6090 return StmtError();
6091 }
6092 if (DSAStack->isParentNowaitRegion()) {
6093 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6094 return StmtError();
6095 }
6096 if (DSAStack->isParentOrderedRegion()) {
6097 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6098 return StmtError();
6099 }
6100 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6101 CancelRegion);
6102}
6103
Alexey Bataev87933c72015-09-18 08:07:34 +00006104StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6105 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006106 SourceLocation EndLoc,
6107 OpenMPDirectiveKind CancelRegion) {
6108 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6109 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6110 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6111 << getOpenMPDirectiveName(CancelRegion);
6112 return StmtError();
6113 }
6114 if (DSAStack->isParentNowaitRegion()) {
6115 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6116 return StmtError();
6117 }
6118 if (DSAStack->isParentOrderedRegion()) {
6119 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6120 return StmtError();
6121 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006122 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006123 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6124 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006125}
6126
Alexey Bataev382967a2015-12-08 12:06:20 +00006127static bool checkGrainsizeNumTasksClauses(Sema &S,
6128 ArrayRef<OMPClause *> Clauses) {
6129 OMPClause *PrevClause = nullptr;
6130 bool ErrorFound = false;
6131 for (auto *C : Clauses) {
6132 if (C->getClauseKind() == OMPC_grainsize ||
6133 C->getClauseKind() == OMPC_num_tasks) {
6134 if (!PrevClause)
6135 PrevClause = C;
6136 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6137 S.Diag(C->getLocStart(),
6138 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6139 << getOpenMPClauseName(C->getClauseKind())
6140 << getOpenMPClauseName(PrevClause->getClauseKind());
6141 S.Diag(PrevClause->getLocStart(),
6142 diag::note_omp_previous_grainsize_num_tasks)
6143 << getOpenMPClauseName(PrevClause->getClauseKind());
6144 ErrorFound = true;
6145 }
6146 }
6147 }
6148 return ErrorFound;
6149}
6150
Alexey Bataev49f6e782015-12-01 04:18:41 +00006151StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6152 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6153 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006154 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006155 if (!AStmt)
6156 return StmtError();
6157
6158 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6159 OMPLoopDirective::HelperExprs B;
6160 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6161 // define the nested loops number.
6162 unsigned NestedLoopCount =
6163 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006164 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006165 VarsWithImplicitDSA, B);
6166 if (NestedLoopCount == 0)
6167 return StmtError();
6168
6169 assert((CurContext->isDependentContext() || B.builtAll()) &&
6170 "omp for loop exprs were not built");
6171
Alexey Bataev382967a2015-12-08 12:06:20 +00006172 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6173 // The grainsize clause and num_tasks clause are mutually exclusive and may
6174 // not appear on the same taskloop directive.
6175 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6176 return StmtError();
6177
Alexey Bataev49f6e782015-12-01 04:18:41 +00006178 getCurFunction()->setHasBranchProtectedScope();
6179 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6180 NestedLoopCount, Clauses, AStmt, B);
6181}
6182
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006183StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6184 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6185 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006186 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006187 if (!AStmt)
6188 return StmtError();
6189
6190 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6191 OMPLoopDirective::HelperExprs B;
6192 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6193 // define the nested loops number.
6194 unsigned NestedLoopCount =
6195 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6196 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6197 VarsWithImplicitDSA, B);
6198 if (NestedLoopCount == 0)
6199 return StmtError();
6200
6201 assert((CurContext->isDependentContext() || B.builtAll()) &&
6202 "omp for loop exprs were not built");
6203
Alexey Bataev382967a2015-12-08 12:06:20 +00006204 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6205 // The grainsize clause and num_tasks clause are mutually exclusive and may
6206 // not appear on the same taskloop directive.
6207 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6208 return StmtError();
6209
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006210 getCurFunction()->setHasBranchProtectedScope();
6211 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6212 NestedLoopCount, Clauses, AStmt, B);
6213}
6214
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006215StmtResult Sema::ActOnOpenMPDistributeDirective(
6216 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6217 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006218 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006219 if (!AStmt)
6220 return StmtError();
6221
6222 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6223 OMPLoopDirective::HelperExprs B;
6224 // In presence of clause 'collapse' with number of loops, it will
6225 // define the nested loops number.
6226 unsigned NestedLoopCount =
6227 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6228 nullptr /*ordered not a clause on distribute*/, AStmt,
6229 *this, *DSAStack, VarsWithImplicitDSA, B);
6230 if (NestedLoopCount == 0)
6231 return StmtError();
6232
6233 assert((CurContext->isDependentContext() || B.builtAll()) &&
6234 "omp for loop exprs were not built");
6235
6236 getCurFunction()->setHasBranchProtectedScope();
6237 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6238 NestedLoopCount, Clauses, AStmt, B);
6239}
6240
Alexey Bataeved09d242014-05-28 05:53:51 +00006241OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006242 SourceLocation StartLoc,
6243 SourceLocation LParenLoc,
6244 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006245 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006246 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006247 case OMPC_final:
6248 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6249 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006250 case OMPC_num_threads:
6251 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6252 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006253 case OMPC_safelen:
6254 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6255 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006256 case OMPC_simdlen:
6257 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6258 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006259 case OMPC_collapse:
6260 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6261 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006262 case OMPC_ordered:
6263 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6264 break;
Michael Wonge710d542015-08-07 16:16:36 +00006265 case OMPC_device:
6266 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6267 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006268 case OMPC_num_teams:
6269 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6270 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006271 case OMPC_thread_limit:
6272 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6273 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006274 case OMPC_priority:
6275 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6276 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006277 case OMPC_grainsize:
6278 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6279 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006280 case OMPC_num_tasks:
6281 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6282 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006283 case OMPC_hint:
6284 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6285 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006286 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006287 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006288 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006289 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006290 case OMPC_private:
6291 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006292 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006293 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006294 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006295 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006296 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006297 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006298 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006299 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006300 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006301 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006302 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006303 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006304 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006305 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006306 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006307 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006308 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006309 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006310 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006311 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006312 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006313 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006314 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006315 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006316 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006317 llvm_unreachable("Clause is not allowed.");
6318 }
6319 return Res;
6320}
6321
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006322OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6323 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006324 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006325 SourceLocation NameModifierLoc,
6326 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006327 SourceLocation EndLoc) {
6328 Expr *ValExpr = Condition;
6329 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6330 !Condition->isInstantiationDependent() &&
6331 !Condition->containsUnexpandedParameterPack()) {
6332 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006333 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006334 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006335 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006336
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006337 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006338 }
6339
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006340 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6341 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006342}
6343
Alexey Bataev3778b602014-07-17 07:32:53 +00006344OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6345 SourceLocation StartLoc,
6346 SourceLocation LParenLoc,
6347 SourceLocation EndLoc) {
6348 Expr *ValExpr = Condition;
6349 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6350 !Condition->isInstantiationDependent() &&
6351 !Condition->containsUnexpandedParameterPack()) {
6352 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6353 Condition->getExprLoc(), Condition);
6354 if (Val.isInvalid())
6355 return nullptr;
6356
6357 ValExpr = Val.get();
6358 }
6359
6360 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6361}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006362ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6363 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006364 if (!Op)
6365 return ExprError();
6366
6367 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6368 public:
6369 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006370 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006371 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6372 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006373 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6374 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006375 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6376 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006377 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6378 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006379 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6380 QualType T,
6381 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006382 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6383 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006384 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6385 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006386 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006387 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006389 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6390 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006391 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6392 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006393 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6394 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006395 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006396 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006397 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006398 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6399 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006400 llvm_unreachable("conversion functions are permitted");
6401 }
6402 } ConvertDiagnoser;
6403 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6404}
6405
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006406static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006407 OpenMPClauseKind CKind,
6408 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006409 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6410 !ValExpr->isInstantiationDependent()) {
6411 SourceLocation Loc = ValExpr->getExprLoc();
6412 ExprResult Value =
6413 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6414 if (Value.isInvalid())
6415 return false;
6416
6417 ValExpr = Value.get();
6418 // The expression must evaluate to a non-negative integer value.
6419 llvm::APSInt Result;
6420 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006421 Result.isSigned() &&
6422 !((!StrictlyPositive && Result.isNonNegative()) ||
6423 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006424 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006425 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6426 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006427 return false;
6428 }
6429 }
6430 return true;
6431}
6432
Alexey Bataev568a8332014-03-06 06:15:19 +00006433OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6434 SourceLocation StartLoc,
6435 SourceLocation LParenLoc,
6436 SourceLocation EndLoc) {
6437 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006438
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006439 // OpenMP [2.5, Restrictions]
6440 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006441 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6442 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006443 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006444
Alexey Bataeved09d242014-05-28 05:53:51 +00006445 return new (Context)
6446 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006447}
6448
Alexey Bataev62c87d22014-03-21 04:51:18 +00006449ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006450 OpenMPClauseKind CKind,
6451 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006452 if (!E)
6453 return ExprError();
6454 if (E->isValueDependent() || E->isTypeDependent() ||
6455 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006456 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006457 llvm::APSInt Result;
6458 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6459 if (ICE.isInvalid())
6460 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006461 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6462 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006463 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006464 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6465 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006466 return ExprError();
6467 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006468 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6469 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6470 << E->getSourceRange();
6471 return ExprError();
6472 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006473 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6474 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006475 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006476 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006477 return ICE;
6478}
6479
6480OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6481 SourceLocation LParenLoc,
6482 SourceLocation EndLoc) {
6483 // OpenMP [2.8.1, simd construct, Description]
6484 // The parameter of the safelen clause must be a constant
6485 // positive integer expression.
6486 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6487 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006488 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006489 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006490 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006491}
6492
Alexey Bataev66b15b52015-08-21 11:14:16 +00006493OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6494 SourceLocation LParenLoc,
6495 SourceLocation EndLoc) {
6496 // OpenMP [2.8.1, simd construct, Description]
6497 // The parameter of the simdlen clause must be a constant
6498 // positive integer expression.
6499 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6500 if (Simdlen.isInvalid())
6501 return nullptr;
6502 return new (Context)
6503 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6504}
6505
Alexander Musman64d33f12014-06-04 07:53:32 +00006506OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6507 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006508 SourceLocation LParenLoc,
6509 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006510 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006511 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006512 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006513 // The parameter of the collapse clause must be a constant
6514 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006515 ExprResult NumForLoopsResult =
6516 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6517 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006518 return nullptr;
6519 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006520 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006521}
6522
Alexey Bataev10e775f2015-07-30 11:36:16 +00006523OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6524 SourceLocation EndLoc,
6525 SourceLocation LParenLoc,
6526 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006527 // OpenMP [2.7.1, loop construct, Description]
6528 // OpenMP [2.8.1, simd construct, Description]
6529 // OpenMP [2.9.6, distribute construct, Description]
6530 // The parameter of the ordered clause must be a constant
6531 // positive integer expression if any.
6532 if (NumForLoops && LParenLoc.isValid()) {
6533 ExprResult NumForLoopsResult =
6534 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6535 if (NumForLoopsResult.isInvalid())
6536 return nullptr;
6537 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006538 } else
6539 NumForLoops = nullptr;
6540 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006541 return new (Context)
6542 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6543}
6544
Alexey Bataeved09d242014-05-28 05:53:51 +00006545OMPClause *Sema::ActOnOpenMPSimpleClause(
6546 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6547 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006548 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006549 switch (Kind) {
6550 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006551 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006552 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6553 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006554 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006555 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006556 Res = ActOnOpenMPProcBindClause(
6557 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6558 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006559 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006560 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006561 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006562 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006563 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006564 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006565 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006566 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006567 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006568 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006569 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006570 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006571 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006572 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006573 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006574 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006575 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006576 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006577 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006578 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006579 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006580 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006581 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006582 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006583 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006584 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006585 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006586 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006587 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006588 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006589 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006590 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006591 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006592 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006593 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006594 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006595 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006596 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006597 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006598 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006599 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006600 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006601 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006602 llvm_unreachable("Clause is not allowed.");
6603 }
6604 return Res;
6605}
6606
Alexey Bataev6402bca2015-12-28 07:25:51 +00006607static std::string
6608getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6609 ArrayRef<unsigned> Exclude = llvm::None) {
6610 std::string Values;
6611 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6612 unsigned Skipped = Exclude.size();
6613 auto S = Exclude.begin(), E = Exclude.end();
6614 for (unsigned i = First; i < Last; ++i) {
6615 if (std::find(S, E, i) != E) {
6616 --Skipped;
6617 continue;
6618 }
6619 Values += "'";
6620 Values += getOpenMPSimpleClauseTypeName(K, i);
6621 Values += "'";
6622 if (i == Bound - Skipped)
6623 Values += " or ";
6624 else if (i != Bound + 1 - Skipped)
6625 Values += ", ";
6626 }
6627 return Values;
6628}
6629
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006630OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6631 SourceLocation KindKwLoc,
6632 SourceLocation StartLoc,
6633 SourceLocation LParenLoc,
6634 SourceLocation EndLoc) {
6635 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006636 static_assert(OMPC_DEFAULT_unknown > 0,
6637 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006638 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006639 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6640 /*Last=*/OMPC_DEFAULT_unknown)
6641 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006642 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006643 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006644 switch (Kind) {
6645 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006646 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006647 break;
6648 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006649 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006650 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006651 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006652 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006653 break;
6654 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006655 return new (Context)
6656 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006657}
6658
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006659OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6660 SourceLocation KindKwLoc,
6661 SourceLocation StartLoc,
6662 SourceLocation LParenLoc,
6663 SourceLocation EndLoc) {
6664 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006665 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006666 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6667 /*Last=*/OMPC_PROC_BIND_unknown)
6668 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006669 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006670 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006671 return new (Context)
6672 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006673}
6674
Alexey Bataev56dafe82014-06-20 07:16:17 +00006675OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006676 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006677 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006678 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006679 SourceLocation EndLoc) {
6680 OMPClause *Res = nullptr;
6681 switch (Kind) {
6682 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006683 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6684 assert(Argument.size() == NumberOfElements &&
6685 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006686 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006687 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6688 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6689 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6690 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6691 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006692 break;
6693 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006694 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6695 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6696 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6697 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006698 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006699 case OMPC_dist_schedule:
6700 Res = ActOnOpenMPDistScheduleClause(
6701 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6702 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6703 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006704 case OMPC_defaultmap:
6705 enum { Modifier, DefaultmapKind };
6706 Res = ActOnOpenMPDefaultmapClause(
6707 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6708 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6709 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6710 ArgumentLoc[DefaultmapKind], EndLoc);
6711 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006712 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006713 case OMPC_num_threads:
6714 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006715 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006716 case OMPC_collapse:
6717 case OMPC_default:
6718 case OMPC_proc_bind:
6719 case OMPC_private:
6720 case OMPC_firstprivate:
6721 case OMPC_lastprivate:
6722 case OMPC_shared:
6723 case OMPC_reduction:
6724 case OMPC_linear:
6725 case OMPC_aligned:
6726 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006727 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006728 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006729 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006730 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006731 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006732 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006733 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006734 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006735 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006736 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006737 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006738 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006739 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006740 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006741 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006742 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006743 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006744 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006745 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006746 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006747 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006748 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006749 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006750 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006751 case OMPC_unknown:
6752 llvm_unreachable("Clause is not allowed.");
6753 }
6754 return Res;
6755}
6756
Alexey Bataev6402bca2015-12-28 07:25:51 +00006757static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
6758 OpenMPScheduleClauseModifier M2,
6759 SourceLocation M1Loc, SourceLocation M2Loc) {
6760 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
6761 SmallVector<unsigned, 2> Excluded;
6762 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
6763 Excluded.push_back(M2);
6764 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
6765 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
6766 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
6767 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
6768 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
6769 << getListOfPossibleValues(OMPC_schedule,
6770 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
6771 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6772 Excluded)
6773 << getOpenMPClauseName(OMPC_schedule);
6774 return true;
6775 }
6776 return false;
6777}
6778
Alexey Bataev56dafe82014-06-20 07:16:17 +00006779OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006780 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006781 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006782 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
6783 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
6784 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
6785 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
6786 return nullptr;
6787 // OpenMP, 2.7.1, Loop Construct, Restrictions
6788 // Either the monotonic modifier or the nonmonotonic modifier can be specified
6789 // but not both.
6790 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
6791 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
6792 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
6793 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
6794 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
6795 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
6796 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
6797 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
6798 return nullptr;
6799 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006800 if (Kind == OMPC_SCHEDULE_unknown) {
6801 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00006802 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
6803 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
6804 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6805 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
6806 Exclude);
6807 } else {
6808 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
6809 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006810 }
6811 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6812 << Values << getOpenMPClauseName(OMPC_schedule);
6813 return nullptr;
6814 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00006815 // OpenMP, 2.7.1, Loop Construct, Restrictions
6816 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
6817 // schedule(guided).
6818 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
6819 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
6820 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
6821 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
6822 diag::err_omp_schedule_nonmonotonic_static);
6823 return nullptr;
6824 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00006825 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00006826 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006827 if (ChunkSize) {
6828 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6829 !ChunkSize->isInstantiationDependent() &&
6830 !ChunkSize->containsUnexpandedParameterPack()) {
6831 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6832 ExprResult Val =
6833 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6834 if (Val.isInvalid())
6835 return nullptr;
6836
6837 ValExpr = Val.get();
6838
6839 // OpenMP [2.7.1, Restrictions]
6840 // chunk_size must be a loop invariant integer expression with a positive
6841 // value.
6842 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006843 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6844 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6845 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006846 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006847 return nullptr;
6848 }
6849 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00006850 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +00006851 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
6852 HelperValStmt =
6853 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
6854 /*NumDecls=*/1),
6855 SourceLocation(), SourceLocation());
6856 ValExpr = DefaultLvalueConversion(ValExpr).get();
Alexey Bataev56dafe82014-06-20 07:16:17 +00006857 }
6858 }
6859 }
6860
Alexey Bataev6402bca2015-12-28 07:25:51 +00006861 return new (Context)
6862 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00006863 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006864}
6865
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006866OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6867 SourceLocation StartLoc,
6868 SourceLocation EndLoc) {
6869 OMPClause *Res = nullptr;
6870 switch (Kind) {
6871 case OMPC_ordered:
6872 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6873 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006874 case OMPC_nowait:
6875 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6876 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006877 case OMPC_untied:
6878 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6879 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006880 case OMPC_mergeable:
6881 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6882 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006883 case OMPC_read:
6884 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6885 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006886 case OMPC_write:
6887 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6888 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006889 case OMPC_update:
6890 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6891 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006892 case OMPC_capture:
6893 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6894 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006895 case OMPC_seq_cst:
6896 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6897 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006898 case OMPC_threads:
6899 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6900 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006901 case OMPC_simd:
6902 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6903 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006904 case OMPC_nogroup:
6905 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6906 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006907 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006908 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006909 case OMPC_num_threads:
6910 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006911 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006912 case OMPC_collapse:
6913 case OMPC_schedule:
6914 case OMPC_private:
6915 case OMPC_firstprivate:
6916 case OMPC_lastprivate:
6917 case OMPC_shared:
6918 case OMPC_reduction:
6919 case OMPC_linear:
6920 case OMPC_aligned:
6921 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006922 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006923 case OMPC_default:
6924 case OMPC_proc_bind:
6925 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006926 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006927 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006928 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006929 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006930 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006931 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006932 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006933 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006934 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006935 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006936 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006937 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006938 case OMPC_unknown:
6939 llvm_unreachable("Clause is not allowed.");
6940 }
6941 return Res;
6942}
6943
Alexey Bataev236070f2014-06-20 11:19:47 +00006944OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6945 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006946 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006947 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6948}
6949
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006950OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6951 SourceLocation EndLoc) {
6952 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6953}
6954
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006955OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6956 SourceLocation EndLoc) {
6957 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6958}
6959
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006960OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6961 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006962 return new (Context) OMPReadClause(StartLoc, EndLoc);
6963}
6964
Alexey Bataevdea47612014-07-23 07:46:59 +00006965OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6966 SourceLocation EndLoc) {
6967 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6968}
6969
Alexey Bataev67a4f222014-07-23 10:25:33 +00006970OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6971 SourceLocation EndLoc) {
6972 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6973}
6974
Alexey Bataev459dec02014-07-24 06:46:57 +00006975OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6976 SourceLocation EndLoc) {
6977 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6978}
6979
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006980OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6981 SourceLocation EndLoc) {
6982 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6983}
6984
Alexey Bataev346265e2015-09-25 10:37:12 +00006985OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6986 SourceLocation EndLoc) {
6987 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6988}
6989
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006990OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6991 SourceLocation EndLoc) {
6992 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6993}
6994
Alexey Bataevb825de12015-12-07 10:51:44 +00006995OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6996 SourceLocation EndLoc) {
6997 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6998}
6999
Alexey Bataevc5e02582014-06-16 07:08:35 +00007000OMPClause *Sema::ActOnOpenMPVarListClause(
7001 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7002 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7003 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007004 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007005 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7006 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7007 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007008 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007009 switch (Kind) {
7010 case OMPC_private:
7011 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7012 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007013 case OMPC_firstprivate:
7014 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7015 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007016 case OMPC_lastprivate:
7017 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7018 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007019 case OMPC_shared:
7020 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7021 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007022 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007023 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7024 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007025 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007026 case OMPC_linear:
7027 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007028 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007029 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007030 case OMPC_aligned:
7031 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7032 ColonLoc, EndLoc);
7033 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007034 case OMPC_copyin:
7035 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7036 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007037 case OMPC_copyprivate:
7038 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7039 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007040 case OMPC_flush:
7041 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7042 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007043 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007044 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7045 StartLoc, LParenLoc, EndLoc);
7046 break;
7047 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007048 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7049 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7050 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007051 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007052 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007053 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007054 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007055 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007056 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007057 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007058 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007059 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007060 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007061 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007062 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007063 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007064 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007065 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007066 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007067 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007068 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007069 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007070 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007071 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007072 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007073 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007074 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007075 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007076 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007077 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007078 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007079 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007080 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007081 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007082 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007083 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007084 llvm_unreachable("Clause is not allowed.");
7085 }
7086 return Res;
7087}
7088
Alexey Bataev90c228f2016-02-08 09:29:13 +00007089ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007090 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007091 ExprResult Res = BuildDeclRefExpr(
7092 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7093 if (!Res.isUsable())
7094 return ExprError();
7095 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7096 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7097 if (!Res.isUsable())
7098 return ExprError();
7099 }
7100 if (VK != VK_LValue && Res.get()->isGLValue()) {
7101 Res = DefaultLvalueConversion(Res.get());
7102 if (!Res.isUsable())
7103 return ExprError();
7104 }
7105 return Res;
7106}
7107
Alexey Bataev60da77e2016-02-29 05:54:20 +00007108static std::pair<ValueDecl *, bool>
7109getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7110 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007111 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7112 RefExpr->containsUnexpandedParameterPack())
7113 return std::make_pair(nullptr, true);
7114
Alexey Bataevd985eda2016-02-10 11:29:16 +00007115 // OpenMP [3.1, C/C++]
7116 // A list item is a variable name.
7117 // OpenMP [2.9.3.3, Restrictions, p.1]
7118 // A variable that is part of another variable (as an array or
7119 // structure element) cannot appear in a private clause.
7120 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007121 enum {
7122 NoArrayExpr = -1,
7123 ArraySubscript = 0,
7124 OMPArraySection = 1
7125 } IsArrayExpr = NoArrayExpr;
7126 if (AllowArraySection) {
7127 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7128 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7129 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7130 Base = TempASE->getBase()->IgnoreParenImpCasts();
7131 RefExpr = Base;
7132 IsArrayExpr = ArraySubscript;
7133 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7134 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7135 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7136 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7137 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7138 Base = TempASE->getBase()->IgnoreParenImpCasts();
7139 RefExpr = Base;
7140 IsArrayExpr = OMPArraySection;
7141 }
7142 }
7143 ELoc = RefExpr->getExprLoc();
7144 ERange = RefExpr->getSourceRange();
7145 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007146 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7147 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7148 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7149 (S.getCurrentThisType().isNull() || !ME ||
7150 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7151 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007152 if (IsArrayExpr != NoArrayExpr)
7153 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7154 << ERange;
7155 else {
7156 S.Diag(ELoc,
7157 AllowArraySection
7158 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7159 : diag::err_omp_expected_var_name_member_expr)
7160 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7161 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007162 return std::make_pair(nullptr, false);
7163 }
7164 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7165}
7166
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007167OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7168 SourceLocation StartLoc,
7169 SourceLocation LParenLoc,
7170 SourceLocation EndLoc) {
7171 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007172 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007173 for (auto &RefExpr : VarList) {
7174 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007175 SourceLocation ELoc;
7176 SourceRange ERange;
7177 Expr *SimpleRefExpr = RefExpr;
7178 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007179 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007180 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007181 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007182 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007183 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007184 ValueDecl *D = Res.first;
7185 if (!D)
7186 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007187
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007188 QualType Type = D->getType();
7189 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007190
7191 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7192 // A variable that appears in a private clause must not have an incomplete
7193 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007194 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007195 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007196 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007197
Alexey Bataev758e55e2013-09-06 18:03:48 +00007198 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7199 // in a Construct]
7200 // Variables with the predetermined data-sharing attributes may not be
7201 // listed in data-sharing attributes clauses, except for the cases
7202 // listed below. For these exceptions only, listing a predetermined
7203 // variable in a data-sharing attribute clause is allowed and overrides
7204 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007205 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007206 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007207 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7208 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007209 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007210 continue;
7211 }
7212
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007213 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007214 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007215 DSAStack->getCurrentDirective() == OMPD_task) {
7216 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7217 << getOpenMPClauseName(OMPC_private) << Type
7218 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7219 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007220 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007221 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007222 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007223 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007224 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007225 continue;
7226 }
7227
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007228 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7229 // A list item cannot appear in both a map clause and a data-sharing
7230 // attribute clause on the same construct
7231 if (DSAStack->getCurrentDirective() == OMPD_target) {
7232 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7233 [&](Expr *RE) -> bool {return true;})) {
7234 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7235 << getOpenMPClauseName(OMPC_private)
7236 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7237 ReportOriginalDSA(*this, DSAStack, D, DVar);
7238 continue;
7239 }
7240 }
7241
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007242 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7243 // A variable of class type (or array thereof) that appears in a private
7244 // clause requires an accessible, unambiguous default constructor for the
7245 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007246 // Generate helper private variable and initialize it with the default
7247 // value. The address of the original variable is replaced by the address of
7248 // the new private variable in CodeGen. This new variable is not added to
7249 // IdResolver, so the code in the OpenMP region uses original variable for
7250 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007251 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007252 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7253 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007254 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007255 if (VDPrivate->isInvalidDecl())
7256 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007257 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007258 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007259
Alexey Bataev90c228f2016-02-08 09:29:13 +00007260 DeclRefExpr *Ref = nullptr;
7261 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007262 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007263 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7264 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007265 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007266 }
7267
Alexey Bataeved09d242014-05-28 05:53:51 +00007268 if (Vars.empty())
7269 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007270
Alexey Bataev03b340a2014-10-21 03:16:40 +00007271 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7272 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007273}
7274
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007275namespace {
7276class DiagsUninitializedSeveretyRAII {
7277private:
7278 DiagnosticsEngine &Diags;
7279 SourceLocation SavedLoc;
7280 bool IsIgnored;
7281
7282public:
7283 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7284 bool IsIgnored)
7285 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7286 if (!IsIgnored) {
7287 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7288 /*Map*/ diag::Severity::Ignored, Loc);
7289 }
7290 }
7291 ~DiagsUninitializedSeveretyRAII() {
7292 if (!IsIgnored)
7293 Diags.popMappings(SavedLoc);
7294 }
7295};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007296}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007297
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007298OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7299 SourceLocation StartLoc,
7300 SourceLocation LParenLoc,
7301 SourceLocation EndLoc) {
7302 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007303 SmallVector<Expr *, 8> PrivateCopies;
7304 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007305 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007306 bool IsImplicitClause =
7307 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7308 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7309
Alexey Bataeved09d242014-05-28 05:53:51 +00007310 for (auto &RefExpr : VarList) {
7311 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007312 SourceLocation ELoc;
7313 SourceRange ERange;
7314 Expr *SimpleRefExpr = RefExpr;
7315 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007316 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007317 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007318 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007319 PrivateCopies.push_back(nullptr);
7320 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007321 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007322 ValueDecl *D = Res.first;
7323 if (!D)
7324 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007325
Alexey Bataev60da77e2016-02-29 05:54:20 +00007326 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007327 QualType Type = D->getType();
7328 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007329
7330 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7331 // A variable that appears in a private clause must not have an incomplete
7332 // type or a reference type.
7333 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007334 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007335 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007336 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007337
7338 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7339 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007340 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007341 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007342 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007343
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007344 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007345 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007346 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007347 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007348 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007349 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007350 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7351 // A list item that specifies a given variable may not appear in more
7352 // than one clause on the same directive, except that a variable may be
7353 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007354 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007355 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007356 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007357 << getOpenMPClauseName(DVar.CKind)
7358 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007359 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007360 continue;
7361 }
7362
7363 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7364 // in a Construct]
7365 // Variables with the predetermined data-sharing attributes may not be
7366 // listed in data-sharing attributes clauses, except for the cases
7367 // listed below. For these exceptions only, listing a predetermined
7368 // variable in a data-sharing attribute clause is allowed and overrides
7369 // the variable's predetermined data-sharing attributes.
7370 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7371 // in a Construct, C/C++, p.2]
7372 // Variables with const-qualified type having no mutable member may be
7373 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007374 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007375 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7376 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007377 << getOpenMPClauseName(DVar.CKind)
7378 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007379 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007380 continue;
7381 }
7382
Alexey Bataevf29276e2014-06-18 04:14:57 +00007383 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007384 // OpenMP [2.9.3.4, Restrictions, p.2]
7385 // A list item that is private within a parallel region must not appear
7386 // in a firstprivate clause on a worksharing construct if any of the
7387 // worksharing regions arising from the worksharing construct ever bind
7388 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007389 if (isOpenMPWorksharingDirective(CurrDir) &&
7390 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007391 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007392 if (DVar.CKind != OMPC_shared &&
7393 (isOpenMPParallelDirective(DVar.DKind) ||
7394 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007395 Diag(ELoc, diag::err_omp_required_access)
7396 << getOpenMPClauseName(OMPC_firstprivate)
7397 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007398 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007399 continue;
7400 }
7401 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007402 // OpenMP [2.9.3.4, Restrictions, p.3]
7403 // A list item that appears in a reduction clause of a parallel construct
7404 // must not appear in a firstprivate clause on a worksharing or task
7405 // construct if any of the worksharing or task regions arising from the
7406 // worksharing or task construct ever bind to any of the parallel regions
7407 // arising from the parallel construct.
7408 // OpenMP [2.9.3.4, Restrictions, p.4]
7409 // A list item that appears in a reduction clause in worksharing
7410 // construct must not appear in a firstprivate clause in a task construct
7411 // encountered during execution of any of the worksharing regions arising
7412 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007413 if (CurrDir == OMPD_task) {
7414 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007415 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007416 [](OpenMPDirectiveKind K) -> bool {
7417 return isOpenMPParallelDirective(K) ||
7418 isOpenMPWorksharingDirective(K);
7419 },
7420 false);
7421 if (DVar.CKind == OMPC_reduction &&
7422 (isOpenMPParallelDirective(DVar.DKind) ||
7423 isOpenMPWorksharingDirective(DVar.DKind))) {
7424 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7425 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007426 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007427 continue;
7428 }
7429 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007430
7431 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7432 // A list item that is private within a teams region must not appear in a
7433 // firstprivate clause on a distribute construct if any of the distribute
7434 // regions arising from the distribute construct ever bind to any of the
7435 // teams regions arising from the teams construct.
7436 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7437 // A list item that appears in a reduction clause of a teams construct
7438 // must not appear in a firstprivate clause on a distribute construct if
7439 // any of the distribute regions arising from the distribute construct
7440 // ever bind to any of the teams regions arising from the teams construct.
7441 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7442 // A list item may appear in a firstprivate or lastprivate clause but not
7443 // both.
7444 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007445 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007446 [](OpenMPDirectiveKind K) -> bool {
7447 return isOpenMPTeamsDirective(K);
7448 },
7449 false);
7450 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7451 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007452 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007453 continue;
7454 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007455 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007456 [](OpenMPDirectiveKind K) -> bool {
7457 return isOpenMPTeamsDirective(K);
7458 },
7459 false);
7460 if (DVar.CKind == OMPC_reduction &&
7461 isOpenMPTeamsDirective(DVar.DKind)) {
7462 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007463 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007464 continue;
7465 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007466 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007467 if (DVar.CKind == OMPC_lastprivate) {
7468 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007469 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007470 continue;
7471 }
7472 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007473 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7474 // A list item cannot appear in both a map clause and a data-sharing
7475 // attribute clause on the same construct
7476 if (CurrDir == OMPD_target) {
7477 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7478 [&](Expr *RE) -> bool {return true;})) {
7479 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7480 << getOpenMPClauseName(OMPC_firstprivate)
7481 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7482 ReportOriginalDSA(*this, DSAStack, D, DVar);
7483 continue;
7484 }
7485 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007486 }
7487
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007488 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007489 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007490 DSAStack->getCurrentDirective() == OMPD_task) {
7491 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7492 << getOpenMPClauseName(OMPC_firstprivate) << Type
7493 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7494 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007495 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007496 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007497 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007498 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007499 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007500 continue;
7501 }
7502
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007503 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007504 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7505 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007506 // Generate helper private variable and initialize it with the value of the
7507 // original variable. The address of the original variable is replaced by
7508 // the address of the new private variable in the CodeGen. This new variable
7509 // is not added to IdResolver, so the code in the OpenMP region uses
7510 // original variable for proper diagnostics and variable capturing.
7511 Expr *VDInitRefExpr = nullptr;
7512 // For arrays generate initializer for single element and replace it by the
7513 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007514 if (Type->isArrayType()) {
7515 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007516 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007517 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007518 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007519 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007520 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007521 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007522 InitializedEntity Entity =
7523 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007524 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7525
7526 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7527 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7528 if (Result.isInvalid())
7529 VDPrivate->setInvalidDecl();
7530 else
7531 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007532 // Remove temp variable declaration.
7533 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007534 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007535 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7536 ".firstprivate.temp");
7537 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7538 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007539 AddInitializerToDecl(VDPrivate,
7540 DefaultLvalueConversion(VDInitRefExpr).get(),
7541 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007542 }
7543 if (VDPrivate->isInvalidDecl()) {
7544 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007545 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007546 diag::note_omp_task_predetermined_firstprivate_here);
7547 }
7548 continue;
7549 }
7550 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007551 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007552 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7553 RefExpr->getExprLoc());
7554 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007555 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007556 if (TopDVar.CKind == OMPC_lastprivate)
7557 Ref = TopDVar.PrivateCopy;
7558 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007559 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007560 if (!IsOpenMPCapturedDecl(D))
7561 ExprCaptures.push_back(Ref->getDecl());
7562 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007563 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007564 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7565 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007566 PrivateCopies.push_back(VDPrivateRefExpr);
7567 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007568 }
7569
Alexey Bataeved09d242014-05-28 05:53:51 +00007570 if (Vars.empty())
7571 return nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007572 Stmt *PreInit = nullptr;
7573 if (!ExprCaptures.empty()) {
7574 PreInit = new (Context)
7575 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7576 ExprCaptures.size()),
7577 SourceLocation(), SourceLocation());
7578 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007579
7580 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev417089f2016-02-17 13:19:37 +00007581 Vars, PrivateCopies, Inits, PreInit);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007582}
7583
Alexander Musman1bb328c2014-06-04 13:06:39 +00007584OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7585 SourceLocation StartLoc,
7586 SourceLocation LParenLoc,
7587 SourceLocation EndLoc) {
7588 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007589 SmallVector<Expr *, 8> SrcExprs;
7590 SmallVector<Expr *, 8> DstExprs;
7591 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007592 SmallVector<Decl *, 4> ExprCaptures;
7593 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007594 for (auto &RefExpr : VarList) {
7595 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007596 SourceLocation ELoc;
7597 SourceRange ERange;
7598 Expr *SimpleRefExpr = RefExpr;
7599 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007600 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007601 // It will be analyzed later.
7602 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007603 SrcExprs.push_back(nullptr);
7604 DstExprs.push_back(nullptr);
7605 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007606 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007607 ValueDecl *D = Res.first;
7608 if (!D)
7609 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007610
Alexey Bataev74caaf22016-02-20 04:09:36 +00007611 QualType Type = D->getType();
7612 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007613
7614 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7615 // A variable that appears in a lastprivate clause must not have an
7616 // incomplete type or a reference type.
7617 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007618 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007619 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007620 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007621
7622 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7623 // in a Construct]
7624 // Variables with the predetermined data-sharing attributes may not be
7625 // listed in data-sharing attributes clauses, except for the cases
7626 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007627 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007628 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7629 DVar.CKind != OMPC_firstprivate &&
7630 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7631 Diag(ELoc, diag::err_omp_wrong_dsa)
7632 << getOpenMPClauseName(DVar.CKind)
7633 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007634 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007635 continue;
7636 }
7637
Alexey Bataevf29276e2014-06-18 04:14:57 +00007638 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7639 // OpenMP [2.14.3.5, Restrictions, p.2]
7640 // A list item that is private within a parallel region, or that appears in
7641 // the reduction clause of a parallel construct, must not appear in a
7642 // lastprivate clause on a worksharing construct if any of the corresponding
7643 // worksharing regions ever binds to any of the corresponding parallel
7644 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007645 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007646 if (isOpenMPWorksharingDirective(CurrDir) &&
7647 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007648 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007649 if (DVar.CKind != OMPC_shared) {
7650 Diag(ELoc, diag::err_omp_required_access)
7651 << getOpenMPClauseName(OMPC_lastprivate)
7652 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007653 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007654 continue;
7655 }
7656 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007657
7658 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7659 // A list item may appear in a firstprivate or lastprivate clause but not
7660 // both.
7661 if (CurrDir == OMPD_distribute) {
7662 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7663 if (DVar.CKind == OMPC_firstprivate) {
7664 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7665 ReportOriginalDSA(*this, DSAStack, D, DVar);
7666 continue;
7667 }
7668 }
7669
Alexander Musman1bb328c2014-06-04 13:06:39 +00007670 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007671 // A variable of class type (or array thereof) that appears in a
7672 // lastprivate clause requires an accessible, unambiguous default
7673 // constructor for the class type, unless the list item is also specified
7674 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007675 // A variable of class type (or array thereof) that appears in a
7676 // lastprivate clause requires an accessible, unambiguous copy assignment
7677 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007678 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007679 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007680 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007681 D->hasAttrs() ? &D->getAttrs() : nullptr);
7682 auto *PseudoSrcExpr =
7683 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007684 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007685 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007686 D->hasAttrs() ? &D->getAttrs() : nullptr);
7687 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007688 // For arrays generate assignment operation for single element and replace
7689 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007690 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007691 PseudoDstExpr, PseudoSrcExpr);
7692 if (AssignmentOp.isInvalid())
7693 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007694 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007695 /*DiscardedValue=*/true);
7696 if (AssignmentOp.isInvalid())
7697 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007698
Alexey Bataev74caaf22016-02-20 04:09:36 +00007699 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007700 if (!VD) {
7701 if (TopDVar.CKind == OMPC_firstprivate)
7702 Ref = TopDVar.PrivateCopy;
7703 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007704 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007705 if (!IsOpenMPCapturedDecl(D))
7706 ExprCaptures.push_back(Ref->getDecl());
7707 }
7708 if (TopDVar.CKind == OMPC_firstprivate ||
7709 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007710 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007711 ExprResult RefRes = DefaultLvalueConversion(Ref);
7712 if (!RefRes.isUsable())
7713 continue;
7714 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007715 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7716 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007717 if (!PostUpdateRes.isUsable())
7718 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007719 ExprPostUpdates.push_back(
7720 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007721 }
7722 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007723 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007724 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7725 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007726 SrcExprs.push_back(PseudoSrcExpr);
7727 DstExprs.push_back(PseudoDstExpr);
7728 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00007729 }
7730
7731 if (Vars.empty())
7732 return nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007733 Stmt *PreInit = nullptr;
7734 if (!ExprCaptures.empty()) {
7735 PreInit = new (Context)
7736 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
7737 ExprCaptures.size()),
7738 SourceLocation(), SourceLocation());
7739 }
7740 Expr *PostUpdate = nullptr;
7741 if (!ExprPostUpdates.empty()) {
7742 for (auto *E : ExprPostUpdates) {
7743 ExprResult PostUpdateRes =
7744 PostUpdate
7745 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
7746 : E;
7747 PostUpdate = PostUpdateRes.get();
7748 }
7749 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00007750
7751 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00007752 Vars, SrcExprs, DstExprs, AssignmentOps,
7753 PreInit, PostUpdate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007754}
7755
Alexey Bataev758e55e2013-09-06 18:03:48 +00007756OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
7757 SourceLocation StartLoc,
7758 SourceLocation LParenLoc,
7759 SourceLocation EndLoc) {
7760 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00007761 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007762 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007763 SourceLocation ELoc;
7764 SourceRange ERange;
7765 Expr *SimpleRefExpr = RefExpr;
7766 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007767 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00007768 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007769 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007770 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007771 ValueDecl *D = Res.first;
7772 if (!D)
7773 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007774
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007775 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007776 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7777 // in a Construct]
7778 // Variables with the predetermined data-sharing attributes may not be
7779 // listed in data-sharing attributes clauses, except for the cases
7780 // listed below. For these exceptions only, listing a predetermined
7781 // variable in a data-sharing attribute clause is allowed and overrides
7782 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007783 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00007784 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
7785 DVar.RefExpr) {
7786 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7787 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007788 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007789 continue;
7790 }
7791
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007792 DeclRefExpr *Ref = nullptr;
7793 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007794 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00007795 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
7796 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007797 }
7798
Alexey Bataeved09d242014-05-28 05:53:51 +00007799 if (Vars.empty())
7800 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007801
7802 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
7803}
7804
Alexey Bataevc5e02582014-06-16 07:08:35 +00007805namespace {
7806class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
7807 DSAStackTy *Stack;
7808
7809public:
7810 bool VisitDeclRefExpr(DeclRefExpr *E) {
7811 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007812 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007813 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
7814 return false;
7815 if (DVar.CKind != OMPC_unknown)
7816 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007817 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007818 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007819 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007820 return true;
7821 return false;
7822 }
7823 return false;
7824 }
7825 bool VisitStmt(Stmt *S) {
7826 for (auto Child : S->children()) {
7827 if (Child && Visit(Child))
7828 return true;
7829 }
7830 return false;
7831 }
Alexey Bataev23b69422014-06-18 07:08:49 +00007832 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00007833};
Alexey Bataev23b69422014-06-18 07:08:49 +00007834} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00007835
Alexey Bataev60da77e2016-02-29 05:54:20 +00007836namespace {
7837// Transform MemberExpression for specified FieldDecl of current class to
7838// DeclRefExpr to specified OMPCapturedExprDecl.
7839class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
7840 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
7841 ValueDecl *Field;
7842 DeclRefExpr *CapturedExpr;
7843
7844public:
7845 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
7846 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
7847
7848 ExprResult TransformMemberExpr(MemberExpr *E) {
7849 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
7850 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00007851 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00007852 return CapturedExpr;
7853 }
7854 return BaseTransform::TransformMemberExpr(E);
7855 }
7856 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
7857};
7858} // namespace
7859
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007860template <typename T>
7861static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
7862 const llvm::function_ref<T(ValueDecl *)> &Gen) {
7863 for (auto &Set : Lookups) {
7864 for (auto *D : Set) {
7865 if (auto Res = Gen(cast<ValueDecl>(D)))
7866 return Res;
7867 }
7868 }
7869 return T();
7870}
7871
7872static ExprResult
7873buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
7874 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
7875 const DeclarationNameInfo &ReductionId, QualType Ty,
7876 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
7877 if (ReductionIdScopeSpec.isInvalid())
7878 return ExprError();
7879 SmallVector<UnresolvedSet<8>, 4> Lookups;
7880 if (S) {
7881 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
7882 Lookup.suppressDiagnostics();
7883 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
7884 auto *D = Lookup.getRepresentativeDecl();
7885 do {
7886 S = S->getParent();
7887 } while (S && !S->isDeclScope(D));
7888 if (S)
7889 S = S->getParent();
7890 Lookups.push_back(UnresolvedSet<8>());
7891 Lookups.back().append(Lookup.begin(), Lookup.end());
7892 Lookup.clear();
7893 }
7894 } else if (auto *ULE =
7895 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
7896 Lookups.push_back(UnresolvedSet<8>());
7897 Decl *PrevD = nullptr;
7898 for(auto *D : ULE->decls()) {
7899 if (D == PrevD)
7900 Lookups.push_back(UnresolvedSet<8>());
7901 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
7902 Lookups.back().addDecl(DRD);
7903 PrevD = D;
7904 }
7905 }
7906 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
7907 Ty->containsUnexpandedParameterPack() ||
7908 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
7909 return !D->isInvalidDecl() &&
7910 (D->getType()->isDependentType() ||
7911 D->getType()->isInstantiationDependentType() ||
7912 D->getType()->containsUnexpandedParameterPack());
7913 })) {
7914 UnresolvedSet<8> ResSet;
7915 for (auto &Set : Lookups) {
7916 ResSet.append(Set.begin(), Set.end());
7917 // The last item marks the end of all declarations at the specified scope.
7918 ResSet.addDecl(Set[Set.size() - 1]);
7919 }
7920 return UnresolvedLookupExpr::Create(
7921 SemaRef.Context, /*NamingClass=*/nullptr,
7922 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
7923 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
7924 }
7925 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7926 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
7927 if (!D->isInvalidDecl() &&
7928 SemaRef.Context.hasSameType(D->getType(), Ty))
7929 return D;
7930 return nullptr;
7931 }))
7932 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7933 if (auto *VD = filterLookupForUDR<ValueDecl *>(
7934 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
7935 if (!D->isInvalidDecl() &&
7936 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
7937 !Ty.isMoreQualifiedThan(D->getType()))
7938 return D;
7939 return nullptr;
7940 })) {
7941 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
7942 /*DetectVirtual=*/false);
7943 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
7944 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
7945 VD->getType().getUnqualifiedType()))) {
7946 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
7947 /*DiagID=*/0) !=
7948 Sema::AR_inaccessible) {
7949 SemaRef.BuildBasePathArray(Paths, BasePath);
7950 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
7951 }
7952 }
7953 }
7954 }
7955 if (ReductionIdScopeSpec.isSet()) {
7956 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
7957 return ExprError();
7958 }
7959 return ExprEmpty();
7960}
7961
Alexey Bataevc5e02582014-06-16 07:08:35 +00007962OMPClause *Sema::ActOnOpenMPReductionClause(
7963 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
7964 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00007965 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
7966 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00007967 auto DN = ReductionId.getName();
7968 auto OOK = DN.getCXXOverloadedOperator();
7969 BinaryOperatorKind BOK = BO_Comma;
7970
7971 // OpenMP [2.14.3.6, reduction clause]
7972 // C
7973 // reduction-identifier is either an identifier or one of the following
7974 // operators: +, -, *, &, |, ^, && and ||
7975 // C++
7976 // reduction-identifier is either an id-expression or one of the following
7977 // operators: +, -, *, &, |, ^, && and ||
7978 // FIXME: Only 'min' and 'max' identifiers are supported for now.
7979 switch (OOK) {
7980 case OO_Plus:
7981 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007982 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007983 break;
7984 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007985 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007986 break;
7987 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007988 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007989 break;
7990 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007991 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007992 break;
7993 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007994 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007995 break;
7996 case OO_AmpAmp:
7997 BOK = BO_LAnd;
7998 break;
7999 case OO_PipePipe:
8000 BOK = BO_LOr;
8001 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008002 case OO_New:
8003 case OO_Delete:
8004 case OO_Array_New:
8005 case OO_Array_Delete:
8006 case OO_Slash:
8007 case OO_Percent:
8008 case OO_Tilde:
8009 case OO_Exclaim:
8010 case OO_Equal:
8011 case OO_Less:
8012 case OO_Greater:
8013 case OO_LessEqual:
8014 case OO_GreaterEqual:
8015 case OO_PlusEqual:
8016 case OO_MinusEqual:
8017 case OO_StarEqual:
8018 case OO_SlashEqual:
8019 case OO_PercentEqual:
8020 case OO_CaretEqual:
8021 case OO_AmpEqual:
8022 case OO_PipeEqual:
8023 case OO_LessLess:
8024 case OO_GreaterGreater:
8025 case OO_LessLessEqual:
8026 case OO_GreaterGreaterEqual:
8027 case OO_EqualEqual:
8028 case OO_ExclaimEqual:
8029 case OO_PlusPlus:
8030 case OO_MinusMinus:
8031 case OO_Comma:
8032 case OO_ArrowStar:
8033 case OO_Arrow:
8034 case OO_Call:
8035 case OO_Subscript:
8036 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008037 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008038 case NUM_OVERLOADED_OPERATORS:
8039 llvm_unreachable("Unexpected reduction identifier");
8040 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008041 if (auto II = DN.getAsIdentifierInfo()) {
8042 if (II->isStr("max"))
8043 BOK = BO_GT;
8044 else if (II->isStr("min"))
8045 BOK = BO_LT;
8046 }
8047 break;
8048 }
8049 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008050 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008051 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008052 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008053
8054 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008055 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008056 SmallVector<Expr *, 8> LHSs;
8057 SmallVector<Expr *, 8> RHSs;
8058 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008059 SmallVector<Decl *, 4> ExprCaptures;
8060 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008061 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8062 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008063 for (auto RefExpr : VarList) {
8064 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008065 // OpenMP [2.1, C/C++]
8066 // A list item is a variable or array section, subject to the restrictions
8067 // specified in Section 2.4 on page 42 and in each of the sections
8068 // describing clauses and directives for which a list appears.
8069 // OpenMP [2.14.3.3, Restrictions, p.1]
8070 // A variable that is part of another variable (as an array or
8071 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008072 if (!FirstIter && IR != ER)
8073 ++IR;
8074 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008075 SourceLocation ELoc;
8076 SourceRange ERange;
8077 Expr *SimpleRefExpr = RefExpr;
8078 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8079 /*AllowArraySection=*/true);
8080 if (Res.second) {
8081 // It will be analyzed later.
8082 Vars.push_back(RefExpr);
8083 Privates.push_back(nullptr);
8084 LHSs.push_back(nullptr);
8085 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008086 // Try to find 'declare reduction' corresponding construct before using
8087 // builtin/overloaded operators.
8088 QualType Type = Context.DependentTy;
8089 CXXCastPath BasePath;
8090 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8091 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8092 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8093 if (CurContext->isDependentContext() &&
8094 (DeclareReductionRef.isUnset() ||
8095 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8096 ReductionOps.push_back(DeclareReductionRef.get());
8097 else
8098 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008099 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008100 ValueDecl *D = Res.first;
8101 if (!D)
8102 continue;
8103
Alexey Bataeva1764212015-09-30 09:22:36 +00008104 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008105 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8106 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8107 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008108 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008109 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008110 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8111 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8112 Type = ATy->getElementType();
8113 else
8114 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008115 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008116 } else
8117 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8118 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008119
Alexey Bataevc5e02582014-06-16 07:08:35 +00008120 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8121 // A variable that appears in a private clause must not have an incomplete
8122 // type or a reference type.
8123 if (RequireCompleteType(ELoc, Type,
8124 diag::err_omp_reduction_incomplete_type))
8125 continue;
8126 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008127 // A list item that appears in a reduction clause must not be
8128 // const-qualified.
8129 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008130 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008131 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008132 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008133 bool IsDecl = !VD ||
8134 VD->isThisDeclarationADefinition(Context) ==
8135 VarDecl::DeclarationOnly;
8136 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008137 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008138 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008139 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008140 continue;
8141 }
8142 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8143 // If a list-item is a reference type then it must bind to the same object
8144 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008145 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008146 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008147 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008148 DSARefChecker Check(DSAStack);
8149 if (Check.Visit(VDDef->getInit())) {
8150 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8151 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8152 continue;
8153 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008154 }
8155 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008156
Alexey Bataevc5e02582014-06-16 07:08:35 +00008157 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8158 // in a Construct]
8159 // Variables with the predetermined data-sharing attributes may not be
8160 // listed in data-sharing attributes clauses, except for the cases
8161 // listed below. For these exceptions only, listing a predetermined
8162 // variable in a data-sharing attribute clause is allowed and overrides
8163 // the variable's predetermined data-sharing attributes.
8164 // OpenMP [2.14.3.6, Restrictions, p.3]
8165 // Any number of reduction clauses can be specified on the directive,
8166 // but a list item can appear only once in the reduction clauses for that
8167 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008168 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008169 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008170 if (DVar.CKind == OMPC_reduction) {
8171 Diag(ELoc, diag::err_omp_once_referenced)
8172 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008173 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008174 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008175 } else if (DVar.CKind != OMPC_unknown) {
8176 Diag(ELoc, diag::err_omp_wrong_dsa)
8177 << getOpenMPClauseName(DVar.CKind)
8178 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008179 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008180 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008181 }
8182
8183 // OpenMP [2.14.3.6, Restrictions, p.1]
8184 // A list item that appears in a reduction clause of a worksharing
8185 // construct must be shared in the parallel regions to which any of the
8186 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008187 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8188 if (isOpenMPWorksharingDirective(CurrDir) &&
8189 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008190 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008191 if (DVar.CKind != OMPC_shared) {
8192 Diag(ELoc, diag::err_omp_required_access)
8193 << getOpenMPClauseName(OMPC_reduction)
8194 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008195 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008196 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008197 }
8198 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008199
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008200 // Try to find 'declare reduction' corresponding construct before using
8201 // builtin/overloaded operators.
8202 CXXCastPath BasePath;
8203 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8204 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8205 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8206 if (DeclareReductionRef.isInvalid())
8207 continue;
8208 if (CurContext->isDependentContext() &&
8209 (DeclareReductionRef.isUnset() ||
8210 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8211 Vars.push_back(RefExpr);
8212 Privates.push_back(nullptr);
8213 LHSs.push_back(nullptr);
8214 RHSs.push_back(nullptr);
8215 ReductionOps.push_back(DeclareReductionRef.get());
8216 continue;
8217 }
8218 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8219 // Not allowed reduction identifier is found.
8220 Diag(ReductionId.getLocStart(),
8221 diag::err_omp_unknown_reduction_identifier)
8222 << Type << ReductionIdRange;
8223 continue;
8224 }
8225
8226 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8227 // The type of a list item that appears in a reduction clause must be valid
8228 // for the reduction-identifier. For a max or min reduction in C, the type
8229 // of the list item must be an allowed arithmetic data type: char, int,
8230 // float, double, or _Bool, possibly modified with long, short, signed, or
8231 // unsigned. For a max or min reduction in C++, the type of the list item
8232 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8233 // double, or bool, possibly modified with long, short, signed, or unsigned.
8234 if (DeclareReductionRef.isUnset()) {
8235 if ((BOK == BO_GT || BOK == BO_LT) &&
8236 !(Type->isScalarType() ||
8237 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8238 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8239 << getLangOpts().CPlusPlus;
8240 if (!ASE && !OASE) {
8241 bool IsDecl = !VD ||
8242 VD->isThisDeclarationADefinition(Context) ==
8243 VarDecl::DeclarationOnly;
8244 Diag(D->getLocation(),
8245 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8246 << D;
8247 }
8248 continue;
8249 }
8250 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8251 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8252 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8253 if (!ASE && !OASE) {
8254 bool IsDecl = !VD ||
8255 VD->isThisDeclarationADefinition(Context) ==
8256 VarDecl::DeclarationOnly;
8257 Diag(D->getLocation(),
8258 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8259 << D;
8260 }
8261 continue;
8262 }
8263 }
8264
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008265 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008266 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008267 D->hasAttrs() ? &D->getAttrs() : nullptr);
8268 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8269 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008270 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008271 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008272 (!ASE &&
8273 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008274 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008275 // Create pseudo array type for private copy. The size for this array will
8276 // be generated during codegen.
8277 // For array subscripts or single variables Private Ty is the same as Type
8278 // (type of the variable or single array element).
8279 PrivateTy = Context.getVariableArrayType(
8280 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8281 Context.getSizeType(), VK_RValue),
8282 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008283 } else if (!ASE && !OASE &&
8284 Context.getAsArrayType(D->getType().getNonReferenceType()))
8285 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008286 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008287 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8288 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008289 // Add initializer for private variable.
8290 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008291 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8292 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8293 if (DeclareReductionRef.isUsable()) {
8294 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8295 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8296 if (DRD->getInitializer()) {
8297 Init = DRDRef;
8298 RHSVD->setInit(DRDRef);
8299 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008300 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008301 } else {
8302 switch (BOK) {
8303 case BO_Add:
8304 case BO_Xor:
8305 case BO_Or:
8306 case BO_LOr:
8307 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8308 if (Type->isScalarType() || Type->isAnyComplexType())
8309 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8310 break;
8311 case BO_Mul:
8312 case BO_LAnd:
8313 if (Type->isScalarType() || Type->isAnyComplexType()) {
8314 // '*' and '&&' reduction ops - initializer is '1'.
8315 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008316 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008317 break;
8318 case BO_And: {
8319 // '&' reduction op - initializer is '~0'.
8320 QualType OrigType = Type;
8321 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8322 Type = ComplexTy->getElementType();
8323 if (Type->isRealFloatingType()) {
8324 llvm::APFloat InitValue =
8325 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8326 /*isIEEE=*/true);
8327 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8328 Type, ELoc);
8329 } else if (Type->isScalarType()) {
8330 auto Size = Context.getTypeSize(Type);
8331 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8332 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8333 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8334 }
8335 if (Init && OrigType->isAnyComplexType()) {
8336 // Init = 0xFFFF + 0xFFFFi;
8337 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8338 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8339 }
8340 Type = OrigType;
8341 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008342 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008343 case BO_LT:
8344 case BO_GT: {
8345 // 'min' reduction op - initializer is 'Largest representable number in
8346 // the reduction list item type'.
8347 // 'max' reduction op - initializer is 'Least representable number in
8348 // the reduction list item type'.
8349 if (Type->isIntegerType() || Type->isPointerType()) {
8350 bool IsSigned = Type->hasSignedIntegerRepresentation();
8351 auto Size = Context.getTypeSize(Type);
8352 QualType IntTy =
8353 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8354 llvm::APInt InitValue =
8355 (BOK != BO_LT)
8356 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8357 : llvm::APInt::getMinValue(Size)
8358 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8359 : llvm::APInt::getMaxValue(Size);
8360 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8361 if (Type->isPointerType()) {
8362 // Cast to pointer type.
8363 auto CastExpr = BuildCStyleCastExpr(
8364 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8365 SourceLocation(), Init);
8366 if (CastExpr.isInvalid())
8367 continue;
8368 Init = CastExpr.get();
8369 }
8370 } else if (Type->isRealFloatingType()) {
8371 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8372 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8373 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8374 Type, ELoc);
8375 }
8376 break;
8377 }
8378 case BO_PtrMemD:
8379 case BO_PtrMemI:
8380 case BO_MulAssign:
8381 case BO_Div:
8382 case BO_Rem:
8383 case BO_Sub:
8384 case BO_Shl:
8385 case BO_Shr:
8386 case BO_LE:
8387 case BO_GE:
8388 case BO_EQ:
8389 case BO_NE:
8390 case BO_AndAssign:
8391 case BO_XorAssign:
8392 case BO_OrAssign:
8393 case BO_Assign:
8394 case BO_AddAssign:
8395 case BO_SubAssign:
8396 case BO_DivAssign:
8397 case BO_RemAssign:
8398 case BO_ShlAssign:
8399 case BO_ShrAssign:
8400 case BO_Comma:
8401 llvm_unreachable("Unexpected reduction operation");
8402 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008403 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008404 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008405 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8406 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008407 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008408 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008409 if (RHSVD->isInvalidDecl())
8410 continue;
8411 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008412 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8413 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008414 bool IsDecl =
8415 !VD ||
8416 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8417 Diag(D->getLocation(),
8418 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8419 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008420 continue;
8421 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008422 // Store initializer for single element in private copy. Will be used during
8423 // codegen.
8424 PrivateVD->setInit(RHSVD->getInit());
8425 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008426 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008427 ExprResult ReductionOp;
8428 if (DeclareReductionRef.isUsable()) {
8429 QualType RedTy = DeclareReductionRef.get()->getType();
8430 QualType PtrRedTy = Context.getPointerType(RedTy);
8431 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8432 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8433 if (!BasePath.empty()) {
8434 LHS = DefaultLvalueConversion(LHS.get());
8435 RHS = DefaultLvalueConversion(RHS.get());
8436 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8437 CK_UncheckedDerivedToBase, LHS.get(),
8438 &BasePath, LHS.get()->getValueKind());
8439 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8440 CK_UncheckedDerivedToBase, RHS.get(),
8441 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008442 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008443 FunctionProtoType::ExtProtoInfo EPI;
8444 QualType Params[] = {PtrRedTy, PtrRedTy};
8445 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8446 auto *OVE = new (Context) OpaqueValueExpr(
8447 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8448 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8449 Expr *Args[] = {LHS.get(), RHS.get()};
8450 ReductionOp = new (Context)
8451 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8452 } else {
8453 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8454 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8455 if (ReductionOp.isUsable()) {
8456 if (BOK != BO_LT && BOK != BO_GT) {
8457 ReductionOp =
8458 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8459 BO_Assign, LHSDRE, ReductionOp.get());
8460 } else {
8461 auto *ConditionalOp = new (Context) ConditionalOperator(
8462 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8463 RHSDRE, Type, VK_LValue, OK_Ordinary);
8464 ReductionOp =
8465 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8466 BO_Assign, LHSDRE, ConditionalOp);
8467 }
8468 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8469 }
8470 if (ReductionOp.isInvalid())
8471 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008472 }
8473
Alexey Bataev60da77e2016-02-29 05:54:20 +00008474 DeclRefExpr *Ref = nullptr;
8475 Expr *VarsExpr = RefExpr->IgnoreParens();
8476 if (!VD) {
8477 if (ASE || OASE) {
8478 TransformExprToCaptures RebuildToCapture(*this, D);
8479 VarsExpr =
8480 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8481 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008482 } else {
8483 VarsExpr = Ref =
8484 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8485 if (!IsOpenMPCapturedDecl(D)) {
8486 ExprCaptures.push_back(Ref->getDecl());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008487 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataev61205072016-03-02 04:57:40 +00008488 ExprResult RefRes = DefaultLvalueConversion(Ref);
8489 if (!RefRes.isUsable())
8490 continue;
8491 ExprResult PostUpdateRes =
8492 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8493 SimpleRefExpr, RefRes.get());
8494 if (!PostUpdateRes.isUsable())
8495 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008496 ExprPostUpdates.push_back(
8497 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008498 }
8499 }
8500 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008501 }
8502 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8503 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008504 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008505 LHSs.push_back(LHSDRE);
8506 RHSs.push_back(RHSDRE);
8507 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008508 }
8509
8510 if (Vars.empty())
8511 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008512 Stmt *PreInit = nullptr;
8513 if (!ExprCaptures.empty()) {
8514 PreInit = new (Context)
8515 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
8516 ExprCaptures.size()),
8517 SourceLocation(), SourceLocation());
8518 }
8519 Expr *PostUpdate = nullptr;
8520 if (!ExprPostUpdates.empty()) {
8521 for (auto *E : ExprPostUpdates) {
8522 ExprResult PostUpdateRes =
8523 PostUpdate
8524 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
8525 : E;
8526 PostUpdate = PostUpdateRes.get();
8527 }
8528 }
8529
Alexey Bataevc5e02582014-06-16 07:08:35 +00008530 return OMPReductionClause::Create(
8531 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008532 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev61205072016-03-02 04:57:40 +00008533 LHSs, RHSs, ReductionOps, PreInit, PostUpdate);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008534}
8535
Alexey Bataev182227b2015-08-20 10:54:39 +00008536OMPClause *Sema::ActOnOpenMPLinearClause(
8537 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8538 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8539 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008540 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008541 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008542 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008543 SmallVector<Decl *, 4> ExprCaptures;
8544 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataev182227b2015-08-20 10:54:39 +00008545 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8546 LinKind == OMPC_LINEAR_unknown) {
8547 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8548 LinKind = OMPC_LINEAR_val;
8549 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008550 for (auto &RefExpr : VarList) {
8551 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008552 SourceLocation ELoc;
8553 SourceRange ERange;
8554 Expr *SimpleRefExpr = RefExpr;
8555 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8556 /*AllowArraySection=*/false);
8557 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008558 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008559 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008560 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008561 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008562 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008563 ValueDecl *D = Res.first;
8564 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008565 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008566
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008567 QualType Type = D->getType();
8568 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008569
8570 // OpenMP [2.14.3.7, linear clause]
8571 // A list-item cannot appear in more than one linear clause.
8572 // A list-item that appears in a linear clause cannot appear in any
8573 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008574 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008575 if (DVar.RefExpr) {
8576 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8577 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008578 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008579 continue;
8580 }
8581
8582 // A variable must not have an incomplete type or a reference type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008583 if (RequireCompleteType(ELoc, Type,
8584 diag::err_omp_linear_incomplete_type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008585 continue;
Alexey Bataev1185e192015-08-20 12:15:57 +00008586 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008587 !Type->isReferenceType()) {
Alexey Bataev1185e192015-08-20 12:15:57 +00008588 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008589 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
Alexey Bataev1185e192015-08-20 12:15:57 +00008590 continue;
8591 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008592 Type = Type.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008593
8594 // A list item must not be const-qualified.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008595 if (Type.isConstant(Context)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008596 Diag(ELoc, diag::err_omp_const_variable)
8597 << getOpenMPClauseName(OMPC_linear);
8598 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008599 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008600 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008601 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008602 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008603 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008604 continue;
8605 }
8606
8607 // A list item must be of integral or pointer type.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008608 Type = Type.getUnqualifiedType().getCanonicalType();
8609 const auto *Ty = Type.getTypePtrOrNull();
Alexander Musman8dba6642014-04-22 13:09:42 +00008610 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8611 !Ty->isPointerType())) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008612 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
Alexander Musman8dba6642014-04-22 13:09:42 +00008613 bool IsDecl =
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008614 !VD ||
Alexander Musman8dba6642014-04-22 13:09:42 +00008615 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008616 Diag(D->getLocation(),
Alexander Musman8dba6642014-04-22 13:09:42 +00008617 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008618 << D;
Alexander Musman8dba6642014-04-22 13:09:42 +00008619 continue;
8620 }
8621
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008622 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008623 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8624 D->hasAttrs() ? &D->getAttrs() : nullptr);
8625 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008626 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008627 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008628 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008629 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008630 if (!VD) {
8631 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8632 if (!IsOpenMPCapturedDecl(D)) {
8633 ExprCaptures.push_back(Ref->getDecl());
8634 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8635 ExprResult RefRes = DefaultLvalueConversion(Ref);
8636 if (!RefRes.isUsable())
8637 continue;
8638 ExprResult PostUpdateRes =
8639 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8640 SimpleRefExpr, RefRes.get());
8641 if (!PostUpdateRes.isUsable())
8642 continue;
8643 ExprPostUpdates.push_back(
8644 IgnoredValueConversions(PostUpdateRes.get()).get());
8645 }
8646 }
8647 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008648 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008649 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008650 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008651 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008652 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008653 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8654 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8655
8656 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8657 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008658 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008659 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008660 }
8661
8662 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008663 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008664
8665 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008666 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008667 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8668 !Step->isInstantiationDependent() &&
8669 !Step->containsUnexpandedParameterPack()) {
8670 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008671 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008672 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008673 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008674 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008675
Alexander Musman3276a272015-03-21 10:12:56 +00008676 // Build var to save the step value.
8677 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008678 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008679 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008680 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008681 ExprResult CalcStep =
8682 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008683 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008684
Alexander Musman8dba6642014-04-22 13:09:42 +00008685 // Warn about zero linear step (it would be probably better specified as
8686 // making corresponding variables 'const').
8687 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008688 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8689 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008690 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8691 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008692 if (!IsConstant && CalcStep.isUsable()) {
8693 // Calculate the step beforehand instead of doing this on each iteration.
8694 // (This is not used if the number of iterations may be kfold-ed).
8695 CalcStepExpr = CalcStep.get();
8696 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008697 }
8698
Alexey Bataev78849fb2016-03-09 09:49:00 +00008699 Stmt *PreInit = nullptr;
8700 if (!ExprCaptures.empty()) {
8701 PreInit = new (Context)
8702 DeclStmt(DeclGroupRef::Create(Context, ExprCaptures.begin(),
8703 ExprCaptures.size()),
8704 SourceLocation(), SourceLocation());
8705 }
8706 Expr *PostUpdate = nullptr;
8707 if (!ExprPostUpdates.empty()) {
8708 for (auto *E : ExprPostUpdates) {
8709 ExprResult PostUpdateRes =
8710 PostUpdate
8711 ? CreateBuiltinBinOp(SourceLocation(), BO_Comma, PostUpdate, E)
8712 : E;
8713 PostUpdate = PostUpdateRes.get();
8714 }
8715 }
8716
Alexey Bataev182227b2015-08-20 10:54:39 +00008717 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8718 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev78849fb2016-03-09 09:49:00 +00008719 StepExpr, CalcStepExpr, PreInit, PostUpdate);
Alexander Musman3276a272015-03-21 10:12:56 +00008720}
8721
8722static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8723 Expr *NumIterations, Sema &SemaRef,
8724 Scope *S) {
8725 // Walk the vars and build update/final expressions for the CodeGen.
8726 SmallVector<Expr *, 8> Updates;
8727 SmallVector<Expr *, 8> Finals;
8728 Expr *Step = Clause.getStep();
8729 Expr *CalcStep = Clause.getCalcStep();
8730 // OpenMP [2.14.3.7, linear clause]
8731 // If linear-step is not specified it is assumed to be 1.
8732 if (Step == nullptr)
8733 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
8734 else if (CalcStep)
8735 Step = cast<BinaryOperator>(CalcStep)->getLHS();
8736 bool HasErrors = false;
8737 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008738 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008739 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008740 for (auto &RefExpr : Clause.varlists()) {
8741 Expr *InitExpr = *CurInit;
8742
8743 // Build privatized reference to the current linear var.
8744 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008745 Expr *CapturedRef;
8746 if (LinKind == OMPC_LINEAR_uval)
8747 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8748 else
8749 CapturedRef =
8750 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8751 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8752 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008753
8754 // Build update: Var = InitExpr + IV * Step
8755 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008756 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00008757 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008758 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
8759 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008760
8761 // Build final: Var = InitExpr + NumIterations * Step
8762 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008763 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00008764 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008765 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
8766 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008767 if (!Update.isUsable() || !Final.isUsable()) {
8768 Updates.push_back(nullptr);
8769 Finals.push_back(nullptr);
8770 HasErrors = true;
8771 } else {
8772 Updates.push_back(Update.get());
8773 Finals.push_back(Final.get());
8774 }
Richard Trieucc3949d2016-02-18 22:34:54 +00008775 ++CurInit;
8776 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00008777 }
8778 Clause.setUpdates(Updates);
8779 Clause.setFinals(Finals);
8780 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00008781}
8782
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008783OMPClause *Sema::ActOnOpenMPAlignedClause(
8784 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
8785 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8786
8787 SmallVector<Expr *, 8> Vars;
8788 for (auto &RefExpr : VarList) {
8789 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
8790 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
8791 // It will be analyzed later.
8792 Vars.push_back(RefExpr);
8793 continue;
8794 }
8795
8796 SourceLocation ELoc = RefExpr->getExprLoc();
8797 // OpenMP [2.1, C/C++]
8798 // A list item is a variable name.
8799 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
8800 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008801 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8802 << 0 << RefExpr->getSourceRange();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008803 continue;
8804 }
8805
8806 VarDecl *VD = cast<VarDecl>(DE->getDecl());
8807
8808 // OpenMP [2.8.1, simd construct, Restrictions]
8809 // The type of list items appearing in the aligned clause must be
8810 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008811 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008812 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008813 const Type *Ty = QType.getTypePtrOrNull();
8814 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
8815 !Ty->isPointerType())) {
8816 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
8817 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
8818 bool IsDecl =
8819 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8820 Diag(VD->getLocation(),
8821 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8822 << VD;
8823 continue;
8824 }
8825
8826 // OpenMP [2.8.1, simd construct, Restrictions]
8827 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008828 if (Expr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008829 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
8830 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
8831 << getOpenMPClauseName(OMPC_aligned);
8832 continue;
8833 }
8834
8835 Vars.push_back(DE);
8836 }
8837
8838 // OpenMP [2.8.1, simd construct, Description]
8839 // The parameter of the aligned clause, alignment, must be a constant
8840 // positive integer expression.
8841 // If no optional parameter is specified, implementation-defined default
8842 // alignments for SIMD instructions on the target platforms are assumed.
8843 if (Alignment != nullptr) {
8844 ExprResult AlignResult =
8845 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
8846 if (AlignResult.isInvalid())
8847 return nullptr;
8848 Alignment = AlignResult.get();
8849 }
8850 if (Vars.empty())
8851 return nullptr;
8852
8853 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
8854 EndLoc, Vars, Alignment);
8855}
8856
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008857OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
8858 SourceLocation StartLoc,
8859 SourceLocation LParenLoc,
8860 SourceLocation EndLoc) {
8861 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008862 SmallVector<Expr *, 8> SrcExprs;
8863 SmallVector<Expr *, 8> DstExprs;
8864 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00008865 for (auto &RefExpr : VarList) {
8866 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
8867 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008868 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008869 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008870 SrcExprs.push_back(nullptr);
8871 DstExprs.push_back(nullptr);
8872 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008873 continue;
8874 }
8875
Alexey Bataeved09d242014-05-28 05:53:51 +00008876 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008877 // OpenMP [2.1, C/C++]
8878 // A list item is a variable name.
8879 // OpenMP [2.14.4.1, Restrictions, p.1]
8880 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00008881 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008882 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00008883 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
8884 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008885 continue;
8886 }
8887
8888 Decl *D = DE->getDecl();
8889 VarDecl *VD = cast<VarDecl>(D);
8890
8891 QualType Type = VD->getType();
8892 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
8893 // It will be analyzed later.
8894 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008895 SrcExprs.push_back(nullptr);
8896 DstExprs.push_back(nullptr);
8897 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008898 continue;
8899 }
8900
8901 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
8902 // A list item that appears in a copyin clause must be threadprivate.
8903 if (!DSAStack->isThreadPrivate(VD)) {
8904 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00008905 << getOpenMPClauseName(OMPC_copyin)
8906 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008907 continue;
8908 }
8909
8910 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
8911 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00008912 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008913 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008914 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008915 auto *SrcVD =
8916 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
8917 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00008918 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008919 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
8920 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008921 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
8922 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008923 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00008924 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008925 // For arrays generate assignment operation for single element and replace
8926 // it by the original array element in CodeGen.
8927 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
8928 PseudoDstExpr, PseudoSrcExpr);
8929 if (AssignmentOp.isInvalid())
8930 continue;
8931 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
8932 /*DiscardedValue=*/true);
8933 if (AssignmentOp.isInvalid())
8934 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008935
8936 DSAStack->addDSA(VD, DE, OMPC_copyin);
8937 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008938 SrcExprs.push_back(PseudoSrcExpr);
8939 DstExprs.push_back(PseudoDstExpr);
8940 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008941 }
8942
Alexey Bataeved09d242014-05-28 05:53:51 +00008943 if (Vars.empty())
8944 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008945
Alexey Bataevf56f98c2015-04-16 05:39:01 +00008946 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8947 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008948}
8949
Alexey Bataevbae9a792014-06-27 10:37:06 +00008950OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
8951 SourceLocation StartLoc,
8952 SourceLocation LParenLoc,
8953 SourceLocation EndLoc) {
8954 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00008955 SmallVector<Expr *, 8> SrcExprs;
8956 SmallVector<Expr *, 8> DstExprs;
8957 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008958 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008959 assert(RefExpr && "NULL expr in OpenMP linear clause.");
8960 SourceLocation ELoc;
8961 SourceRange ERange;
8962 Expr *SimpleRefExpr = RefExpr;
8963 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8964 /*AllowArraySection=*/false);
8965 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008966 // It will be analyzed later.
8967 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008968 SrcExprs.push_back(nullptr);
8969 DstExprs.push_back(nullptr);
8970 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008971 }
Alexey Bataeve122da12016-03-17 10:50:17 +00008972 ValueDecl *D = Res.first;
8973 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00008974 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00008975
Alexey Bataeve122da12016-03-17 10:50:17 +00008976 QualType Type = D->getType();
8977 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008978
8979 // OpenMP [2.14.4.2, Restrictions, p.2]
8980 // A list item that appears in a copyprivate clause may not appear in a
8981 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00008982 if (!VD || !DSAStack->isThreadPrivate(VD)) {
8983 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00008984 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
8985 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00008986 Diag(ELoc, diag::err_omp_wrong_dsa)
8987 << getOpenMPClauseName(DVar.CKind)
8988 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00008989 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008990 continue;
8991 }
8992
8993 // OpenMP [2.11.4.2, Restrictions, p.1]
8994 // All list items that appear in a copyprivate clause must be either
8995 // threadprivate or private in the enclosing context.
8996 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00008997 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00008998 if (DVar.CKind == OMPC_shared) {
8999 Diag(ELoc, diag::err_omp_required_access)
9000 << getOpenMPClauseName(OMPC_copyprivate)
9001 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009002 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009003 continue;
9004 }
9005 }
9006 }
9007
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009008 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009009 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009010 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009011 << getOpenMPClauseName(OMPC_copyprivate) << Type
9012 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009013 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009014 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009015 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009016 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009017 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009018 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009019 continue;
9020 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009021
Alexey Bataevbae9a792014-06-27 10:37:06 +00009022 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9023 // A variable of class type (or array thereof) that appears in a
9024 // copyin clause requires an accessible, unambiguous copy assignment
9025 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009026 Type = Context.getBaseElementType(Type.getNonReferenceType())
9027 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009028 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009029 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9030 D->hasAttrs() ? &D->getAttrs() : nullptr);
9031 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009032 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009033 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9034 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009035 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009036 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9037 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009038 PseudoDstExpr, PseudoSrcExpr);
9039 if (AssignmentOp.isInvalid())
9040 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009041 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009042 /*DiscardedValue=*/true);
9043 if (AssignmentOp.isInvalid())
9044 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009045
9046 // No need to mark vars as copyprivate, they are already threadprivate or
9047 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009048 assert(VD || IsOpenMPCapturedDecl(D));
9049 Vars.push_back(
9050 VD ? RefExpr->IgnoreParens()
9051 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009052 SrcExprs.push_back(PseudoSrcExpr);
9053 DstExprs.push_back(PseudoDstExpr);
9054 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009055 }
9056
9057 if (Vars.empty())
9058 return nullptr;
9059
Alexey Bataeva63048e2015-03-23 06:18:07 +00009060 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9061 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009062}
9063
Alexey Bataev6125da92014-07-21 11:26:11 +00009064OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9065 SourceLocation StartLoc,
9066 SourceLocation LParenLoc,
9067 SourceLocation EndLoc) {
9068 if (VarList.empty())
9069 return nullptr;
9070
9071 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9072}
Alexey Bataevdea47612014-07-23 07:46:59 +00009073
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009074OMPClause *
9075Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9076 SourceLocation DepLoc, SourceLocation ColonLoc,
9077 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9078 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009079 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009080 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009081 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009082 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009083 return nullptr;
9084 }
9085 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009086 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9087 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009088 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009089 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009090 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9091 /*Last=*/OMPC_DEPEND_unknown, Except)
9092 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009093 return nullptr;
9094 }
9095 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009096 llvm::APSInt DepCounter(/*BitWidth=*/32);
9097 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9098 if (DepKind == OMPC_DEPEND_sink) {
9099 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9100 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9101 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009102 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009103 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009104 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9105 DSAStack->getParentOrderedRegionParam()) {
9106 for (auto &RefExpr : VarList) {
9107 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9108 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9109 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9110 // It will be analyzed later.
9111 Vars.push_back(RefExpr);
9112 continue;
9113 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009114
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009115 SourceLocation ELoc = RefExpr->getExprLoc();
9116 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9117 if (DepKind == OMPC_DEPEND_sink) {
9118 if (DepCounter >= TotalDepCount) {
9119 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9120 continue;
9121 }
9122 ++DepCounter;
9123 // OpenMP [2.13.9, Summary]
9124 // depend(dependence-type : vec), where dependence-type is:
9125 // 'sink' and where vec is the iteration vector, which has the form:
9126 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9127 // where n is the value specified by the ordered clause in the loop
9128 // directive, xi denotes the loop iteration variable of the i-th nested
9129 // loop associated with the loop directive, and di is a constant
9130 // non-negative integer.
9131 SimpleExpr = SimpleExpr->IgnoreImplicit();
9132 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9133 if (!DE) {
9134 OverloadedOperatorKind OOK = OO_None;
9135 SourceLocation OOLoc;
9136 Expr *LHS, *RHS;
9137 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9138 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9139 OOLoc = BO->getOperatorLoc();
9140 LHS = BO->getLHS()->IgnoreParenImpCasts();
9141 RHS = BO->getRHS()->IgnoreParenImpCasts();
9142 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9143 OOK = OCE->getOperator();
9144 OOLoc = OCE->getOperatorLoc();
9145 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9146 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9147 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9148 OOK = MCE->getMethodDecl()
9149 ->getNameInfo()
9150 .getName()
9151 .getCXXOverloadedOperator();
9152 OOLoc = MCE->getCallee()->getExprLoc();
9153 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9154 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9155 } else {
9156 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9157 continue;
9158 }
9159 DE = dyn_cast<DeclRefExpr>(LHS);
9160 if (!DE) {
9161 Diag(LHS->getExprLoc(),
9162 diag::err_omp_depend_sink_expected_loop_iteration)
9163 << DSAStack->getParentLoopControlVariable(
9164 DepCounter.getZExtValue());
9165 continue;
9166 }
9167 if (OOK != OO_Plus && OOK != OO_Minus) {
9168 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9169 continue;
9170 }
9171 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9172 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9173 if (Res.isInvalid())
9174 continue;
9175 }
9176 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9177 if (!CurContext->isDependentContext() &&
9178 DSAStack->getParentOrderedRegionParam() &&
9179 (!VD || DepCounter != DSAStack->isParentLoopControlVariable(VD))) {
9180 Diag(DE->getExprLoc(),
9181 diag::err_omp_depend_sink_expected_loop_iteration)
9182 << DSAStack->getParentLoopControlVariable(
9183 DepCounter.getZExtValue());
9184 continue;
9185 }
9186 } else {
9187 // OpenMP [2.11.1.1, Restrictions, p.3]
9188 // A variable that is part of another variable (such as a field of a
9189 // structure) but is not an array element or an array section cannot
9190 // appear in a depend clause.
9191 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9192 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9193 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9194 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9195 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009196 (ASE &&
9197 !ASE->getBase()
9198 ->getType()
9199 .getNonReferenceType()
9200 ->isPointerType() &&
9201 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009202 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9203 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009204 continue;
9205 }
9206 }
9207
9208 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9209 }
9210
9211 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9212 TotalDepCount > VarList.size() &&
9213 DSAStack->getParentOrderedRegionParam()) {
9214 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9215 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9216 }
9217 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9218 Vars.empty())
9219 return nullptr;
9220 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009221
9222 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9223 DepLoc, ColonLoc, Vars);
9224}
Michael Wonge710d542015-08-07 16:16:36 +00009225
9226OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9227 SourceLocation LParenLoc,
9228 SourceLocation EndLoc) {
9229 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009230
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009231 // OpenMP [2.9.1, Restrictions]
9232 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009233 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9234 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009235 return nullptr;
9236
Michael Wonge710d542015-08-07 16:16:36 +00009237 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9238}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009239
9240static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9241 DSAStackTy *Stack, CXXRecordDecl *RD) {
9242 if (!RD || RD->isInvalidDecl())
9243 return true;
9244
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009245 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9246 if (auto *CTD = CTSD->getSpecializedTemplate())
9247 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009248 auto QTy = SemaRef.Context.getRecordType(RD);
9249 if (RD->isDynamicClass()) {
9250 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9251 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9252 return false;
9253 }
9254 auto *DC = RD;
9255 bool IsCorrect = true;
9256 for (auto *I : DC->decls()) {
9257 if (I) {
9258 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9259 if (MD->isStatic()) {
9260 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9261 SemaRef.Diag(MD->getLocation(),
9262 diag::note_omp_static_member_in_target);
9263 IsCorrect = false;
9264 }
9265 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9266 if (VD->isStaticDataMember()) {
9267 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9268 SemaRef.Diag(VD->getLocation(),
9269 diag::note_omp_static_member_in_target);
9270 IsCorrect = false;
9271 }
9272 }
9273 }
9274 }
9275
9276 for (auto &I : RD->bases()) {
9277 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9278 I.getType()->getAsCXXRecordDecl()))
9279 IsCorrect = false;
9280 }
9281 return IsCorrect;
9282}
9283
9284static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9285 DSAStackTy *Stack, QualType QTy) {
9286 NamedDecl *ND;
9287 if (QTy->isIncompleteType(&ND)) {
9288 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9289 return false;
9290 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9291 if (!RD->isInvalidDecl() &&
9292 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9293 return false;
9294 }
9295 return true;
9296}
9297
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009298/// \brief Return true if it can be proven that the provided array expression
9299/// (array section or array subscript) does NOT specify the whole size of the
9300/// array whose base type is \a BaseQTy.
9301static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9302 const Expr *E,
9303 QualType BaseQTy) {
9304 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9305
9306 // If this is an array subscript, it refers to the whole size if the size of
9307 // the dimension is constant and equals 1. Also, an array section assumes the
9308 // format of an array subscript if no colon is used.
9309 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9310 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9311 return ATy->getSize().getSExtValue() != 1;
9312 // Size can't be evaluated statically.
9313 return false;
9314 }
9315
9316 assert(OASE && "Expecting array section if not an array subscript.");
9317 auto *LowerBound = OASE->getLowerBound();
9318 auto *Length = OASE->getLength();
9319
9320 // If there is a lower bound that does not evaluates to zero, we are not
9321 // convering the whole dimension.
9322 if (LowerBound) {
9323 llvm::APSInt ConstLowerBound;
9324 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9325 return false; // Can't get the integer value as a constant.
9326 if (ConstLowerBound.getSExtValue())
9327 return true;
9328 }
9329
9330 // If we don't have a length we covering the whole dimension.
9331 if (!Length)
9332 return false;
9333
9334 // If the base is a pointer, we don't have a way to get the size of the
9335 // pointee.
9336 if (BaseQTy->isPointerType())
9337 return false;
9338
9339 // We can only check if the length is the same as the size of the dimension
9340 // if we have a constant array.
9341 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9342 if (!CATy)
9343 return false;
9344
9345 llvm::APSInt ConstLength;
9346 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9347 return false; // Can't get the integer value as a constant.
9348
9349 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9350}
9351
9352// Return true if it can be proven that the provided array expression (array
9353// section or array subscript) does NOT specify a single element of the array
9354// whose base type is \a BaseQTy.
9355static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9356 const Expr *E,
9357 QualType BaseQTy) {
9358 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9359
9360 // An array subscript always refer to a single element. Also, an array section
9361 // assumes the format of an array subscript if no colon is used.
9362 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9363 return false;
9364
9365 assert(OASE && "Expecting array section if not an array subscript.");
9366 auto *Length = OASE->getLength();
9367
9368 // If we don't have a length we have to check if the array has unitary size
9369 // for this dimension. Also, we should always expect a length if the base type
9370 // is pointer.
9371 if (!Length) {
9372 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9373 return ATy->getSize().getSExtValue() != 1;
9374 // We cannot assume anything.
9375 return false;
9376 }
9377
9378 // Check if the length evaluates to 1.
9379 llvm::APSInt ConstLength;
9380 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9381 return false; // Can't get the integer value as a constant.
9382
9383 return ConstLength.getSExtValue() != 1;
9384}
9385
Samuel Antao5de996e2016-01-22 20:21:36 +00009386// Return the expression of the base of the map clause or null if it cannot
9387// be determined and do all the necessary checks to see if the expression is
9388// valid as a standalone map clause expression.
9389static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9390 SourceLocation ELoc = E->getExprLoc();
9391 SourceRange ERange = E->getSourceRange();
9392
9393 // The base of elements of list in a map clause have to be either:
9394 // - a reference to variable or field.
9395 // - a member expression.
9396 // - an array expression.
9397 //
9398 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9399 // reference to 'r'.
9400 //
9401 // If we have:
9402 //
9403 // struct SS {
9404 // Bla S;
9405 // foo() {
9406 // #pragma omp target map (S.Arr[:12]);
9407 // }
9408 // }
9409 //
9410 // We want to retrieve the member expression 'this->S';
9411
9412 Expr *RelevantExpr = nullptr;
9413
Samuel Antao5de996e2016-01-22 20:21:36 +00009414 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9415 // If a list item is an array section, it must specify contiguous storage.
9416 //
9417 // For this restriction it is sufficient that we make sure only references
9418 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009419 // exist except in the rightmost expression (unless they cover the whole
9420 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009421 //
9422 // r.ArrS[3:5].Arr[6:7]
9423 //
9424 // r.ArrS[3:5].x
9425 //
9426 // but these would be valid:
9427 // r.ArrS[3].Arr[6:7]
9428 //
9429 // r.ArrS[3].x
9430
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009431 bool AllowUnitySizeArraySection = true;
9432 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009433
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009434 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009435 E = E->IgnoreParenImpCasts();
9436
9437 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9438 if (!isa<VarDecl>(CurE->getDecl()))
9439 break;
9440
9441 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009442
9443 // If we got a reference to a declaration, we should not expect any array
9444 // section before that.
9445 AllowUnitySizeArraySection = false;
9446 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009447 continue;
9448 }
9449
9450 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9451 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9452
9453 if (isa<CXXThisExpr>(BaseE))
9454 // We found a base expression: this->Val.
9455 RelevantExpr = CurE;
9456 else
9457 E = BaseE;
9458
9459 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9460 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9461 << CurE->getSourceRange();
9462 break;
9463 }
9464
9465 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9466
9467 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9468 // A bit-field cannot appear in a map clause.
9469 //
9470 if (FD->isBitField()) {
9471 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9472 << CurE->getSourceRange();
9473 break;
9474 }
9475
9476 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9477 // If the type of a list item is a reference to a type T then the type
9478 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009479 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009480
9481 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9482 // A list item cannot be a variable that is a member of a structure with
9483 // a union type.
9484 //
9485 if (auto *RT = CurType->getAs<RecordType>())
9486 if (RT->isUnionType()) {
9487 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9488 << CurE->getSourceRange();
9489 break;
9490 }
9491
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009492 // If we got a member expression, we should not expect any array section
9493 // before that:
9494 //
9495 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9496 // If a list item is an element of a structure, only the rightmost symbol
9497 // of the variable reference can be an array section.
9498 //
9499 AllowUnitySizeArraySection = false;
9500 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009501 continue;
9502 }
9503
9504 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9505 E = CurE->getBase()->IgnoreParenImpCasts();
9506
9507 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9508 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9509 << 0 << CurE->getSourceRange();
9510 break;
9511 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009512
9513 // If we got an array subscript that express the whole dimension we
9514 // can have any array expressions before. If it only expressing part of
9515 // the dimension, we can only have unitary-size array expressions.
9516 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9517 E->getType()))
9518 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009519 continue;
9520 }
9521
9522 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009523 E = CurE->getBase()->IgnoreParenImpCasts();
9524
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009525 auto CurType =
9526 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9527
Samuel Antao5de996e2016-01-22 20:21:36 +00009528 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9529 // If the type of a list item is a reference to a type T then the type
9530 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009531 if (CurType->isReferenceType())
9532 CurType = CurType->getPointeeType();
9533
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009534 bool IsPointer = CurType->isAnyPointerType();
9535
9536 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009537 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9538 << 0 << CurE->getSourceRange();
9539 break;
9540 }
9541
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009542 bool NotWhole =
9543 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9544 bool NotUnity =
9545 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9546
9547 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9548 // Any array section is currently allowed.
9549 //
9550 // If this array section refers to the whole dimension we can still
9551 // accept other array sections before this one, except if the base is a
9552 // pointer. Otherwise, only unitary sections are accepted.
9553 if (NotWhole || IsPointer)
9554 AllowWholeSizeArraySection = false;
9555 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9556 (AllowWholeSizeArraySection && NotWhole)) {
9557 // A unity or whole array section is not allowed and that is not
9558 // compatible with the properties of the current array section.
9559 SemaRef.Diag(
9560 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9561 << CurE->getSourceRange();
9562 break;
9563 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009564 continue;
9565 }
9566
9567 // If nothing else worked, this is not a valid map clause expression.
9568 SemaRef.Diag(ELoc,
9569 diag::err_omp_expected_named_var_member_or_array_expression)
9570 << ERange;
9571 break;
9572 }
9573
9574 return RelevantExpr;
9575}
9576
9577// Return true if expression E associated with value VD has conflicts with other
9578// map information.
9579static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9580 Expr *E, bool CurrentRegionOnly) {
9581 assert(VD && E);
9582
9583 // Types used to organize the components of a valid map clause.
9584 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9585 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9586
9587 // Helper to extract the components in the map clause expression E and store
9588 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9589 // it has already passed the single clause checks.
9590 auto ExtractMapExpressionComponents = [](Expr *TE,
9591 MapExpressionComponents &MEC) {
9592 while (true) {
9593 TE = TE->IgnoreParenImpCasts();
9594
9595 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9596 MEC.push_back(
9597 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9598 break;
9599 }
9600
9601 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9602 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9603
9604 MEC.push_back(MapExpressionComponent(
9605 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9606 if (isa<CXXThisExpr>(BaseE))
9607 break;
9608
9609 TE = BaseE;
9610 continue;
9611 }
9612
9613 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9614 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9615 TE = CurE->getBase()->IgnoreParenImpCasts();
9616 continue;
9617 }
9618
9619 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9620 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9621 TE = CurE->getBase()->IgnoreParenImpCasts();
9622 continue;
9623 }
9624
9625 llvm_unreachable(
9626 "Expecting only valid map clause expressions at this point!");
9627 }
9628 };
9629
9630 SourceLocation ELoc = E->getExprLoc();
9631 SourceRange ERange = E->getSourceRange();
9632
9633 // In order to easily check the conflicts we need to match each component of
9634 // the expression under test with the components of the expressions that are
9635 // already in the stack.
9636
9637 MapExpressionComponents CurComponents;
9638 ExtractMapExpressionComponents(E, CurComponents);
9639
9640 assert(!CurComponents.empty() && "Map clause expression with no components!");
9641 assert(CurComponents.back().second == VD &&
9642 "Map clause expression with unexpected base!");
9643
9644 // Variables to help detecting enclosing problems in data environment nests.
9645 bool IsEnclosedByDataEnvironmentExpr = false;
9646 Expr *EnclosingExpr = nullptr;
9647
9648 bool FoundError =
9649 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9650 MapExpressionComponents StackComponents;
9651 ExtractMapExpressionComponents(RE, StackComponents);
9652 assert(!StackComponents.empty() &&
9653 "Map clause expression with no components!");
9654 assert(StackComponents.back().second == VD &&
9655 "Map clause expression with unexpected base!");
9656
9657 // Expressions must start from the same base. Here we detect at which
9658 // point both expressions diverge from each other and see if we can
9659 // detect if the memory referred to both expressions is contiguous and
9660 // do not overlap.
9661 auto CI = CurComponents.rbegin();
9662 auto CE = CurComponents.rend();
9663 auto SI = StackComponents.rbegin();
9664 auto SE = StackComponents.rend();
9665 for (; CI != CE && SI != SE; ++CI, ++SI) {
9666
9667 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9668 // At most one list item can be an array item derived from a given
9669 // variable in map clauses of the same construct.
9670 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9671 isa<OMPArraySectionExpr>(CI->first)) &&
9672 (isa<ArraySubscriptExpr>(SI->first) ||
9673 isa<OMPArraySectionExpr>(SI->first))) {
9674 SemaRef.Diag(CI->first->getExprLoc(),
9675 diag::err_omp_multiple_array_items_in_map_clause)
9676 << CI->first->getSourceRange();
9677 ;
9678 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9679 << SI->first->getSourceRange();
9680 return true;
9681 }
9682
9683 // Do both expressions have the same kind?
9684 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9685 break;
9686
9687 // Are we dealing with different variables/fields?
9688 if (CI->second != SI->second)
9689 break;
9690 }
9691
9692 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9693 // List items of map clauses in the same construct must not share
9694 // original storage.
9695 //
9696 // If the expressions are exactly the same or one is a subset of the
9697 // other, it means they are sharing storage.
9698 if (CI == CE && SI == SE) {
9699 if (CurrentRegionOnly) {
9700 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9701 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9702 << RE->getSourceRange();
9703 return true;
9704 } else {
9705 // If we find the same expression in the enclosing data environment,
9706 // that is legal.
9707 IsEnclosedByDataEnvironmentExpr = true;
9708 return false;
9709 }
9710 }
9711
9712 QualType DerivedType = std::prev(CI)->first->getType();
9713 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9714
9715 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9716 // If the type of a list item is a reference to a type T then the type
9717 // will be considered to be T for all purposes of this clause.
9718 if (DerivedType->isReferenceType())
9719 DerivedType = DerivedType->getPointeeType();
9720
9721 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9722 // A variable for which the type is pointer and an array section
9723 // derived from that variable must not appear as list items of map
9724 // clauses of the same construct.
9725 //
9726 // Also, cover one of the cases in:
9727 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9728 // If any part of the original storage of a list item has corresponding
9729 // storage in the device data environment, all of the original storage
9730 // must have corresponding storage in the device data environment.
9731 //
9732 if (DerivedType->isAnyPointerType()) {
9733 if (CI == CE || SI == SE) {
9734 SemaRef.Diag(
9735 DerivedLoc,
9736 diag::err_omp_pointer_mapped_along_with_derived_section)
9737 << DerivedLoc;
9738 } else {
9739 assert(CI != CE && SI != SE);
9740 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9741 << DerivedLoc;
9742 }
9743 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9744 << RE->getSourceRange();
9745 return true;
9746 }
9747
9748 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9749 // List items of map clauses in the same construct must not share
9750 // original storage.
9751 //
9752 // An expression is a subset of the other.
9753 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
9754 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9755 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9756 << RE->getSourceRange();
9757 return true;
9758 }
9759
9760 // The current expression uses the same base as other expression in the
9761 // data environment but does not contain it completelly.
9762 if (!CurrentRegionOnly && SI != SE)
9763 EnclosingExpr = RE;
9764
9765 // The current expression is a subset of the expression in the data
9766 // environment.
9767 IsEnclosedByDataEnvironmentExpr |=
9768 (!CurrentRegionOnly && CI != CE && SI == SE);
9769
9770 return false;
9771 });
9772
9773 if (CurrentRegionOnly)
9774 return FoundError;
9775
9776 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9777 // If any part of the original storage of a list item has corresponding
9778 // storage in the device data environment, all of the original storage must
9779 // have corresponding storage in the device data environment.
9780 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
9781 // If a list item is an element of a structure, and a different element of
9782 // the structure has a corresponding list item in the device data environment
9783 // prior to a task encountering the construct associated with the map clause,
9784 // then the list item must also have a correspnding list item in the device
9785 // data environment prior to the task encountering the construct.
9786 //
9787 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
9788 SemaRef.Diag(ELoc,
9789 diag::err_omp_original_storage_is_shared_and_does_not_contain)
9790 << ERange;
9791 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
9792 << EnclosingExpr->getSourceRange();
9793 return true;
9794 }
9795
9796 return FoundError;
9797}
9798
Samuel Antao23abd722016-01-19 20:40:49 +00009799OMPClause *
9800Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
9801 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9802 SourceLocation MapLoc, SourceLocation ColonLoc,
9803 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9804 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009805 SmallVector<Expr *, 4> Vars;
9806
9807 for (auto &RE : VarList) {
9808 assert(RE && "Null expr in omp map");
9809 if (isa<DependentScopeDeclRefExpr>(RE)) {
9810 // It will be analyzed later.
9811 Vars.push_back(RE);
9812 continue;
9813 }
9814 SourceLocation ELoc = RE->getExprLoc();
9815
Kelvin Li0bff7af2015-11-23 05:32:03 +00009816 auto *VE = RE->IgnoreParenLValueCasts();
9817
9818 if (VE->isValueDependent() || VE->isTypeDependent() ||
9819 VE->isInstantiationDependent() ||
9820 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009821 // We can only analyze this information once the missing information is
9822 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009823 Vars.push_back(RE);
9824 continue;
9825 }
9826
9827 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009828
Samuel Antao5de996e2016-01-22 20:21:36 +00009829 if (!RE->IgnoreParenImpCasts()->isLValue()) {
9830 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
9831 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009832 continue;
9833 }
9834
Samuel Antao5de996e2016-01-22 20:21:36 +00009835 // Obtain the array or member expression bases if required.
9836 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
9837 if (!BE)
9838 continue;
9839
9840 // If the base is a reference to a variable, we rely on that variable for
9841 // the following checks. If it is a 'this' expression we rely on the field.
9842 ValueDecl *D = nullptr;
9843 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
9844 D = DRE->getDecl();
9845 } else {
9846 auto *ME = cast<MemberExpr>(BE);
9847 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
9848 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009849 }
9850 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +00009851
Samuel Antao5de996e2016-01-22 20:21:36 +00009852 auto *VD = dyn_cast<VarDecl>(D);
9853 auto *FD = dyn_cast<FieldDecl>(D);
9854
9855 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +00009856 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +00009857
9858 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
9859 // threadprivate variables cannot appear in a map clause.
9860 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +00009861 auto DVar = DSAStack->getTopDSA(VD, false);
9862 Diag(ELoc, diag::err_omp_threadprivate_in_map);
9863 ReportOriginalDSA(*this, DSAStack, VD, DVar);
9864 continue;
9865 }
9866
Samuel Antao5de996e2016-01-22 20:21:36 +00009867 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
9868 // A list item cannot appear in both a map clause and a data-sharing
9869 // attribute clause on the same construct.
9870 //
9871 // TODO: Implement this check - it cannot currently be tested because of
9872 // missing implementation of the other data sharing clauses in target
9873 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009874
Samuel Antao5de996e2016-01-22 20:21:36 +00009875 // Check conflicts with other map clause expressions. We check the conflicts
9876 // with the current construct separately from the enclosing data
9877 // environment, because the restrictions are different.
9878 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9879 /*CurrentRegionOnly=*/true))
9880 break;
9881 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
9882 /*CurrentRegionOnly=*/false))
9883 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +00009884
Samuel Antao5de996e2016-01-22 20:21:36 +00009885 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9886 // If the type of a list item is a reference to a type T then the type will
9887 // be considered to be T for all purposes of this clause.
9888 QualType Type = D->getType();
9889 if (Type->isReferenceType())
9890 Type = Type->getPointeeType();
9891
9892 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +00009893 // A list item must have a mappable type.
9894 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
9895 DSAStack, Type))
9896 continue;
9897
Samuel Antaodf67fc42016-01-19 19:15:56 +00009898 // target enter data
9899 // OpenMP [2.10.2, Restrictions, p. 99]
9900 // A map-type must be specified in all map clauses and must be either
9901 // to or alloc.
9902 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9903 if (DKind == OMPD_target_enter_data &&
9904 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
9905 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009906 << (IsMapTypeImplicit ? 1 : 0)
9907 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +00009908 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009909 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +00009910 }
9911
Samuel Antao72590762016-01-19 20:04:50 +00009912 // target exit_data
9913 // OpenMP [2.10.3, Restrictions, p. 102]
9914 // A map-type must be specified in all map clauses and must be either
9915 // from, release, or delete.
9916 DKind = DSAStack->getCurrentDirective();
9917 if (DKind == OMPD_target_exit_data &&
9918 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
9919 MapType == OMPC_MAP_delete)) {
9920 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +00009921 << (IsMapTypeImplicit ? 1 : 0)
9922 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +00009923 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +00009924 continue;
Samuel Antao72590762016-01-19 20:04:50 +00009925 }
9926
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009927 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9928 // A list item cannot appear in both a map clause and a data-sharing
9929 // attribute clause on the same construct
9930 if (DKind == OMPD_target && VD) {
9931 auto DVar = DSAStack->getTopDSA(VD, false);
9932 if (isOpenMPPrivate(DVar.CKind)) {
9933 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
9934 << getOpenMPClauseName(DVar.CKind)
9935 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9936 ReportOriginalDSA(*this, DSAStack, D, DVar);
9937 continue;
9938 }
9939 }
9940
Kelvin Li0bff7af2015-11-23 05:32:03 +00009941 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +00009942 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009943 }
Kelvin Li0bff7af2015-11-23 05:32:03 +00009944
Samuel Antao5de996e2016-01-22 20:21:36 +00009945 // We need to produce a map clause even if we don't have variables so that
9946 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +00009947 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +00009948 MapTypeModifier, MapType, IsMapTypeImplicit,
9949 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +00009950}
Kelvin Li099bb8c2015-11-24 20:50:12 +00009951
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00009952QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
9953 TypeResult ParsedType) {
9954 assert(ParsedType.isUsable());
9955
9956 QualType ReductionType = GetTypeFromParser(ParsedType.get());
9957 if (ReductionType.isNull())
9958 return QualType();
9959
9960 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
9961 // A type name in a declare reduction directive cannot be a function type, an
9962 // array type, a reference type, or a type qualified with const, volatile or
9963 // restrict.
9964 if (ReductionType.hasQualifiers()) {
9965 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
9966 return QualType();
9967 }
9968
9969 if (ReductionType->isFunctionType()) {
9970 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
9971 return QualType();
9972 }
9973 if (ReductionType->isReferenceType()) {
9974 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
9975 return QualType();
9976 }
9977 if (ReductionType->isArrayType()) {
9978 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
9979 return QualType();
9980 }
9981 return ReductionType;
9982}
9983
9984Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
9985 Scope *S, DeclContext *DC, DeclarationName Name,
9986 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
9987 AccessSpecifier AS, Decl *PrevDeclInScope) {
9988 SmallVector<Decl *, 8> Decls;
9989 Decls.reserve(ReductionTypes.size());
9990
9991 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
9992 ForRedeclaration);
9993 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
9994 // A reduction-identifier may not be re-declared in the current scope for the
9995 // same type or for a type that is compatible according to the base language
9996 // rules.
9997 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
9998 OMPDeclareReductionDecl *PrevDRD = nullptr;
9999 bool InCompoundScope = true;
10000 if (S != nullptr) {
10001 // Find previous declaration with the same name not referenced in other
10002 // declarations.
10003 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10004 InCompoundScope =
10005 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10006 LookupName(Lookup, S);
10007 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10008 /*AllowInlineNamespace=*/false);
10009 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10010 auto Filter = Lookup.makeFilter();
10011 while (Filter.hasNext()) {
10012 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10013 if (InCompoundScope) {
10014 auto I = UsedAsPrevious.find(PrevDecl);
10015 if (I == UsedAsPrevious.end())
10016 UsedAsPrevious[PrevDecl] = false;
10017 if (auto *D = PrevDecl->getPrevDeclInScope())
10018 UsedAsPrevious[D] = true;
10019 }
10020 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10021 PrevDecl->getLocation();
10022 }
10023 Filter.done();
10024 if (InCompoundScope) {
10025 for (auto &PrevData : UsedAsPrevious) {
10026 if (!PrevData.second) {
10027 PrevDRD = PrevData.first;
10028 break;
10029 }
10030 }
10031 }
10032 } else if (PrevDeclInScope != nullptr) {
10033 auto *PrevDRDInScope = PrevDRD =
10034 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10035 do {
10036 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10037 PrevDRDInScope->getLocation();
10038 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10039 } while (PrevDRDInScope != nullptr);
10040 }
10041 for (auto &TyData : ReductionTypes) {
10042 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10043 bool Invalid = false;
10044 if (I != PreviousRedeclTypes.end()) {
10045 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10046 << TyData.first;
10047 Diag(I->second, diag::note_previous_definition);
10048 Invalid = true;
10049 }
10050 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10051 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10052 Name, TyData.first, PrevDRD);
10053 DC->addDecl(DRD);
10054 DRD->setAccess(AS);
10055 Decls.push_back(DRD);
10056 if (Invalid)
10057 DRD->setInvalidDecl();
10058 else
10059 PrevDRD = DRD;
10060 }
10061
10062 return DeclGroupPtrTy::make(
10063 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10064}
10065
10066void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10067 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10068
10069 // Enter new function scope.
10070 PushFunctionScope();
10071 getCurFunction()->setHasBranchProtectedScope();
10072 getCurFunction()->setHasOMPDeclareReductionCombiner();
10073
10074 if (S != nullptr)
10075 PushDeclContext(S, DRD);
10076 else
10077 CurContext = DRD;
10078
10079 PushExpressionEvaluationContext(PotentiallyEvaluated);
10080
10081 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010082 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10083 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10084 // uses semantics of argument handles by value, but it should be passed by
10085 // reference. C lang does not support references, so pass all parameters as
10086 // pointers.
10087 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010088 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010089 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010090 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10091 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10092 // uses semantics of argument handles by value, but it should be passed by
10093 // reference. C lang does not support references, so pass all parameters as
10094 // pointers.
10095 // Create 'T omp_out;' variable.
10096 auto *OmpOutParm =
10097 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10098 if (S != nullptr) {
10099 PushOnScopeChains(OmpInParm, S);
10100 PushOnScopeChains(OmpOutParm, S);
10101 } else {
10102 DRD->addDecl(OmpInParm);
10103 DRD->addDecl(OmpOutParm);
10104 }
10105}
10106
10107void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10108 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10109 DiscardCleanupsInEvaluationContext();
10110 PopExpressionEvaluationContext();
10111
10112 PopDeclContext();
10113 PopFunctionScopeInfo();
10114
10115 if (Combiner != nullptr)
10116 DRD->setCombiner(Combiner);
10117 else
10118 DRD->setInvalidDecl();
10119}
10120
10121void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10122 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10123
10124 // Enter new function scope.
10125 PushFunctionScope();
10126 getCurFunction()->setHasBranchProtectedScope();
10127
10128 if (S != nullptr)
10129 PushDeclContext(S, DRD);
10130 else
10131 CurContext = DRD;
10132
10133 PushExpressionEvaluationContext(PotentiallyEvaluated);
10134
10135 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010136 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10137 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10138 // uses semantics of argument handles by value, but it should be passed by
10139 // reference. C lang does not support references, so pass all parameters as
10140 // pointers.
10141 // Create 'T omp_priv;' variable.
10142 auto *OmpPrivParm =
10143 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010144 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10145 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10146 // uses semantics of argument handles by value, but it should be passed by
10147 // reference. C lang does not support references, so pass all parameters as
10148 // pointers.
10149 // Create 'T omp_orig;' variable.
10150 auto *OmpOrigParm =
10151 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010152 if (S != nullptr) {
10153 PushOnScopeChains(OmpPrivParm, S);
10154 PushOnScopeChains(OmpOrigParm, S);
10155 } else {
10156 DRD->addDecl(OmpPrivParm);
10157 DRD->addDecl(OmpOrigParm);
10158 }
10159}
10160
10161void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10162 Expr *Initializer) {
10163 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10164 DiscardCleanupsInEvaluationContext();
10165 PopExpressionEvaluationContext();
10166
10167 PopDeclContext();
10168 PopFunctionScopeInfo();
10169
10170 if (Initializer != nullptr)
10171 DRD->setInitializer(Initializer);
10172 else
10173 DRD->setInvalidDecl();
10174}
10175
10176Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10177 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10178 for (auto *D : DeclReductions.get()) {
10179 if (IsValid) {
10180 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10181 if (S != nullptr)
10182 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10183 } else
10184 D->setInvalidDecl();
10185 }
10186 return DeclReductions;
10187}
10188
Kelvin Li099bb8c2015-11-24 20:50:12 +000010189OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10190 SourceLocation StartLoc,
10191 SourceLocation LParenLoc,
10192 SourceLocation EndLoc) {
10193 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010194
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010195 // OpenMP [teams Constrcut, Restrictions]
10196 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010197 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10198 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010199 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010200
10201 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10202}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010203
10204OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10205 SourceLocation StartLoc,
10206 SourceLocation LParenLoc,
10207 SourceLocation EndLoc) {
10208 Expr *ValExpr = ThreadLimit;
10209
10210 // OpenMP [teams Constrcut, Restrictions]
10211 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010212 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10213 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010214 return nullptr;
10215
10216 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10217 EndLoc);
10218}
Alexey Bataeva0569352015-12-01 10:17:31 +000010219
10220OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10221 SourceLocation StartLoc,
10222 SourceLocation LParenLoc,
10223 SourceLocation EndLoc) {
10224 Expr *ValExpr = Priority;
10225
10226 // OpenMP [2.9.1, task Constrcut]
10227 // The priority-value is a non-negative numerical scalar expression.
10228 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10229 /*StrictlyPositive=*/false))
10230 return nullptr;
10231
10232 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10233}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010234
10235OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10236 SourceLocation StartLoc,
10237 SourceLocation LParenLoc,
10238 SourceLocation EndLoc) {
10239 Expr *ValExpr = Grainsize;
10240
10241 // OpenMP [2.9.2, taskloop Constrcut]
10242 // The parameter of the grainsize clause must be a positive integer
10243 // expression.
10244 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10245 /*StrictlyPositive=*/true))
10246 return nullptr;
10247
10248 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10249}
Alexey Bataev382967a2015-12-08 12:06:20 +000010250
10251OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10252 SourceLocation StartLoc,
10253 SourceLocation LParenLoc,
10254 SourceLocation EndLoc) {
10255 Expr *ValExpr = NumTasks;
10256
10257 // OpenMP [2.9.2, taskloop Constrcut]
10258 // The parameter of the num_tasks clause must be a positive integer
10259 // expression.
10260 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10261 /*StrictlyPositive=*/true))
10262 return nullptr;
10263
10264 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10265}
10266
Alexey Bataev28c75412015-12-15 08:19:24 +000010267OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10268 SourceLocation LParenLoc,
10269 SourceLocation EndLoc) {
10270 // OpenMP [2.13.2, critical construct, Description]
10271 // ... where hint-expression is an integer constant expression that evaluates
10272 // to a valid lock hint.
10273 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10274 if (HintExpr.isInvalid())
10275 return nullptr;
10276 return new (Context)
10277 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10278}
10279
Carlo Bertollib4adf552016-01-15 18:50:31 +000010280OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10281 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10282 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10283 SourceLocation EndLoc) {
10284 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10285 std::string Values;
10286 Values += "'";
10287 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10288 Values += "'";
10289 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10290 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10291 return nullptr;
10292 }
10293 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010294 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010295 if (ChunkSize) {
10296 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10297 !ChunkSize->isInstantiationDependent() &&
10298 !ChunkSize->containsUnexpandedParameterPack()) {
10299 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10300 ExprResult Val =
10301 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10302 if (Val.isInvalid())
10303 return nullptr;
10304
10305 ValExpr = Val.get();
10306
10307 // OpenMP [2.7.1, Restrictions]
10308 // chunk_size must be a loop invariant integer expression with a positive
10309 // value.
10310 llvm::APSInt Result;
10311 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10312 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10313 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10314 << "dist_schedule" << ChunkSize->getSourceRange();
10315 return nullptr;
10316 }
10317 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010318 ValExpr = buildCapture(*this, ValExpr);
Alexey Bataev3392d762016-02-16 11:18:12 +000010319 Decl *D = cast<DeclRefExpr>(ValExpr)->getDecl();
10320 HelperValStmt =
10321 new (Context) DeclStmt(DeclGroupRef::Create(Context, &D,
10322 /*NumDecls=*/1),
10323 SourceLocation(), SourceLocation());
10324 ValExpr = DefaultLvalueConversion(ValExpr).get();
Carlo Bertollib4adf552016-01-15 18:50:31 +000010325 }
10326 }
10327 }
10328
10329 return new (Context)
10330 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010331 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010332}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010333
10334OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10335 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10336 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10337 SourceLocation KindLoc, SourceLocation EndLoc) {
10338 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10339 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10340 Kind != OMPC_DEFAULTMAP_scalar) {
10341 std::string Value;
10342 SourceLocation Loc;
10343 Value += "'";
10344 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10345 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10346 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10347 Loc = MLoc;
10348 } else {
10349 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10350 OMPC_DEFAULTMAP_scalar);
10351 Loc = KindLoc;
10352 }
10353 Value += "'";
10354 Diag(Loc, diag::err_omp_unexpected_clause_value)
10355 << Value << getOpenMPClauseName(OMPC_defaultmap);
10356 return nullptr;
10357 }
10358
10359 return new (Context)
10360 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10361}