blob: 01474518a5831fd4fd434e8db4625c8c49f71cc5 [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 Bataevc6ad97a2016-04-01 09:23:34 +000093 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
94 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000095 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000096 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000098
99 struct SharingMapTy {
100 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000102 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000103 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 OpenMPDirectiveKind Directive;
107 DeclarationNameInfo DirectiveName;
108 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000109 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000110 /// \brief first argument (Expr *) contains optional argument of the
111 /// 'ordered' clause, the second one is true if the regions has 'ordered'
112 /// clause, false otherwise.
113 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000114 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000115 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000116 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000117 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000118 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000121 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000122 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000127 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000128 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129 };
130
Axel Naumann323862e2016-02-03 10:45:22 +0000131 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132
133 /// \brief Stack of used declaration and their data-sharing attributes.
134 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000135 /// \brief true, if check for DSA must be from parent directive, false, if
136 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000137 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000138 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000139 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000140 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141
142 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
143
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000144 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000145
146 /// \brief Checks if the variable is a local for OpenMP region.
147 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000148
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000150 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000151 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
152 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000153
Alexey Bataevaac108a2015-06-23 04:51:00 +0000154 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
155 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000156
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000157 bool isForceVarCapturing() const { return ForceCapturing; }
158 void setForceVarCapturing(bool V) { ForceCapturing = V; }
159
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000161 Scope *CurScope, SourceLocation Loc) {
162 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
163 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000164 }
165
166 void pop() {
167 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
168 Stack.pop_back();
169 }
170
Alexey Bataev28c75412015-12-15 08:19:24 +0000171 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
172 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
173 }
174 const std::pair<OMPCriticalDirective *, llvm::APSInt>
175 getCriticalWithHint(const DeclarationNameInfo &Name) const {
176 auto I = Criticals.find(Name.getAsString());
177 if (I != Criticals.end())
178 return I->second;
179 return std::make_pair(nullptr, llvm::APSInt());
180 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000182 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000183 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000185
Alexey Bataev9c821032015-04-30 04:23:23 +0000186 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000187 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000188 /// \brief Check if the specified variable is a loop control variable for
189 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \return The index of the loop control variable in the list of associated
191 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000192 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000193 /// \brief Check if the specified variable is a loop control variable for
194 /// parent region.
195 /// \return The index of the loop control variable in the list of associated
196 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000197 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000198 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
199 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000200 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000201
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000203 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
204 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data sharing attributes from top of the stack for the
207 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000208 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000210 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000211 /// \brief Checks if the specified variables has data-sharing attributes which
212 /// match specified \a CPred predicate in any directive which matches \a DPred
213 /// predicate.
214 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000215 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000216 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000217 /// \brief Checks if the specified variables has data-sharing attributes which
218 /// match specified \a CPred predicate in any innermost directive which
219 /// matches \a DPred predicate.
220 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000221 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
222 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000223 /// \brief Checks if the specified variables has explicit data-sharing
224 /// attributes which match specified \a CPred predicate at the specified
225 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000226 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000227 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
228 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000229
230 /// \brief Returns true if the directive at level \Level matches in the
231 /// specified \a DPred predicate.
232 bool hasExplicitDirective(
233 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
234 unsigned Level);
235
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000236 /// \brief Finds a directive which matches specified \a DPred predicate.
237 template <class NamedDirectivesPredicate>
238 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000239
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240 /// \brief Returns currently analyzed directive.
241 OpenMPDirectiveKind getCurrentDirective() const {
242 return Stack.back().Directive;
243 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000244 /// \brief Returns parent directive.
245 OpenMPDirectiveKind getParentDirective() const {
246 if (Stack.size() > 2)
247 return Stack[Stack.size() - 2].Directive;
248 return OMPD_unknown;
249 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000250 /// \brief Return the directive associated with the provided scope.
251 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252
253 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000254 void setDefaultDSANone(SourceLocation Loc) {
255 Stack.back().DefaultAttr = DSA_none;
256 Stack.back().DefaultAttrLoc = Loc;
257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000259 void setDefaultDSAShared(SourceLocation Loc) {
260 Stack.back().DefaultAttr = DSA_shared;
261 Stack.back().DefaultAttrLoc = Loc;
262 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
264 DefaultDataSharingAttributes getDefaultDSA() const {
265 return Stack.back().DefaultAttr;
266 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000267 SourceLocation getDefaultDSALocation() const {
268 return Stack.back().DefaultAttrLoc;
269 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000273 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000274 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000275 }
276
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000277 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000278 void setOrderedRegion(bool IsOrdered, Expr *Param) {
279 Stack.back().OrderedRegion.setInt(IsOrdered);
280 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000281 }
282 /// \brief Returns true, if parent region is ordered (has associated
283 /// 'ordered' clause), false - otherwise.
284 bool isParentOrderedRegion() const {
285 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000287 return false;
288 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000289 /// \brief Returns optional parameter for the ordered region.
290 Expr *getParentOrderedRegionParam() const {
291 if (Stack.size() > 2)
292 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
293 return nullptr;
294 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000295 /// \brief Marks current region as nowait (it has a 'nowait' clause).
296 void setNowaitRegion(bool IsNowait = true) {
297 Stack.back().NowaitRegion = IsNowait;
298 }
299 /// \brief Returns true, if parent region is nowait (has associated
300 /// 'nowait' clause), false - otherwise.
301 bool isParentNowaitRegion() const {
302 if (Stack.size() > 2)
303 return Stack[Stack.size() - 2].NowaitRegion;
304 return false;
305 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000306 /// \brief Marks parent region as cancel region.
307 void setParentCancelRegion(bool Cancel = true) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].CancelRegion =
310 Stack[Stack.size() - 2].CancelRegion || Cancel;
311 }
312 /// \brief Return true if current region has inner cancel construct.
313 bool isCancelRegion() const {
314 return Stack.back().CancelRegion;
315 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000316
Alexey Bataev9c821032015-04-30 04:23:23 +0000317 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000318 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000319 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000320 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000321
Alexey Bataev13314bf2014-10-09 04:18:56 +0000322 /// \brief Marks current target region as one with closely nested teams
323 /// region.
324 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
325 if (Stack.size() > 2)
326 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
327 }
328 /// \brief Returns true, if current region has closely nested teams region.
329 bool hasInnerTeamsRegion() const {
330 return getInnerTeamsRegionLoc().isValid();
331 }
332 /// \brief Returns location of the nested teams region (if any).
333 SourceLocation getInnerTeamsRegionLoc() const {
334 if (Stack.size() > 1)
335 return Stack.back().InnerTeamsRegionLoc;
336 return SourceLocation();
337 }
338
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000339 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000341 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000342
Samuel Antao5de996e2016-01-22 20:21:36 +0000343 // Do the check specified in MapInfoCheck and return true if any issue is
344 // found.
345 template <class MapInfoCheck>
346 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
347 MapInfoCheck Check) {
348 auto SI = Stack.rbegin();
349 auto SE = Stack.rend();
350
351 if (SI == SE)
352 return false;
353
354 if (CurrentRegionOnly) {
355 SE = std::next(SI);
356 } else {
357 ++SI;
358 }
359
360 for (; SI != SE; ++SI) {
361 auto MI = SI->MappedDecls.find(VD);
362 if (MI != SI->MappedDecls.end()) {
363 for (Expr *E : MI->second) {
364 if (Check(E))
365 return true;
366 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 }
371
Samuel Antao5de996e2016-01-22 20:21:36 +0000372 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000373 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000374 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000375 }
376 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000378bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000379 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
380 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
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
Dmitry Polukhindc78bc822016-04-01 09:52:30 +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 Bataev35aaee62016-04-13 13:36:48 +0000476 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 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
Alexey Bataev35aaee62016-04-13 13:36:48 +0000481 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 // In a task construct, if no default clause is present, a variable
483 // whose data-sharing attribute is not determined by the rules above is
484 // firstprivate.
485 DVarTemp = getDSA(I, D);
486 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000487 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000494 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000495 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 return DVar;
497 }
498 }
499 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
500 // in a Construct, implicitly determined, p.3]
501 // For constructs other than task, if no default clause is present, these
502 // variables inherit their data-sharing attributes from the enclosing
503 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000504 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505}
506
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000507Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000508 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000509 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000510 auto It = Stack.back().AlignedMap.find(D);
511 if (It == Stack.back().AlignedMap.end()) {
512 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
513 Stack.back().AlignedMap[D] = NewDE;
514 return nullptr;
515 } else {
516 assert(It->second && "Unexpected nullptr expr in the aligned map");
517 return It->second;
518 }
519 return nullptr;
520}
521
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000522void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000523 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000525 Stack.back().LCVMap.insert(
526 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000529DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
533 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534}
535
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000536DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000537 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000538 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000539 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
540 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000542}
543
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000544ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000545 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
546 if (Stack[Stack.size() - 2].LCVMap.size() < I)
547 return nullptr;
548 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000549 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000550 return Pair.first;
551 }
552 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000553}
554
Alexey Bataev90c228f2016-02-08 09:29:13 +0000555void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
556 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000557 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000558 if (A == OMPC_threadprivate) {
559 Stack[0].SharingMap[D].Attributes = A;
560 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000561 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 } else {
563 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
564 Stack.back().SharingMap[D].Attributes = A;
565 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000566 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
567 if (PrivateCopy)
568 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000569 }
570}
571
Alexey Bataeved09d242014-05-28 05:53:51 +0000572bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000573 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000574 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000575 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000576 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000577 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 ++I;
579 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000580 if (I == E)
581 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000582 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 Scope *CurScope = getCurScope();
584 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000585 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000586 }
587 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000588 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000589 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000590}
591
Alexey Bataev39f915b82015-05-08 10:41:21 +0000592/// \brief Build a variable declaration for OpenMP loop iteration variable.
593static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000594 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000595 DeclContext *DC = SemaRef.CurContext;
596 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
597 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
598 VarDecl *Decl =
599 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000600 if (Attrs) {
601 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
602 I != E; ++I)
603 Decl->addAttr(*I);
604 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000605 Decl->setImplicit();
606 return Decl;
607}
608
609static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
610 SourceLocation Loc,
611 bool RefersToCapture = false) {
612 D->setReferenced();
613 D->markUsed(S.Context);
614 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
615 SourceLocation(), D, RefersToCapture, Loc, Ty,
616 VK_LValue);
617}
618
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000619DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
620 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621 DSAVarData DVar;
622
623 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
624 // in a Construct, C/C++, predetermined, p.1]
625 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000626 auto *VD = dyn_cast<VarDecl>(D);
627 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
628 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000629 SemaRef.getLangOpts().OpenMPUseTLS &&
630 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000631 (VD && VD->getStorageClass() == SC_Register &&
632 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
633 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000634 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000635 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000636 }
637 if (Stack[0].SharingMap.count(D)) {
638 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
639 DVar.CKind = OMPC_threadprivate;
640 return DVar;
641 }
642
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000643 if (Stack.size() == 1) {
644 // Not in OpenMP execution region and top scope was already checked.
645 return DVar;
646 }
647
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000649 // in a Construct, C/C++, predetermined, p.4]
650 // Static data members are shared.
651 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
652 // in a Construct, C/C++, predetermined, p.7]
653 // Variables with static storage duration that are declared in a scope
654 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000655 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000656 DSAVarData DVarTemp =
657 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
658 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000659 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000660
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000661 DVar.CKind = OMPC_shared;
662 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000663 }
664
665 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000666 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
667 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000668 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
669 // in a Construct, C/C++, predetermined, p.6]
670 // Variables with const qualified type having no mutable member are
671 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000672 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000673 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000674 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
675 if (auto *CTD = CTSD->getSpecializedTemplate())
676 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000678 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
679 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000680 // Variables with const-qualified type having no mutable member may be
681 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000682 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
683 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000684 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
685 return DVar;
686
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 DVar.CKind = OMPC_shared;
688 return DVar;
689 }
690
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 // Explicitly specified attributes and local variables with predetermined
692 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000693 auto StartI = std::next(Stack.rbegin());
694 auto EndI = std::prev(Stack.rend());
695 if (FromParent && StartI != EndI) {
696 StartI = std::next(StartI);
697 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000698 auto I = std::prev(StartI);
699 if (I->SharingMap.count(D)) {
700 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000701 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000702 DVar.CKind = I->SharingMap[D].Attributes;
703 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000704 }
705
706 return DVar;
707}
708
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000709DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
710 bool FromParent) {
711 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000712 auto StartI = Stack.rbegin();
713 auto EndI = std::prev(Stack.rend());
714 if (FromParent && StartI != EndI) {
715 StartI = std::next(StartI);
716 }
717 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000718}
719
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000721DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000722 DirectivesPredicate DPred,
723 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000724 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000725 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000726 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000727 if (FromParent && StartI != EndI) {
728 StartI = std::next(StartI);
729 }
730 for (auto I = StartI, EE = EndI; I != EE; ++I) {
731 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000733 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000735 return DVar;
736 }
737 return DSAVarData();
738}
739
Alexey Bataevf29276e2014-06-18 04:14:57 +0000740template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000741DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000742DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000743 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000744 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000745 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000746 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000747 if (FromParent && StartI != EndI) {
748 StartI = std::next(StartI);
749 }
750 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000751 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000753 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000754 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000755 return DVar;
756 return DSAVarData();
757 }
758 return DSAVarData();
759}
760
Alexey Bataevaac108a2015-06-23 04:51:00 +0000761bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 unsigned Level) {
764 if (CPred(ClauseKindMode))
765 return true;
766 if (isClauseParsingMode())
767 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000768 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000769 auto StartI = Stack.rbegin();
770 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000771 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000772 return false;
773 std::advance(StartI, Level);
774 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
775 CPred(StartI->SharingMap[D].Attributes);
776}
777
Samuel Antao4be30e92015-10-02 17:14:03 +0000778bool DSAStackTy::hasExplicitDirective(
779 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
780 unsigned Level) {
781 if (isClauseParsingMode())
782 ++Level;
783 auto StartI = Stack.rbegin();
784 auto EndI = std::prev(Stack.rend());
785 if (std::distance(StartI, EndI) <= (int)Level)
786 return false;
787 std::advance(StartI, Level);
788 return DPred(StartI->Directive);
789}
790
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000791template <class NamedDirectivesPredicate>
792bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
793 auto StartI = std::next(Stack.rbegin());
794 auto EndI = std::prev(Stack.rend());
795 if (FromParent && StartI != EndI) {
796 StartI = std::next(StartI);
797 }
798 for (auto I = StartI, EE = EndI; I != EE; ++I) {
799 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
800 return true;
801 }
802 return false;
803}
804
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000805OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
806 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
807 if (I->CurScope == S)
808 return I->Directive;
809 return OMPD_unknown;
810}
811
Alexey Bataev758e55e2013-09-06 18:03:48 +0000812void Sema::InitDataSharingAttributesStack() {
813 VarDataSharingAttributesStack = new DSAStackTy(*this);
814}
815
816#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
817
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000818bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000819 const CapturedRegionScopeInfo *RSI) {
820 assert(LangOpts.OpenMP && "OpenMP is not allowed");
821
822 auto &Ctx = getASTContext();
823 bool IsByRef = true;
824
825 // Find the directive that is associated with the provided scope.
826 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000827 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000828
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000829 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000830 // This table summarizes how a given variable should be passed to the device
831 // given its type and the clauses where it appears. This table is based on
832 // the description in OpenMP 4.5 [2.10.4, target Construct] and
833 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
834 //
835 // =========================================================================
836 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
837 // | |(tofrom:scalar)| | pvt | | | |
838 // =========================================================================
839 // | scl | | | | - | | bycopy|
840 // | scl | | - | x | - | - | bycopy|
841 // | scl | | x | - | - | - | null |
842 // | scl | x | | | - | | byref |
843 // | scl | x | - | x | - | - | bycopy|
844 // | scl | x | x | - | - | - | null |
845 // | scl | | - | - | - | x | byref |
846 // | scl | x | - | - | - | x | byref |
847 //
848 // | agg | n.a. | | | - | | byref |
849 // | agg | n.a. | - | x | - | - | byref |
850 // | agg | n.a. | x | - | - | - | null |
851 // | agg | n.a. | - | - | - | x | byref |
852 // | agg | n.a. | - | - | - | x[] | byref |
853 //
854 // | ptr | n.a. | | | - | | bycopy|
855 // | ptr | n.a. | - | x | - | - | bycopy|
856 // | ptr | n.a. | x | - | - | - | null |
857 // | ptr | n.a. | - | - | - | x | byref |
858 // | ptr | n.a. | - | - | - | x[] | bycopy|
859 // | ptr | n.a. | - | - | x | | bycopy|
860 // | ptr | n.a. | - | - | x | x | bycopy|
861 // | ptr | n.a. | - | - | x | x[] | bycopy|
862 // =========================================================================
863 // Legend:
864 // scl - scalar
865 // ptr - pointer
866 // agg - aggregate
867 // x - applies
868 // - - invalid in this combination
869 // [] - mapped with an array section
870 // byref - should be mapped by reference
871 // byval - should be mapped by value
872 // null - initialize a local variable to null on the device
873 //
874 // Observations:
875 // - All scalar declarations that show up in a map clause have to be passed
876 // by reference, because they may have been mapped in the enclosing data
877 // environment.
878 // - If the scalar value does not fit the size of uintptr, it has to be
879 // passed by reference, regardless the result in the table above.
880 // - For pointers mapped by value that have either an implicit map or an
881 // array section, the runtime library may pass the NULL value to the
882 // device instead of the value passed to it by the compiler.
883
884 // FIXME: Right now, only implicit maps are implemented. Properly mapping
885 // values requires having the map, private, and firstprivate clauses SEMA
886 // and parsing in place, which we don't yet.
887
888 if (Ty->isReferenceType())
889 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
890 IsByRef = !Ty->isScalarType();
891 }
892
893 // When passing data by value, we need to make sure it fits the uintptr size
894 // and alignment, because the runtime library only deals with uintptr types.
895 // If it does not fit the uintptr size, we need to pass the data by reference
896 // instead.
897 if (!IsByRef &&
898 (Ctx.getTypeSizeInChars(Ty) >
899 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000900 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000901 IsByRef = true;
902
903 return IsByRef;
904}
905
Alexey Bataev90c228f2016-02-08 09:29:13 +0000906VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000907 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000908 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000909
910 // If we are attempting to capture a global variable in a directive with
911 // 'target' we return true so that this global is also mapped to the device.
912 //
913 // FIXME: If the declaration is enclosed in a 'declare target' directive,
914 // then it should not be captured. Therefore, an extra check has to be
915 // inserted here once support for 'declare target' is added.
916 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000917 auto *VD = dyn_cast<VarDecl>(D);
918 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000919 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000920 !DSAStack->isClauseParsingMode())
921 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000922 if (DSAStack->getCurScope() &&
923 DSAStack->hasDirective(
924 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
925 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000926 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000927 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000928 false))
929 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000930 }
931
Alexey Bataev48977c32015-08-04 08:10:48 +0000932 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
933 (!DSAStack->isClauseParsingMode() ||
934 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000935 auto &&Info = DSAStack->isLoopControlVariable(D);
936 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000938 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000939 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000940 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000941 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000942 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000943 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000944 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000945 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000946 if (DVarPrivate.CKind != OMPC_unknown)
947 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000948 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000949 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000950}
951
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000952bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000953 assert(LangOpts.OpenMP && "OpenMP is not allowed");
954 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000956}
957
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000958bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000959 assert(LangOpts.OpenMP && "OpenMP is not allowed");
960 // Return true if the current level is no longer enclosed in a target region.
961
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000962 auto *VD = dyn_cast<VarDecl>(D);
963 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000964 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
965 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000966}
967
Alexey Bataeved09d242014-05-28 05:53:51 +0000968void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969
970void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
971 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000972 Scope *CurScope, SourceLocation Loc) {
973 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000974 PushExpressionEvaluationContext(PotentiallyEvaluated);
975}
976
Alexey Bataevaac108a2015-06-23 04:51:00 +0000977void Sema::StartOpenMPClause(OpenMPClauseKind K) {
978 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000979}
980
Alexey Bataevaac108a2015-06-23 04:51:00 +0000981void Sema::EndOpenMPClause() {
982 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000983}
984
Alexey Bataev758e55e2013-09-06 18:03:48 +0000985void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000986 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
987 // A variable of class type (or array thereof) that appears in a lastprivate
988 // clause requires an accessible, unambiguous default constructor for the
989 // class type, unless the list item is also specified in a firstprivate
990 // clause.
991 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 for (auto *C : D->clauses()) {
993 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
994 SmallVector<Expr *, 8> PrivateCopies;
995 for (auto *DE : Clause->varlists()) {
996 if (DE->isValueDependent() || DE->isTypeDependent()) {
997 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000998 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000999 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001000 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001001 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1002 QualType Type = VD->getType().getNonReferenceType();
1003 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001004 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001005 // Generate helper private variable and initialize it with the
1006 // default value. The address of the original variable is replaced
1007 // by the address of the new private variable in CodeGen. This new
1008 // variable is not added to IdResolver, so the code in the OpenMP
1009 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001010 auto *VDPrivate = buildVarDecl(
1011 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001012 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001013 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1014 if (VDPrivate->isInvalidDecl())
1015 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001016 PrivateCopies.push_back(buildDeclRefExpr(
1017 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001018 } else {
1019 // The variable is also a firstprivate, so initialization sequence
1020 // for private copy is generated already.
1021 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001022 }
1023 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001024 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001025 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001026 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001027 }
1028 }
1029 }
1030
Alexey Bataev758e55e2013-09-06 18:03:48 +00001031 DSAStack->pop();
1032 DiscardCleanupsInEvaluationContext();
1033 PopExpressionEvaluationContext();
1034}
1035
Alexey Bataev5a3af132016-03-29 08:58:54 +00001036static bool
1037FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1038 Expr *NumIterations, Sema &SemaRef, Scope *S);
Alexander Musman3276a272015-03-21 10:12:56 +00001039
Alexey Bataeva769e072013-03-22 06:34:35 +00001040namespace {
1041
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042class VarDeclFilterCCC : public CorrectionCandidateCallback {
1043private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001044 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001048 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 NamedDecl *ND = Candidate.getCorrectionDecl();
1050 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1051 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001052 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1053 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001054 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001055 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001056 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057};
Alexey Bataeved09d242014-05-28 05:53:51 +00001058} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059
1060ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1061 CXXScopeSpec &ScopeSpec,
1062 const DeclarationNameInfo &Id) {
1063 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1064 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1065
1066 if (Lookup.isAmbiguous())
1067 return ExprError();
1068
1069 VarDecl *VD;
1070 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001071 if (TypoCorrection Corrected = CorrectTypo(
1072 Id, LookupOrdinaryName, CurScope, nullptr,
1073 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001074 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001075 PDiag(Lookup.empty()
1076 ? diag::err_undeclared_var_use_suggest
1077 : diag::err_omp_expected_var_arg_suggest)
1078 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001079 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001081 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1082 : diag::err_omp_expected_var_arg)
1083 << Id.getName();
1084 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001085 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 } else {
1087 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001088 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1090 return ExprError();
1091 }
1092 }
1093 Lookup.suppressDiagnostics();
1094
1095 // OpenMP [2.9.2, Syntax, C/C++]
1096 // Variables must be file-scope, namespace-scope, or static block-scope.
1097 if (!VD->hasGlobalStorage()) {
1098 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001099 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1100 bool IsDecl =
1101 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1104 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 return ExprError();
1106 }
1107
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001108 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1109 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1111 // A threadprivate directive for file-scope variables must appear outside
1112 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001113 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1114 !getCurLexicalContext()->isTranslationUnit()) {
1115 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001116 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1117 bool IsDecl =
1118 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1119 Diag(VD->getLocation(),
1120 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1121 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001122 return ExprError();
1123 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001124 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1125 // A threadprivate directive for static class member variables must appear
1126 // in the class definition, in the same scope in which the member
1127 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001128 if (CanonicalVD->isStaticDataMember() &&
1129 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1130 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001131 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1132 bool IsDecl =
1133 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1134 Diag(VD->getLocation(),
1135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1136 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001137 return ExprError();
1138 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001139 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1140 // A threadprivate directive for namespace-scope variables must appear
1141 // outside any definition or declaration other than the namespace
1142 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001143 if (CanonicalVD->getDeclContext()->isNamespace() &&
1144 (!getCurLexicalContext()->isFileContext() ||
1145 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1146 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001147 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1148 bool IsDecl =
1149 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1150 Diag(VD->getLocation(),
1151 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1152 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001153 return ExprError();
1154 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1156 // A threadprivate directive for static block-scope variables must appear
1157 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001158 if (CanonicalVD->isStaticLocal() && CurScope &&
1159 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001161 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1162 bool IsDecl =
1163 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1164 Diag(VD->getLocation(),
1165 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1166 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 return ExprError();
1168 }
1169
1170 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1171 // A threadprivate directive must lexically precede all references to any
1172 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001173 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 return ExprError();
1177 }
1178
1179 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001180 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1181 SourceLocation(), VD,
1182 /*RefersToEnclosingVariableOrCapture=*/false,
1183 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184}
1185
Alexey Bataeved09d242014-05-28 05:53:51 +00001186Sema::DeclGroupPtrTy
1187Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1188 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001189 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001190 CurContext->addDecl(D);
1191 return DeclGroupPtrTy::make(DeclGroupRef(D));
1192 }
David Blaikie0403cb12016-01-15 23:43:25 +00001193 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001194}
1195
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001196namespace {
1197class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1198 Sema &SemaRef;
1199
1200public:
1201 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1202 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1203 if (VD->hasLocalStorage()) {
1204 SemaRef.Diag(E->getLocStart(),
1205 diag::err_omp_local_var_in_threadprivate_init)
1206 << E->getSourceRange();
1207 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1208 << VD << VD->getSourceRange();
1209 return true;
1210 }
1211 }
1212 return false;
1213 }
1214 bool VisitStmt(const Stmt *S) {
1215 for (auto Child : S->children()) {
1216 if (Child && Visit(Child))
1217 return true;
1218 }
1219 return false;
1220 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001221 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001222};
1223} // namespace
1224
Alexey Bataeved09d242014-05-28 05:53:51 +00001225OMPThreadPrivateDecl *
1226Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001227 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001228 for (auto &RefExpr : VarList) {
1229 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1231 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001232
Alexey Bataev376b4a42016-02-09 09:41:09 +00001233 // Mark variable as used.
1234 VD->setReferenced();
1235 VD->markUsed(Context);
1236
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001237 QualType QType = VD->getType();
1238 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1239 // It will be analyzed later.
1240 Vars.push_back(DE);
1241 continue;
1242 }
1243
Alexey Bataeva769e072013-03-22 06:34:35 +00001244 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1245 // A threadprivate variable must not have an incomplete type.
1246 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001247 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 continue;
1249 }
1250
1251 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1252 // A threadprivate variable must not have a reference type.
1253 if (VD->getType()->isReferenceType()) {
1254 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001255 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1256 bool IsDecl =
1257 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1258 Diag(VD->getLocation(),
1259 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1260 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001261 continue;
1262 }
1263
Samuel Antaof8b50122015-07-13 22:54:53 +00001264 // Check if this is a TLS variable. If TLS is not being supported, produce
1265 // the corresponding diagnostic.
1266 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1267 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1268 getLangOpts().OpenMPUseTLS &&
1269 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001270 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1271 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001272 Diag(ILoc, diag::err_omp_var_thread_local)
1273 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001274 bool IsDecl =
1275 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1276 Diag(VD->getLocation(),
1277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1278 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001279 continue;
1280 }
1281
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001282 // Check if initial value of threadprivate variable reference variable with
1283 // local storage (it is not supported by runtime).
1284 if (auto Init = VD->getAnyInitializer()) {
1285 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001286 if (Checker.Visit(Init))
1287 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001288 }
1289
Alexey Bataeved09d242014-05-28 05:53:51 +00001290 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001291 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001292 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1293 Context, SourceRange(Loc, Loc)));
1294 if (auto *ML = Context.getASTMutationListener())
1295 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001296 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001297 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001298 if (!Vars.empty()) {
1299 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1300 Vars);
1301 D->setAccess(AS_public);
1302 }
1303 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001304}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001305
Alexey Bataev7ff55242014-06-19 09:13:45 +00001306static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001307 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001308 bool IsLoopIterVar = false) {
1309 if (DVar.RefExpr) {
1310 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1311 << getOpenMPClauseName(DVar.CKind);
1312 return;
1313 }
1314 enum {
1315 PDSA_StaticMemberShared,
1316 PDSA_StaticLocalVarShared,
1317 PDSA_LoopIterVarPrivate,
1318 PDSA_LoopIterVarLinear,
1319 PDSA_LoopIterVarLastprivate,
1320 PDSA_ConstVarShared,
1321 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001323 PDSA_LocalVarPrivate,
1324 PDSA_Implicit
1325 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001327 auto ReportLoc = D->getLocation();
1328 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 if (IsLoopIterVar) {
1330 if (DVar.CKind == OMPC_private)
1331 Reason = PDSA_LoopIterVarPrivate;
1332 else if (DVar.CKind == OMPC_lastprivate)
1333 Reason = PDSA_LoopIterVarLastprivate;
1334 else
1335 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001336 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1337 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 Reason = PDSA_TaskVarFirstprivate;
1339 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001344 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001349 ReportHint = true;
1350 Reason = PDSA_LocalVarPrivate;
1351 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001352 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001353 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001354 << Reason << ReportHint
1355 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1356 } else if (DVar.ImplicitDSALoc.isValid()) {
1357 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1358 << getOpenMPClauseName(DVar.CKind);
1359 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001360}
1361
Alexey Bataev758e55e2013-09-06 18:03:48 +00001362namespace {
1363class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1364 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001365 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366 bool ErrorFound;
1367 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001368 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001369 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001370
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371public:
1372 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001373 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001375 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1376 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001378 auto DVar = Stack->getTopDSA(VD, false);
1379 // Check if the variable has explicit DSA set and stop analysis if it so.
1380 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001381
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001382 auto ELoc = E->getExprLoc();
1383 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384 // The default(none) clause requires that each variable that is referenced
1385 // in the construct, and does not have a predetermined data-sharing
1386 // attribute, must have its data-sharing attribute explicitly determined
1387 // by being listed in a data-sharing attribute clause.
1388 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001389 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001390 VarsWithInheritedDSA.count(VD) == 0) {
1391 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001392 return;
1393 }
1394
1395 // OpenMP [2.9.3.6, Restrictions, p.2]
1396 // A list item that appears in a reduction clause of the innermost
1397 // enclosing worksharing or parallel construct may not be accessed in an
1398 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001399 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001400 [](OpenMPDirectiveKind K) -> bool {
1401 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001402 isOpenMPWorksharingDirective(K) ||
1403 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001404 },
1405 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001406 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001407 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001408 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1409 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001410 return;
1411 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001412
1413 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001414 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001415 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1416 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001418 }
1419 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001420 void VisitMemberExpr(MemberExpr *E) {
1421 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1422 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1423 auto DVar = Stack->getTopDSA(FD, false);
1424 // Check if the variable has explicit DSA set and stop analysis if it
1425 // so.
1426 if (DVar.RefExpr)
1427 return;
1428
1429 auto ELoc = E->getExprLoc();
1430 auto DKind = Stack->getCurrentDirective();
1431 // OpenMP [2.9.3.6, Restrictions, p.2]
1432 // A list item that appears in a reduction clause of the innermost
1433 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001434 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 DVar =
1436 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1437 [](OpenMPDirectiveKind K) -> bool {
1438 return isOpenMPParallelDirective(K) ||
1439 isOpenMPWorksharingDirective(K) ||
1440 isOpenMPTeamsDirective(K);
1441 },
1442 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001443 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001444 ErrorFound = true;
1445 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1446 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1447 return;
1448 }
1449
1450 // Define implicit data-sharing attributes for task.
1451 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001452 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1453 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001454 ImplicitFirstprivate.push_back(E);
1455 }
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 for (auto *C : S->clauses()) {
1460 // Skip analysis of arguments of implicitly defined firstprivate clause
1461 // for task directives.
1462 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1463 for (auto *CC : C->children()) {
1464 if (CC)
1465 Visit(CC);
1466 }
1467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 }
1469 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 for (auto *C : S->children()) {
1471 if (C && !isa<OMPExecutableDirective>(C))
1472 Visit(C);
1473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
1476 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001478 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001479 return VarsWithInheritedDSA;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev7ff55242014-06-19 09:13:45 +00001482 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1483 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484};
Alexey Bataeved09d242014-05-28 05:53:51 +00001485} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486
Alexey Bataevbae9a792014-06-27 10:37:06 +00001487void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 switch (DKind) {
1489 case OMPD_parallel: {
1490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001491 QualType KmpInt32PtrTy =
1492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(".global_tid.", KmpInt32PtrTy),
1495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500 break;
1501 }
1502 case OMPD_simd: {
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
1505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 break;
1509 }
1510 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001511 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001512 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001516 break;
1517 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001518 case OMPD_for_simd: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
1522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
1524 break;
1525 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 case OMPD_sections: {
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 Bataevd3f8dd22014-06-25 11:44:49 +00001532 break;
1533 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 case OMPD_section: {
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 Bataev1e0498a2014-06-26 08:21:58 +00001540 break;
1541 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 case OMPD_single: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 break;
1549 }
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master: {
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 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001558 case OMPD_critical: {
1559 Sema::CapturedParamNameType Params[] = {
1560 std::make_pair(StringRef(), QualType()) // __context with shared vars
1561 };
1562 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1563 Params);
1564 break;
1565 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 case OMPD_parallel_for: {
1567 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001568 QualType KmpInt32PtrTy =
1569 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 Sema::CapturedParamNameType Params[] = {
1571 std::make_pair(".global_tid.", KmpInt32PtrTy),
1572 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 case OMPD_parallel_for_simd: {
1580 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001581 QualType KmpInt32PtrTy =
1582 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(".global_tid.", KmpInt32PtrTy),
1585 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1586 std::make_pair(StringRef(), QualType()) // __context with shared vars
1587 };
1588 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1589 Params);
1590 break;
1591 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001594 QualType KmpInt32PtrTy =
1595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001597 std::make_pair(".global_tid.", KmpInt32PtrTy),
1598 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
1601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
1603 break;
1604 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001607 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1608 FunctionProtoType::ExtProtoInfo EPI;
1609 EPI.Variadic = true;
1610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001613 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1614 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1615 std::make_pair(".copy_fn.",
1616 Context.getPointerType(CopyFnType).withConst()),
1617 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001618 std::make_pair(StringRef(), QualType()) // __context with shared vars
1619 };
1620 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1621 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001622 // Mark this captured region as inlined, because we don't use outlined
1623 // function directly.
1624 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1625 AlwaysInlineAttr::CreateImplicit(
1626 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001627 break;
1628 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001629 case OMPD_ordered: {
1630 Sema::CapturedParamNameType Params[] = {
1631 std::make_pair(StringRef(), QualType()) // __context with shared vars
1632 };
1633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1634 Params);
1635 break;
1636 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001637 case OMPD_atomic: {
1638 Sema::CapturedParamNameType Params[] = {
1639 std::make_pair(StringRef(), QualType()) // __context with shared vars
1640 };
1641 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1642 Params);
1643 break;
1644 }
Michael Wong65f367f2015-07-21 13:44:28 +00001645 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001646 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001647 case OMPD_target_parallel:
1648 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001649 Sema::CapturedParamNameType Params[] = {
1650 std::make_pair(StringRef(), QualType()) // __context with shared vars
1651 };
1652 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1653 Params);
1654 break;
1655 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001656 case OMPD_teams: {
1657 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001658 QualType KmpInt32PtrTy =
1659 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001660 Sema::CapturedParamNameType Params[] = {
1661 std::make_pair(".global_tid.", KmpInt32PtrTy),
1662 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1663 std::make_pair(StringRef(), QualType()) // __context with shared vars
1664 };
1665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1666 Params);
1667 break;
1668 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001669 case OMPD_taskgroup: {
1670 Sema::CapturedParamNameType Params[] = {
1671 std::make_pair(StringRef(), QualType()) // __context with shared vars
1672 };
1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1674 Params);
1675 break;
1676 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001677 case OMPD_taskloop: {
1678 Sema::CapturedParamNameType Params[] = {
1679 std::make_pair(StringRef(), QualType()) // __context with shared vars
1680 };
1681 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1682 Params);
1683 break;
1684 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001685 case OMPD_taskloop_simd: {
1686 Sema::CapturedParamNameType Params[] = {
1687 std::make_pair(StringRef(), QualType()) // __context with shared vars
1688 };
1689 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1690 Params);
1691 break;
1692 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001693 case OMPD_distribute: {
1694 Sema::CapturedParamNameType Params[] = {
1695 std::make_pair(StringRef(), QualType()) // __context with shared vars
1696 };
1697 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1698 Params);
1699 break;
1700 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001701 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001702 case OMPD_taskyield:
1703 case OMPD_barrier:
1704 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001705 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001706 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001707 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001708 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001709 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001710 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001711 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001712 case OMPD_declare_target:
1713 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("OpenMP Directive is not allowed");
1715 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001716 llvm_unreachable("Unknown OpenMP directive");
1717 }
1718}
1719
Alexey Bataev3392d762016-02-16 11:18:12 +00001720static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001721 Expr *CaptureExpr, bool WithInit,
1722 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001723 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001724 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001725 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001726 QualType Ty = Init->getType();
1727 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1728 if (S.getLangOpts().CPlusPlus)
1729 Ty = C.getLValueReferenceType(Ty);
1730 else {
1731 Ty = C.getPointerType(Ty);
1732 ExprResult Res =
1733 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1734 if (!Res.isUsable())
1735 return nullptr;
1736 Init = Res.get();
1737 }
Alexey Bataev61205072016-03-02 04:57:40 +00001738 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001739 }
1740 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001741 if (!WithInit)
1742 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001743 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001744 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1745 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001746 return CED;
1747}
1748
Alexey Bataev61205072016-03-02 04:57:40 +00001749static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1750 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001751 OMPCapturedExprDecl *CD;
1752 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1753 CD = cast<OMPCapturedExprDecl>(VD);
1754 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001755 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1756 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001757 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001758 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001759}
1760
Alexey Bataev5a3af132016-03-29 08:58:54 +00001761static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1762 if (!Ref) {
1763 auto *CD =
1764 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1765 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1766 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1767 CaptureExpr->getExprLoc());
1768 }
1769 ExprResult Res = Ref;
1770 if (!S.getLangOpts().CPlusPlus &&
1771 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1772 Ref->getType()->isPointerType())
1773 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1774 if (!Res.isUsable())
1775 return ExprError();
1776 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001777}
1778
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001779StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1780 ArrayRef<OMPClause *> Clauses) {
1781 if (!S.isUsable()) {
1782 ActOnCapturedRegionError();
1783 return StmtError();
1784 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001785
1786 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001787 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001788 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001789 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001790 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001791 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001792 Clause->getClauseKind() == OMPC_copyprivate ||
1793 (getLangOpts().OpenMPUseTLS &&
1794 getASTContext().getTargetInfo().isTLSSupported() &&
1795 Clause->getClauseKind() == OMPC_copyin)) {
1796 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001797 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001798 for (auto *VarRef : Clause->children()) {
1799 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001800 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001801 }
1802 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001803 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001804 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001805 // Mark all variables in private list clauses as used in inner region.
1806 // Required for proper codegen of combined directives.
1807 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001808 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001809 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1810 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001811 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1812 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001813 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001814 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1815 if (auto *E = C->getPostUpdateExpr())
1816 MarkDeclarationsReferencedInExpr(E);
1817 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001818 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001819 if (Clause->getClauseKind() == OMPC_schedule)
1820 SC = cast<OMPScheduleClause>(Clause);
1821 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001822 OC = cast<OMPOrderedClause>(Clause);
1823 else if (Clause->getClauseKind() == OMPC_linear)
1824 LCs.push_back(cast<OMPLinearClause>(Clause));
1825 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001826 bool ErrorFound = false;
1827 // OpenMP, 2.7.1 Loop Construct, Restrictions
1828 // The nonmonotonic modifier cannot be specified if an ordered clause is
1829 // specified.
1830 if (SC &&
1831 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1832 SC->getSecondScheduleModifier() ==
1833 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1834 OC) {
1835 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1836 ? SC->getFirstScheduleModifierLoc()
1837 : SC->getSecondScheduleModifierLoc(),
1838 diag::err_omp_schedule_nonmonotonic_ordered)
1839 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1840 ErrorFound = true;
1841 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001842 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1843 for (auto *C : LCs) {
1844 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1845 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1846 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001847 ErrorFound = true;
1848 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001849 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1850 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1851 OC->getNumForLoops()) {
1852 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1853 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1854 ErrorFound = true;
1855 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001856 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001857 ActOnCapturedRegionError();
1858 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001859 }
1860 return ActOnCapturedRegionEnd(S.get());
1861}
1862
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001863static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1864 OpenMPDirectiveKind CurrentRegion,
1865 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001866 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001867 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001868 // Allowed nesting of constructs
1869 // +------------------+-----------------+------------------------------------+
1870 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1871 // +------------------+-----------------+------------------------------------+
1872 // | parallel | parallel | * |
1873 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001874 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001875 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001876 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001877 // | parallel | simd | * |
1878 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001879 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001881 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001882 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001883 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001884 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001885 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001886 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001887 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001888 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001889 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001890 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001891 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001892 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001893 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001894 // | parallel | target parallel | * |
1895 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001896 // | parallel | target enter | * |
1897 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001898 // | parallel | target exit | * |
1899 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001900 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001901 // | parallel | cancellation | |
1902 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001903 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001904 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001905 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001906 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001907 // +------------------+-----------------+------------------------------------+
1908 // | for | parallel | * |
1909 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001910 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001911 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001912 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001913 // | for | simd | * |
1914 // | for | sections | + |
1915 // | for | section | + |
1916 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001917 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001918 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001919 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001920 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001921 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001922 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001923 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001924 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001925 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001926 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001927 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001928 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001929 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001930 // | for | target parallel | * |
1931 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001932 // | for | target enter | * |
1933 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001934 // | for | target exit | * |
1935 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001936 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001937 // | for | cancellation | |
1938 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001939 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001940 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001941 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001942 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001943 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001944 // | master | parallel | * |
1945 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001946 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001947 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001948 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001949 // | master | simd | * |
1950 // | master | sections | + |
1951 // | master | section | + |
1952 // | master | single | + |
1953 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001954 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001955 // | master |parallel sections| * |
1956 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001957 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001958 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001959 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001960 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001961 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001962 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001963 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001964 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001965 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001966 // | master | target parallel | * |
1967 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001968 // | master | target enter | * |
1969 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001970 // | master | target exit | * |
1971 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001972 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001973 // | master | cancellation | |
1974 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001975 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001976 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001977 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001978 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001979 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | critical | parallel | * |
1981 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001982 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001983 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001984 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001985 // | critical | simd | * |
1986 // | critical | sections | + |
1987 // | critical | section | + |
1988 // | critical | single | + |
1989 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001990 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001991 // | critical |parallel sections| * |
1992 // | critical | task | * |
1993 // | critical | taskyield | * |
1994 // | critical | barrier | + |
1995 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001996 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001997 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001998 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001999 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002000 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002001 // | critical | target parallel | * |
2002 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002003 // | critical | target enter | * |
2004 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002005 // | critical | target exit | * |
2006 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002007 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002008 // | critical | cancellation | |
2009 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002010 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002011 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002012 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002013 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002015 // | simd | parallel | |
2016 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002017 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002018 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002019 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002020 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002021 // | simd | sections | |
2022 // | simd | section | |
2023 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002024 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002025 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002026 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002027 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002028 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002029 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002030 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002031 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002032 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002033 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002034 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002035 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002036 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002037 // | simd | target parallel | |
2038 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002039 // | simd | target enter | |
2040 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002041 // | simd | target exit | |
2042 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002043 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002044 // | simd | cancellation | |
2045 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002046 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002047 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002048 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002049 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002050 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002051 // | for simd | parallel | |
2052 // | for simd | for | |
2053 // | for simd | for simd | |
2054 // | for simd | master | |
2055 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002056 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002057 // | for simd | sections | |
2058 // | for simd | section | |
2059 // | for simd | single | |
2060 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002061 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002062 // | for simd |parallel sections| |
2063 // | for simd | task | |
2064 // | for simd | taskyield | |
2065 // | for simd | barrier | |
2066 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002067 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002068 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002069 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002070 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002071 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002072 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002073 // | for simd | target parallel | |
2074 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002075 // | for simd | target enter | |
2076 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002077 // | for simd | target exit | |
2078 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002079 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002080 // | for simd | cancellation | |
2081 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002082 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002083 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002084 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002085 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002086 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002087 // | parallel for simd| parallel | |
2088 // | parallel for simd| for | |
2089 // | parallel for simd| for simd | |
2090 // | parallel for simd| master | |
2091 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002093 // | parallel for simd| sections | |
2094 // | parallel for simd| section | |
2095 // | parallel for simd| single | |
2096 // | parallel for simd| parallel for | |
2097 // | parallel for simd|parallel for simd| |
2098 // | parallel for simd|parallel sections| |
2099 // | parallel for simd| task | |
2100 // | parallel for simd| taskyield | |
2101 // | parallel for simd| barrier | |
2102 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002104 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002106 // | parallel for simd| atomic | |
2107 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | parallel for simd| target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | parallel for simd| target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | parallel for simd| target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | parallel for simd| cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002122 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002123 // | sections | parallel | * |
2124 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002125 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002126 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002127 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002128 // | sections | simd | * |
2129 // | sections | sections | + |
2130 // | sections | section | * |
2131 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002132 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002133 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002134 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002135 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002136 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002137 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002138 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002139 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002140 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002141 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002142 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002143 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002144 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002145 // | sections | target parallel | * |
2146 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002147 // | sections | target enter | * |
2148 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002149 // | sections | target exit | * |
2150 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002151 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002152 // | sections | cancellation | |
2153 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002154 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002155 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002156 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002157 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002158 // +------------------+-----------------+------------------------------------+
2159 // | section | parallel | * |
2160 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002161 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002162 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002163 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002164 // | section | simd | * |
2165 // | section | sections | + |
2166 // | section | section | + |
2167 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002168 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002169 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002170 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002171 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002172 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002173 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002174 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002175 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002176 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002177 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002178 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002179 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002180 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002181 // | section | target parallel | * |
2182 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002183 // | section | target enter | * |
2184 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002185 // | section | target exit | * |
2186 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002187 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002188 // | section | cancellation | |
2189 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002190 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002191 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002192 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002193 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002194 // +------------------+-----------------+------------------------------------+
2195 // | single | parallel | * |
2196 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002197 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002198 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002199 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002200 // | single | simd | * |
2201 // | single | sections | + |
2202 // | single | section | + |
2203 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002204 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002205 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002206 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002207 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002208 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002209 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002210 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002211 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002212 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002214 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002216 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002217 // | single | target parallel | * |
2218 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002219 // | single | target enter | * |
2220 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002221 // | single | target exit | * |
2222 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002223 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002224 // | single | cancellation | |
2225 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002226 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002227 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002228 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002229 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002230 // +------------------+-----------------+------------------------------------+
2231 // | parallel for | parallel | * |
2232 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002233 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002234 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002235 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002236 // | parallel for | simd | * |
2237 // | parallel for | sections | + |
2238 // | parallel for | section | + |
2239 // | parallel for | single | + |
2240 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002241 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002242 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002243 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002244 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002245 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002246 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002247 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002248 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002249 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002250 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002251 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002252 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002253 // | parallel for | target parallel | * |
2254 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002255 // | parallel for | target enter | * |
2256 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002257 // | parallel for | target exit | * |
2258 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002259 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002260 // | parallel for | cancellation | |
2261 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002262 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002263 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002264 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002265 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002266 // +------------------+-----------------+------------------------------------+
2267 // | parallel sections| parallel | * |
2268 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002269 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002270 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002271 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002272 // | parallel sections| simd | * |
2273 // | parallel sections| sections | + |
2274 // | parallel sections| section | * |
2275 // | parallel sections| single | + |
2276 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002277 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002278 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002279 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002280 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002281 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002282 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002283 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002284 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002285 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002286 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002287 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002288 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002289 // | parallel sections| target parallel | * |
2290 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002291 // | parallel sections| target enter | * |
2292 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002293 // | parallel sections| target exit | * |
2294 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002295 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002296 // | parallel sections| cancellation | |
2297 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002298 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002299 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002300 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002301 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002302 // +------------------+-----------------+------------------------------------+
2303 // | task | parallel | * |
2304 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002305 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002306 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002307 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002308 // | task | simd | * |
2309 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002310 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002311 // | task | single | + |
2312 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002313 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002314 // | task |parallel sections| * |
2315 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002316 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002317 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002318 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002319 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002320 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002321 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002322 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002323 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002324 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002325 // | task | target parallel | * |
2326 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002327 // | task | target enter | * |
2328 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002329 // | task | target exit | * |
2330 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002331 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002332 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002333 // | | point | ! |
2334 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002335 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002336 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002337 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002338 // +------------------+-----------------+------------------------------------+
2339 // | ordered | parallel | * |
2340 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002341 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002342 // | ordered | master | * |
2343 // | ordered | critical | * |
2344 // | ordered | simd | * |
2345 // | ordered | sections | + |
2346 // | ordered | section | + |
2347 // | ordered | single | + |
2348 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002349 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002350 // | ordered |parallel sections| * |
2351 // | ordered | task | * |
2352 // | ordered | taskyield | * |
2353 // | ordered | barrier | + |
2354 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002355 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002356 // | ordered | flush | * |
2357 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002358 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002359 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002360 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002361 // | ordered | target parallel | * |
2362 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002363 // | ordered | target enter | * |
2364 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002365 // | ordered | target exit | * |
2366 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002367 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002368 // | ordered | cancellation | |
2369 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002370 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002371 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002372 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002373 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002374 // +------------------+-----------------+------------------------------------+
2375 // | atomic | parallel | |
2376 // | atomic | for | |
2377 // | atomic | for simd | |
2378 // | atomic | master | |
2379 // | atomic | critical | |
2380 // | atomic | simd | |
2381 // | atomic | sections | |
2382 // | atomic | section | |
2383 // | atomic | single | |
2384 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002385 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002386 // | atomic |parallel sections| |
2387 // | atomic | task | |
2388 // | atomic | taskyield | |
2389 // | atomic | barrier | |
2390 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002391 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002392 // | atomic | flush | |
2393 // | atomic | ordered | |
2394 // | atomic | atomic | |
2395 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002396 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002397 // | atomic | target parallel | |
2398 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | atomic | target enter | |
2400 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002401 // | atomic | target exit | |
2402 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002403 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002404 // | atomic | cancellation | |
2405 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002406 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002407 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002408 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002409 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002410 // +------------------+-----------------+------------------------------------+
2411 // | target | parallel | * |
2412 // | target | for | * |
2413 // | target | for simd | * |
2414 // | target | master | * |
2415 // | target | critical | * |
2416 // | target | simd | * |
2417 // | target | sections | * |
2418 // | target | section | * |
2419 // | target | single | * |
2420 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002421 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002422 // | target |parallel sections| * |
2423 // | target | task | * |
2424 // | target | taskyield | * |
2425 // | target | barrier | * |
2426 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002427 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002428 // | target | flush | * |
2429 // | target | ordered | * |
2430 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002431 // | target | target | |
2432 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002433 // | target | target parallel | |
2434 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002435 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002436 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002437 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002438 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002439 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002440 // | target | cancellation | |
2441 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002442 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002443 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002444 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002445 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002446 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002447 // | target parallel | parallel | * |
2448 // | target parallel | for | * |
2449 // | target parallel | for simd | * |
2450 // | target parallel | master | * |
2451 // | target parallel | critical | * |
2452 // | target parallel | simd | * |
2453 // | target parallel | sections | * |
2454 // | target parallel | section | * |
2455 // | target parallel | single | * |
2456 // | target parallel | parallel for | * |
2457 // | target parallel |parallel for simd| * |
2458 // | target parallel |parallel sections| * |
2459 // | target parallel | task | * |
2460 // | target parallel | taskyield | * |
2461 // | target parallel | barrier | * |
2462 // | target parallel | taskwait | * |
2463 // | target parallel | taskgroup | * |
2464 // | target parallel | flush | * |
2465 // | target parallel | ordered | * |
2466 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002467 // | target parallel | target | |
2468 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002469 // | target parallel | target parallel | |
2470 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002471 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002472 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002473 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002474 // | | data | |
2475 // | target parallel | teams | |
2476 // | target parallel | cancellation | |
2477 // | | point | ! |
2478 // | target parallel | cancel | ! |
2479 // | target parallel | taskloop | * |
2480 // | target parallel | taskloop simd | * |
2481 // | target parallel | distribute | |
2482 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002483 // | target parallel | parallel | * |
2484 // | for | | |
2485 // | target parallel | for | * |
2486 // | for | | |
2487 // | target parallel | for simd | * |
2488 // | for | | |
2489 // | target parallel | master | * |
2490 // | for | | |
2491 // | target parallel | critical | * |
2492 // | for | | |
2493 // | target parallel | simd | * |
2494 // | for | | |
2495 // | target parallel | sections | * |
2496 // | for | | |
2497 // | target parallel | section | * |
2498 // | for | | |
2499 // | target parallel | single | * |
2500 // | for | | |
2501 // | target parallel | parallel for | * |
2502 // | for | | |
2503 // | target parallel |parallel for simd| * |
2504 // | for | | |
2505 // | target parallel |parallel sections| * |
2506 // | for | | |
2507 // | target parallel | task | * |
2508 // | for | | |
2509 // | target parallel | taskyield | * |
2510 // | for | | |
2511 // | target parallel | barrier | * |
2512 // | for | | |
2513 // | target parallel | taskwait | * |
2514 // | for | | |
2515 // | target parallel | taskgroup | * |
2516 // | for | | |
2517 // | target parallel | flush | * |
2518 // | for | | |
2519 // | target parallel | ordered | * |
2520 // | for | | |
2521 // | target parallel | atomic | * |
2522 // | for | | |
2523 // | target parallel | target | |
2524 // | for | | |
2525 // | target parallel | target parallel | |
2526 // | for | | |
2527 // | target parallel | target parallel | |
2528 // | for | for | |
2529 // | target parallel | target enter | |
2530 // | for | data | |
2531 // | target parallel | target exit | |
2532 // | for | data | |
2533 // | target parallel | teams | |
2534 // | for | | |
2535 // | target parallel | cancellation | |
2536 // | for | point | ! |
2537 // | target parallel | cancel | ! |
2538 // | for | | |
2539 // | target parallel | taskloop | * |
2540 // | for | | |
2541 // | target parallel | taskloop simd | * |
2542 // | for | | |
2543 // | target parallel | distribute | |
2544 // | for | | |
2545 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002546 // | teams | parallel | * |
2547 // | teams | for | + |
2548 // | teams | for simd | + |
2549 // | teams | master | + |
2550 // | teams | critical | + |
2551 // | teams | simd | + |
2552 // | teams | sections | + |
2553 // | teams | section | + |
2554 // | teams | single | + |
2555 // | teams | parallel for | * |
2556 // | teams |parallel for simd| * |
2557 // | teams |parallel sections| * |
2558 // | teams | task | + |
2559 // | teams | taskyield | + |
2560 // | teams | barrier | + |
2561 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002562 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002563 // | teams | flush | + |
2564 // | teams | ordered | + |
2565 // | teams | atomic | + |
2566 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002567 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002568 // | teams | target parallel | + |
2569 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002570 // | teams | target enter | + |
2571 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002572 // | teams | target exit | + |
2573 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002574 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002575 // | teams | cancellation | |
2576 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002577 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002578 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002579 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002580 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002581 // +------------------+-----------------+------------------------------------+
2582 // | taskloop | parallel | * |
2583 // | taskloop | for | + |
2584 // | taskloop | for simd | + |
2585 // | taskloop | master | + |
2586 // | taskloop | critical | * |
2587 // | taskloop | simd | * |
2588 // | taskloop | sections | + |
2589 // | taskloop | section | + |
2590 // | taskloop | single | + |
2591 // | taskloop | parallel for | * |
2592 // | taskloop |parallel for simd| * |
2593 // | taskloop |parallel sections| * |
2594 // | taskloop | task | * |
2595 // | taskloop | taskyield | * |
2596 // | taskloop | barrier | + |
2597 // | taskloop | taskwait | * |
2598 // | taskloop | taskgroup | * |
2599 // | taskloop | flush | * |
2600 // | taskloop | ordered | + |
2601 // | taskloop | atomic | * |
2602 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002603 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002604 // | taskloop | target parallel | * |
2605 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002606 // | taskloop | target enter | * |
2607 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002608 // | taskloop | target exit | * |
2609 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002610 // | taskloop | teams | + |
2611 // | taskloop | cancellation | |
2612 // | | point | |
2613 // | taskloop | cancel | |
2614 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002615 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002616 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002617 // | taskloop simd | parallel | |
2618 // | taskloop simd | for | |
2619 // | taskloop simd | for simd | |
2620 // | taskloop simd | master | |
2621 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002622 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002623 // | taskloop simd | sections | |
2624 // | taskloop simd | section | |
2625 // | taskloop simd | single | |
2626 // | taskloop simd | parallel for | |
2627 // | taskloop simd |parallel for simd| |
2628 // | taskloop simd |parallel sections| |
2629 // | taskloop simd | task | |
2630 // | taskloop simd | taskyield | |
2631 // | taskloop simd | barrier | |
2632 // | taskloop simd | taskwait | |
2633 // | taskloop simd | taskgroup | |
2634 // | taskloop simd | flush | |
2635 // | taskloop simd | ordered | + (with simd clause) |
2636 // | taskloop simd | atomic | |
2637 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002638 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002639 // | taskloop simd | target parallel | |
2640 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002641 // | taskloop simd | target enter | |
2642 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002643 // | taskloop simd | target exit | |
2644 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002645 // | taskloop simd | teams | |
2646 // | taskloop simd | cancellation | |
2647 // | | point | |
2648 // | taskloop simd | cancel | |
2649 // | taskloop simd | taskloop | |
2650 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002651 // | taskloop simd | distribute | |
2652 // +------------------+-----------------+------------------------------------+
2653 // | distribute | parallel | * |
2654 // | distribute | for | * |
2655 // | distribute | for simd | * |
2656 // | distribute | master | * |
2657 // | distribute | critical | * |
2658 // | distribute | simd | * |
2659 // | distribute | sections | * |
2660 // | distribute | section | * |
2661 // | distribute | single | * |
2662 // | distribute | parallel for | * |
2663 // | distribute |parallel for simd| * |
2664 // | distribute |parallel sections| * |
2665 // | distribute | task | * |
2666 // | distribute | taskyield | * |
2667 // | distribute | barrier | * |
2668 // | distribute | taskwait | * |
2669 // | distribute | taskgroup | * |
2670 // | distribute | flush | * |
2671 // | distribute | ordered | + |
2672 // | distribute | atomic | * |
2673 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002674 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002675 // | distribute | target parallel | |
2676 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002677 // | distribute | target enter | |
2678 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002679 // | distribute | target exit | |
2680 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002681 // | distribute | teams | |
2682 // | distribute | cancellation | + |
2683 // | | point | |
2684 // | distribute | cancel | + |
2685 // | distribute | taskloop | * |
2686 // | distribute | taskloop simd | * |
2687 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002688 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002689 if (Stack->getCurScope()) {
2690 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002691 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002692 bool NestingProhibited = false;
2693 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002694 enum {
2695 NoRecommend,
2696 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002697 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002698 ShouldBeInTargetRegion,
2699 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002700 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002701 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2702 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002703 // OpenMP [2.16, Nesting of Regions]
2704 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002705 // OpenMP [2.8.1,simd Construct, Restrictions]
2706 // An ordered construct with the simd clause is the only OpenMP construct
2707 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002708 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2709 return true;
2710 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002711 if (ParentRegion == OMPD_atomic) {
2712 // OpenMP [2.16, Nesting of Regions]
2713 // OpenMP constructs may not be nested inside an atomic region.
2714 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2715 return true;
2716 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002717 if (CurrentRegion == OMPD_section) {
2718 // OpenMP [2.7.2, sections Construct, Restrictions]
2719 // Orphaned section directives are prohibited. That is, the section
2720 // directives must appear within the sections construct and must not be
2721 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002722 if (ParentRegion != OMPD_sections &&
2723 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002724 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2725 << (ParentRegion != OMPD_unknown)
2726 << getOpenMPDirectiveName(ParentRegion);
2727 return true;
2728 }
2729 return false;
2730 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002731 // Allow some constructs to be orphaned (they could be used in functions,
2732 // called from OpenMP regions with the required preconditions).
2733 if (ParentRegion == OMPD_unknown)
2734 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002735 if (CurrentRegion == OMPD_cancellation_point ||
2736 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002737 // OpenMP [2.16, Nesting of Regions]
2738 // A cancellation point construct for which construct-type-clause is
2739 // taskgroup must be nested inside a task construct. A cancellation
2740 // point construct for which construct-type-clause is not taskgroup must
2741 // be closely nested inside an OpenMP construct that matches the type
2742 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002743 // A cancel construct for which construct-type-clause is taskgroup must be
2744 // nested inside a task construct. A cancel construct for which
2745 // construct-type-clause is not taskgroup must be closely nested inside an
2746 // OpenMP construct that matches the type specified in
2747 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002748 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002749 !((CancelRegion == OMPD_parallel &&
2750 (ParentRegion == OMPD_parallel ||
2751 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002752 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002753 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2754 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002755 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2756 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002757 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2758 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002759 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002760 // OpenMP [2.16, Nesting of Regions]
2761 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002762 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002763 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002764 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002765 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2766 // OpenMP [2.16, Nesting of Regions]
2767 // A critical region may not be nested (closely or otherwise) inside a
2768 // critical region with the same name. Note that this restriction is not
2769 // sufficient to prevent deadlock.
2770 SourceLocation PreviousCriticalLoc;
2771 bool DeadLock =
2772 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2773 OpenMPDirectiveKind K,
2774 const DeclarationNameInfo &DNI,
2775 SourceLocation Loc)
2776 ->bool {
2777 if (K == OMPD_critical &&
2778 DNI.getName() == CurrentName.getName()) {
2779 PreviousCriticalLoc = Loc;
2780 return true;
2781 } else
2782 return false;
2783 },
2784 false /* skip top directive */);
2785 if (DeadLock) {
2786 SemaRef.Diag(StartLoc,
2787 diag::err_omp_prohibited_region_critical_same_name)
2788 << CurrentName.getName();
2789 if (PreviousCriticalLoc.isValid())
2790 SemaRef.Diag(PreviousCriticalLoc,
2791 diag::note_omp_previous_critical_region);
2792 return true;
2793 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002794 } else if (CurrentRegion == OMPD_barrier) {
2795 // OpenMP [2.16, Nesting of Regions]
2796 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002797 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002798 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2799 isOpenMPTaskingDirective(ParentRegion) ||
2800 ParentRegion == OMPD_master ||
2801 ParentRegion == OMPD_critical ||
2802 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002803 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002804 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002805 // OpenMP [2.16, Nesting of Regions]
2806 // A worksharing region may not be closely nested inside a worksharing,
2807 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002808 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2809 isOpenMPTaskingDirective(ParentRegion) ||
2810 ParentRegion == OMPD_master ||
2811 ParentRegion == OMPD_critical ||
2812 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002813 Recommend = ShouldBeInParallelRegion;
2814 } else if (CurrentRegion == OMPD_ordered) {
2815 // OpenMP [2.16, Nesting of Regions]
2816 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002817 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002818 // An ordered region must be closely nested inside a loop region (or
2819 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002820 // OpenMP [2.8.1,simd Construct, Restrictions]
2821 // An ordered construct with the simd clause is the only OpenMP construct
2822 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002823 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002824 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002825 !(isOpenMPSimdDirective(ParentRegion) ||
2826 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002827 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002828 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2829 // OpenMP [2.16, Nesting of Regions]
2830 // If specified, a teams construct must be contained within a target
2831 // construct.
2832 NestingProhibited = ParentRegion != OMPD_target;
2833 Recommend = ShouldBeInTargetRegion;
2834 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2835 }
2836 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2837 // OpenMP [2.16, Nesting of Regions]
2838 // distribute, parallel, parallel sections, parallel workshare, and the
2839 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2840 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002841 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2842 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002843 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002844 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002845 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2846 // OpenMP 4.5 [2.17 Nesting of Regions]
2847 // The region associated with the distribute construct must be strictly
2848 // nested inside a teams region
2849 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2850 Recommend = ShouldBeInTeamsRegion;
2851 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002852 if (!NestingProhibited &&
2853 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2854 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2855 // OpenMP 4.5 [2.17 Nesting of Regions]
2856 // If a target, target update, target data, target enter data, or
2857 // target exit data construct is encountered during execution of a
2858 // target region, the behavior is unspecified.
2859 NestingProhibited = Stack->hasDirective(
2860 [&OffendingRegion](OpenMPDirectiveKind K,
2861 const DeclarationNameInfo &DNI,
2862 SourceLocation Loc) -> bool {
2863 if (isOpenMPTargetExecutionDirective(K)) {
2864 OffendingRegion = K;
2865 return true;
2866 } else
2867 return false;
2868 },
2869 false /* don't skip top directive */);
2870 CloseNesting = false;
2871 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002872 if (NestingProhibited) {
2873 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002874 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2875 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002876 return true;
2877 }
2878 }
2879 return false;
2880}
2881
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002882static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2883 ArrayRef<OMPClause *> Clauses,
2884 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2885 bool ErrorFound = false;
2886 unsigned NamedModifiersNumber = 0;
2887 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2888 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002889 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002890 for (const auto *C : Clauses) {
2891 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2892 // At most one if clause without a directive-name-modifier can appear on
2893 // the directive.
2894 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2895 if (FoundNameModifiers[CurNM]) {
2896 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2897 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2898 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2899 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002900 } else if (CurNM != OMPD_unknown) {
2901 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002902 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002903 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002904 FoundNameModifiers[CurNM] = IC;
2905 if (CurNM == OMPD_unknown)
2906 continue;
2907 // Check if the specified name modifier is allowed for the current
2908 // directive.
2909 // At most one if clause with the particular directive-name-modifier can
2910 // appear on the directive.
2911 bool MatchFound = false;
2912 for (auto NM : AllowedNameModifiers) {
2913 if (CurNM == NM) {
2914 MatchFound = true;
2915 break;
2916 }
2917 }
2918 if (!MatchFound) {
2919 S.Diag(IC->getNameModifierLoc(),
2920 diag::err_omp_wrong_if_directive_name_modifier)
2921 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2922 ErrorFound = true;
2923 }
2924 }
2925 }
2926 // If any if clause on the directive includes a directive-name-modifier then
2927 // all if clauses on the directive must include a directive-name-modifier.
2928 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2929 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2930 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2931 diag::err_omp_no_more_if_clause);
2932 } else {
2933 std::string Values;
2934 std::string Sep(", ");
2935 unsigned AllowedCnt = 0;
2936 unsigned TotalAllowedNum =
2937 AllowedNameModifiers.size() - NamedModifiersNumber;
2938 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2939 ++Cnt) {
2940 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2941 if (!FoundNameModifiers[NM]) {
2942 Values += "'";
2943 Values += getOpenMPDirectiveName(NM);
2944 Values += "'";
2945 if (AllowedCnt + 2 == TotalAllowedNum)
2946 Values += " or ";
2947 else if (AllowedCnt + 1 != TotalAllowedNum)
2948 Values += Sep;
2949 ++AllowedCnt;
2950 }
2951 }
2952 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2953 diag::err_omp_unnamed_if_clause)
2954 << (TotalAllowedNum > 1) << Values;
2955 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002956 for (auto Loc : NameModifierLoc) {
2957 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2958 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002959 ErrorFound = true;
2960 }
2961 return ErrorFound;
2962}
2963
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002964StmtResult Sema::ActOnOpenMPExecutableDirective(
2965 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2966 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2967 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002968 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002969 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2970 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002971 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002972
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002973 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002974 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002975 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002976 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002977 if (AStmt) {
2978 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2979
2980 // Check default data sharing attributes for referenced variables.
2981 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2982 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2983 if (DSAChecker.isErrorFound())
2984 return StmtError();
2985 // Generate list of implicitly defined firstprivate variables.
2986 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002987
2988 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2989 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2990 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2991 SourceLocation(), SourceLocation())) {
2992 ClausesWithImplicit.push_back(Implicit);
2993 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2994 DSAChecker.getImplicitFirstprivate().size();
2995 } else
2996 ErrorFound = true;
2997 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002998 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002999
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003000 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003001 switch (Kind) {
3002 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003003 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3004 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003005 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003006 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003007 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003008 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3009 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003010 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003011 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003012 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3013 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003014 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003015 case OMPD_for_simd:
3016 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3017 EndLoc, VarsWithInheritedDSA);
3018 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003019 case OMPD_sections:
3020 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3021 EndLoc);
3022 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003023 case OMPD_section:
3024 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003025 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003026 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3027 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003028 case OMPD_single:
3029 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3030 EndLoc);
3031 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003032 case OMPD_master:
3033 assert(ClausesWithImplicit.empty() &&
3034 "No clauses are allowed for 'omp master' directive");
3035 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3036 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003037 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003038 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3039 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003040 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003041 case OMPD_parallel_for:
3042 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3043 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003044 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003045 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003046 case OMPD_parallel_for_simd:
3047 Res = ActOnOpenMPParallelForSimdDirective(
3048 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003049 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003050 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003051 case OMPD_parallel_sections:
3052 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3053 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003054 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003055 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003056 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003057 Res =
3058 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003059 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003060 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003061 case OMPD_taskyield:
3062 assert(ClausesWithImplicit.empty() &&
3063 "No clauses are allowed for 'omp taskyield' directive");
3064 assert(AStmt == nullptr &&
3065 "No associated statement allowed for 'omp taskyield' directive");
3066 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3067 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003068 case OMPD_barrier:
3069 assert(ClausesWithImplicit.empty() &&
3070 "No clauses are allowed for 'omp barrier' directive");
3071 assert(AStmt == nullptr &&
3072 "No associated statement allowed for 'omp barrier' directive");
3073 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3074 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003075 case OMPD_taskwait:
3076 assert(ClausesWithImplicit.empty() &&
3077 "No clauses are allowed for 'omp taskwait' directive");
3078 assert(AStmt == nullptr &&
3079 "No associated statement allowed for 'omp taskwait' directive");
3080 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3081 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003082 case OMPD_taskgroup:
3083 assert(ClausesWithImplicit.empty() &&
3084 "No clauses are allowed for 'omp taskgroup' directive");
3085 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3086 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003087 case OMPD_flush:
3088 assert(AStmt == nullptr &&
3089 "No associated statement allowed for 'omp flush' directive");
3090 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3091 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003092 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003093 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3094 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003095 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003096 case OMPD_atomic:
3097 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc);
3099 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003100 case OMPD_teams:
3101 Res =
3102 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3103 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003104 case OMPD_target:
3105 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3106 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003107 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003108 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003109 case OMPD_target_parallel:
3110 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3111 StartLoc, EndLoc);
3112 AllowedNameModifiers.push_back(OMPD_target);
3113 AllowedNameModifiers.push_back(OMPD_parallel);
3114 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003115 case OMPD_target_parallel_for:
3116 Res = ActOnOpenMPTargetParallelForDirective(
3117 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3118 AllowedNameModifiers.push_back(OMPD_target);
3119 AllowedNameModifiers.push_back(OMPD_parallel);
3120 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003121 case OMPD_cancellation_point:
3122 assert(ClausesWithImplicit.empty() &&
3123 "No clauses are allowed for 'omp cancellation point' directive");
3124 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3125 "cancellation point' directive");
3126 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3127 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003128 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003129 assert(AStmt == nullptr &&
3130 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003131 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3132 CancelRegion);
3133 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003134 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003135 case OMPD_target_data:
3136 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3137 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003138 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003139 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003140 case OMPD_target_enter_data:
3141 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3142 EndLoc);
3143 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3144 break;
Samuel Antao72590762016-01-19 20:04:50 +00003145 case OMPD_target_exit_data:
3146 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3147 EndLoc);
3148 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3149 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003150 case OMPD_taskloop:
3151 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3152 EndLoc, VarsWithInheritedDSA);
3153 AllowedNameModifiers.push_back(OMPD_taskloop);
3154 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003155 case OMPD_taskloop_simd:
3156 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3157 EndLoc, VarsWithInheritedDSA);
3158 AllowedNameModifiers.push_back(OMPD_taskloop);
3159 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003160 case OMPD_distribute:
3161 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3162 EndLoc, VarsWithInheritedDSA);
3163 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003164 case OMPD_declare_target:
3165 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003166 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003167 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003168 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003169 llvm_unreachable("OpenMP Directive is not allowed");
3170 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003171 llvm_unreachable("Unknown OpenMP directive");
3172 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003173
Alexey Bataev4acb8592014-07-07 13:01:15 +00003174 for (auto P : VarsWithInheritedDSA) {
3175 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3176 << P.first << P.second->getSourceRange();
3177 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003178 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3179
3180 if (!AllowedNameModifiers.empty())
3181 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3182 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003183
Alexey Bataeved09d242014-05-28 05:53:51 +00003184 if (ErrorFound)
3185 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003186 return Res;
3187}
3188
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003189Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3190 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003191 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003192 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3193 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003194 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003195 assert(Linears.size() == LinModifiers.size());
3196 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003197 if (!DG || DG.get().isNull())
3198 return DeclGroupPtrTy();
3199
3200 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003201 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003202 return DG;
3203 }
3204 auto *ADecl = DG.get().getSingleDecl();
3205 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3206 ADecl = FTD->getTemplatedDecl();
3207
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003208 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3209 if (!FD) {
3210 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003211 return DeclGroupPtrTy();
3212 }
3213
Alexey Bataev2af33e32016-04-07 12:45:37 +00003214 // OpenMP [2.8.2, declare simd construct, Description]
3215 // The parameter of the simdlen clause must be a constant positive integer
3216 // expression.
3217 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003218 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003219 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003220 // OpenMP [2.8.2, declare simd construct, Description]
3221 // The special this pointer can be used as if was one of the arguments to the
3222 // function in any of the linear, aligned, or uniform clauses.
3223 // The uniform clause declares one or more arguments to have an invariant
3224 // value for all concurrent invocations of the function in the execution of a
3225 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003226 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3227 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003228 for (auto *E : Uniforms) {
3229 E = E->IgnoreParenImpCasts();
3230 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3231 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3232 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3233 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003234 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3235 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003236 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003237 }
3238 if (isa<CXXThisExpr>(E)) {
3239 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003240 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003241 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003242 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3243 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003244 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003245 // OpenMP [2.8.2, declare simd construct, Description]
3246 // The aligned clause declares that the object to which each list item points
3247 // is aligned to the number of bytes expressed in the optional parameter of
3248 // the aligned clause.
3249 // The special this pointer can be used as if was one of the arguments to the
3250 // function in any of the linear, aligned, or uniform clauses.
3251 // The type of list items appearing in the aligned clause must be array,
3252 // pointer, reference to array, or reference to pointer.
3253 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3254 Expr *AlignedThis = nullptr;
3255 for (auto *E : Aligneds) {
3256 E = E->IgnoreParenImpCasts();
3257 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3258 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3259 auto *CanonPVD = PVD->getCanonicalDecl();
3260 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3261 FD->getParamDecl(PVD->getFunctionScopeIndex())
3262 ->getCanonicalDecl() == CanonPVD) {
3263 // OpenMP [2.8.1, simd construct, Restrictions]
3264 // A list-item cannot appear in more than one aligned clause.
3265 if (AlignedArgs.count(CanonPVD) > 0) {
3266 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3267 << 1 << E->getSourceRange();
3268 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3269 diag::note_omp_explicit_dsa)
3270 << getOpenMPClauseName(OMPC_aligned);
3271 continue;
3272 }
3273 AlignedArgs[CanonPVD] = E;
3274 QualType QTy = PVD->getType()
3275 .getNonReferenceType()
3276 .getUnqualifiedType()
3277 .getCanonicalType();
3278 const Type *Ty = QTy.getTypePtrOrNull();
3279 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3280 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3281 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3282 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3283 }
3284 continue;
3285 }
3286 }
3287 if (isa<CXXThisExpr>(E)) {
3288 if (AlignedThis) {
3289 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3290 << 2 << E->getSourceRange();
3291 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3292 << getOpenMPClauseName(OMPC_aligned);
3293 }
3294 AlignedThis = E;
3295 continue;
3296 }
3297 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3298 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3299 }
3300 // The optional parameter of the aligned clause, alignment, must be a constant
3301 // positive integer expression. If no optional parameter is specified,
3302 // implementation-defined default alignments for SIMD instructions on the
3303 // target platforms are assumed.
3304 SmallVector<Expr *, 4> NewAligns;
3305 for (auto *E : Alignments) {
3306 ExprResult Align;
3307 if (E)
3308 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3309 NewAligns.push_back(Align.get());
3310 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003311 // OpenMP [2.8.2, declare simd construct, Description]
3312 // The linear clause declares one or more list items to be private to a SIMD
3313 // lane and to have a linear relationship with respect to the iteration space
3314 // of a loop.
3315 // The special this pointer can be used as if was one of the arguments to the
3316 // function in any of the linear, aligned, or uniform clauses.
3317 // When a linear-step expression is specified in a linear clause it must be
3318 // either a constant integer expression or an integer-typed parameter that is
3319 // specified in a uniform clause on the directive.
3320 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3321 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3322 auto MI = LinModifiers.begin();
3323 for (auto *E : Linears) {
3324 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3325 ++MI;
3326 E = E->IgnoreParenImpCasts();
3327 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3328 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3329 auto *CanonPVD = PVD->getCanonicalDecl();
3330 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3331 FD->getParamDecl(PVD->getFunctionScopeIndex())
3332 ->getCanonicalDecl() == CanonPVD) {
3333 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3334 // A list-item cannot appear in more than one linear clause.
3335 if (LinearArgs.count(CanonPVD) > 0) {
3336 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3337 << getOpenMPClauseName(OMPC_linear)
3338 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3339 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3340 diag::note_omp_explicit_dsa)
3341 << getOpenMPClauseName(OMPC_linear);
3342 continue;
3343 }
3344 // Each argument can appear in at most one uniform or linear clause.
3345 if (UniformedArgs.count(CanonPVD) > 0) {
3346 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3347 << getOpenMPClauseName(OMPC_linear)
3348 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3349 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3350 diag::note_omp_explicit_dsa)
3351 << getOpenMPClauseName(OMPC_uniform);
3352 continue;
3353 }
3354 LinearArgs[CanonPVD] = E;
3355 if (E->isValueDependent() || E->isTypeDependent() ||
3356 E->isInstantiationDependent() ||
3357 E->containsUnexpandedParameterPack())
3358 continue;
3359 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3360 PVD->getOriginalType());
3361 continue;
3362 }
3363 }
3364 if (isa<CXXThisExpr>(E)) {
3365 if (UniformedLinearThis) {
3366 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3367 << getOpenMPClauseName(OMPC_linear)
3368 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3369 << E->getSourceRange();
3370 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3371 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3372 : OMPC_linear);
3373 continue;
3374 }
3375 UniformedLinearThis = E;
3376 if (E->isValueDependent() || E->isTypeDependent() ||
3377 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3378 continue;
3379 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3380 E->getType());
3381 continue;
3382 }
3383 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3384 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3385 }
3386 Expr *Step = nullptr;
3387 Expr *NewStep = nullptr;
3388 SmallVector<Expr *, 4> NewSteps;
3389 for (auto *E : Steps) {
3390 // Skip the same step expression, it was checked already.
3391 if (Step == E || !E) {
3392 NewSteps.push_back(E ? NewStep : nullptr);
3393 continue;
3394 }
3395 Step = E;
3396 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3397 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3398 auto *CanonPVD = PVD->getCanonicalDecl();
3399 if (UniformedArgs.count(CanonPVD) == 0) {
3400 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3401 << Step->getSourceRange();
3402 } else if (E->isValueDependent() || E->isTypeDependent() ||
3403 E->isInstantiationDependent() ||
3404 E->containsUnexpandedParameterPack() ||
3405 CanonPVD->getType()->hasIntegerRepresentation())
3406 NewSteps.push_back(Step);
3407 else {
3408 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3409 << Step->getSourceRange();
3410 }
3411 continue;
3412 }
3413 NewStep = Step;
3414 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3415 !Step->isInstantiationDependent() &&
3416 !Step->containsUnexpandedParameterPack()) {
3417 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3418 .get();
3419 if (NewStep)
3420 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3421 }
3422 NewSteps.push_back(NewStep);
3423 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003424 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3425 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003426 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003427 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3428 const_cast<Expr **>(Linears.data()), Linears.size(),
3429 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3430 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003431 ADecl->addAttr(NewAttr);
3432 return ConvertDeclToDeclGroup(ADecl);
3433}
3434
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003435StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3436 Stmt *AStmt,
3437 SourceLocation StartLoc,
3438 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003439 if (!AStmt)
3440 return StmtError();
3441
Alexey Bataev9959db52014-05-06 10:08:46 +00003442 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3443 // 1.2.2 OpenMP Language Terminology
3444 // Structured block - An executable statement with a single entry at the
3445 // top and a single exit at the bottom.
3446 // The point of exit cannot be a branch out of the structured block.
3447 // longjmp() and throw() must not violate the entry/exit criteria.
3448 CS->getCapturedDecl()->setNothrow();
3449
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003450 getCurFunction()->setHasBranchProtectedScope();
3451
Alexey Bataev25e5b442015-09-15 12:52:43 +00003452 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3453 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003454}
3455
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003456namespace {
3457/// \brief Helper class for checking canonical form of the OpenMP loops and
3458/// extracting iteration space of each loop in the loop nest, that will be used
3459/// for IR generation.
3460class OpenMPIterationSpaceChecker {
3461 /// \brief Reference to Sema.
3462 Sema &SemaRef;
3463 /// \brief A location for diagnostics (when there is no some better location).
3464 SourceLocation DefaultLoc;
3465 /// \brief A location for diagnostics (when increment is not compatible).
3466 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467 /// \brief A source location for referring to loop init later.
3468 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003469 /// \brief A source location for referring to condition later.
3470 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003471 /// \brief A source location for referring to increment later.
3472 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003473 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003474 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003475 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003476 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003477 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003478 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003479 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003480 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003481 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003482 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003483 /// \brief This flag is true when condition is one of:
3484 /// Var < UB
3485 /// Var <= UB
3486 /// UB > Var
3487 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003488 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003489 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003490 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003491 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003492 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003493
3494public:
3495 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003496 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003497 /// \brief Check init-expr for canonical loop form and save loop counter
3498 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003499 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003500 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3501 /// for less/greater and for strict/non-strict comparison.
3502 bool CheckCond(Expr *S);
3503 /// \brief Check incr-expr for canonical loop form and return true if it
3504 /// does not conform, otherwise save loop step (#Step).
3505 bool CheckInc(Expr *S);
3506 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003507 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003508 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003509 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003510 /// \brief Source range of the loop init.
3511 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3512 /// \brief Source range of the loop condition.
3513 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3514 /// \brief Source range of the loop increment.
3515 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3516 /// \brief True if the step should be subtracted.
3517 bool ShouldSubtractStep() const { return SubtractStep; }
3518 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003519 Expr *
3520 BuildNumIterations(Scope *S, const bool LimitedType,
3521 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003522 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003523 Expr *BuildPreCond(Scope *S, Expr *Cond,
3524 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003526 DeclRefExpr *
3527 BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003528 /// \brief Build reference expression to the private counter be used for
3529 /// codegen.
3530 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003531 /// \brief Build initization of the counter be used for codegen.
3532 Expr *BuildCounterInit() const;
3533 /// \brief Build step of the counter be used for codegen.
3534 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003535 /// \brief Return true if any expression is dependent.
3536 bool Dependent() const;
3537
3538private:
3539 /// \brief Check the right-hand side of an assignment in the increment
3540 /// expression.
3541 bool CheckIncRHS(Expr *RHS);
3542 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003543 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003544 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003545 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003546 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003547 /// \brief Helper to set loop increment.
3548 bool SetStep(Expr *NewStep, bool Subtract);
3549};
3550
3551bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003552 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003553 assert(!LB && !UB && !Step);
3554 return false;
3555 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003556 return LCDecl->getType()->isDependentType() ||
3557 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3558 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559}
3560
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003561static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003562 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3563 E = ExprTemp->getSubExpr();
3564
3565 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3566 E = MTE->GetTemporaryExpr();
3567
3568 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3569 E = Binder->getSubExpr();
3570
3571 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3572 E = ICE->getSubExprAsWritten();
3573 return E->IgnoreParens();
3574}
3575
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003576bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3577 Expr *NewLCRefExpr,
3578 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003580 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003581 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003582 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003583 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003584 LCDecl = getCanonicalDecl(NewLCDecl);
3585 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003586 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3587 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003588 if ((Ctor->isCopyOrMoveConstructor() ||
3589 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3590 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003591 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003592 LB = NewLB;
3593 return false;
3594}
3595
3596bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003597 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003598 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003599 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3600 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003601 if (!NewUB)
3602 return true;
3603 UB = NewUB;
3604 TestIsLessOp = LessOp;
3605 TestIsStrictOp = StrictOp;
3606 ConditionSrcRange = SR;
3607 ConditionLoc = SL;
3608 return false;
3609}
3610
3611bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3612 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003613 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003614 if (!NewStep)
3615 return true;
3616 if (!NewStep->isValueDependent()) {
3617 // Check that the step is integer expression.
3618 SourceLocation StepLoc = NewStep->getLocStart();
3619 ExprResult Val =
3620 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3621 if (Val.isInvalid())
3622 return true;
3623 NewStep = Val.get();
3624
3625 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3626 // If test-expr is of form var relational-op b and relational-op is < or
3627 // <= then incr-expr must cause var to increase on each iteration of the
3628 // loop. If test-expr is of form var relational-op b and relational-op is
3629 // > or >= then incr-expr must cause var to decrease on each iteration of
3630 // the loop.
3631 // If test-expr is of form b relational-op var and relational-op is < or
3632 // <= then incr-expr must cause var to decrease on each iteration of the
3633 // loop. If test-expr is of form b relational-op var and relational-op is
3634 // > or >= then incr-expr must cause var to increase on each iteration of
3635 // the loop.
3636 llvm::APSInt Result;
3637 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3638 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3639 bool IsConstNeg =
3640 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003641 bool IsConstPos =
3642 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003643 bool IsConstZero = IsConstant && !Result.getBoolValue();
3644 if (UB && (IsConstZero ||
3645 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003646 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003647 SemaRef.Diag(NewStep->getExprLoc(),
3648 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003649 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003650 SemaRef.Diag(ConditionLoc,
3651 diag::note_omp_loop_cond_requres_compatible_incr)
3652 << TestIsLessOp << ConditionSrcRange;
3653 return true;
3654 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003655 if (TestIsLessOp == Subtract) {
3656 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3657 NewStep).get();
3658 Subtract = !Subtract;
3659 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660 }
3661
3662 Step = NewStep;
3663 SubtractStep = Subtract;
3664 return false;
3665}
3666
Alexey Bataev9c821032015-04-30 04:23:23 +00003667bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003668 // Check init-expr for canonical loop form and save loop counter
3669 // variable - #Var and its initialization value - #LB.
3670 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3671 // var = lb
3672 // integer-type var = lb
3673 // random-access-iterator-type var = lb
3674 // pointer-type var = lb
3675 //
3676 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003677 if (EmitDiags) {
3678 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3679 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003680 return true;
3681 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003682 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003683 if (Expr *E = dyn_cast<Expr>(S))
3684 S = E->IgnoreParens();
3685 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003686 if (BO->getOpcode() == BO_Assign) {
3687 auto *LHS = BO->getLHS()->IgnoreParens();
3688 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3689 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3690 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3691 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3692 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3693 }
3694 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3695 if (ME->isArrow() &&
3696 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3697 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3698 }
3699 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003700 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3701 if (DS->isSingleDecl()) {
3702 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003703 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003704 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003705 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003706 SemaRef.Diag(S->getLocStart(),
3707 diag::ext_omp_loop_not_canonical_init)
3708 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003709 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003710 }
3711 }
3712 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003713 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3714 if (CE->getOperator() == OO_Equal) {
3715 auto *LHS = CE->getArg(0);
3716 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3717 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3718 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3719 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3720 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3721 }
3722 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3723 if (ME->isArrow() &&
3724 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3725 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3726 }
3727 }
3728 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003729
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003730 if (Dependent() || SemaRef.CurContext->isDependentContext())
3731 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003732 if (EmitDiags) {
3733 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3734 << S->getSourceRange();
3735 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003736 return true;
3737}
3738
Alexey Bataev23b69422014-06-18 07:08:49 +00003739/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003740/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003741static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003742 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003743 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003744 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3746 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003747 if ((Ctor->isCopyOrMoveConstructor() ||
3748 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3749 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003750 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003751 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3752 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3753 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3754 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3755 return getCanonicalDecl(ME->getMemberDecl());
3756 return getCanonicalDecl(VD);
3757 }
3758 }
3759 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3760 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3761 return getCanonicalDecl(ME->getMemberDecl());
3762 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003763}
3764
3765bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3766 // Check test-expr for canonical form, save upper-bound UB, flags for
3767 // less/greater and for strict/non-strict comparison.
3768 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3769 // var relational-op b
3770 // b relational-op var
3771 //
3772 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003773 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003774 return true;
3775 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003776 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003777 SourceLocation CondLoc = S->getLocStart();
3778 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3779 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003780 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 return SetUB(BO->getRHS(),
3782 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3783 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3784 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003785 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003786 return SetUB(BO->getLHS(),
3787 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3788 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3789 BO->getSourceRange(), BO->getOperatorLoc());
3790 }
3791 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3792 if (CE->getNumArgs() == 2) {
3793 auto Op = CE->getOperator();
3794 switch (Op) {
3795 case OO_Greater:
3796 case OO_GreaterEqual:
3797 case OO_Less:
3798 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003799 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3801 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3802 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003803 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003804 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3805 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3806 CE->getOperatorLoc());
3807 break;
3808 default:
3809 break;
3810 }
3811 }
3812 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003813 if (Dependent() || SemaRef.CurContext->isDependentContext())
3814 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003815 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003816 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 return true;
3818}
3819
3820bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3821 // RHS of canonical loop form increment can be:
3822 // var + incr
3823 // incr + var
3824 // var - incr
3825 //
3826 RHS = RHS->IgnoreParenImpCasts();
3827 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3828 if (BO->isAdditiveOp()) {
3829 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003830 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003831 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003832 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003833 return SetStep(BO->getLHS(), false);
3834 }
3835 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3836 bool IsAdd = CE->getOperator() == OO_Plus;
3837 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003838 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003839 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003840 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003841 return SetStep(CE->getArg(0), false);
3842 }
3843 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003844 if (Dependent() || SemaRef.CurContext->isDependentContext())
3845 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003846 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003847 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003848 return true;
3849}
3850
3851bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3852 // Check incr-expr for canonical loop form and return true if it
3853 // does not conform.
3854 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3855 // ++var
3856 // var++
3857 // --var
3858 // var--
3859 // var += incr
3860 // var -= incr
3861 // var = var + incr
3862 // var = incr + var
3863 // var = var - incr
3864 //
3865 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003866 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867 return true;
3868 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003869 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 S = S->IgnoreParens();
3871 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003872 if (UO->isIncrementDecrementOp() &&
3873 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003874 return SetStep(
3875 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3876 (UO->isDecrementOp() ? -1 : 1)).get(),
3877 false);
3878 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3879 switch (BO->getOpcode()) {
3880 case BO_AddAssign:
3881 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003882 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3884 break;
3885 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003886 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003887 return CheckIncRHS(BO->getRHS());
3888 break;
3889 default:
3890 break;
3891 }
3892 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3893 switch (CE->getOperator()) {
3894 case OO_PlusPlus:
3895 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003896 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897 return SetStep(
3898 SemaRef.ActOnIntegerConstant(
3899 CE->getLocStart(),
3900 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3901 false);
3902 break;
3903 case OO_PlusEqual:
3904 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003905 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3907 break;
3908 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 return CheckIncRHS(CE->getArg(1));
3911 break;
3912 default:
3913 break;
3914 }
3915 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003916 if (Dependent() || SemaRef.CurContext->isDependentContext())
3917 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920 return true;
3921}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003922
Alexey Bataev5a3af132016-03-29 08:58:54 +00003923static ExprResult
3924tryBuildCapture(Sema &SemaRef, Expr *Capture,
3925 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3926 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3927 return SemaRef.PerformImplicitConversion(
3928 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3929 /*AllowExplicit=*/true);
3930 auto I = Captures.find(Capture);
3931 if (I != Captures.end())
3932 return buildCapture(SemaRef, Capture, I->second);
3933 DeclRefExpr *Ref = nullptr;
3934 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3935 Captures[Capture] = Ref;
3936 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003937}
3938
Alexander Musmana5f070a2014-10-01 06:03:56 +00003939/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003940Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3941 Scope *S, const bool LimitedType,
3942 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003943 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003945 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003946 SemaRef.getLangOpts().CPlusPlus) {
3947 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003948 auto *UBExpr = TestIsLessOp ? UB : LB;
3949 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003950 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3951 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003952 if (!Upper || !Lower)
3953 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954
3955 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3956
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003957 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003958 // BuildBinOp already emitted error, this one is to point user to upper
3959 // and lower bound, and to tell what is passed to 'operator-'.
3960 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3961 << Upper->getSourceRange() << Lower->getSourceRange();
3962 return nullptr;
3963 }
3964 }
3965
3966 if (!Diff.isUsable())
3967 return nullptr;
3968
3969 // Upper - Lower [- 1]
3970 if (TestIsStrictOp)
3971 Diff = SemaRef.BuildBinOp(
3972 S, DefaultLoc, BO_Sub, Diff.get(),
3973 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3974 if (!Diff.isUsable())
3975 return nullptr;
3976
3977 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003978 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3979 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003980 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003981 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 if (!Diff.isUsable())
3983 return nullptr;
3984
3985 // Parentheses (for dumping/debugging purposes only).
3986 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3987 if (!Diff.isUsable())
3988 return nullptr;
3989
3990 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003991 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003992 if (!Diff.isUsable())
3993 return nullptr;
3994
Alexander Musman174b3ca2014-10-06 11:16:29 +00003995 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003996 QualType Type = Diff.get()->getType();
3997 auto &C = SemaRef.Context;
3998 bool UseVarType = VarType->hasIntegerRepresentation() &&
3999 C.getTypeSize(Type) > C.getTypeSize(VarType);
4000 if (!Type->isIntegerType() || UseVarType) {
4001 unsigned NewSize =
4002 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4003 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4004 : Type->hasSignedIntegerRepresentation();
4005 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004006 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4007 Diff = SemaRef.PerformImplicitConversion(
4008 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4009 if (!Diff.isUsable())
4010 return nullptr;
4011 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004012 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004013 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004014 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4015 if (NewSize != C.getTypeSize(Type)) {
4016 if (NewSize < C.getTypeSize(Type)) {
4017 assert(NewSize == 64 && "incorrect loop var size");
4018 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4019 << InitSrcRange << ConditionSrcRange;
4020 }
4021 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004022 NewSize, Type->hasSignedIntegerRepresentation() ||
4023 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004024 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4025 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4026 Sema::AA_Converting, true);
4027 if (!Diff.isUsable())
4028 return nullptr;
4029 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004030 }
4031 }
4032
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 return Diff.get();
4034}
4035
Alexey Bataev5a3af132016-03-29 08:58:54 +00004036Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4037 Scope *S, Expr *Cond,
4038 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004039 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4040 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4041 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004042
Alexey Bataev5a3af132016-03-29 08:58:54 +00004043 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4044 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4045 if (!NewLB.isUsable() || !NewUB.isUsable())
4046 return nullptr;
4047
Alexey Bataev62dbb972015-04-22 11:59:37 +00004048 auto CondExpr = SemaRef.BuildBinOp(
4049 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4050 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004051 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004052 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004053 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4054 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004055 CondExpr = SemaRef.PerformImplicitConversion(
4056 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4057 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004058 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004059 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4060 // Otherwise use original loop conditon and evaluate it in runtime.
4061 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4062}
4063
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004065DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4066 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4067 auto *VD = dyn_cast<VarDecl>(LCDecl);
4068 if (!VD) {
4069 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4070 auto *Ref = buildDeclRefExpr(
4071 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4072 Captures.insert(std::make_pair(LCRef, Ref));
4073 return Ref;
4074 }
4075 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004076 DefaultLoc);
4077}
4078
4079Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004080 if (LCDecl && !LCDecl->isInvalidDecl()) {
4081 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004082 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004083 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4084 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004085 if (PrivateVar->isInvalidDecl())
4086 return nullptr;
4087 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4088 }
4089 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004090}
4091
4092/// \brief Build initization of the counter be used for codegen.
4093Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4094
4095/// \brief Build step of the counter be used for codegen.
4096Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4097
4098/// \brief Iteration space of a single for loop.
4099struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004100 /// \brief Condition of the loop.
4101 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004102 /// \brief This expression calculates the number of iterations in the loop.
4103 /// It is always possible to calculate it before starting the loop.
4104 Expr *NumIterations;
4105 /// \brief The loop counter variable.
4106 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004107 /// \brief Private loop counter variable.
4108 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004109 /// \brief This is initializer for the initial value of #CounterVar.
4110 Expr *CounterInit;
4111 /// \brief This is step for the #CounterVar used to generate its update:
4112 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4113 Expr *CounterStep;
4114 /// \brief Should step be subtracted?
4115 bool Subtract;
4116 /// \brief Source range of the loop init.
4117 SourceRange InitSrcRange;
4118 /// \brief Source range of the loop condition.
4119 SourceRange CondSrcRange;
4120 /// \brief Source range of the loop increment.
4121 SourceRange IncSrcRange;
4122};
4123
Alexey Bataev23b69422014-06-18 07:08:49 +00004124} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004125
Alexey Bataev9c821032015-04-30 04:23:23 +00004126void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4127 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4128 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004129 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4130 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004131 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4132 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004133 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4134 if (auto *D = ISC.GetLoopDecl()) {
4135 auto *VD = dyn_cast<VarDecl>(D);
4136 if (!VD) {
4137 if (auto *Private = IsOpenMPCapturedDecl(D))
4138 VD = Private;
4139 else {
4140 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4141 /*WithInit=*/false);
4142 VD = cast<VarDecl>(Ref->getDecl());
4143 }
4144 }
4145 DSAStack->addLoopControlVariable(D, VD);
4146 }
4147 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004148 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004149 }
4150}
4151
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152/// \brief Called on a for stmt to check and extract its iteration space
4153/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004154static bool CheckOpenMPIterationSpace(
4155 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4156 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004157 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004158 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004159 LoopIterationSpace &ResultIterSpace,
4160 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004161 // OpenMP [2.6, Canonical Loop Form]
4162 // for (init-expr; test-expr; incr-expr) structured-block
4163 auto For = dyn_cast_or_null<ForStmt>(S);
4164 if (!For) {
4165 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004166 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4167 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4168 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4169 if (NestedLoopCount > 1) {
4170 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4171 SemaRef.Diag(DSA.getConstructLoc(),
4172 diag::note_omp_collapse_ordered_expr)
4173 << 2 << CollapseLoopCountExpr->getSourceRange()
4174 << OrderedLoopCountExpr->getSourceRange();
4175 else if (CollapseLoopCountExpr)
4176 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4177 diag::note_omp_collapse_ordered_expr)
4178 << 0 << CollapseLoopCountExpr->getSourceRange();
4179 else
4180 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4181 diag::note_omp_collapse_ordered_expr)
4182 << 1 << OrderedLoopCountExpr->getSourceRange();
4183 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004184 return true;
4185 }
4186 assert(For->getBody());
4187
4188 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4189
4190 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004191 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004192 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004194
4195 bool HasErrors = false;
4196
4197 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004198 if (auto *LCDecl = ISC.GetLoopDecl()) {
4199 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004201 // OpenMP [2.6, Canonical Loop Form]
4202 // Var is one of the following:
4203 // A variable of signed or unsigned integer type.
4204 // For C++, a variable of a random access iterator type.
4205 // For C, a variable of a pointer type.
4206 auto VarType = LCDecl->getType().getNonReferenceType();
4207 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4208 !VarType->isPointerType() &&
4209 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4210 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4211 << SemaRef.getLangOpts().CPlusPlus;
4212 HasErrors = true;
4213 }
4214
4215 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4216 // a Construct
4217 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4218 // parallel for construct is (are) private.
4219 // The loop iteration variable in the associated for-loop of a simd
4220 // construct with just one associated for-loop is linear with a
4221 // constant-linear-step that is the increment of the associated for-loop.
4222 // Exclude loop var from the list of variables with implicitly defined data
4223 // sharing attributes.
4224 VarsWithImplicitDSA.erase(LCDecl);
4225
4226 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4227 // in a Construct, C/C++].
4228 // The loop iteration variable in the associated for-loop of a simd
4229 // construct with just one associated for-loop may be listed in a linear
4230 // clause with a constant-linear-step that is the increment of the
4231 // associated for-loop.
4232 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4233 // parallel for construct may be listed in a private or lastprivate clause.
4234 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4235 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4236 // declared in the loop and it is predetermined as a private.
4237 auto PredeterminedCKind =
4238 isOpenMPSimdDirective(DKind)
4239 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4240 : OMPC_private;
4241 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4242 DVar.CKind != PredeterminedCKind) ||
4243 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4244 isOpenMPDistributeDirective(DKind)) &&
4245 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4246 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4247 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4248 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4249 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4250 << getOpenMPClauseName(PredeterminedCKind);
4251 if (DVar.RefExpr == nullptr)
4252 DVar.CKind = PredeterminedCKind;
4253 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4254 HasErrors = true;
4255 } else if (LoopDeclRefExpr != nullptr) {
4256 // Make the loop iteration variable private (for worksharing constructs),
4257 // linear (for simd directives with the only one associated loop) or
4258 // lastprivate (for simd directives with several collapsed or ordered
4259 // loops).
4260 if (DVar.CKind == OMPC_unknown)
4261 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4262 /*FromParent=*/false);
4263 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4264 }
4265
4266 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4267
4268 // Check test-expr.
4269 HasErrors |= ISC.CheckCond(For->getCond());
4270
4271 // Check incr-expr.
4272 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004273 }
4274
Alexander Musmana5f070a2014-10-01 06:03:56 +00004275 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004276 return HasErrors;
4277
Alexander Musmana5f070a2014-10-01 06:03:56 +00004278 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004279 ResultIterSpace.PreCond =
4280 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004281 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004282 DSA.getCurScope(),
4283 (isOpenMPWorksharingDirective(DKind) ||
4284 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4285 Captures);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004286 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures);
Alexey Bataeva8899172015-08-06 12:30:57 +00004287 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004288 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4289 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4290 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4291 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4292 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4293 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4294
Alexey Bataev62dbb972015-04-22 11:59:37 +00004295 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4296 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004297 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004298 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004299 ResultIterSpace.CounterInit == nullptr ||
4300 ResultIterSpace.CounterStep == nullptr);
4301
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004302 return HasErrors;
4303}
4304
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004305/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004306static ExprResult
4307BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4308 ExprResult Start,
4309 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004310 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004311 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4312 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004313 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004314 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004315 VarRef.get()->getType())) {
4316 NewStart = SemaRef.PerformImplicitConversion(
4317 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4318 /*AllowExplicit=*/true);
4319 if (!NewStart.isUsable())
4320 return ExprError();
4321 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004322
4323 auto Init =
4324 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4325 return Init;
4326}
4327
Alexander Musmana5f070a2014-10-01 06:03:56 +00004328/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004329static ExprResult
4330BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4331 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4332 ExprResult Step, bool Subtract,
4333 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004334 // Add parentheses (for debugging purposes only).
4335 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4336 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4337 !Step.isUsable())
4338 return ExprError();
4339
Alexey Bataev5a3af132016-03-29 08:58:54 +00004340 ExprResult NewStep = Step;
4341 if (Captures)
4342 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004343 if (NewStep.isInvalid())
4344 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004345 ExprResult Update =
4346 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004347 if (!Update.isUsable())
4348 return ExprError();
4349
Alexey Bataevc0214e02016-02-16 12:13:49 +00004350 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4351 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004352 ExprResult NewStart = Start;
4353 if (Captures)
4354 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004355 if (NewStart.isInvalid())
4356 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004357
Alexey Bataevc0214e02016-02-16 12:13:49 +00004358 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4359 ExprResult SavedUpdate = Update;
4360 ExprResult UpdateVal;
4361 if (VarRef.get()->getType()->isOverloadableType() ||
4362 NewStart.get()->getType()->isOverloadableType() ||
4363 Update.get()->getType()->isOverloadableType()) {
4364 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4365 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4366 Update =
4367 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4368 if (Update.isUsable()) {
4369 UpdateVal =
4370 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4371 VarRef.get(), SavedUpdate.get());
4372 if (UpdateVal.isUsable()) {
4373 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4374 UpdateVal.get());
4375 }
4376 }
4377 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4378 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379
Alexey Bataevc0214e02016-02-16 12:13:49 +00004380 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4381 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4382 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4383 NewStart.get(), SavedUpdate.get());
4384 if (!Update.isUsable())
4385 return ExprError();
4386
Alexey Bataev11481f52016-02-17 10:29:05 +00004387 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4388 VarRef.get()->getType())) {
4389 Update = SemaRef.PerformImplicitConversion(
4390 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4391 if (!Update.isUsable())
4392 return ExprError();
4393 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004394
4395 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4396 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004397 return Update;
4398}
4399
4400/// \brief Convert integer expression \a E to make it have at least \a Bits
4401/// bits.
4402static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4403 Sema &SemaRef) {
4404 if (E == nullptr)
4405 return ExprError();
4406 auto &C = SemaRef.Context;
4407 QualType OldType = E->getType();
4408 unsigned HasBits = C.getTypeSize(OldType);
4409 if (HasBits >= Bits)
4410 return ExprResult(E);
4411 // OK to convert to signed, because new type has more bits than old.
4412 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4413 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4414 true);
4415}
4416
4417/// \brief Check if the given expression \a E is a constant integer that fits
4418/// into \a Bits bits.
4419static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4420 if (E == nullptr)
4421 return false;
4422 llvm::APSInt Result;
4423 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4424 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4425 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004426}
4427
Alexey Bataev5a3af132016-03-29 08:58:54 +00004428/// Build preinits statement for the given declarations.
4429static Stmt *buildPreInits(ASTContext &Context,
4430 SmallVectorImpl<Decl *> &PreInits) {
4431 if (!PreInits.empty()) {
4432 return new (Context) DeclStmt(
4433 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4434 SourceLocation(), SourceLocation());
4435 }
4436 return nullptr;
4437}
4438
4439/// Build preinits statement for the given declarations.
4440static Stmt *buildPreInits(ASTContext &Context,
4441 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4442 if (!Captures.empty()) {
4443 SmallVector<Decl *, 16> PreInits;
4444 for (auto &Pair : Captures)
4445 PreInits.push_back(Pair.second->getDecl());
4446 return buildPreInits(Context, PreInits);
4447 }
4448 return nullptr;
4449}
4450
4451/// Build postupdate expression for the given list of postupdates expressions.
4452static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4453 Expr *PostUpdate = nullptr;
4454 if (!PostUpdates.empty()) {
4455 for (auto *E : PostUpdates) {
4456 Expr *ConvE = S.BuildCStyleCastExpr(
4457 E->getExprLoc(),
4458 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4459 E->getExprLoc(), E)
4460 .get();
4461 PostUpdate = PostUpdate
4462 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4463 PostUpdate, ConvE)
4464 .get()
4465 : ConvE;
4466 }
4467 }
4468 return PostUpdate;
4469}
4470
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004471/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004472/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4473/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004474static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004475CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4476 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4477 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004478 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004479 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004480 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004481 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004482 // Found 'collapse' clause - calculate collapse number.
4483 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004484 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004485 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004486 }
4487 if (OrderedLoopCountExpr) {
4488 // Found 'ordered' clause - calculate collapse number.
4489 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004490 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4491 if (Result.getLimitedValue() < NestedLoopCount) {
4492 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4493 diag::err_omp_wrong_ordered_loop_count)
4494 << OrderedLoopCountExpr->getSourceRange();
4495 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4496 diag::note_collapse_loop_count)
4497 << CollapseLoopCountExpr->getSourceRange();
4498 }
4499 NestedLoopCount = Result.getLimitedValue();
4500 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004501 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004502 // This is helper routine for loop directives (e.g., 'for', 'simd',
4503 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004504 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004505 SmallVector<LoopIterationSpace, 4> IterSpaces;
4506 IterSpaces.resize(NestedLoopCount);
4507 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004508 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004509 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004510 NestedLoopCount, CollapseLoopCountExpr,
4511 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004512 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004513 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004514 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004515 // OpenMP [2.8.1, simd construct, Restrictions]
4516 // All loops associated with the construct must be perfectly nested; that
4517 // is, there must be no intervening code nor any OpenMP directive between
4518 // any two loops.
4519 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004520 }
4521
Alexander Musmana5f070a2014-10-01 06:03:56 +00004522 Built.clear(/* size */ NestedLoopCount);
4523
4524 if (SemaRef.CurContext->isDependentContext())
4525 return NestedLoopCount;
4526
4527 // An example of what is generated for the following code:
4528 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004529 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004530 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004531 // for (k = 0; k < NK; ++k)
4532 // for (j = J0; j < NJ; j+=2) {
4533 // <loop body>
4534 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004535 //
4536 // We generate the code below.
4537 // Note: the loop body may be outlined in CodeGen.
4538 // Note: some counters may be C++ classes, operator- is used to find number of
4539 // iterations and operator+= to calculate counter value.
4540 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4541 // or i64 is currently supported).
4542 //
4543 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4544 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4545 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4546 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4547 // // similar updates for vars in clauses (e.g. 'linear')
4548 // <loop body (using local i and j)>
4549 // }
4550 // i = NI; // assign final values of counters
4551 // j = NJ;
4552 //
4553
4554 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4555 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004556 // Precondition tests if there is at least one iteration (all conditions are
4557 // true).
4558 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004559 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004560 ExprResult LastIteration32 = WidenIterationCount(
4561 32 /* Bits */, SemaRef.PerformImplicitConversion(
4562 N0->IgnoreImpCasts(), N0->getType(),
4563 Sema::AA_Converting, /*AllowExplicit=*/true)
4564 .get(),
4565 SemaRef);
4566 ExprResult LastIteration64 = WidenIterationCount(
4567 64 /* Bits */, SemaRef.PerformImplicitConversion(
4568 N0->IgnoreImpCasts(), N0->getType(),
4569 Sema::AA_Converting, /*AllowExplicit=*/true)
4570 .get(),
4571 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004572
4573 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4574 return NestedLoopCount;
4575
4576 auto &C = SemaRef.Context;
4577 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4578
4579 Scope *CurScope = DSA.getCurScope();
4580 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004581 if (PreCond.isUsable()) {
4582 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4583 PreCond.get(), IterSpaces[Cnt].PreCond);
4584 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004585 auto N = IterSpaces[Cnt].NumIterations;
4586 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4587 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004588 LastIteration32 = SemaRef.BuildBinOp(
4589 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4590 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4591 Sema::AA_Converting,
4592 /*AllowExplicit=*/true)
4593 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004594 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004595 LastIteration64 = SemaRef.BuildBinOp(
4596 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4597 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4598 Sema::AA_Converting,
4599 /*AllowExplicit=*/true)
4600 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004601 }
4602
4603 // Choose either the 32-bit or 64-bit version.
4604 ExprResult LastIteration = LastIteration64;
4605 if (LastIteration32.isUsable() &&
4606 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4607 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4608 FitsInto(
4609 32 /* Bits */,
4610 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4611 LastIteration64.get(), SemaRef)))
4612 LastIteration = LastIteration32;
4613
4614 if (!LastIteration.isUsable())
4615 return 0;
4616
4617 // Save the number of iterations.
4618 ExprResult NumIterations = LastIteration;
4619 {
4620 LastIteration = SemaRef.BuildBinOp(
4621 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4622 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4623 if (!LastIteration.isUsable())
4624 return 0;
4625 }
4626
4627 // Calculate the last iteration number beforehand instead of doing this on
4628 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4629 llvm::APSInt Result;
4630 bool IsConstant =
4631 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4632 ExprResult CalcLastIteration;
4633 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004634 ExprResult SaveRef =
4635 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004636 LastIteration = SaveRef;
4637
4638 // Prepare SaveRef + 1.
4639 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004640 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004641 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4642 if (!NumIterations.isUsable())
4643 return 0;
4644 }
4645
4646 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4647
Alexander Musmanc6388682014-12-15 07:07:06 +00004648 QualType VType = LastIteration.get()->getType();
4649 // Build variables passed into runtime, nesessary for worksharing directives.
4650 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004651 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4652 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004653 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004654 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4655 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004656 SemaRef.AddInitializerToDecl(
4657 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4658 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4659
4660 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004661 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4662 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004663 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4664 /*DirectInit*/ false,
4665 /*TypeMayContainAuto*/ false);
4666
4667 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4668 // This will be used to implement clause 'lastprivate'.
4669 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004670 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4671 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004672 SemaRef.AddInitializerToDecl(
4673 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4674 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4675
4676 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004677 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4678 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004679 SemaRef.AddInitializerToDecl(
4680 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4681 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4682
4683 // Build expression: UB = min(UB, LastIteration)
4684 // It is nesessary for CodeGen of directives with static scheduling.
4685 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4686 UB.get(), LastIteration.get());
4687 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4688 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4689 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4690 CondOp.get());
4691 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4692 }
4693
4694 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004695 ExprResult IV;
4696 ExprResult Init;
4697 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004698 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4699 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004700 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004701 isOpenMPTaskLoopDirective(DKind) ||
4702 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004703 ? LB.get()
4704 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4705 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4706 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004707 }
4708
Alexander Musmanc6388682014-12-15 07:07:06 +00004709 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004710 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004711 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004712 (isOpenMPWorksharingDirective(DKind) ||
4713 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004714 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4715 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4716 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004717
4718 // Loop increment (IV = IV + 1)
4719 SourceLocation IncLoc;
4720 ExprResult Inc =
4721 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4722 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4723 if (!Inc.isUsable())
4724 return 0;
4725 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004726 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4727 if (!Inc.isUsable())
4728 return 0;
4729
4730 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4731 // Used for directives with static scheduling.
4732 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004733 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4734 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004735 // LB + ST
4736 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4737 if (!NextLB.isUsable())
4738 return 0;
4739 // LB = LB + ST
4740 NextLB =
4741 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4742 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4743 if (!NextLB.isUsable())
4744 return 0;
4745 // UB + ST
4746 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4747 if (!NextUB.isUsable())
4748 return 0;
4749 // UB = UB + ST
4750 NextUB =
4751 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4752 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4753 if (!NextUB.isUsable())
4754 return 0;
4755 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004756
4757 // Build updates and final values of the loop counters.
4758 bool HasErrors = false;
4759 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004760 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004761 Built.Updates.resize(NestedLoopCount);
4762 Built.Finals.resize(NestedLoopCount);
4763 {
4764 ExprResult Div;
4765 // Go from inner nested loop to outer.
4766 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4767 LoopIterationSpace &IS = IterSpaces[Cnt];
4768 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4769 // Build: Iter = (IV / Div) % IS.NumIters
4770 // where Div is product of previous iterations' IS.NumIters.
4771 ExprResult Iter;
4772 if (Div.isUsable()) {
4773 Iter =
4774 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4775 } else {
4776 Iter = IV;
4777 assert((Cnt == (int)NestedLoopCount - 1) &&
4778 "unusable div expected on first iteration only");
4779 }
4780
4781 if (Cnt != 0 && Iter.isUsable())
4782 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4783 IS.NumIterations);
4784 if (!Iter.isUsable()) {
4785 HasErrors = true;
4786 break;
4787 }
4788
Alexey Bataev39f915b82015-05-08 10:41:21 +00004789 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4790 auto *CounterVar = buildDeclRefExpr(
4791 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
4792 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
4793 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004794 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004795 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004796 if (!Init.isUsable()) {
4797 HasErrors = true;
4798 break;
4799 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004800 ExprResult Update = BuildCounterUpdate(
4801 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4802 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004803 if (!Update.isUsable()) {
4804 HasErrors = true;
4805 break;
4806 }
4807
4808 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4809 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004810 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004811 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004812 if (!Final.isUsable()) {
4813 HasErrors = true;
4814 break;
4815 }
4816
4817 // Build Div for the next iteration: Div <- Div * IS.NumIters
4818 if (Cnt != 0) {
4819 if (Div.isUnset())
4820 Div = IS.NumIterations;
4821 else
4822 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4823 IS.NumIterations);
4824
4825 // Add parentheses (for debugging purposes only).
4826 if (Div.isUsable())
4827 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4828 if (!Div.isUsable()) {
4829 HasErrors = true;
4830 break;
4831 }
4832 }
4833 if (!Update.isUsable() || !Final.isUsable()) {
4834 HasErrors = true;
4835 break;
4836 }
4837 // Save results
4838 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004839 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004840 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004841 Built.Updates[Cnt] = Update.get();
4842 Built.Finals[Cnt] = Final.get();
4843 }
4844 }
4845
4846 if (HasErrors)
4847 return 0;
4848
4849 // Save results
4850 Built.IterationVarRef = IV.get();
4851 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004852 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004853 Built.CalcLastIteration =
4854 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004855 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004856 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004857 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004858 Built.Init = Init.get();
4859 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004860 Built.LB = LB.get();
4861 Built.UB = UB.get();
4862 Built.IL = IL.get();
4863 Built.ST = ST.get();
4864 Built.EUB = EUB.get();
4865 Built.NLB = NextLB.get();
4866 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867
Alexey Bataevabfc0692014-06-25 06:52:00 +00004868 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004869}
4870
Alexey Bataev10e775f2015-07-30 11:36:16 +00004871static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004872 auto CollapseClauses =
4873 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4874 if (CollapseClauses.begin() != CollapseClauses.end())
4875 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004876 return nullptr;
4877}
4878
Alexey Bataev10e775f2015-07-30 11:36:16 +00004879static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004880 auto OrderedClauses =
4881 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4882 if (OrderedClauses.begin() != OrderedClauses.end())
4883 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004884 return nullptr;
4885}
4886
Alexey Bataev66b15b52015-08-21 11:14:16 +00004887static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4888 const Expr *Safelen) {
4889 llvm::APSInt SimdlenRes, SafelenRes;
4890 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4891 Simdlen->isInstantiationDependent() ||
4892 Simdlen->containsUnexpandedParameterPack())
4893 return false;
4894 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4895 Safelen->isInstantiationDependent() ||
4896 Safelen->containsUnexpandedParameterPack())
4897 return false;
4898 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4899 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4900 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4901 // If both simdlen and safelen clauses are specified, the value of the simdlen
4902 // parameter must be less than or equal to the value of the safelen parameter.
4903 if (SimdlenRes > SafelenRes) {
4904 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4905 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4906 return true;
4907 }
4908 return false;
4909}
4910
Alexey Bataev4acb8592014-07-07 13:01:15 +00004911StmtResult Sema::ActOnOpenMPSimdDirective(
4912 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4913 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004914 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004915 if (!AStmt)
4916 return StmtError();
4917
4918 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004919 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004920 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4921 // define the nested loops number.
4922 unsigned NestedLoopCount = CheckOpenMPLoop(
4923 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4924 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004925 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004926 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004927
Alexander Musmana5f070a2014-10-01 06:03:56 +00004928 assert((CurContext->isDependentContext() || B.builtAll()) &&
4929 "omp simd loop exprs were not built");
4930
Alexander Musman3276a272015-03-21 10:12:56 +00004931 if (!CurContext->isDependentContext()) {
4932 // Finalize the clauses that need pre-built expressions for CodeGen.
4933 for (auto C : Clauses) {
4934 if (auto LC = dyn_cast<OMPLinearClause>(C))
4935 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4936 B.NumIterations, *this, CurScope))
4937 return StmtError();
4938 }
4939 }
4940
Alexey Bataev66b15b52015-08-21 11:14:16 +00004941 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4942 // If both simdlen and safelen clauses are specified, the value of the simdlen
4943 // parameter must be less than or equal to the value of the safelen parameter.
4944 OMPSafelenClause *Safelen = nullptr;
4945 OMPSimdlenClause *Simdlen = nullptr;
4946 for (auto *Clause : Clauses) {
4947 if (Clause->getClauseKind() == OMPC_safelen)
4948 Safelen = cast<OMPSafelenClause>(Clause);
4949 else if (Clause->getClauseKind() == OMPC_simdlen)
4950 Simdlen = cast<OMPSimdlenClause>(Clause);
4951 if (Safelen && Simdlen)
4952 break;
4953 }
4954 if (Simdlen && Safelen &&
4955 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4956 Safelen->getSafelen()))
4957 return StmtError();
4958
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004959 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004960 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4961 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004962}
4963
Alexey Bataev4acb8592014-07-07 13:01:15 +00004964StmtResult Sema::ActOnOpenMPForDirective(
4965 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4966 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004967 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004968 if (!AStmt)
4969 return StmtError();
4970
4971 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004972 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004973 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4974 // define the nested loops number.
4975 unsigned NestedLoopCount = CheckOpenMPLoop(
4976 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4977 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004978 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004979 return StmtError();
4980
Alexander Musmana5f070a2014-10-01 06:03:56 +00004981 assert((CurContext->isDependentContext() || B.builtAll()) &&
4982 "omp for loop exprs were not built");
4983
Alexey Bataev54acd402015-08-04 11:18:19 +00004984 if (!CurContext->isDependentContext()) {
4985 // Finalize the clauses that need pre-built expressions for CodeGen.
4986 for (auto C : Clauses) {
4987 if (auto LC = dyn_cast<OMPLinearClause>(C))
4988 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4989 B.NumIterations, *this, CurScope))
4990 return StmtError();
4991 }
4992 }
4993
Alexey Bataevf29276e2014-06-18 04:14:57 +00004994 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004995 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004996 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004997}
4998
Alexander Musmanf82886e2014-09-18 05:12:34 +00004999StmtResult Sema::ActOnOpenMPForSimdDirective(
5000 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5001 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005002 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005003 if (!AStmt)
5004 return StmtError();
5005
5006 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005007 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005008 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5009 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005010 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005011 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5012 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5013 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005014 if (NestedLoopCount == 0)
5015 return StmtError();
5016
Alexander Musmanc6388682014-12-15 07:07:06 +00005017 assert((CurContext->isDependentContext() || B.builtAll()) &&
5018 "omp for simd loop exprs were not built");
5019
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005020 if (!CurContext->isDependentContext()) {
5021 // Finalize the clauses that need pre-built expressions for CodeGen.
5022 for (auto C : Clauses) {
5023 if (auto LC = dyn_cast<OMPLinearClause>(C))
5024 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5025 B.NumIterations, *this, CurScope))
5026 return StmtError();
5027 }
5028 }
5029
Alexey Bataev66b15b52015-08-21 11:14:16 +00005030 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5031 // If both simdlen and safelen clauses are specified, the value of the simdlen
5032 // parameter must be less than or equal to the value of the safelen parameter.
5033 OMPSafelenClause *Safelen = nullptr;
5034 OMPSimdlenClause *Simdlen = nullptr;
5035 for (auto *Clause : Clauses) {
5036 if (Clause->getClauseKind() == OMPC_safelen)
5037 Safelen = cast<OMPSafelenClause>(Clause);
5038 else if (Clause->getClauseKind() == OMPC_simdlen)
5039 Simdlen = cast<OMPSimdlenClause>(Clause);
5040 if (Safelen && Simdlen)
5041 break;
5042 }
5043 if (Simdlen && Safelen &&
5044 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5045 Safelen->getSafelen()))
5046 return StmtError();
5047
Alexander Musmanf82886e2014-09-18 05:12:34 +00005048 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005049 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5050 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005051}
5052
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005053StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5054 Stmt *AStmt,
5055 SourceLocation StartLoc,
5056 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005057 if (!AStmt)
5058 return StmtError();
5059
5060 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005061 auto BaseStmt = AStmt;
5062 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5063 BaseStmt = CS->getCapturedStmt();
5064 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5065 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005066 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005067 return StmtError();
5068 // All associated statements must be '#pragma omp section' except for
5069 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005070 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005071 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5072 if (SectionStmt)
5073 Diag(SectionStmt->getLocStart(),
5074 diag::err_omp_sections_substmt_not_section);
5075 return StmtError();
5076 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005077 cast<OMPSectionDirective>(SectionStmt)
5078 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005079 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005080 } else {
5081 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5082 return StmtError();
5083 }
5084
5085 getCurFunction()->setHasBranchProtectedScope();
5086
Alexey Bataev25e5b442015-09-15 12:52:43 +00005087 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5088 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005089}
5090
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005091StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5092 SourceLocation StartLoc,
5093 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005094 if (!AStmt)
5095 return StmtError();
5096
5097 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005098
5099 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005100 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005101
Alexey Bataev25e5b442015-09-15 12:52:43 +00005102 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5103 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005104}
5105
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005106StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5107 Stmt *AStmt,
5108 SourceLocation StartLoc,
5109 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005110 if (!AStmt)
5111 return StmtError();
5112
5113 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005114
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005115 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005116
Alexey Bataev3255bf32015-01-19 05:20:46 +00005117 // OpenMP [2.7.3, single Construct, Restrictions]
5118 // The copyprivate clause must not be used with the nowait clause.
5119 OMPClause *Nowait = nullptr;
5120 OMPClause *Copyprivate = nullptr;
5121 for (auto *Clause : Clauses) {
5122 if (Clause->getClauseKind() == OMPC_nowait)
5123 Nowait = Clause;
5124 else if (Clause->getClauseKind() == OMPC_copyprivate)
5125 Copyprivate = Clause;
5126 if (Copyprivate && Nowait) {
5127 Diag(Copyprivate->getLocStart(),
5128 diag::err_omp_single_copyprivate_with_nowait);
5129 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5130 return StmtError();
5131 }
5132 }
5133
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005134 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5135}
5136
Alexander Musman80c22892014-07-17 08:54:58 +00005137StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5138 SourceLocation StartLoc,
5139 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005140 if (!AStmt)
5141 return StmtError();
5142
5143 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005144
5145 getCurFunction()->setHasBranchProtectedScope();
5146
5147 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5148}
5149
Alexey Bataev28c75412015-12-15 08:19:24 +00005150StmtResult Sema::ActOnOpenMPCriticalDirective(
5151 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5152 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005153 if (!AStmt)
5154 return StmtError();
5155
5156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005157
Alexey Bataev28c75412015-12-15 08:19:24 +00005158 bool ErrorFound = false;
5159 llvm::APSInt Hint;
5160 SourceLocation HintLoc;
5161 bool DependentHint = false;
5162 for (auto *C : Clauses) {
5163 if (C->getClauseKind() == OMPC_hint) {
5164 if (!DirName.getName()) {
5165 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5166 ErrorFound = true;
5167 }
5168 Expr *E = cast<OMPHintClause>(C)->getHint();
5169 if (E->isTypeDependent() || E->isValueDependent() ||
5170 E->isInstantiationDependent())
5171 DependentHint = true;
5172 else {
5173 Hint = E->EvaluateKnownConstInt(Context);
5174 HintLoc = C->getLocStart();
5175 }
5176 }
5177 }
5178 if (ErrorFound)
5179 return StmtError();
5180 auto Pair = DSAStack->getCriticalWithHint(DirName);
5181 if (Pair.first && DirName.getName() && !DependentHint) {
5182 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5183 Diag(StartLoc, diag::err_omp_critical_with_hint);
5184 if (HintLoc.isValid()) {
5185 Diag(HintLoc, diag::note_omp_critical_hint_here)
5186 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5187 } else
5188 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5189 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5190 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5191 << 1
5192 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5193 /*Radix=*/10, /*Signed=*/false);
5194 } else
5195 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5196 }
5197 }
5198
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005199 getCurFunction()->setHasBranchProtectedScope();
5200
Alexey Bataev28c75412015-12-15 08:19:24 +00005201 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5202 Clauses, AStmt);
5203 if (!Pair.first && DirName.getName() && !DependentHint)
5204 DSAStack->addCriticalWithHint(Dir, Hint);
5205 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005206}
5207
Alexey Bataev4acb8592014-07-07 13:01:15 +00005208StmtResult Sema::ActOnOpenMPParallelForDirective(
5209 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5210 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005211 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005212 if (!AStmt)
5213 return StmtError();
5214
Alexey Bataev4acb8592014-07-07 13:01:15 +00005215 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5216 // 1.2.2 OpenMP Language Terminology
5217 // Structured block - An executable statement with a single entry at the
5218 // top and a single exit at the bottom.
5219 // The point of exit cannot be a branch out of the structured block.
5220 // longjmp() and throw() must not violate the entry/exit criteria.
5221 CS->getCapturedDecl()->setNothrow();
5222
Alexander Musmanc6388682014-12-15 07:07:06 +00005223 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005224 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5225 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005226 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005227 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5228 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5229 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005230 if (NestedLoopCount == 0)
5231 return StmtError();
5232
Alexander Musmana5f070a2014-10-01 06:03:56 +00005233 assert((CurContext->isDependentContext() || B.builtAll()) &&
5234 "omp parallel for loop exprs were not built");
5235
Alexey Bataev54acd402015-08-04 11:18:19 +00005236 if (!CurContext->isDependentContext()) {
5237 // Finalize the clauses that need pre-built expressions for CodeGen.
5238 for (auto C : Clauses) {
5239 if (auto LC = dyn_cast<OMPLinearClause>(C))
5240 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5241 B.NumIterations, *this, CurScope))
5242 return StmtError();
5243 }
5244 }
5245
Alexey Bataev4acb8592014-07-07 13:01:15 +00005246 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005247 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005248 NestedLoopCount, Clauses, AStmt, B,
5249 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005250}
5251
Alexander Musmane4e893b2014-09-23 09:33:00 +00005252StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5253 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5254 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005255 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005256 if (!AStmt)
5257 return StmtError();
5258
Alexander Musmane4e893b2014-09-23 09:33:00 +00005259 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5260 // 1.2.2 OpenMP Language Terminology
5261 // Structured block - An executable statement with a single entry at the
5262 // top and a single exit at the bottom.
5263 // The point of exit cannot be a branch out of the structured block.
5264 // longjmp() and throw() must not violate the entry/exit criteria.
5265 CS->getCapturedDecl()->setNothrow();
5266
Alexander Musmanc6388682014-12-15 07:07:06 +00005267 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005268 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5269 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005270 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005271 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5272 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5273 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005274 if (NestedLoopCount == 0)
5275 return StmtError();
5276
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005277 if (!CurContext->isDependentContext()) {
5278 // Finalize the clauses that need pre-built expressions for CodeGen.
5279 for (auto C : Clauses) {
5280 if (auto LC = dyn_cast<OMPLinearClause>(C))
5281 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5282 B.NumIterations, *this, CurScope))
5283 return StmtError();
5284 }
5285 }
5286
Alexey Bataev66b15b52015-08-21 11:14:16 +00005287 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5288 // If both simdlen and safelen clauses are specified, the value of the simdlen
5289 // parameter must be less than or equal to the value of the safelen parameter.
5290 OMPSafelenClause *Safelen = nullptr;
5291 OMPSimdlenClause *Simdlen = nullptr;
5292 for (auto *Clause : Clauses) {
5293 if (Clause->getClauseKind() == OMPC_safelen)
5294 Safelen = cast<OMPSafelenClause>(Clause);
5295 else if (Clause->getClauseKind() == OMPC_simdlen)
5296 Simdlen = cast<OMPSimdlenClause>(Clause);
5297 if (Safelen && Simdlen)
5298 break;
5299 }
5300 if (Simdlen && Safelen &&
5301 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5302 Safelen->getSafelen()))
5303 return StmtError();
5304
Alexander Musmane4e893b2014-09-23 09:33:00 +00005305 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005306 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005307 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005308}
5309
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005310StmtResult
5311Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5312 Stmt *AStmt, SourceLocation StartLoc,
5313 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005314 if (!AStmt)
5315 return StmtError();
5316
5317 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005318 auto BaseStmt = AStmt;
5319 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5320 BaseStmt = CS->getCapturedStmt();
5321 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5322 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005323 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005324 return StmtError();
5325 // All associated statements must be '#pragma omp section' except for
5326 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005327 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005328 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5329 if (SectionStmt)
5330 Diag(SectionStmt->getLocStart(),
5331 diag::err_omp_parallel_sections_substmt_not_section);
5332 return StmtError();
5333 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005334 cast<OMPSectionDirective>(SectionStmt)
5335 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005336 }
5337 } else {
5338 Diag(AStmt->getLocStart(),
5339 diag::err_omp_parallel_sections_not_compound_stmt);
5340 return StmtError();
5341 }
5342
5343 getCurFunction()->setHasBranchProtectedScope();
5344
Alexey Bataev25e5b442015-09-15 12:52:43 +00005345 return OMPParallelSectionsDirective::Create(
5346 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005347}
5348
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005349StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5350 Stmt *AStmt, SourceLocation StartLoc,
5351 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005352 if (!AStmt)
5353 return StmtError();
5354
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005355 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5356 // 1.2.2 OpenMP Language Terminology
5357 // Structured block - An executable statement with a single entry at the
5358 // top and a single exit at the bottom.
5359 // The point of exit cannot be a branch out of the structured block.
5360 // longjmp() and throw() must not violate the entry/exit criteria.
5361 CS->getCapturedDecl()->setNothrow();
5362
5363 getCurFunction()->setHasBranchProtectedScope();
5364
Alexey Bataev25e5b442015-09-15 12:52:43 +00005365 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5366 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005367}
5368
Alexey Bataev68446b72014-07-18 07:47:19 +00005369StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5370 SourceLocation EndLoc) {
5371 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5372}
5373
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005374StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5375 SourceLocation EndLoc) {
5376 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5377}
5378
Alexey Bataev2df347a2014-07-18 10:17:07 +00005379StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5380 SourceLocation EndLoc) {
5381 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5382}
5383
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005384StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5385 SourceLocation StartLoc,
5386 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005387 if (!AStmt)
5388 return StmtError();
5389
5390 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005391
5392 getCurFunction()->setHasBranchProtectedScope();
5393
5394 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5395}
5396
Alexey Bataev6125da92014-07-21 11:26:11 +00005397StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5398 SourceLocation StartLoc,
5399 SourceLocation EndLoc) {
5400 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5401 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5402}
5403
Alexey Bataev346265e2015-09-25 10:37:12 +00005404StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5405 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005406 SourceLocation StartLoc,
5407 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005408 OMPClause *DependFound = nullptr;
5409 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005410 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005411 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005412 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005413 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005414 for (auto *C : Clauses) {
5415 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5416 DependFound = C;
5417 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5418 if (DependSourceClause) {
5419 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5420 << getOpenMPDirectiveName(OMPD_ordered)
5421 << getOpenMPClauseName(OMPC_depend) << 2;
5422 ErrorFound = true;
5423 } else
5424 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005425 if (DependSinkClause) {
5426 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5427 << 0;
5428 ErrorFound = true;
5429 }
5430 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5431 if (DependSourceClause) {
5432 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5433 << 1;
5434 ErrorFound = true;
5435 }
5436 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005437 }
5438 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005439 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005440 else if (C->getClauseKind() == OMPC_simd)
5441 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005442 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005443 if (!ErrorFound && !SC &&
5444 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005445 // OpenMP [2.8.1,simd Construct, Restrictions]
5446 // An ordered construct with the simd clause is the only OpenMP construct
5447 // that can appear in the simd region.
5448 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005449 ErrorFound = true;
5450 } else if (DependFound && (TC || SC)) {
5451 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5452 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5453 ErrorFound = true;
5454 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5455 Diag(DependFound->getLocStart(),
5456 diag::err_omp_ordered_directive_without_param);
5457 ErrorFound = true;
5458 } else if (TC || Clauses.empty()) {
5459 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5460 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5461 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5462 << (TC != nullptr);
5463 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5464 ErrorFound = true;
5465 }
5466 }
5467 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005468 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005469
5470 if (AStmt) {
5471 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5472
5473 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005474 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005475
5476 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005477}
5478
Alexey Bataev1d160b12015-03-13 12:27:31 +00005479namespace {
5480/// \brief Helper class for checking expression in 'omp atomic [update]'
5481/// construct.
5482class OpenMPAtomicUpdateChecker {
5483 /// \brief Error results for atomic update expressions.
5484 enum ExprAnalysisErrorCode {
5485 /// \brief A statement is not an expression statement.
5486 NotAnExpression,
5487 /// \brief Expression is not builtin binary or unary operation.
5488 NotABinaryOrUnaryExpression,
5489 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5490 NotAnUnaryIncDecExpression,
5491 /// \brief An expression is not of scalar type.
5492 NotAScalarType,
5493 /// \brief A binary operation is not an assignment operation.
5494 NotAnAssignmentOp,
5495 /// \brief RHS part of the binary operation is not a binary expression.
5496 NotABinaryExpression,
5497 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5498 /// expression.
5499 NotABinaryOperator,
5500 /// \brief RHS binary operation does not have reference to the updated LHS
5501 /// part.
5502 NotAnUpdateExpression,
5503 /// \brief No errors is found.
5504 NoError
5505 };
5506 /// \brief Reference to Sema.
5507 Sema &SemaRef;
5508 /// \brief A location for note diagnostics (when error is found).
5509 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005510 /// \brief 'x' lvalue part of the source atomic expression.
5511 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005512 /// \brief 'expr' rvalue part of the source atomic expression.
5513 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005514 /// \brief Helper expression of the form
5515 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5516 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5517 Expr *UpdateExpr;
5518 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5519 /// important for non-associative operations.
5520 bool IsXLHSInRHSPart;
5521 BinaryOperatorKind Op;
5522 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005523 /// \brief true if the source expression is a postfix unary operation, false
5524 /// if it is a prefix unary operation.
5525 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005526
5527public:
5528 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005529 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005530 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005531 /// \brief Check specified statement that it is suitable for 'atomic update'
5532 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005533 /// expression. If DiagId and NoteId == 0, then only check is performed
5534 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005535 /// \param DiagId Diagnostic which should be emitted if error is found.
5536 /// \param NoteId Diagnostic note for the main error message.
5537 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005538 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005539 /// \brief Return the 'x' lvalue part of the source atomic expression.
5540 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005541 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5542 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005543 /// \brief Return the update expression used in calculation of the updated
5544 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5545 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5546 Expr *getUpdateExpr() const { return UpdateExpr; }
5547 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5548 /// false otherwise.
5549 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5550
Alexey Bataevb78ca832015-04-01 03:33:17 +00005551 /// \brief true if the source expression is a postfix unary operation, false
5552 /// if it is a prefix unary operation.
5553 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5554
Alexey Bataev1d160b12015-03-13 12:27:31 +00005555private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005556 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5557 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005558};
5559} // namespace
5560
5561bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5562 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5563 ExprAnalysisErrorCode ErrorFound = NoError;
5564 SourceLocation ErrorLoc, NoteLoc;
5565 SourceRange ErrorRange, NoteRange;
5566 // Allowed constructs are:
5567 // x = x binop expr;
5568 // x = expr binop x;
5569 if (AtomicBinOp->getOpcode() == BO_Assign) {
5570 X = AtomicBinOp->getLHS();
5571 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5572 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5573 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5574 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5575 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005576 Op = AtomicInnerBinOp->getOpcode();
5577 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005578 auto *LHS = AtomicInnerBinOp->getLHS();
5579 auto *RHS = AtomicInnerBinOp->getRHS();
5580 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5581 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5582 /*Canonical=*/true);
5583 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5584 /*Canonical=*/true);
5585 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5586 /*Canonical=*/true);
5587 if (XId == LHSId) {
5588 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005589 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005590 } else if (XId == RHSId) {
5591 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005592 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005593 } else {
5594 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5595 ErrorRange = AtomicInnerBinOp->getSourceRange();
5596 NoteLoc = X->getExprLoc();
5597 NoteRange = X->getSourceRange();
5598 ErrorFound = NotAnUpdateExpression;
5599 }
5600 } else {
5601 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5602 ErrorRange = AtomicInnerBinOp->getSourceRange();
5603 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5604 NoteRange = SourceRange(NoteLoc, NoteLoc);
5605 ErrorFound = NotABinaryOperator;
5606 }
5607 } else {
5608 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5609 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5610 ErrorFound = NotABinaryExpression;
5611 }
5612 } else {
5613 ErrorLoc = AtomicBinOp->getExprLoc();
5614 ErrorRange = AtomicBinOp->getSourceRange();
5615 NoteLoc = AtomicBinOp->getOperatorLoc();
5616 NoteRange = SourceRange(NoteLoc, NoteLoc);
5617 ErrorFound = NotAnAssignmentOp;
5618 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005619 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005620 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5621 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5622 return true;
5623 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005624 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005625 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005626}
5627
5628bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5629 unsigned NoteId) {
5630 ExprAnalysisErrorCode ErrorFound = NoError;
5631 SourceLocation ErrorLoc, NoteLoc;
5632 SourceRange ErrorRange, NoteRange;
5633 // Allowed constructs are:
5634 // x++;
5635 // x--;
5636 // ++x;
5637 // --x;
5638 // x binop= expr;
5639 // x = x binop expr;
5640 // x = expr binop x;
5641 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5642 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5643 if (AtomicBody->getType()->isScalarType() ||
5644 AtomicBody->isInstantiationDependent()) {
5645 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5646 AtomicBody->IgnoreParenImpCasts())) {
5647 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005648 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005649 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005650 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005651 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005652 X = AtomicCompAssignOp->getLHS();
5653 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005654 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5655 AtomicBody->IgnoreParenImpCasts())) {
5656 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005657 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5658 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005659 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005660 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5661 // Check for Unary Operation
5662 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005663 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005664 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5665 OpLoc = AtomicUnaryOp->getOperatorLoc();
5666 X = AtomicUnaryOp->getSubExpr();
5667 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5668 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005669 } else {
5670 ErrorFound = NotAnUnaryIncDecExpression;
5671 ErrorLoc = AtomicUnaryOp->getExprLoc();
5672 ErrorRange = AtomicUnaryOp->getSourceRange();
5673 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5674 NoteRange = SourceRange(NoteLoc, NoteLoc);
5675 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005676 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005677 ErrorFound = NotABinaryOrUnaryExpression;
5678 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5679 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5680 }
5681 } else {
5682 ErrorFound = NotAScalarType;
5683 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5684 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5685 }
5686 } else {
5687 ErrorFound = NotAnExpression;
5688 NoteLoc = ErrorLoc = S->getLocStart();
5689 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5690 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005691 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005692 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5693 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5694 return true;
5695 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005696 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005697 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005698 // Build an update expression of form 'OpaqueValueExpr(x) binop
5699 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5700 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5701 auto *OVEX = new (SemaRef.getASTContext())
5702 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5703 auto *OVEExpr = new (SemaRef.getASTContext())
5704 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5705 auto Update =
5706 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5707 IsXLHSInRHSPart ? OVEExpr : OVEX);
5708 if (Update.isInvalid())
5709 return true;
5710 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5711 Sema::AA_Casting);
5712 if (Update.isInvalid())
5713 return true;
5714 UpdateExpr = Update.get();
5715 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005716 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005717}
5718
Alexey Bataev0162e452014-07-22 10:10:35 +00005719StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5720 Stmt *AStmt,
5721 SourceLocation StartLoc,
5722 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005723 if (!AStmt)
5724 return StmtError();
5725
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005726 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005727 // 1.2.2 OpenMP Language Terminology
5728 // Structured block - An executable statement with a single entry at the
5729 // top and a single exit at the bottom.
5730 // The point of exit cannot be a branch out of the structured block.
5731 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005732 OpenMPClauseKind AtomicKind = OMPC_unknown;
5733 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005734 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005735 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005736 C->getClauseKind() == OMPC_update ||
5737 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005738 if (AtomicKind != OMPC_unknown) {
5739 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5740 << SourceRange(C->getLocStart(), C->getLocEnd());
5741 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5742 << getOpenMPClauseName(AtomicKind);
5743 } else {
5744 AtomicKind = C->getClauseKind();
5745 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005746 }
5747 }
5748 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005749
Alexey Bataev459dec02014-07-24 06:46:57 +00005750 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005751 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5752 Body = EWC->getSubExpr();
5753
Alexey Bataev62cec442014-11-18 10:14:22 +00005754 Expr *X = nullptr;
5755 Expr *V = nullptr;
5756 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005757 Expr *UE = nullptr;
5758 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005759 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005760 // OpenMP [2.12.6, atomic Construct]
5761 // In the next expressions:
5762 // * x and v (as applicable) are both l-value expressions with scalar type.
5763 // * During the execution of an atomic region, multiple syntactic
5764 // occurrences of x must designate the same storage location.
5765 // * Neither of v and expr (as applicable) may access the storage location
5766 // designated by x.
5767 // * Neither of x and expr (as applicable) may access the storage location
5768 // designated by v.
5769 // * expr is an expression with scalar type.
5770 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5771 // * binop, binop=, ++, and -- are not overloaded operators.
5772 // * The expression x binop expr must be numerically equivalent to x binop
5773 // (expr). This requirement is satisfied if the operators in expr have
5774 // precedence greater than binop, or by using parentheses around expr or
5775 // subexpressions of expr.
5776 // * The expression expr binop x must be numerically equivalent to (expr)
5777 // binop x. This requirement is satisfied if the operators in expr have
5778 // precedence equal to or greater than binop, or by using parentheses around
5779 // expr or subexpressions of expr.
5780 // * For forms that allow multiple occurrences of x, the number of times
5781 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005782 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005783 enum {
5784 NotAnExpression,
5785 NotAnAssignmentOp,
5786 NotAScalarType,
5787 NotAnLValue,
5788 NoError
5789 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005790 SourceLocation ErrorLoc, NoteLoc;
5791 SourceRange ErrorRange, NoteRange;
5792 // If clause is read:
5793 // v = x;
5794 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5795 auto AtomicBinOp =
5796 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5797 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5798 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5799 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5800 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5801 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5802 if (!X->isLValue() || !V->isLValue()) {
5803 auto NotLValueExpr = X->isLValue() ? V : X;
5804 ErrorFound = NotAnLValue;
5805 ErrorLoc = AtomicBinOp->getExprLoc();
5806 ErrorRange = AtomicBinOp->getSourceRange();
5807 NoteLoc = NotLValueExpr->getExprLoc();
5808 NoteRange = NotLValueExpr->getSourceRange();
5809 }
5810 } else if (!X->isInstantiationDependent() ||
5811 !V->isInstantiationDependent()) {
5812 auto NotScalarExpr =
5813 (X->isInstantiationDependent() || X->getType()->isScalarType())
5814 ? V
5815 : X;
5816 ErrorFound = NotAScalarType;
5817 ErrorLoc = AtomicBinOp->getExprLoc();
5818 ErrorRange = AtomicBinOp->getSourceRange();
5819 NoteLoc = NotScalarExpr->getExprLoc();
5820 NoteRange = NotScalarExpr->getSourceRange();
5821 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005822 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005823 ErrorFound = NotAnAssignmentOp;
5824 ErrorLoc = AtomicBody->getExprLoc();
5825 ErrorRange = AtomicBody->getSourceRange();
5826 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5827 : AtomicBody->getExprLoc();
5828 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5829 : AtomicBody->getSourceRange();
5830 }
5831 } else {
5832 ErrorFound = NotAnExpression;
5833 NoteLoc = ErrorLoc = Body->getLocStart();
5834 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005835 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005836 if (ErrorFound != NoError) {
5837 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5838 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005839 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5840 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005841 return StmtError();
5842 } else if (CurContext->isDependentContext())
5843 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005844 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005845 enum {
5846 NotAnExpression,
5847 NotAnAssignmentOp,
5848 NotAScalarType,
5849 NotAnLValue,
5850 NoError
5851 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005852 SourceLocation ErrorLoc, NoteLoc;
5853 SourceRange ErrorRange, NoteRange;
5854 // If clause is write:
5855 // x = expr;
5856 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5857 auto AtomicBinOp =
5858 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5859 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005860 X = AtomicBinOp->getLHS();
5861 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005862 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5863 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5864 if (!X->isLValue()) {
5865 ErrorFound = NotAnLValue;
5866 ErrorLoc = AtomicBinOp->getExprLoc();
5867 ErrorRange = AtomicBinOp->getSourceRange();
5868 NoteLoc = X->getExprLoc();
5869 NoteRange = X->getSourceRange();
5870 }
5871 } else if (!X->isInstantiationDependent() ||
5872 !E->isInstantiationDependent()) {
5873 auto NotScalarExpr =
5874 (X->isInstantiationDependent() || X->getType()->isScalarType())
5875 ? E
5876 : X;
5877 ErrorFound = NotAScalarType;
5878 ErrorLoc = AtomicBinOp->getExprLoc();
5879 ErrorRange = AtomicBinOp->getSourceRange();
5880 NoteLoc = NotScalarExpr->getExprLoc();
5881 NoteRange = NotScalarExpr->getSourceRange();
5882 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005883 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005884 ErrorFound = NotAnAssignmentOp;
5885 ErrorLoc = AtomicBody->getExprLoc();
5886 ErrorRange = AtomicBody->getSourceRange();
5887 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5888 : AtomicBody->getExprLoc();
5889 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5890 : AtomicBody->getSourceRange();
5891 }
5892 } else {
5893 ErrorFound = NotAnExpression;
5894 NoteLoc = ErrorLoc = Body->getLocStart();
5895 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005896 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005897 if (ErrorFound != NoError) {
5898 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5899 << ErrorRange;
5900 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5901 << NoteRange;
5902 return StmtError();
5903 } else if (CurContext->isDependentContext())
5904 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005905 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005906 // If clause is update:
5907 // x++;
5908 // x--;
5909 // ++x;
5910 // --x;
5911 // x binop= expr;
5912 // x = x binop expr;
5913 // x = expr binop x;
5914 OpenMPAtomicUpdateChecker Checker(*this);
5915 if (Checker.checkStatement(
5916 Body, (AtomicKind == OMPC_update)
5917 ? diag::err_omp_atomic_update_not_expression_statement
5918 : diag::err_omp_atomic_not_expression_statement,
5919 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005920 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005921 if (!CurContext->isDependentContext()) {
5922 E = Checker.getExpr();
5923 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005924 UE = Checker.getUpdateExpr();
5925 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005926 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005927 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005928 enum {
5929 NotAnAssignmentOp,
5930 NotACompoundStatement,
5931 NotTwoSubstatements,
5932 NotASpecificExpression,
5933 NoError
5934 } ErrorFound = NoError;
5935 SourceLocation ErrorLoc, NoteLoc;
5936 SourceRange ErrorRange, NoteRange;
5937 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5938 // If clause is a capture:
5939 // v = x++;
5940 // v = x--;
5941 // v = ++x;
5942 // v = --x;
5943 // v = x binop= expr;
5944 // v = x = x binop expr;
5945 // v = x = expr binop x;
5946 auto *AtomicBinOp =
5947 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5948 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5949 V = AtomicBinOp->getLHS();
5950 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5951 OpenMPAtomicUpdateChecker Checker(*this);
5952 if (Checker.checkStatement(
5953 Body, diag::err_omp_atomic_capture_not_expression_statement,
5954 diag::note_omp_atomic_update))
5955 return StmtError();
5956 E = Checker.getExpr();
5957 X = Checker.getX();
5958 UE = Checker.getUpdateExpr();
5959 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5960 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005961 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005962 ErrorLoc = AtomicBody->getExprLoc();
5963 ErrorRange = AtomicBody->getSourceRange();
5964 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5965 : AtomicBody->getExprLoc();
5966 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5967 : AtomicBody->getSourceRange();
5968 ErrorFound = NotAnAssignmentOp;
5969 }
5970 if (ErrorFound != NoError) {
5971 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5972 << ErrorRange;
5973 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5974 return StmtError();
5975 } else if (CurContext->isDependentContext()) {
5976 UE = V = E = X = nullptr;
5977 }
5978 } else {
5979 // If clause is a capture:
5980 // { v = x; x = expr; }
5981 // { v = x; x++; }
5982 // { v = x; x--; }
5983 // { v = x; ++x; }
5984 // { v = x; --x; }
5985 // { v = x; x binop= expr; }
5986 // { v = x; x = x binop expr; }
5987 // { v = x; x = expr binop x; }
5988 // { x++; v = x; }
5989 // { x--; v = x; }
5990 // { ++x; v = x; }
5991 // { --x; v = x; }
5992 // { x binop= expr; v = x; }
5993 // { x = x binop expr; v = x; }
5994 // { x = expr binop x; v = x; }
5995 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5996 // Check that this is { expr1; expr2; }
5997 if (CS->size() == 2) {
5998 auto *First = CS->body_front();
5999 auto *Second = CS->body_back();
6000 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6001 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6002 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6003 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6004 // Need to find what subexpression is 'v' and what is 'x'.
6005 OpenMPAtomicUpdateChecker Checker(*this);
6006 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6007 BinaryOperator *BinOp = nullptr;
6008 if (IsUpdateExprFound) {
6009 BinOp = dyn_cast<BinaryOperator>(First);
6010 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6011 }
6012 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6013 // { v = x; x++; }
6014 // { v = x; x--; }
6015 // { v = x; ++x; }
6016 // { v = x; --x; }
6017 // { v = x; x binop= expr; }
6018 // { v = x; x = x binop expr; }
6019 // { v = x; x = expr binop x; }
6020 // Check that the first expression has form v = x.
6021 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6022 llvm::FoldingSetNodeID XId, PossibleXId;
6023 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6024 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6025 IsUpdateExprFound = XId == PossibleXId;
6026 if (IsUpdateExprFound) {
6027 V = BinOp->getLHS();
6028 X = Checker.getX();
6029 E = Checker.getExpr();
6030 UE = Checker.getUpdateExpr();
6031 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006032 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006033 }
6034 }
6035 if (!IsUpdateExprFound) {
6036 IsUpdateExprFound = !Checker.checkStatement(First);
6037 BinOp = nullptr;
6038 if (IsUpdateExprFound) {
6039 BinOp = dyn_cast<BinaryOperator>(Second);
6040 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6041 }
6042 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6043 // { x++; v = x; }
6044 // { x--; v = x; }
6045 // { ++x; v = x; }
6046 // { --x; v = x; }
6047 // { x binop= expr; v = x; }
6048 // { x = x binop expr; v = x; }
6049 // { x = expr binop x; v = x; }
6050 // Check that the second expression has form v = x.
6051 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6052 llvm::FoldingSetNodeID XId, PossibleXId;
6053 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6054 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6055 IsUpdateExprFound = XId == PossibleXId;
6056 if (IsUpdateExprFound) {
6057 V = BinOp->getLHS();
6058 X = Checker.getX();
6059 E = Checker.getExpr();
6060 UE = Checker.getUpdateExpr();
6061 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006062 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006063 }
6064 }
6065 }
6066 if (!IsUpdateExprFound) {
6067 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006068 auto *FirstExpr = dyn_cast<Expr>(First);
6069 auto *SecondExpr = dyn_cast<Expr>(Second);
6070 if (!FirstExpr || !SecondExpr ||
6071 !(FirstExpr->isInstantiationDependent() ||
6072 SecondExpr->isInstantiationDependent())) {
6073 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6074 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006075 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006076 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6077 : First->getLocStart();
6078 NoteRange = ErrorRange = FirstBinOp
6079 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006080 : SourceRange(ErrorLoc, ErrorLoc);
6081 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006082 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6083 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6084 ErrorFound = NotAnAssignmentOp;
6085 NoteLoc = ErrorLoc = SecondBinOp
6086 ? SecondBinOp->getOperatorLoc()
6087 : Second->getLocStart();
6088 NoteRange = ErrorRange =
6089 SecondBinOp ? SecondBinOp->getSourceRange()
6090 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006091 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006092 auto *PossibleXRHSInFirst =
6093 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6094 auto *PossibleXLHSInSecond =
6095 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6096 llvm::FoldingSetNodeID X1Id, X2Id;
6097 PossibleXRHSInFirst->Profile(X1Id, Context,
6098 /*Canonical=*/true);
6099 PossibleXLHSInSecond->Profile(X2Id, Context,
6100 /*Canonical=*/true);
6101 IsUpdateExprFound = X1Id == X2Id;
6102 if (IsUpdateExprFound) {
6103 V = FirstBinOp->getLHS();
6104 X = SecondBinOp->getLHS();
6105 E = SecondBinOp->getRHS();
6106 UE = nullptr;
6107 IsXLHSInRHSPart = false;
6108 IsPostfixUpdate = true;
6109 } else {
6110 ErrorFound = NotASpecificExpression;
6111 ErrorLoc = FirstBinOp->getExprLoc();
6112 ErrorRange = FirstBinOp->getSourceRange();
6113 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6114 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6115 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006116 }
6117 }
6118 }
6119 }
6120 } else {
6121 NoteLoc = ErrorLoc = Body->getLocStart();
6122 NoteRange = ErrorRange =
6123 SourceRange(Body->getLocStart(), Body->getLocStart());
6124 ErrorFound = NotTwoSubstatements;
6125 }
6126 } else {
6127 NoteLoc = ErrorLoc = Body->getLocStart();
6128 NoteRange = ErrorRange =
6129 SourceRange(Body->getLocStart(), Body->getLocStart());
6130 ErrorFound = NotACompoundStatement;
6131 }
6132 if (ErrorFound != NoError) {
6133 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6134 << ErrorRange;
6135 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6136 return StmtError();
6137 } else if (CurContext->isDependentContext()) {
6138 UE = V = E = X = nullptr;
6139 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006140 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006141 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006142
6143 getCurFunction()->setHasBranchProtectedScope();
6144
Alexey Bataev62cec442014-11-18 10:14:22 +00006145 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006146 X, V, E, UE, IsXLHSInRHSPart,
6147 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006148}
6149
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006150StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6151 Stmt *AStmt,
6152 SourceLocation StartLoc,
6153 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006154 if (!AStmt)
6155 return StmtError();
6156
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006157 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6158 // 1.2.2 OpenMP Language Terminology
6159 // Structured block - An executable statement with a single entry at the
6160 // top and a single exit at the bottom.
6161 // The point of exit cannot be a branch out of the structured block.
6162 // longjmp() and throw() must not violate the entry/exit criteria.
6163 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006164
Alexey Bataev13314bf2014-10-09 04:18:56 +00006165 // OpenMP [2.16, Nesting of Regions]
6166 // If specified, a teams construct must be contained within a target
6167 // construct. That target construct must contain no statements or directives
6168 // outside of the teams construct.
6169 if (DSAStack->hasInnerTeamsRegion()) {
6170 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6171 bool OMPTeamsFound = true;
6172 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6173 auto I = CS->body_begin();
6174 while (I != CS->body_end()) {
6175 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6176 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6177 OMPTeamsFound = false;
6178 break;
6179 }
6180 ++I;
6181 }
6182 assert(I != CS->body_end() && "Not found statement");
6183 S = *I;
6184 }
6185 if (!OMPTeamsFound) {
6186 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6187 Diag(DSAStack->getInnerTeamsRegionLoc(),
6188 diag::note_omp_nested_teams_construct_here);
6189 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6190 << isa<OMPExecutableDirective>(S);
6191 return StmtError();
6192 }
6193 }
6194
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006195 getCurFunction()->setHasBranchProtectedScope();
6196
6197 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6198}
6199
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006200StmtResult
6201Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6202 Stmt *AStmt, SourceLocation StartLoc,
6203 SourceLocation EndLoc) {
6204 if (!AStmt)
6205 return StmtError();
6206
6207 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6208 // 1.2.2 OpenMP Language Terminology
6209 // Structured block - An executable statement with a single entry at the
6210 // top and a single exit at the bottom.
6211 // The point of exit cannot be a branch out of the structured block.
6212 // longjmp() and throw() must not violate the entry/exit criteria.
6213 CS->getCapturedDecl()->setNothrow();
6214
6215 getCurFunction()->setHasBranchProtectedScope();
6216
6217 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6218 AStmt);
6219}
6220
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006221StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6222 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6223 SourceLocation EndLoc,
6224 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6225 if (!AStmt)
6226 return StmtError();
6227
6228 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6229 // 1.2.2 OpenMP Language Terminology
6230 // Structured block - An executable statement with a single entry at the
6231 // top and a single exit at the bottom.
6232 // The point of exit cannot be a branch out of the structured block.
6233 // longjmp() and throw() must not violate the entry/exit criteria.
6234 CS->getCapturedDecl()->setNothrow();
6235
6236 OMPLoopDirective::HelperExprs B;
6237 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6238 // define the nested loops number.
6239 unsigned NestedLoopCount =
6240 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6241 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6242 VarsWithImplicitDSA, B);
6243 if (NestedLoopCount == 0)
6244 return StmtError();
6245
6246 assert((CurContext->isDependentContext() || B.builtAll()) &&
6247 "omp target parallel for loop exprs were not built");
6248
6249 if (!CurContext->isDependentContext()) {
6250 // Finalize the clauses that need pre-built expressions for CodeGen.
6251 for (auto C : Clauses) {
6252 if (auto LC = dyn_cast<OMPLinearClause>(C))
6253 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6254 B.NumIterations, *this, CurScope))
6255 return StmtError();
6256 }
6257 }
6258
6259 getCurFunction()->setHasBranchProtectedScope();
6260 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6261 NestedLoopCount, Clauses, AStmt,
6262 B, DSAStack->isCancelRegion());
6263}
6264
Samuel Antaodf67fc42016-01-19 19:15:56 +00006265/// \brief Check for existence of a map clause in the list of clauses.
6266static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6267 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6268 I != E; ++I) {
6269 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6270 return true;
6271 }
6272 }
6273
6274 return false;
6275}
6276
Michael Wong65f367f2015-07-21 13:44:28 +00006277StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6278 Stmt *AStmt,
6279 SourceLocation StartLoc,
6280 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006281 if (!AStmt)
6282 return StmtError();
6283
6284 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6285
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006286 // OpenMP [2.10.1, Restrictions, p. 97]
6287 // At least one map clause must appear on the directive.
6288 if (!HasMapClause(Clauses)) {
6289 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6290 getOpenMPDirectiveName(OMPD_target_data);
6291 return StmtError();
6292 }
6293
Michael Wong65f367f2015-07-21 13:44:28 +00006294 getCurFunction()->setHasBranchProtectedScope();
6295
6296 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6297 AStmt);
6298}
6299
Samuel Antaodf67fc42016-01-19 19:15:56 +00006300StmtResult
6301Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6302 SourceLocation StartLoc,
6303 SourceLocation EndLoc) {
6304 // OpenMP [2.10.2, Restrictions, p. 99]
6305 // At least one map clause must appear on the directive.
6306 if (!HasMapClause(Clauses)) {
6307 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6308 << getOpenMPDirectiveName(OMPD_target_enter_data);
6309 return StmtError();
6310 }
6311
6312 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6313 Clauses);
6314}
6315
Samuel Antao72590762016-01-19 20:04:50 +00006316StmtResult
6317Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6318 SourceLocation StartLoc,
6319 SourceLocation EndLoc) {
6320 // OpenMP [2.10.3, Restrictions, p. 102]
6321 // At least one map clause must appear on the directive.
6322 if (!HasMapClause(Clauses)) {
6323 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6324 << getOpenMPDirectiveName(OMPD_target_exit_data);
6325 return StmtError();
6326 }
6327
6328 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6329}
6330
Alexey Bataev13314bf2014-10-09 04:18:56 +00006331StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6332 Stmt *AStmt, SourceLocation StartLoc,
6333 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006334 if (!AStmt)
6335 return StmtError();
6336
Alexey Bataev13314bf2014-10-09 04:18:56 +00006337 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6338 // 1.2.2 OpenMP Language Terminology
6339 // Structured block - An executable statement with a single entry at the
6340 // top and a single exit at the bottom.
6341 // The point of exit cannot be a branch out of the structured block.
6342 // longjmp() and throw() must not violate the entry/exit criteria.
6343 CS->getCapturedDecl()->setNothrow();
6344
6345 getCurFunction()->setHasBranchProtectedScope();
6346
6347 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6348}
6349
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006350StmtResult
6351Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6352 SourceLocation EndLoc,
6353 OpenMPDirectiveKind CancelRegion) {
6354 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6355 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6356 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6357 << getOpenMPDirectiveName(CancelRegion);
6358 return StmtError();
6359 }
6360 if (DSAStack->isParentNowaitRegion()) {
6361 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6362 return StmtError();
6363 }
6364 if (DSAStack->isParentOrderedRegion()) {
6365 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6366 return StmtError();
6367 }
6368 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6369 CancelRegion);
6370}
6371
Alexey Bataev87933c72015-09-18 08:07:34 +00006372StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6373 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006374 SourceLocation EndLoc,
6375 OpenMPDirectiveKind CancelRegion) {
6376 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6377 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6378 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6379 << getOpenMPDirectiveName(CancelRegion);
6380 return StmtError();
6381 }
6382 if (DSAStack->isParentNowaitRegion()) {
6383 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6384 return StmtError();
6385 }
6386 if (DSAStack->isParentOrderedRegion()) {
6387 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6388 return StmtError();
6389 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006390 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006391 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6392 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006393}
6394
Alexey Bataev382967a2015-12-08 12:06:20 +00006395static bool checkGrainsizeNumTasksClauses(Sema &S,
6396 ArrayRef<OMPClause *> Clauses) {
6397 OMPClause *PrevClause = nullptr;
6398 bool ErrorFound = false;
6399 for (auto *C : Clauses) {
6400 if (C->getClauseKind() == OMPC_grainsize ||
6401 C->getClauseKind() == OMPC_num_tasks) {
6402 if (!PrevClause)
6403 PrevClause = C;
6404 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6405 S.Diag(C->getLocStart(),
6406 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6407 << getOpenMPClauseName(C->getClauseKind())
6408 << getOpenMPClauseName(PrevClause->getClauseKind());
6409 S.Diag(PrevClause->getLocStart(),
6410 diag::note_omp_previous_grainsize_num_tasks)
6411 << getOpenMPClauseName(PrevClause->getClauseKind());
6412 ErrorFound = true;
6413 }
6414 }
6415 }
6416 return ErrorFound;
6417}
6418
Alexey Bataev49f6e782015-12-01 04:18:41 +00006419StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6420 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6421 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006422 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006423 if (!AStmt)
6424 return StmtError();
6425
6426 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6427 OMPLoopDirective::HelperExprs B;
6428 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6429 // define the nested loops number.
6430 unsigned NestedLoopCount =
6431 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006432 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006433 VarsWithImplicitDSA, B);
6434 if (NestedLoopCount == 0)
6435 return StmtError();
6436
6437 assert((CurContext->isDependentContext() || B.builtAll()) &&
6438 "omp for loop exprs were not built");
6439
Alexey Bataev382967a2015-12-08 12:06:20 +00006440 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6441 // The grainsize clause and num_tasks clause are mutually exclusive and may
6442 // not appear on the same taskloop directive.
6443 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6444 return StmtError();
6445
Alexey Bataev49f6e782015-12-01 04:18:41 +00006446 getCurFunction()->setHasBranchProtectedScope();
6447 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6448 NestedLoopCount, Clauses, AStmt, B);
6449}
6450
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006451StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6452 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6453 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006454 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006455 if (!AStmt)
6456 return StmtError();
6457
6458 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6459 OMPLoopDirective::HelperExprs B;
6460 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6461 // define the nested loops number.
6462 unsigned NestedLoopCount =
6463 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6464 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6465 VarsWithImplicitDSA, B);
6466 if (NestedLoopCount == 0)
6467 return StmtError();
6468
6469 assert((CurContext->isDependentContext() || B.builtAll()) &&
6470 "omp for loop exprs were not built");
6471
Alexey Bataev5a3af132016-03-29 08:58:54 +00006472 if (!CurContext->isDependentContext()) {
6473 // Finalize the clauses that need pre-built expressions for CodeGen.
6474 for (auto C : Clauses) {
6475 if (auto LC = dyn_cast<OMPLinearClause>(C))
6476 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6477 B.NumIterations, *this, CurScope))
6478 return StmtError();
6479 }
6480 }
6481
Alexey Bataev382967a2015-12-08 12:06:20 +00006482 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6483 // The grainsize clause and num_tasks clause are mutually exclusive and may
6484 // not appear on the same taskloop directive.
6485 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6486 return StmtError();
6487
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006488 getCurFunction()->setHasBranchProtectedScope();
6489 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6490 NestedLoopCount, Clauses, AStmt, B);
6491}
6492
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006493StmtResult Sema::ActOnOpenMPDistributeDirective(
6494 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6495 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006496 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006497 if (!AStmt)
6498 return StmtError();
6499
6500 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6501 OMPLoopDirective::HelperExprs B;
6502 // In presence of clause 'collapse' with number of loops, it will
6503 // define the nested loops number.
6504 unsigned NestedLoopCount =
6505 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6506 nullptr /*ordered not a clause on distribute*/, AStmt,
6507 *this, *DSAStack, VarsWithImplicitDSA, B);
6508 if (NestedLoopCount == 0)
6509 return StmtError();
6510
6511 assert((CurContext->isDependentContext() || B.builtAll()) &&
6512 "omp for loop exprs were not built");
6513
6514 getCurFunction()->setHasBranchProtectedScope();
6515 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6516 NestedLoopCount, Clauses, AStmt, B);
6517}
6518
Alexey Bataeved09d242014-05-28 05:53:51 +00006519OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006520 SourceLocation StartLoc,
6521 SourceLocation LParenLoc,
6522 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006523 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006524 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006525 case OMPC_final:
6526 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6527 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006528 case OMPC_num_threads:
6529 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6530 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006531 case OMPC_safelen:
6532 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6533 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006534 case OMPC_simdlen:
6535 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6536 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006537 case OMPC_collapse:
6538 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6539 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006540 case OMPC_ordered:
6541 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6542 break;
Michael Wonge710d542015-08-07 16:16:36 +00006543 case OMPC_device:
6544 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6545 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006546 case OMPC_num_teams:
6547 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6548 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006549 case OMPC_thread_limit:
6550 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6551 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006552 case OMPC_priority:
6553 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6554 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006555 case OMPC_grainsize:
6556 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6557 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006558 case OMPC_num_tasks:
6559 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6560 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006561 case OMPC_hint:
6562 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6563 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006564 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006565 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006566 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006567 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006568 case OMPC_private:
6569 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006570 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006571 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006572 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006573 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006574 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006575 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006576 case OMPC_copyprivate:
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 Bataevaadd52e2014-02-13 05:29:23 +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:
Alexey Bataev346265e2015-09-25 10:37:12 +00006588 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006589 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006590 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006591 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006592 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006593 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006594 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006595 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006596 llvm_unreachable("Clause is not allowed.");
6597 }
6598 return Res;
6599}
6600
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006601OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6602 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006603 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006604 SourceLocation NameModifierLoc,
6605 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006606 SourceLocation EndLoc) {
6607 Expr *ValExpr = Condition;
6608 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6609 !Condition->isInstantiationDependent() &&
6610 !Condition->containsUnexpandedParameterPack()) {
6611 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006612 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006613 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006614 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006615
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006616 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006617 }
6618
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006619 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6620 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006621}
6622
Alexey Bataev3778b602014-07-17 07:32:53 +00006623OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6624 SourceLocation StartLoc,
6625 SourceLocation LParenLoc,
6626 SourceLocation EndLoc) {
6627 Expr *ValExpr = Condition;
6628 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6629 !Condition->isInstantiationDependent() &&
6630 !Condition->containsUnexpandedParameterPack()) {
6631 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6632 Condition->getExprLoc(), Condition);
6633 if (Val.isInvalid())
6634 return nullptr;
6635
6636 ValExpr = Val.get();
6637 }
6638
6639 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6640}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006641ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6642 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006643 if (!Op)
6644 return ExprError();
6645
6646 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6647 public:
6648 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006649 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006650 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6651 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006652 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6653 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006654 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6655 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006656 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6657 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006658 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6659 QualType T,
6660 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006661 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6662 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006663 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6664 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006665 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006666 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006667 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006668 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6669 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006670 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6671 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006672 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6673 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006674 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006675 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006676 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006677 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6678 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006679 llvm_unreachable("conversion functions are permitted");
6680 }
6681 } ConvertDiagnoser;
6682 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6683}
6684
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006685static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006686 OpenMPClauseKind CKind,
6687 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006688 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6689 !ValExpr->isInstantiationDependent()) {
6690 SourceLocation Loc = ValExpr->getExprLoc();
6691 ExprResult Value =
6692 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6693 if (Value.isInvalid())
6694 return false;
6695
6696 ValExpr = Value.get();
6697 // The expression must evaluate to a non-negative integer value.
6698 llvm::APSInt Result;
6699 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006700 Result.isSigned() &&
6701 !((!StrictlyPositive && Result.isNonNegative()) ||
6702 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006703 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006704 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6705 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006706 return false;
6707 }
6708 }
6709 return true;
6710}
6711
Alexey Bataev568a8332014-03-06 06:15:19 +00006712OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6713 SourceLocation StartLoc,
6714 SourceLocation LParenLoc,
6715 SourceLocation EndLoc) {
6716 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006717
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006718 // OpenMP [2.5, Restrictions]
6719 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006720 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6721 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006722 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006723
Alexey Bataeved09d242014-05-28 05:53:51 +00006724 return new (Context)
6725 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006726}
6727
Alexey Bataev62c87d22014-03-21 04:51:18 +00006728ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006729 OpenMPClauseKind CKind,
6730 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006731 if (!E)
6732 return ExprError();
6733 if (E->isValueDependent() || E->isTypeDependent() ||
6734 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006735 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006736 llvm::APSInt Result;
6737 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6738 if (ICE.isInvalid())
6739 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006740 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6741 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006742 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006743 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6744 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006745 return ExprError();
6746 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006747 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6748 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6749 << E->getSourceRange();
6750 return ExprError();
6751 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006752 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6753 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006754 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006755 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006756 return ICE;
6757}
6758
6759OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6760 SourceLocation LParenLoc,
6761 SourceLocation EndLoc) {
6762 // OpenMP [2.8.1, simd construct, Description]
6763 // The parameter of the safelen clause must be a constant
6764 // positive integer expression.
6765 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6766 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006767 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006768 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006769 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006770}
6771
Alexey Bataev66b15b52015-08-21 11:14:16 +00006772OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6773 SourceLocation LParenLoc,
6774 SourceLocation EndLoc) {
6775 // OpenMP [2.8.1, simd construct, Description]
6776 // The parameter of the simdlen clause must be a constant
6777 // positive integer expression.
6778 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6779 if (Simdlen.isInvalid())
6780 return nullptr;
6781 return new (Context)
6782 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6783}
6784
Alexander Musman64d33f12014-06-04 07:53:32 +00006785OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6786 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006787 SourceLocation LParenLoc,
6788 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006789 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006790 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006791 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006792 // The parameter of the collapse clause must be a constant
6793 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006794 ExprResult NumForLoopsResult =
6795 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6796 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006797 return nullptr;
6798 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006799 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006800}
6801
Alexey Bataev10e775f2015-07-30 11:36:16 +00006802OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6803 SourceLocation EndLoc,
6804 SourceLocation LParenLoc,
6805 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006806 // OpenMP [2.7.1, loop construct, Description]
6807 // OpenMP [2.8.1, simd construct, Description]
6808 // OpenMP [2.9.6, distribute construct, Description]
6809 // The parameter of the ordered clause must be a constant
6810 // positive integer expression if any.
6811 if (NumForLoops && LParenLoc.isValid()) {
6812 ExprResult NumForLoopsResult =
6813 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6814 if (NumForLoopsResult.isInvalid())
6815 return nullptr;
6816 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006817 } else
6818 NumForLoops = nullptr;
6819 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006820 return new (Context)
6821 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6822}
6823
Alexey Bataeved09d242014-05-28 05:53:51 +00006824OMPClause *Sema::ActOnOpenMPSimpleClause(
6825 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6826 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006827 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006828 switch (Kind) {
6829 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006830 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006831 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6832 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006833 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006834 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006835 Res = ActOnOpenMPProcBindClause(
6836 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6837 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006838 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006839 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006840 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006841 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006842 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006843 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006844 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006845 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006846 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006847 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006848 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006849 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006850 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006851 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006852 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006853 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006854 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006855 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006856 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006857 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006858 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006859 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006860 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006861 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006862 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006863 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006864 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006865 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006866 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006867 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006868 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006869 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006870 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006871 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006872 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006873 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006874 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006875 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006876 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006877 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006878 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006879 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006880 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006881 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006882 llvm_unreachable("Clause is not allowed.");
6883 }
6884 return Res;
6885}
6886
Alexey Bataev6402bca2015-12-28 07:25:51 +00006887static std::string
6888getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6889 ArrayRef<unsigned> Exclude = llvm::None) {
6890 std::string Values;
6891 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6892 unsigned Skipped = Exclude.size();
6893 auto S = Exclude.begin(), E = Exclude.end();
6894 for (unsigned i = First; i < Last; ++i) {
6895 if (std::find(S, E, i) != E) {
6896 --Skipped;
6897 continue;
6898 }
6899 Values += "'";
6900 Values += getOpenMPSimpleClauseTypeName(K, i);
6901 Values += "'";
6902 if (i == Bound - Skipped)
6903 Values += " or ";
6904 else if (i != Bound + 1 - Skipped)
6905 Values += ", ";
6906 }
6907 return Values;
6908}
6909
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006910OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6911 SourceLocation KindKwLoc,
6912 SourceLocation StartLoc,
6913 SourceLocation LParenLoc,
6914 SourceLocation EndLoc) {
6915 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006916 static_assert(OMPC_DEFAULT_unknown > 0,
6917 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006918 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006919 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6920 /*Last=*/OMPC_DEFAULT_unknown)
6921 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006922 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006923 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006924 switch (Kind) {
6925 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006926 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006927 break;
6928 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006929 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006930 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006931 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006932 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006933 break;
6934 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006935 return new (Context)
6936 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006937}
6938
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006939OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6940 SourceLocation KindKwLoc,
6941 SourceLocation StartLoc,
6942 SourceLocation LParenLoc,
6943 SourceLocation EndLoc) {
6944 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006945 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006946 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6947 /*Last=*/OMPC_PROC_BIND_unknown)
6948 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006949 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006950 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006951 return new (Context)
6952 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006953}
6954
Alexey Bataev56dafe82014-06-20 07:16:17 +00006955OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006956 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006957 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006958 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006959 SourceLocation EndLoc) {
6960 OMPClause *Res = nullptr;
6961 switch (Kind) {
6962 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006963 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6964 assert(Argument.size() == NumberOfElements &&
6965 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006966 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006967 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6968 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6969 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6970 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6971 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006972 break;
6973 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006974 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6975 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6976 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6977 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006978 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006979 case OMPC_dist_schedule:
6980 Res = ActOnOpenMPDistScheduleClause(
6981 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6982 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6983 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006984 case OMPC_defaultmap:
6985 enum { Modifier, DefaultmapKind };
6986 Res = ActOnOpenMPDefaultmapClause(
6987 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6988 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
6989 StartLoc, LParenLoc, ArgumentLoc[Modifier],
6990 ArgumentLoc[DefaultmapKind], EndLoc);
6991 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00006992 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006993 case OMPC_num_threads:
6994 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006995 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006996 case OMPC_collapse:
6997 case OMPC_default:
6998 case OMPC_proc_bind:
6999 case OMPC_private:
7000 case OMPC_firstprivate:
7001 case OMPC_lastprivate:
7002 case OMPC_shared:
7003 case OMPC_reduction:
7004 case OMPC_linear:
7005 case OMPC_aligned:
7006 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007007 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007008 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007009 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007010 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007011 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007012 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007013 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007014 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007015 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007016 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007017 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007018 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007019 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007020 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007021 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007022 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007023 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007024 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007025 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007026 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007027 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007028 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007029 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007030 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007031 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007032 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007033 llvm_unreachable("Clause is not allowed.");
7034 }
7035 return Res;
7036}
7037
Alexey Bataev6402bca2015-12-28 07:25:51 +00007038static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7039 OpenMPScheduleClauseModifier M2,
7040 SourceLocation M1Loc, SourceLocation M2Loc) {
7041 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7042 SmallVector<unsigned, 2> Excluded;
7043 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7044 Excluded.push_back(M2);
7045 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7046 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7047 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7048 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7049 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7050 << getListOfPossibleValues(OMPC_schedule,
7051 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7052 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7053 Excluded)
7054 << getOpenMPClauseName(OMPC_schedule);
7055 return true;
7056 }
7057 return false;
7058}
7059
Alexey Bataev56dafe82014-06-20 07:16:17 +00007060OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007061 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007062 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007063 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7064 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7065 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7066 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7067 return nullptr;
7068 // OpenMP, 2.7.1, Loop Construct, Restrictions
7069 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7070 // but not both.
7071 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7072 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7073 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7074 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7075 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7076 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7077 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7078 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7079 return nullptr;
7080 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007081 if (Kind == OMPC_SCHEDULE_unknown) {
7082 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007083 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7084 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7085 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7086 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7087 Exclude);
7088 } else {
7089 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7090 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007091 }
7092 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7093 << Values << getOpenMPClauseName(OMPC_schedule);
7094 return nullptr;
7095 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007096 // OpenMP, 2.7.1, Loop Construct, Restrictions
7097 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7098 // schedule(guided).
7099 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7100 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7101 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7102 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7103 diag::err_omp_schedule_nonmonotonic_static);
7104 return nullptr;
7105 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007106 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007107 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007108 if (ChunkSize) {
7109 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7110 !ChunkSize->isInstantiationDependent() &&
7111 !ChunkSize->containsUnexpandedParameterPack()) {
7112 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7113 ExprResult Val =
7114 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7115 if (Val.isInvalid())
7116 return nullptr;
7117
7118 ValExpr = Val.get();
7119
7120 // OpenMP [2.7.1, Restrictions]
7121 // chunk_size must be a loop invariant integer expression with a positive
7122 // value.
7123 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007124 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7125 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7126 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007127 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007128 return nullptr;
7129 }
7130 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007131 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7132 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7133 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007134 }
7135 }
7136 }
7137
Alexey Bataev6402bca2015-12-28 07:25:51 +00007138 return new (Context)
7139 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007140 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007141}
7142
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007143OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7144 SourceLocation StartLoc,
7145 SourceLocation EndLoc) {
7146 OMPClause *Res = nullptr;
7147 switch (Kind) {
7148 case OMPC_ordered:
7149 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7150 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007151 case OMPC_nowait:
7152 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7153 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007154 case OMPC_untied:
7155 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7156 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007157 case OMPC_mergeable:
7158 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7159 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007160 case OMPC_read:
7161 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7162 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007163 case OMPC_write:
7164 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7165 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007166 case OMPC_update:
7167 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7168 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007169 case OMPC_capture:
7170 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7171 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007172 case OMPC_seq_cst:
7173 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7174 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007175 case OMPC_threads:
7176 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7177 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007178 case OMPC_simd:
7179 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7180 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007181 case OMPC_nogroup:
7182 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7183 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007184 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007185 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007186 case OMPC_num_threads:
7187 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007188 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007189 case OMPC_collapse:
7190 case OMPC_schedule:
7191 case OMPC_private:
7192 case OMPC_firstprivate:
7193 case OMPC_lastprivate:
7194 case OMPC_shared:
7195 case OMPC_reduction:
7196 case OMPC_linear:
7197 case OMPC_aligned:
7198 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007199 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007200 case OMPC_default:
7201 case OMPC_proc_bind:
7202 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007203 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007204 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007205 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007206 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007207 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007208 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007209 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007210 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007211 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007212 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007213 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007214 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007215 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007216 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007217 llvm_unreachable("Clause is not allowed.");
7218 }
7219 return Res;
7220}
7221
Alexey Bataev236070f2014-06-20 11:19:47 +00007222OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7223 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007224 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007225 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7226}
7227
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007228OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7229 SourceLocation EndLoc) {
7230 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7231}
7232
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007233OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7234 SourceLocation EndLoc) {
7235 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7236}
7237
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007238OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7239 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007240 return new (Context) OMPReadClause(StartLoc, EndLoc);
7241}
7242
Alexey Bataevdea47612014-07-23 07:46:59 +00007243OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7244 SourceLocation EndLoc) {
7245 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7246}
7247
Alexey Bataev67a4f222014-07-23 10:25:33 +00007248OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7249 SourceLocation EndLoc) {
7250 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7251}
7252
Alexey Bataev459dec02014-07-24 06:46:57 +00007253OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7254 SourceLocation EndLoc) {
7255 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7256}
7257
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007258OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7259 SourceLocation EndLoc) {
7260 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7261}
7262
Alexey Bataev346265e2015-09-25 10:37:12 +00007263OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7264 SourceLocation EndLoc) {
7265 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7266}
7267
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007268OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7269 SourceLocation EndLoc) {
7270 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7271}
7272
Alexey Bataevb825de12015-12-07 10:51:44 +00007273OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7274 SourceLocation EndLoc) {
7275 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7276}
7277
Alexey Bataevc5e02582014-06-16 07:08:35 +00007278OMPClause *Sema::ActOnOpenMPVarListClause(
7279 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7280 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7281 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007282 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007283 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7284 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7285 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007286 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007287 switch (Kind) {
7288 case OMPC_private:
7289 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7290 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007291 case OMPC_firstprivate:
7292 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7293 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007294 case OMPC_lastprivate:
7295 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7296 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007297 case OMPC_shared:
7298 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7299 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007300 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007301 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7302 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007303 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007304 case OMPC_linear:
7305 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007306 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007307 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007308 case OMPC_aligned:
7309 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7310 ColonLoc, EndLoc);
7311 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007312 case OMPC_copyin:
7313 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7314 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007315 case OMPC_copyprivate:
7316 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7317 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007318 case OMPC_flush:
7319 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7320 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007321 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007322 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7323 StartLoc, LParenLoc, EndLoc);
7324 break;
7325 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007326 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7327 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7328 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007329 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007330 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007331 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007332 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007333 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007334 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007335 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007336 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007337 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007338 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007339 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007340 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007341 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007342 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007343 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007344 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007345 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007346 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007347 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007348 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007349 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007350 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007351 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007352 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007353 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007354 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007355 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007356 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007357 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007358 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007359 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007360 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007361 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007362 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007363 llvm_unreachable("Clause is not allowed.");
7364 }
7365 return Res;
7366}
7367
Alexey Bataev90c228f2016-02-08 09:29:13 +00007368ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007369 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007370 ExprResult Res = BuildDeclRefExpr(
7371 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7372 if (!Res.isUsable())
7373 return ExprError();
7374 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7375 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7376 if (!Res.isUsable())
7377 return ExprError();
7378 }
7379 if (VK != VK_LValue && Res.get()->isGLValue()) {
7380 Res = DefaultLvalueConversion(Res.get());
7381 if (!Res.isUsable())
7382 return ExprError();
7383 }
7384 return Res;
7385}
7386
Alexey Bataev60da77e2016-02-29 05:54:20 +00007387static std::pair<ValueDecl *, bool>
7388getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7389 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007390 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7391 RefExpr->containsUnexpandedParameterPack())
7392 return std::make_pair(nullptr, true);
7393
Alexey Bataevd985eda2016-02-10 11:29:16 +00007394 // OpenMP [3.1, C/C++]
7395 // A list item is a variable name.
7396 // OpenMP [2.9.3.3, Restrictions, p.1]
7397 // A variable that is part of another variable (as an array or
7398 // structure element) cannot appear in a private clause.
7399 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007400 enum {
7401 NoArrayExpr = -1,
7402 ArraySubscript = 0,
7403 OMPArraySection = 1
7404 } IsArrayExpr = NoArrayExpr;
7405 if (AllowArraySection) {
7406 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7407 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7408 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7409 Base = TempASE->getBase()->IgnoreParenImpCasts();
7410 RefExpr = Base;
7411 IsArrayExpr = ArraySubscript;
7412 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7413 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7414 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7415 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7416 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7417 Base = TempASE->getBase()->IgnoreParenImpCasts();
7418 RefExpr = Base;
7419 IsArrayExpr = OMPArraySection;
7420 }
7421 }
7422 ELoc = RefExpr->getExprLoc();
7423 ERange = RefExpr->getSourceRange();
7424 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007425 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7426 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7427 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7428 (S.getCurrentThisType().isNull() || !ME ||
7429 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7430 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007431 if (IsArrayExpr != NoArrayExpr)
7432 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7433 << ERange;
7434 else {
7435 S.Diag(ELoc,
7436 AllowArraySection
7437 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7438 : diag::err_omp_expected_var_name_member_expr)
7439 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7440 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007441 return std::make_pair(nullptr, false);
7442 }
7443 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7444}
7445
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007446OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7447 SourceLocation StartLoc,
7448 SourceLocation LParenLoc,
7449 SourceLocation EndLoc) {
7450 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007451 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007452 for (auto &RefExpr : VarList) {
7453 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007454 SourceLocation ELoc;
7455 SourceRange ERange;
7456 Expr *SimpleRefExpr = RefExpr;
7457 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007458 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007459 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007460 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007461 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007462 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007463 ValueDecl *D = Res.first;
7464 if (!D)
7465 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007466
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007467 QualType Type = D->getType();
7468 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007469
7470 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7471 // A variable that appears in a private clause must not have an incomplete
7472 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007473 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007474 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007475 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007476
Alexey Bataev758e55e2013-09-06 18:03:48 +00007477 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7478 // in a Construct]
7479 // Variables with the predetermined data-sharing attributes may not be
7480 // listed in data-sharing attributes clauses, except for the cases
7481 // listed below. For these exceptions only, listing a predetermined
7482 // variable in a data-sharing attribute clause is allowed and overrides
7483 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007484 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007485 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007486 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7487 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007488 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007489 continue;
7490 }
7491
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007492 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007493 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007494 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007495 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7496 << getOpenMPClauseName(OMPC_private) << Type
7497 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7498 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007499 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007500 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007501 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007503 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007504 continue;
7505 }
7506
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007507 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7508 // A list item cannot appear in both a map clause and a data-sharing
7509 // attribute clause on the same construct
7510 if (DSAStack->getCurrentDirective() == OMPD_target) {
7511 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7512 [&](Expr *RE) -> bool {return true;})) {
7513 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7514 << getOpenMPClauseName(OMPC_private)
7515 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7516 ReportOriginalDSA(*this, DSAStack, D, DVar);
7517 continue;
7518 }
7519 }
7520
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007521 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7522 // A variable of class type (or array thereof) that appears in a private
7523 // clause requires an accessible, unambiguous default constructor for the
7524 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007525 // Generate helper private variable and initialize it with the default
7526 // value. The address of the original variable is replaced by the address of
7527 // the new private variable in CodeGen. This new variable is not added to
7528 // IdResolver, so the code in the OpenMP region uses original variable for
7529 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007530 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007531 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7532 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007533 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007534 if (VDPrivate->isInvalidDecl())
7535 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007536 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007537 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007538
Alexey Bataev90c228f2016-02-08 09:29:13 +00007539 DeclRefExpr *Ref = nullptr;
7540 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007541 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007542 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7543 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007544 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007545 }
7546
Alexey Bataeved09d242014-05-28 05:53:51 +00007547 if (Vars.empty())
7548 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007549
Alexey Bataev03b340a2014-10-21 03:16:40 +00007550 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7551 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007552}
7553
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007554namespace {
7555class DiagsUninitializedSeveretyRAII {
7556private:
7557 DiagnosticsEngine &Diags;
7558 SourceLocation SavedLoc;
7559 bool IsIgnored;
7560
7561public:
7562 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7563 bool IsIgnored)
7564 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7565 if (!IsIgnored) {
7566 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7567 /*Map*/ diag::Severity::Ignored, Loc);
7568 }
7569 }
7570 ~DiagsUninitializedSeveretyRAII() {
7571 if (!IsIgnored)
7572 Diags.popMappings(SavedLoc);
7573 }
7574};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007575}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007576
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007577OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7578 SourceLocation StartLoc,
7579 SourceLocation LParenLoc,
7580 SourceLocation EndLoc) {
7581 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007582 SmallVector<Expr *, 8> PrivateCopies;
7583 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007584 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007585 bool IsImplicitClause =
7586 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7587 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7588
Alexey Bataeved09d242014-05-28 05:53:51 +00007589 for (auto &RefExpr : VarList) {
7590 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007591 SourceLocation ELoc;
7592 SourceRange ERange;
7593 Expr *SimpleRefExpr = RefExpr;
7594 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007595 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007596 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007597 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007598 PrivateCopies.push_back(nullptr);
7599 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007600 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007601 ValueDecl *D = Res.first;
7602 if (!D)
7603 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007604
Alexey Bataev60da77e2016-02-29 05:54:20 +00007605 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007606 QualType Type = D->getType();
7607 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007608
7609 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7610 // A variable that appears in a private clause must not have an incomplete
7611 // type or a reference type.
7612 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007613 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007614 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007615 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007616
7617 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7618 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007619 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007620 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007621 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007622
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007623 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007624 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007625 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007626 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007627 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007628 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007629 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7630 // A list item that specifies a given variable may not appear in more
7631 // than one clause on the same directive, except that a variable may be
7632 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007633 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007634 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007635 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007636 << getOpenMPClauseName(DVar.CKind)
7637 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007638 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007639 continue;
7640 }
7641
7642 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7643 // in a Construct]
7644 // Variables with the predetermined data-sharing attributes may not be
7645 // listed in data-sharing attributes clauses, except for the cases
7646 // listed below. For these exceptions only, listing a predetermined
7647 // variable in a data-sharing attribute clause is allowed and overrides
7648 // the variable's predetermined data-sharing attributes.
7649 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7650 // in a Construct, C/C++, p.2]
7651 // Variables with const-qualified type having no mutable member may be
7652 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007653 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007654 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7655 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007656 << getOpenMPClauseName(DVar.CKind)
7657 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007658 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007659 continue;
7660 }
7661
Alexey Bataevf29276e2014-06-18 04:14:57 +00007662 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007663 // OpenMP [2.9.3.4, Restrictions, p.2]
7664 // A list item that is private within a parallel region must not appear
7665 // in a firstprivate clause on a worksharing construct if any of the
7666 // worksharing regions arising from the worksharing construct ever bind
7667 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007668 if (isOpenMPWorksharingDirective(CurrDir) &&
7669 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007670 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007671 if (DVar.CKind != OMPC_shared &&
7672 (isOpenMPParallelDirective(DVar.DKind) ||
7673 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007674 Diag(ELoc, diag::err_omp_required_access)
7675 << getOpenMPClauseName(OMPC_firstprivate)
7676 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007677 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007678 continue;
7679 }
7680 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007681 // OpenMP [2.9.3.4, Restrictions, p.3]
7682 // A list item that appears in a reduction clause of a parallel construct
7683 // must not appear in a firstprivate clause on a worksharing or task
7684 // construct if any of the worksharing or task regions arising from the
7685 // worksharing or task construct ever bind to any of the parallel regions
7686 // arising from the parallel construct.
7687 // OpenMP [2.9.3.4, Restrictions, p.4]
7688 // A list item that appears in a reduction clause in worksharing
7689 // construct must not appear in a firstprivate clause in a task construct
7690 // encountered during execution of any of the worksharing regions arising
7691 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007692 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007693 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007694 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007695 [](OpenMPDirectiveKind K) -> bool {
7696 return isOpenMPParallelDirective(K) ||
7697 isOpenMPWorksharingDirective(K);
7698 },
7699 false);
7700 if (DVar.CKind == OMPC_reduction &&
7701 (isOpenMPParallelDirective(DVar.DKind) ||
7702 isOpenMPWorksharingDirective(DVar.DKind))) {
7703 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7704 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007705 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007706 continue;
7707 }
7708 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007709
7710 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7711 // A list item that is private within a teams region must not appear in a
7712 // firstprivate clause on a distribute construct if any of the distribute
7713 // regions arising from the distribute construct ever bind to any of the
7714 // teams regions arising from the teams construct.
7715 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7716 // A list item that appears in a reduction clause of a teams construct
7717 // must not appear in a firstprivate clause on a distribute construct if
7718 // any of the distribute regions arising from the distribute construct
7719 // ever bind to any of the teams regions arising from the teams construct.
7720 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7721 // A list item may appear in a firstprivate or lastprivate clause but not
7722 // both.
7723 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007724 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007725 [](OpenMPDirectiveKind K) -> bool {
7726 return isOpenMPTeamsDirective(K);
7727 },
7728 false);
7729 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7730 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007731 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007732 continue;
7733 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007734 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007735 [](OpenMPDirectiveKind K) -> bool {
7736 return isOpenMPTeamsDirective(K);
7737 },
7738 false);
7739 if (DVar.CKind == OMPC_reduction &&
7740 isOpenMPTeamsDirective(DVar.DKind)) {
7741 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007742 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007743 continue;
7744 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007745 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007746 if (DVar.CKind == OMPC_lastprivate) {
7747 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007748 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007749 continue;
7750 }
7751 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007752 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7753 // A list item cannot appear in both a map clause and a data-sharing
7754 // attribute clause on the same construct
7755 if (CurrDir == OMPD_target) {
7756 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7757 [&](Expr *RE) -> bool {return true;})) {
7758 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7759 << getOpenMPClauseName(OMPC_firstprivate)
7760 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7761 ReportOriginalDSA(*this, DSAStack, D, DVar);
7762 continue;
7763 }
7764 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007765 }
7766
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007767 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007768 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007769 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007770 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7771 << getOpenMPClauseName(OMPC_firstprivate) << Type
7772 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7773 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007774 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007775 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007776 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007777 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007778 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007779 continue;
7780 }
7781
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007782 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007783 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7784 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007785 // Generate helper private variable and initialize it with the value of the
7786 // original variable. The address of the original variable is replaced by
7787 // the address of the new private variable in the CodeGen. This new variable
7788 // is not added to IdResolver, so the code in the OpenMP region uses
7789 // original variable for proper diagnostics and variable capturing.
7790 Expr *VDInitRefExpr = nullptr;
7791 // For arrays generate initializer for single element and replace it by the
7792 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007793 if (Type->isArrayType()) {
7794 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007795 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007796 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007797 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007798 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007799 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007800 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007801 InitializedEntity Entity =
7802 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007803 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7804
7805 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7806 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7807 if (Result.isInvalid())
7808 VDPrivate->setInvalidDecl();
7809 else
7810 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007811 // Remove temp variable declaration.
7812 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007813 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007814 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7815 ".firstprivate.temp");
7816 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7817 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007818 AddInitializerToDecl(VDPrivate,
7819 DefaultLvalueConversion(VDInitRefExpr).get(),
7820 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007821 }
7822 if (VDPrivate->isInvalidDecl()) {
7823 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007824 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007825 diag::note_omp_task_predetermined_firstprivate_here);
7826 }
7827 continue;
7828 }
7829 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007830 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007831 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7832 RefExpr->getExprLoc());
7833 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007834 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007835 if (TopDVar.CKind == OMPC_lastprivate)
7836 Ref = TopDVar.PrivateCopy;
7837 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007838 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007839 if (!IsOpenMPCapturedDecl(D))
7840 ExprCaptures.push_back(Ref->getDecl());
7841 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007842 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007843 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7844 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007845 PrivateCopies.push_back(VDPrivateRefExpr);
7846 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007847 }
7848
Alexey Bataeved09d242014-05-28 05:53:51 +00007849 if (Vars.empty())
7850 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007851
7852 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007853 Vars, PrivateCopies, Inits,
7854 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007855}
7856
Alexander Musman1bb328c2014-06-04 13:06:39 +00007857OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7858 SourceLocation StartLoc,
7859 SourceLocation LParenLoc,
7860 SourceLocation EndLoc) {
7861 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007862 SmallVector<Expr *, 8> SrcExprs;
7863 SmallVector<Expr *, 8> DstExprs;
7864 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007865 SmallVector<Decl *, 4> ExprCaptures;
7866 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007867 for (auto &RefExpr : VarList) {
7868 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007869 SourceLocation ELoc;
7870 SourceRange ERange;
7871 Expr *SimpleRefExpr = RefExpr;
7872 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007873 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007874 // It will be analyzed later.
7875 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007876 SrcExprs.push_back(nullptr);
7877 DstExprs.push_back(nullptr);
7878 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007879 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007880 ValueDecl *D = Res.first;
7881 if (!D)
7882 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007883
Alexey Bataev74caaf22016-02-20 04:09:36 +00007884 QualType Type = D->getType();
7885 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007886
7887 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7888 // A variable that appears in a lastprivate clause must not have an
7889 // incomplete type or a reference type.
7890 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007891 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007892 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007893 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007894
7895 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7896 // in a Construct]
7897 // Variables with the predetermined data-sharing attributes may not be
7898 // listed in data-sharing attributes clauses, except for the cases
7899 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007900 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007901 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7902 DVar.CKind != OMPC_firstprivate &&
7903 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7904 Diag(ELoc, diag::err_omp_wrong_dsa)
7905 << getOpenMPClauseName(DVar.CKind)
7906 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007907 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007908 continue;
7909 }
7910
Alexey Bataevf29276e2014-06-18 04:14:57 +00007911 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7912 // OpenMP [2.14.3.5, Restrictions, p.2]
7913 // A list item that is private within a parallel region, or that appears in
7914 // the reduction clause of a parallel construct, must not appear in a
7915 // lastprivate clause on a worksharing construct if any of the corresponding
7916 // worksharing regions ever binds to any of the corresponding parallel
7917 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007918 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007919 if (isOpenMPWorksharingDirective(CurrDir) &&
7920 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007921 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007922 if (DVar.CKind != OMPC_shared) {
7923 Diag(ELoc, diag::err_omp_required_access)
7924 << getOpenMPClauseName(OMPC_lastprivate)
7925 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007926 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007927 continue;
7928 }
7929 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007930
7931 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7932 // A list item may appear in a firstprivate or lastprivate clause but not
7933 // both.
7934 if (CurrDir == OMPD_distribute) {
7935 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7936 if (DVar.CKind == OMPC_firstprivate) {
7937 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7938 ReportOriginalDSA(*this, DSAStack, D, DVar);
7939 continue;
7940 }
7941 }
7942
Alexander Musman1bb328c2014-06-04 13:06:39 +00007943 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007944 // A variable of class type (or array thereof) that appears in a
7945 // lastprivate clause requires an accessible, unambiguous default
7946 // constructor for the class type, unless the list item is also specified
7947 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007948 // A variable of class type (or array thereof) that appears in a
7949 // lastprivate clause requires an accessible, unambiguous copy assignment
7950 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007951 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007952 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007953 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007954 D->hasAttrs() ? &D->getAttrs() : nullptr);
7955 auto *PseudoSrcExpr =
7956 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007957 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007958 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007959 D->hasAttrs() ? &D->getAttrs() : nullptr);
7960 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007961 // For arrays generate assignment operation for single element and replace
7962 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007963 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007964 PseudoDstExpr, PseudoSrcExpr);
7965 if (AssignmentOp.isInvalid())
7966 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007967 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007968 /*DiscardedValue=*/true);
7969 if (AssignmentOp.isInvalid())
7970 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007971
Alexey Bataev74caaf22016-02-20 04:09:36 +00007972 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007973 if (!VD) {
7974 if (TopDVar.CKind == OMPC_firstprivate)
7975 Ref = TopDVar.PrivateCopy;
7976 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007977 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007978 if (!IsOpenMPCapturedDecl(D))
7979 ExprCaptures.push_back(Ref->getDecl());
7980 }
7981 if (TopDVar.CKind == OMPC_firstprivate ||
7982 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007983 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007984 ExprResult RefRes = DefaultLvalueConversion(Ref);
7985 if (!RefRes.isUsable())
7986 continue;
7987 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007988 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
7989 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007990 if (!PostUpdateRes.isUsable())
7991 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00007992 ExprPostUpdates.push_back(
7993 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00007994 }
7995 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00007996 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00007997 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
7998 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00007999 SrcExprs.push_back(PseudoSrcExpr);
8000 DstExprs.push_back(PseudoDstExpr);
8001 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008002 }
8003
8004 if (Vars.empty())
8005 return nullptr;
8006
8007 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008008 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008009 buildPreInits(Context, ExprCaptures),
8010 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008011}
8012
Alexey Bataev758e55e2013-09-06 18:03:48 +00008013OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8014 SourceLocation StartLoc,
8015 SourceLocation LParenLoc,
8016 SourceLocation EndLoc) {
8017 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008018 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008019 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008020 SourceLocation ELoc;
8021 SourceRange ERange;
8022 Expr *SimpleRefExpr = RefExpr;
8023 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008024 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008025 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008026 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008027 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008028 ValueDecl *D = Res.first;
8029 if (!D)
8030 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008031
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008032 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008033 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8034 // in a Construct]
8035 // Variables with the predetermined data-sharing attributes may not be
8036 // listed in data-sharing attributes clauses, except for the cases
8037 // listed below. For these exceptions only, listing a predetermined
8038 // variable in a data-sharing attribute clause is allowed and overrides
8039 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008040 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008041 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8042 DVar.RefExpr) {
8043 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8044 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008045 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008046 continue;
8047 }
8048
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008049 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008050 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008051 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008052 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008053 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008054 }
8055
Alexey Bataeved09d242014-05-28 05:53:51 +00008056 if (Vars.empty())
8057 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008058
8059 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8060}
8061
Alexey Bataevc5e02582014-06-16 07:08:35 +00008062namespace {
8063class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8064 DSAStackTy *Stack;
8065
8066public:
8067 bool VisitDeclRefExpr(DeclRefExpr *E) {
8068 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008069 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008070 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8071 return false;
8072 if (DVar.CKind != OMPC_unknown)
8073 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008074 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008075 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008076 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008077 return true;
8078 return false;
8079 }
8080 return false;
8081 }
8082 bool VisitStmt(Stmt *S) {
8083 for (auto Child : S->children()) {
8084 if (Child && Visit(Child))
8085 return true;
8086 }
8087 return false;
8088 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008089 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008090};
Alexey Bataev23b69422014-06-18 07:08:49 +00008091} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008092
Alexey Bataev60da77e2016-02-29 05:54:20 +00008093namespace {
8094// Transform MemberExpression for specified FieldDecl of current class to
8095// DeclRefExpr to specified OMPCapturedExprDecl.
8096class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8097 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8098 ValueDecl *Field;
8099 DeclRefExpr *CapturedExpr;
8100
8101public:
8102 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8103 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8104
8105 ExprResult TransformMemberExpr(MemberExpr *E) {
8106 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8107 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008108 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008109 return CapturedExpr;
8110 }
8111 return BaseTransform::TransformMemberExpr(E);
8112 }
8113 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8114};
8115} // namespace
8116
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008117template <typename T>
8118static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8119 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8120 for (auto &Set : Lookups) {
8121 for (auto *D : Set) {
8122 if (auto Res = Gen(cast<ValueDecl>(D)))
8123 return Res;
8124 }
8125 }
8126 return T();
8127}
8128
8129static ExprResult
8130buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8131 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8132 const DeclarationNameInfo &ReductionId, QualType Ty,
8133 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8134 if (ReductionIdScopeSpec.isInvalid())
8135 return ExprError();
8136 SmallVector<UnresolvedSet<8>, 4> Lookups;
8137 if (S) {
8138 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8139 Lookup.suppressDiagnostics();
8140 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8141 auto *D = Lookup.getRepresentativeDecl();
8142 do {
8143 S = S->getParent();
8144 } while (S && !S->isDeclScope(D));
8145 if (S)
8146 S = S->getParent();
8147 Lookups.push_back(UnresolvedSet<8>());
8148 Lookups.back().append(Lookup.begin(), Lookup.end());
8149 Lookup.clear();
8150 }
8151 } else if (auto *ULE =
8152 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8153 Lookups.push_back(UnresolvedSet<8>());
8154 Decl *PrevD = nullptr;
8155 for(auto *D : ULE->decls()) {
8156 if (D == PrevD)
8157 Lookups.push_back(UnresolvedSet<8>());
8158 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8159 Lookups.back().addDecl(DRD);
8160 PrevD = D;
8161 }
8162 }
8163 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8164 Ty->containsUnexpandedParameterPack() ||
8165 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8166 return !D->isInvalidDecl() &&
8167 (D->getType()->isDependentType() ||
8168 D->getType()->isInstantiationDependentType() ||
8169 D->getType()->containsUnexpandedParameterPack());
8170 })) {
8171 UnresolvedSet<8> ResSet;
8172 for (auto &Set : Lookups) {
8173 ResSet.append(Set.begin(), Set.end());
8174 // The last item marks the end of all declarations at the specified scope.
8175 ResSet.addDecl(Set[Set.size() - 1]);
8176 }
8177 return UnresolvedLookupExpr::Create(
8178 SemaRef.Context, /*NamingClass=*/nullptr,
8179 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8180 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8181 }
8182 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8183 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8184 if (!D->isInvalidDecl() &&
8185 SemaRef.Context.hasSameType(D->getType(), Ty))
8186 return D;
8187 return nullptr;
8188 }))
8189 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8190 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8191 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8192 if (!D->isInvalidDecl() &&
8193 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8194 !Ty.isMoreQualifiedThan(D->getType()))
8195 return D;
8196 return nullptr;
8197 })) {
8198 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8199 /*DetectVirtual=*/false);
8200 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8201 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8202 VD->getType().getUnqualifiedType()))) {
8203 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8204 /*DiagID=*/0) !=
8205 Sema::AR_inaccessible) {
8206 SemaRef.BuildBasePathArray(Paths, BasePath);
8207 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8208 }
8209 }
8210 }
8211 }
8212 if (ReductionIdScopeSpec.isSet()) {
8213 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8214 return ExprError();
8215 }
8216 return ExprEmpty();
8217}
8218
Alexey Bataevc5e02582014-06-16 07:08:35 +00008219OMPClause *Sema::ActOnOpenMPReductionClause(
8220 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8221 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008222 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8223 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008224 auto DN = ReductionId.getName();
8225 auto OOK = DN.getCXXOverloadedOperator();
8226 BinaryOperatorKind BOK = BO_Comma;
8227
8228 // OpenMP [2.14.3.6, reduction clause]
8229 // C
8230 // reduction-identifier is either an identifier or one of the following
8231 // operators: +, -, *, &, |, ^, && and ||
8232 // C++
8233 // reduction-identifier is either an id-expression or one of the following
8234 // operators: +, -, *, &, |, ^, && and ||
8235 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8236 switch (OOK) {
8237 case OO_Plus:
8238 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008239 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008240 break;
8241 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008242 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008243 break;
8244 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008245 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008246 break;
8247 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008248 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008249 break;
8250 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008251 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008252 break;
8253 case OO_AmpAmp:
8254 BOK = BO_LAnd;
8255 break;
8256 case OO_PipePipe:
8257 BOK = BO_LOr;
8258 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008259 case OO_New:
8260 case OO_Delete:
8261 case OO_Array_New:
8262 case OO_Array_Delete:
8263 case OO_Slash:
8264 case OO_Percent:
8265 case OO_Tilde:
8266 case OO_Exclaim:
8267 case OO_Equal:
8268 case OO_Less:
8269 case OO_Greater:
8270 case OO_LessEqual:
8271 case OO_GreaterEqual:
8272 case OO_PlusEqual:
8273 case OO_MinusEqual:
8274 case OO_StarEqual:
8275 case OO_SlashEqual:
8276 case OO_PercentEqual:
8277 case OO_CaretEqual:
8278 case OO_AmpEqual:
8279 case OO_PipeEqual:
8280 case OO_LessLess:
8281 case OO_GreaterGreater:
8282 case OO_LessLessEqual:
8283 case OO_GreaterGreaterEqual:
8284 case OO_EqualEqual:
8285 case OO_ExclaimEqual:
8286 case OO_PlusPlus:
8287 case OO_MinusMinus:
8288 case OO_Comma:
8289 case OO_ArrowStar:
8290 case OO_Arrow:
8291 case OO_Call:
8292 case OO_Subscript:
8293 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008294 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008295 case NUM_OVERLOADED_OPERATORS:
8296 llvm_unreachable("Unexpected reduction identifier");
8297 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008298 if (auto II = DN.getAsIdentifierInfo()) {
8299 if (II->isStr("max"))
8300 BOK = BO_GT;
8301 else if (II->isStr("min"))
8302 BOK = BO_LT;
8303 }
8304 break;
8305 }
8306 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008307 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008308 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008309 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008310
8311 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008312 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008313 SmallVector<Expr *, 8> LHSs;
8314 SmallVector<Expr *, 8> RHSs;
8315 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008316 SmallVector<Decl *, 4> ExprCaptures;
8317 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008318 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8319 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008320 for (auto RefExpr : VarList) {
8321 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008322 // OpenMP [2.1, C/C++]
8323 // A list item is a variable or array section, subject to the restrictions
8324 // specified in Section 2.4 on page 42 and in each of the sections
8325 // describing clauses and directives for which a list appears.
8326 // OpenMP [2.14.3.3, Restrictions, p.1]
8327 // A variable that is part of another variable (as an array or
8328 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008329 if (!FirstIter && IR != ER)
8330 ++IR;
8331 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008332 SourceLocation ELoc;
8333 SourceRange ERange;
8334 Expr *SimpleRefExpr = RefExpr;
8335 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8336 /*AllowArraySection=*/true);
8337 if (Res.second) {
8338 // It will be analyzed later.
8339 Vars.push_back(RefExpr);
8340 Privates.push_back(nullptr);
8341 LHSs.push_back(nullptr);
8342 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008343 // Try to find 'declare reduction' corresponding construct before using
8344 // builtin/overloaded operators.
8345 QualType Type = Context.DependentTy;
8346 CXXCastPath BasePath;
8347 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8348 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8349 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8350 if (CurContext->isDependentContext() &&
8351 (DeclareReductionRef.isUnset() ||
8352 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8353 ReductionOps.push_back(DeclareReductionRef.get());
8354 else
8355 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008356 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008357 ValueDecl *D = Res.first;
8358 if (!D)
8359 continue;
8360
Alexey Bataeva1764212015-09-30 09:22:36 +00008361 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008362 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8363 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8364 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008365 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008366 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008367 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8368 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8369 Type = ATy->getElementType();
8370 else
8371 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008372 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008373 } else
8374 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8375 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008376
Alexey Bataevc5e02582014-06-16 07:08:35 +00008377 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8378 // A variable that appears in a private clause must not have an incomplete
8379 // type or a reference type.
8380 if (RequireCompleteType(ELoc, Type,
8381 diag::err_omp_reduction_incomplete_type))
8382 continue;
8383 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008384 // A list item that appears in a reduction clause must not be
8385 // const-qualified.
8386 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008387 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008388 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008389 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008390 bool IsDecl = !VD ||
8391 VD->isThisDeclarationADefinition(Context) ==
8392 VarDecl::DeclarationOnly;
8393 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008394 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008395 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008396 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008397 continue;
8398 }
8399 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8400 // If a list-item is a reference type then it must bind to the same object
8401 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008402 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008403 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008404 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008405 DSARefChecker Check(DSAStack);
8406 if (Check.Visit(VDDef->getInit())) {
8407 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8408 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8409 continue;
8410 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008411 }
8412 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008413
Alexey Bataevc5e02582014-06-16 07:08:35 +00008414 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8415 // in a Construct]
8416 // Variables with the predetermined data-sharing attributes may not be
8417 // listed in data-sharing attributes clauses, except for the cases
8418 // listed below. For these exceptions only, listing a predetermined
8419 // variable in a data-sharing attribute clause is allowed and overrides
8420 // the variable's predetermined data-sharing attributes.
8421 // OpenMP [2.14.3.6, Restrictions, p.3]
8422 // Any number of reduction clauses can be specified on the directive,
8423 // but a list item can appear only once in the reduction clauses for that
8424 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008425 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008426 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008427 if (DVar.CKind == OMPC_reduction) {
8428 Diag(ELoc, diag::err_omp_once_referenced)
8429 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008430 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008431 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008432 } else if (DVar.CKind != OMPC_unknown) {
8433 Diag(ELoc, diag::err_omp_wrong_dsa)
8434 << getOpenMPClauseName(DVar.CKind)
8435 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008436 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008437 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008438 }
8439
8440 // OpenMP [2.14.3.6, Restrictions, p.1]
8441 // A list item that appears in a reduction clause of a worksharing
8442 // construct must be shared in the parallel regions to which any of the
8443 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008444 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8445 if (isOpenMPWorksharingDirective(CurrDir) &&
8446 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008447 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008448 if (DVar.CKind != OMPC_shared) {
8449 Diag(ELoc, diag::err_omp_required_access)
8450 << getOpenMPClauseName(OMPC_reduction)
8451 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008452 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008453 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008454 }
8455 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008456
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008457 // Try to find 'declare reduction' corresponding construct before using
8458 // builtin/overloaded operators.
8459 CXXCastPath BasePath;
8460 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8461 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8462 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8463 if (DeclareReductionRef.isInvalid())
8464 continue;
8465 if (CurContext->isDependentContext() &&
8466 (DeclareReductionRef.isUnset() ||
8467 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8468 Vars.push_back(RefExpr);
8469 Privates.push_back(nullptr);
8470 LHSs.push_back(nullptr);
8471 RHSs.push_back(nullptr);
8472 ReductionOps.push_back(DeclareReductionRef.get());
8473 continue;
8474 }
8475 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8476 // Not allowed reduction identifier is found.
8477 Diag(ReductionId.getLocStart(),
8478 diag::err_omp_unknown_reduction_identifier)
8479 << Type << ReductionIdRange;
8480 continue;
8481 }
8482
8483 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8484 // The type of a list item that appears in a reduction clause must be valid
8485 // for the reduction-identifier. For a max or min reduction in C, the type
8486 // of the list item must be an allowed arithmetic data type: char, int,
8487 // float, double, or _Bool, possibly modified with long, short, signed, or
8488 // unsigned. For a max or min reduction in C++, the type of the list item
8489 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8490 // double, or bool, possibly modified with long, short, signed, or unsigned.
8491 if (DeclareReductionRef.isUnset()) {
8492 if ((BOK == BO_GT || BOK == BO_LT) &&
8493 !(Type->isScalarType() ||
8494 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8495 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8496 << getLangOpts().CPlusPlus;
8497 if (!ASE && !OASE) {
8498 bool IsDecl = !VD ||
8499 VD->isThisDeclarationADefinition(Context) ==
8500 VarDecl::DeclarationOnly;
8501 Diag(D->getLocation(),
8502 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8503 << D;
8504 }
8505 continue;
8506 }
8507 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8508 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8509 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8510 if (!ASE && !OASE) {
8511 bool IsDecl = !VD ||
8512 VD->isThisDeclarationADefinition(Context) ==
8513 VarDecl::DeclarationOnly;
8514 Diag(D->getLocation(),
8515 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8516 << D;
8517 }
8518 continue;
8519 }
8520 }
8521
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008522 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008523 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008524 D->hasAttrs() ? &D->getAttrs() : nullptr);
8525 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8526 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008527 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008528 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008529 (!ASE &&
8530 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008531 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008532 // Create pseudo array type for private copy. The size for this array will
8533 // be generated during codegen.
8534 // For array subscripts or single variables Private Ty is the same as Type
8535 // (type of the variable or single array element).
8536 PrivateTy = Context.getVariableArrayType(
8537 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8538 Context.getSizeType(), VK_RValue),
8539 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008540 } else if (!ASE && !OASE &&
8541 Context.getAsArrayType(D->getType().getNonReferenceType()))
8542 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008543 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008544 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8545 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008546 // Add initializer for private variable.
8547 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008548 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8549 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8550 if (DeclareReductionRef.isUsable()) {
8551 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8552 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8553 if (DRD->getInitializer()) {
8554 Init = DRDRef;
8555 RHSVD->setInit(DRDRef);
8556 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008557 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008558 } else {
8559 switch (BOK) {
8560 case BO_Add:
8561 case BO_Xor:
8562 case BO_Or:
8563 case BO_LOr:
8564 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8565 if (Type->isScalarType() || Type->isAnyComplexType())
8566 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8567 break;
8568 case BO_Mul:
8569 case BO_LAnd:
8570 if (Type->isScalarType() || Type->isAnyComplexType()) {
8571 // '*' and '&&' reduction ops - initializer is '1'.
8572 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008573 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008574 break;
8575 case BO_And: {
8576 // '&' reduction op - initializer is '~0'.
8577 QualType OrigType = Type;
8578 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8579 Type = ComplexTy->getElementType();
8580 if (Type->isRealFloatingType()) {
8581 llvm::APFloat InitValue =
8582 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8583 /*isIEEE=*/true);
8584 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8585 Type, ELoc);
8586 } else if (Type->isScalarType()) {
8587 auto Size = Context.getTypeSize(Type);
8588 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8589 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8590 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8591 }
8592 if (Init && OrigType->isAnyComplexType()) {
8593 // Init = 0xFFFF + 0xFFFFi;
8594 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8595 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8596 }
8597 Type = OrigType;
8598 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008599 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008600 case BO_LT:
8601 case BO_GT: {
8602 // 'min' reduction op - initializer is 'Largest representable number in
8603 // the reduction list item type'.
8604 // 'max' reduction op - initializer is 'Least representable number in
8605 // the reduction list item type'.
8606 if (Type->isIntegerType() || Type->isPointerType()) {
8607 bool IsSigned = Type->hasSignedIntegerRepresentation();
8608 auto Size = Context.getTypeSize(Type);
8609 QualType IntTy =
8610 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8611 llvm::APInt InitValue =
8612 (BOK != BO_LT)
8613 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8614 : llvm::APInt::getMinValue(Size)
8615 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8616 : llvm::APInt::getMaxValue(Size);
8617 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8618 if (Type->isPointerType()) {
8619 // Cast to pointer type.
8620 auto CastExpr = BuildCStyleCastExpr(
8621 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8622 SourceLocation(), Init);
8623 if (CastExpr.isInvalid())
8624 continue;
8625 Init = CastExpr.get();
8626 }
8627 } else if (Type->isRealFloatingType()) {
8628 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8629 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8630 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8631 Type, ELoc);
8632 }
8633 break;
8634 }
8635 case BO_PtrMemD:
8636 case BO_PtrMemI:
8637 case BO_MulAssign:
8638 case BO_Div:
8639 case BO_Rem:
8640 case BO_Sub:
8641 case BO_Shl:
8642 case BO_Shr:
8643 case BO_LE:
8644 case BO_GE:
8645 case BO_EQ:
8646 case BO_NE:
8647 case BO_AndAssign:
8648 case BO_XorAssign:
8649 case BO_OrAssign:
8650 case BO_Assign:
8651 case BO_AddAssign:
8652 case BO_SubAssign:
8653 case BO_DivAssign:
8654 case BO_RemAssign:
8655 case BO_ShlAssign:
8656 case BO_ShrAssign:
8657 case BO_Comma:
8658 llvm_unreachable("Unexpected reduction operation");
8659 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008660 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008661 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008662 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8663 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008664 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008665 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008666 if (RHSVD->isInvalidDecl())
8667 continue;
8668 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008669 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8670 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008671 bool IsDecl =
8672 !VD ||
8673 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8674 Diag(D->getLocation(),
8675 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8676 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008677 continue;
8678 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008679 // Store initializer for single element in private copy. Will be used during
8680 // codegen.
8681 PrivateVD->setInit(RHSVD->getInit());
8682 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008683 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008684 ExprResult ReductionOp;
8685 if (DeclareReductionRef.isUsable()) {
8686 QualType RedTy = DeclareReductionRef.get()->getType();
8687 QualType PtrRedTy = Context.getPointerType(RedTy);
8688 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8689 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8690 if (!BasePath.empty()) {
8691 LHS = DefaultLvalueConversion(LHS.get());
8692 RHS = DefaultLvalueConversion(RHS.get());
8693 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8694 CK_UncheckedDerivedToBase, LHS.get(),
8695 &BasePath, LHS.get()->getValueKind());
8696 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8697 CK_UncheckedDerivedToBase, RHS.get(),
8698 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008699 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008700 FunctionProtoType::ExtProtoInfo EPI;
8701 QualType Params[] = {PtrRedTy, PtrRedTy};
8702 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8703 auto *OVE = new (Context) OpaqueValueExpr(
8704 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8705 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8706 Expr *Args[] = {LHS.get(), RHS.get()};
8707 ReductionOp = new (Context)
8708 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8709 } else {
8710 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8711 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8712 if (ReductionOp.isUsable()) {
8713 if (BOK != BO_LT && BOK != BO_GT) {
8714 ReductionOp =
8715 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8716 BO_Assign, LHSDRE, ReductionOp.get());
8717 } else {
8718 auto *ConditionalOp = new (Context) ConditionalOperator(
8719 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8720 RHSDRE, Type, VK_LValue, OK_Ordinary);
8721 ReductionOp =
8722 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8723 BO_Assign, LHSDRE, ConditionalOp);
8724 }
8725 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8726 }
8727 if (ReductionOp.isInvalid())
8728 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008729 }
8730
Alexey Bataev60da77e2016-02-29 05:54:20 +00008731 DeclRefExpr *Ref = nullptr;
8732 Expr *VarsExpr = RefExpr->IgnoreParens();
8733 if (!VD) {
8734 if (ASE || OASE) {
8735 TransformExprToCaptures RebuildToCapture(*this, D);
8736 VarsExpr =
8737 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8738 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008739 } else {
8740 VarsExpr = Ref =
8741 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008742 }
8743 if (!IsOpenMPCapturedDecl(D)) {
8744 ExprCaptures.push_back(Ref->getDecl());
8745 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8746 ExprResult RefRes = DefaultLvalueConversion(Ref);
8747 if (!RefRes.isUsable())
8748 continue;
8749 ExprResult PostUpdateRes =
8750 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8751 SimpleRefExpr, RefRes.get());
8752 if (!PostUpdateRes.isUsable())
8753 continue;
8754 ExprPostUpdates.push_back(
8755 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008756 }
8757 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008758 }
8759 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8760 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008761 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008762 LHSs.push_back(LHSDRE);
8763 RHSs.push_back(RHSDRE);
8764 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008765 }
8766
8767 if (Vars.empty())
8768 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008769
Alexey Bataevc5e02582014-06-16 07:08:35 +00008770 return OMPReductionClause::Create(
8771 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008772 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008773 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8774 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008775}
8776
Alexey Bataevecba70f2016-04-12 11:02:11 +00008777bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8778 SourceLocation LinLoc) {
8779 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8780 LinKind == OMPC_LINEAR_unknown) {
8781 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8782 return true;
8783 }
8784 return false;
8785}
8786
8787bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8788 OpenMPLinearClauseKind LinKind,
8789 QualType Type) {
8790 auto *VD = dyn_cast_or_null<VarDecl>(D);
8791 // A variable must not have an incomplete type or a reference type.
8792 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8793 return true;
8794 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8795 !Type->isReferenceType()) {
8796 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8797 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8798 return true;
8799 }
8800 Type = Type.getNonReferenceType();
8801
8802 // A list item must not be const-qualified.
8803 if (Type.isConstant(Context)) {
8804 Diag(ELoc, diag::err_omp_const_variable)
8805 << getOpenMPClauseName(OMPC_linear);
8806 if (D) {
8807 bool IsDecl =
8808 !VD ||
8809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8810 Diag(D->getLocation(),
8811 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8812 << D;
8813 }
8814 return true;
8815 }
8816
8817 // A list item must be of integral or pointer type.
8818 Type = Type.getUnqualifiedType().getCanonicalType();
8819 const auto *Ty = Type.getTypePtrOrNull();
8820 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8821 !Ty->isPointerType())) {
8822 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8823 if (D) {
8824 bool IsDecl =
8825 !VD ||
8826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8827 Diag(D->getLocation(),
8828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8829 << D;
8830 }
8831 return true;
8832 }
8833 return false;
8834}
8835
Alexey Bataev182227b2015-08-20 10:54:39 +00008836OMPClause *Sema::ActOnOpenMPLinearClause(
8837 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8838 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8839 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008840 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008841 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008842 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008843 SmallVector<Decl *, 4> ExprCaptures;
8844 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008845 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008846 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008847 for (auto &RefExpr : VarList) {
8848 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008849 SourceLocation ELoc;
8850 SourceRange ERange;
8851 Expr *SimpleRefExpr = RefExpr;
8852 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8853 /*AllowArraySection=*/false);
8854 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008855 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008856 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008857 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008858 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008859 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008860 ValueDecl *D = Res.first;
8861 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008862 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008863
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008864 QualType Type = D->getType();
8865 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008866
8867 // OpenMP [2.14.3.7, linear clause]
8868 // A list-item cannot appear in more than one linear clause.
8869 // A list-item that appears in a linear clause cannot appear in any
8870 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008871 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008872 if (DVar.RefExpr) {
8873 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8874 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008875 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008876 continue;
8877 }
8878
Alexey Bataevecba70f2016-04-12 11:02:11 +00008879 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008880 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008881 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008882
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008883 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008884 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8885 D->hasAttrs() ? &D->getAttrs() : nullptr);
8886 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008887 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008888 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008889 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008890 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008891 if (!VD) {
8892 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8893 if (!IsOpenMPCapturedDecl(D)) {
8894 ExprCaptures.push_back(Ref->getDecl());
8895 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8896 ExprResult RefRes = DefaultLvalueConversion(Ref);
8897 if (!RefRes.isUsable())
8898 continue;
8899 ExprResult PostUpdateRes =
8900 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8901 SimpleRefExpr, RefRes.get());
8902 if (!PostUpdateRes.isUsable())
8903 continue;
8904 ExprPostUpdates.push_back(
8905 IgnoredValueConversions(PostUpdateRes.get()).get());
8906 }
8907 }
8908 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008909 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008910 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008911 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008912 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008913 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008914 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8915 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8916
8917 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8918 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008919 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008920 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008921 }
8922
8923 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008924 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008925
8926 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008927 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008928 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8929 !Step->isInstantiationDependent() &&
8930 !Step->containsUnexpandedParameterPack()) {
8931 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008932 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008933 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008934 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008935 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008936
Alexander Musman3276a272015-03-21 10:12:56 +00008937 // Build var to save the step value.
8938 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008939 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008940 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008941 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008942 ExprResult CalcStep =
8943 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008944 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008945
Alexander Musman8dba6642014-04-22 13:09:42 +00008946 // Warn about zero linear step (it would be probably better specified as
8947 // making corresponding variables 'const').
8948 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008949 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8950 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008951 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8952 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008953 if (!IsConstant && CalcStep.isUsable()) {
8954 // Calculate the step beforehand instead of doing this on each iteration.
8955 // (This is not used if the number of iterations may be kfold-ed).
8956 CalcStepExpr = CalcStep.get();
8957 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008958 }
8959
Alexey Bataev182227b2015-08-20 10:54:39 +00008960 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8961 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008962 StepExpr, CalcStepExpr,
8963 buildPreInits(Context, ExprCaptures),
8964 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008965}
8966
Alexey Bataev5a3af132016-03-29 08:58:54 +00008967static bool
8968FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8969 Expr *NumIterations, Sema &SemaRef, Scope *S) {
Alexander Musman3276a272015-03-21 10:12:56 +00008970 // Walk the vars and build update/final expressions for the CodeGen.
8971 SmallVector<Expr *, 8> Updates;
8972 SmallVector<Expr *, 8> Finals;
8973 Expr *Step = Clause.getStep();
8974 Expr *CalcStep = Clause.getCalcStep();
8975 // OpenMP [2.14.3.7, linear clause]
8976 // If linear-step is not specified it is assumed to be 1.
8977 if (Step == nullptr)
8978 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008979 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008980 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008981 }
Alexander Musman3276a272015-03-21 10:12:56 +00008982 bool HasErrors = false;
8983 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008984 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008985 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008986 for (auto &RefExpr : Clause.varlists()) {
8987 Expr *InitExpr = *CurInit;
8988
8989 // Build privatized reference to the current linear var.
8990 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008991 Expr *CapturedRef;
8992 if (LinKind == OMPC_LINEAR_uval)
8993 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
8994 else
8995 CapturedRef =
8996 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
8997 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
8998 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00008999
9000 // Build update: Var = InitExpr + IV * Step
9001 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009002 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00009003 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009004 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9005 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009006
9007 // Build final: Var = InitExpr + NumIterations * Step
9008 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009009 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00009010 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009011 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9012 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009013 if (!Update.isUsable() || !Final.isUsable()) {
9014 Updates.push_back(nullptr);
9015 Finals.push_back(nullptr);
9016 HasErrors = true;
9017 } else {
9018 Updates.push_back(Update.get());
9019 Finals.push_back(Final.get());
9020 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009021 ++CurInit;
9022 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009023 }
9024 Clause.setUpdates(Updates);
9025 Clause.setFinals(Finals);
9026 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009027}
9028
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009029OMPClause *Sema::ActOnOpenMPAlignedClause(
9030 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9031 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9032
9033 SmallVector<Expr *, 8> Vars;
9034 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009035 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9036 SourceLocation ELoc;
9037 SourceRange ERange;
9038 Expr *SimpleRefExpr = RefExpr;
9039 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9040 /*AllowArraySection=*/false);
9041 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009042 // It will be analyzed later.
9043 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009044 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009045 ValueDecl *D = Res.first;
9046 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009047 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009048
Alexey Bataev1efd1662016-03-29 10:59:56 +00009049 QualType QType = D->getType();
9050 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009051
9052 // OpenMP [2.8.1, simd construct, Restrictions]
9053 // The type of list items appearing in the aligned clause must be
9054 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009055 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009056 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009057 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009058 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009059 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009060 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009061 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009062 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009063 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009064 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009065 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009066 continue;
9067 }
9068
9069 // OpenMP [2.8.1, simd construct, Restrictions]
9070 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009071 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009072 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009073 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9074 << getOpenMPClauseName(OMPC_aligned);
9075 continue;
9076 }
9077
Alexey Bataev1efd1662016-03-29 10:59:56 +00009078 DeclRefExpr *Ref = nullptr;
9079 if (!VD && IsOpenMPCapturedDecl(D))
9080 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9081 Vars.push_back(DefaultFunctionArrayConversion(
9082 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9083 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009084 }
9085
9086 // OpenMP [2.8.1, simd construct, Description]
9087 // The parameter of the aligned clause, alignment, must be a constant
9088 // positive integer expression.
9089 // If no optional parameter is specified, implementation-defined default
9090 // alignments for SIMD instructions on the target platforms are assumed.
9091 if (Alignment != nullptr) {
9092 ExprResult AlignResult =
9093 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9094 if (AlignResult.isInvalid())
9095 return nullptr;
9096 Alignment = AlignResult.get();
9097 }
9098 if (Vars.empty())
9099 return nullptr;
9100
9101 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9102 EndLoc, Vars, Alignment);
9103}
9104
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009105OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9106 SourceLocation StartLoc,
9107 SourceLocation LParenLoc,
9108 SourceLocation EndLoc) {
9109 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009110 SmallVector<Expr *, 8> SrcExprs;
9111 SmallVector<Expr *, 8> DstExprs;
9112 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009113 for (auto &RefExpr : VarList) {
9114 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9115 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009116 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009117 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009118 SrcExprs.push_back(nullptr);
9119 DstExprs.push_back(nullptr);
9120 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009121 continue;
9122 }
9123
Alexey Bataeved09d242014-05-28 05:53:51 +00009124 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009125 // OpenMP [2.1, C/C++]
9126 // A list item is a variable name.
9127 // OpenMP [2.14.4.1, Restrictions, p.1]
9128 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009129 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009130 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009131 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9132 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009133 continue;
9134 }
9135
9136 Decl *D = DE->getDecl();
9137 VarDecl *VD = cast<VarDecl>(D);
9138
9139 QualType Type = VD->getType();
9140 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9141 // It will be analyzed later.
9142 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009143 SrcExprs.push_back(nullptr);
9144 DstExprs.push_back(nullptr);
9145 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009146 continue;
9147 }
9148
9149 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9150 // A list item that appears in a copyin clause must be threadprivate.
9151 if (!DSAStack->isThreadPrivate(VD)) {
9152 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009153 << getOpenMPClauseName(OMPC_copyin)
9154 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009155 continue;
9156 }
9157
9158 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9159 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009160 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009161 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009162 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009163 auto *SrcVD =
9164 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9165 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009166 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009167 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9168 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009169 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9170 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009171 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009172 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009173 // For arrays generate assignment operation for single element and replace
9174 // it by the original array element in CodeGen.
9175 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9176 PseudoDstExpr, PseudoSrcExpr);
9177 if (AssignmentOp.isInvalid())
9178 continue;
9179 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9180 /*DiscardedValue=*/true);
9181 if (AssignmentOp.isInvalid())
9182 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009183
9184 DSAStack->addDSA(VD, DE, OMPC_copyin);
9185 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009186 SrcExprs.push_back(PseudoSrcExpr);
9187 DstExprs.push_back(PseudoDstExpr);
9188 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009189 }
9190
Alexey Bataeved09d242014-05-28 05:53:51 +00009191 if (Vars.empty())
9192 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009193
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009194 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9195 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009196}
9197
Alexey Bataevbae9a792014-06-27 10:37:06 +00009198OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9199 SourceLocation StartLoc,
9200 SourceLocation LParenLoc,
9201 SourceLocation EndLoc) {
9202 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009203 SmallVector<Expr *, 8> SrcExprs;
9204 SmallVector<Expr *, 8> DstExprs;
9205 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009206 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009207 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9208 SourceLocation ELoc;
9209 SourceRange ERange;
9210 Expr *SimpleRefExpr = RefExpr;
9211 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9212 /*AllowArraySection=*/false);
9213 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009214 // It will be analyzed later.
9215 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009216 SrcExprs.push_back(nullptr);
9217 DstExprs.push_back(nullptr);
9218 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009219 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009220 ValueDecl *D = Res.first;
9221 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009222 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009223
Alexey Bataeve122da12016-03-17 10:50:17 +00009224 QualType Type = D->getType();
9225 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009226
9227 // OpenMP [2.14.4.2, Restrictions, p.2]
9228 // A list item that appears in a copyprivate clause may not appear in a
9229 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009230 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9231 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009232 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9233 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009234 Diag(ELoc, diag::err_omp_wrong_dsa)
9235 << getOpenMPClauseName(DVar.CKind)
9236 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009237 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009238 continue;
9239 }
9240
9241 // OpenMP [2.11.4.2, Restrictions, p.1]
9242 // All list items that appear in a copyprivate clause must be either
9243 // threadprivate or private in the enclosing context.
9244 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009245 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009246 if (DVar.CKind == OMPC_shared) {
9247 Diag(ELoc, diag::err_omp_required_access)
9248 << getOpenMPClauseName(OMPC_copyprivate)
9249 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009250 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009251 continue;
9252 }
9253 }
9254 }
9255
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009256 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009257 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009258 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009259 << getOpenMPClauseName(OMPC_copyprivate) << Type
9260 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009261 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009262 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009263 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009264 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009265 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009266 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009267 continue;
9268 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009269
Alexey Bataevbae9a792014-06-27 10:37:06 +00009270 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9271 // A variable of class type (or array thereof) that appears in a
9272 // copyin clause requires an accessible, unambiguous copy assignment
9273 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009274 Type = Context.getBaseElementType(Type.getNonReferenceType())
9275 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009276 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009277 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9278 D->hasAttrs() ? &D->getAttrs() : nullptr);
9279 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009280 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009281 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9282 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009283 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009284 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9285 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009286 PseudoDstExpr, PseudoSrcExpr);
9287 if (AssignmentOp.isInvalid())
9288 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009289 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009290 /*DiscardedValue=*/true);
9291 if (AssignmentOp.isInvalid())
9292 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009293
9294 // No need to mark vars as copyprivate, they are already threadprivate or
9295 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009296 assert(VD || IsOpenMPCapturedDecl(D));
9297 Vars.push_back(
9298 VD ? RefExpr->IgnoreParens()
9299 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009300 SrcExprs.push_back(PseudoSrcExpr);
9301 DstExprs.push_back(PseudoDstExpr);
9302 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009303 }
9304
9305 if (Vars.empty())
9306 return nullptr;
9307
Alexey Bataeva63048e2015-03-23 06:18:07 +00009308 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9309 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009310}
9311
Alexey Bataev6125da92014-07-21 11:26:11 +00009312OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9313 SourceLocation StartLoc,
9314 SourceLocation LParenLoc,
9315 SourceLocation EndLoc) {
9316 if (VarList.empty())
9317 return nullptr;
9318
9319 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9320}
Alexey Bataevdea47612014-07-23 07:46:59 +00009321
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009322OMPClause *
9323Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9324 SourceLocation DepLoc, SourceLocation ColonLoc,
9325 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9326 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009327 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009328 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009329 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009330 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009331 return nullptr;
9332 }
9333 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009334 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9335 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009336 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009337 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009338 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9339 /*Last=*/OMPC_DEPEND_unknown, Except)
9340 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009341 return nullptr;
9342 }
9343 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009344 llvm::APSInt DepCounter(/*BitWidth=*/32);
9345 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9346 if (DepKind == OMPC_DEPEND_sink) {
9347 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9348 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9349 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009350 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009351 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009352 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9353 DSAStack->getParentOrderedRegionParam()) {
9354 for (auto &RefExpr : VarList) {
9355 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9356 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9357 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9358 // It will be analyzed later.
9359 Vars.push_back(RefExpr);
9360 continue;
9361 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009362
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009363 SourceLocation ELoc = RefExpr->getExprLoc();
9364 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9365 if (DepKind == OMPC_DEPEND_sink) {
9366 if (DepCounter >= TotalDepCount) {
9367 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9368 continue;
9369 }
9370 ++DepCounter;
9371 // OpenMP [2.13.9, Summary]
9372 // depend(dependence-type : vec), where dependence-type is:
9373 // 'sink' and where vec is the iteration vector, which has the form:
9374 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9375 // where n is the value specified by the ordered clause in the loop
9376 // directive, xi denotes the loop iteration variable of the i-th nested
9377 // loop associated with the loop directive, and di is a constant
9378 // non-negative integer.
9379 SimpleExpr = SimpleExpr->IgnoreImplicit();
9380 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9381 if (!DE) {
9382 OverloadedOperatorKind OOK = OO_None;
9383 SourceLocation OOLoc;
9384 Expr *LHS, *RHS;
9385 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9386 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9387 OOLoc = BO->getOperatorLoc();
9388 LHS = BO->getLHS()->IgnoreParenImpCasts();
9389 RHS = BO->getRHS()->IgnoreParenImpCasts();
9390 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9391 OOK = OCE->getOperator();
9392 OOLoc = OCE->getOperatorLoc();
9393 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9394 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9395 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9396 OOK = MCE->getMethodDecl()
9397 ->getNameInfo()
9398 .getName()
9399 .getCXXOverloadedOperator();
9400 OOLoc = MCE->getCallee()->getExprLoc();
9401 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9402 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9403 } else {
9404 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9405 continue;
9406 }
9407 DE = dyn_cast<DeclRefExpr>(LHS);
9408 if (!DE) {
9409 Diag(LHS->getExprLoc(),
9410 diag::err_omp_depend_sink_expected_loop_iteration)
9411 << DSAStack->getParentLoopControlVariable(
9412 DepCounter.getZExtValue());
9413 continue;
9414 }
9415 if (OOK != OO_Plus && OOK != OO_Minus) {
9416 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9417 continue;
9418 }
9419 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9420 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9421 if (Res.isInvalid())
9422 continue;
9423 }
9424 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9425 if (!CurContext->isDependentContext() &&
9426 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009427 (!VD ||
9428 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009429 Diag(DE->getExprLoc(),
9430 diag::err_omp_depend_sink_expected_loop_iteration)
9431 << DSAStack->getParentLoopControlVariable(
9432 DepCounter.getZExtValue());
9433 continue;
9434 }
9435 } else {
9436 // OpenMP [2.11.1.1, Restrictions, p.3]
9437 // A variable that is part of another variable (such as a field of a
9438 // structure) but is not an array element or an array section cannot
9439 // appear in a depend clause.
9440 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9441 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9442 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9443 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9444 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009445 (ASE &&
9446 !ASE->getBase()
9447 ->getType()
9448 .getNonReferenceType()
9449 ->isPointerType() &&
9450 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009451 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9452 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009453 continue;
9454 }
9455 }
9456
9457 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9458 }
9459
9460 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9461 TotalDepCount > VarList.size() &&
9462 DSAStack->getParentOrderedRegionParam()) {
9463 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9464 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9465 }
9466 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9467 Vars.empty())
9468 return nullptr;
9469 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009470
9471 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9472 DepLoc, ColonLoc, Vars);
9473}
Michael Wonge710d542015-08-07 16:16:36 +00009474
9475OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9476 SourceLocation LParenLoc,
9477 SourceLocation EndLoc) {
9478 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009479
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009480 // OpenMP [2.9.1, Restrictions]
9481 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009482 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9483 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009484 return nullptr;
9485
Michael Wonge710d542015-08-07 16:16:36 +00009486 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9487}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009488
9489static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9490 DSAStackTy *Stack, CXXRecordDecl *RD) {
9491 if (!RD || RD->isInvalidDecl())
9492 return true;
9493
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009494 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9495 if (auto *CTD = CTSD->getSpecializedTemplate())
9496 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009497 auto QTy = SemaRef.Context.getRecordType(RD);
9498 if (RD->isDynamicClass()) {
9499 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9500 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9501 return false;
9502 }
9503 auto *DC = RD;
9504 bool IsCorrect = true;
9505 for (auto *I : DC->decls()) {
9506 if (I) {
9507 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9508 if (MD->isStatic()) {
9509 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9510 SemaRef.Diag(MD->getLocation(),
9511 diag::note_omp_static_member_in_target);
9512 IsCorrect = false;
9513 }
9514 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9515 if (VD->isStaticDataMember()) {
9516 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9517 SemaRef.Diag(VD->getLocation(),
9518 diag::note_omp_static_member_in_target);
9519 IsCorrect = false;
9520 }
9521 }
9522 }
9523 }
9524
9525 for (auto &I : RD->bases()) {
9526 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9527 I.getType()->getAsCXXRecordDecl()))
9528 IsCorrect = false;
9529 }
9530 return IsCorrect;
9531}
9532
9533static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9534 DSAStackTy *Stack, QualType QTy) {
9535 NamedDecl *ND;
9536 if (QTy->isIncompleteType(&ND)) {
9537 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9538 return false;
9539 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9540 if (!RD->isInvalidDecl() &&
9541 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9542 return false;
9543 }
9544 return true;
9545}
9546
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009547/// \brief Return true if it can be proven that the provided array expression
9548/// (array section or array subscript) does NOT specify the whole size of the
9549/// array whose base type is \a BaseQTy.
9550static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9551 const Expr *E,
9552 QualType BaseQTy) {
9553 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9554
9555 // If this is an array subscript, it refers to the whole size if the size of
9556 // the dimension is constant and equals 1. Also, an array section assumes the
9557 // format of an array subscript if no colon is used.
9558 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9559 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9560 return ATy->getSize().getSExtValue() != 1;
9561 // Size can't be evaluated statically.
9562 return false;
9563 }
9564
9565 assert(OASE && "Expecting array section if not an array subscript.");
9566 auto *LowerBound = OASE->getLowerBound();
9567 auto *Length = OASE->getLength();
9568
9569 // If there is a lower bound that does not evaluates to zero, we are not
9570 // convering the whole dimension.
9571 if (LowerBound) {
9572 llvm::APSInt ConstLowerBound;
9573 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9574 return false; // Can't get the integer value as a constant.
9575 if (ConstLowerBound.getSExtValue())
9576 return true;
9577 }
9578
9579 // If we don't have a length we covering the whole dimension.
9580 if (!Length)
9581 return false;
9582
9583 // If the base is a pointer, we don't have a way to get the size of the
9584 // pointee.
9585 if (BaseQTy->isPointerType())
9586 return false;
9587
9588 // We can only check if the length is the same as the size of the dimension
9589 // if we have a constant array.
9590 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9591 if (!CATy)
9592 return false;
9593
9594 llvm::APSInt ConstLength;
9595 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9596 return false; // Can't get the integer value as a constant.
9597
9598 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9599}
9600
9601// Return true if it can be proven that the provided array expression (array
9602// section or array subscript) does NOT specify a single element of the array
9603// whose base type is \a BaseQTy.
9604static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9605 const Expr *E,
9606 QualType BaseQTy) {
9607 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9608
9609 // An array subscript always refer to a single element. Also, an array section
9610 // assumes the format of an array subscript if no colon is used.
9611 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9612 return false;
9613
9614 assert(OASE && "Expecting array section if not an array subscript.");
9615 auto *Length = OASE->getLength();
9616
9617 // If we don't have a length we have to check if the array has unitary size
9618 // for this dimension. Also, we should always expect a length if the base type
9619 // is pointer.
9620 if (!Length) {
9621 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9622 return ATy->getSize().getSExtValue() != 1;
9623 // We cannot assume anything.
9624 return false;
9625 }
9626
9627 // Check if the length evaluates to 1.
9628 llvm::APSInt ConstLength;
9629 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9630 return false; // Can't get the integer value as a constant.
9631
9632 return ConstLength.getSExtValue() != 1;
9633}
9634
Samuel Antao5de996e2016-01-22 20:21:36 +00009635// Return the expression of the base of the map clause or null if it cannot
9636// be determined and do all the necessary checks to see if the expression is
9637// valid as a standalone map clause expression.
9638static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9639 SourceLocation ELoc = E->getExprLoc();
9640 SourceRange ERange = E->getSourceRange();
9641
9642 // The base of elements of list in a map clause have to be either:
9643 // - a reference to variable or field.
9644 // - a member expression.
9645 // - an array expression.
9646 //
9647 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9648 // reference to 'r'.
9649 //
9650 // If we have:
9651 //
9652 // struct SS {
9653 // Bla S;
9654 // foo() {
9655 // #pragma omp target map (S.Arr[:12]);
9656 // }
9657 // }
9658 //
9659 // We want to retrieve the member expression 'this->S';
9660
9661 Expr *RelevantExpr = nullptr;
9662
Samuel Antao5de996e2016-01-22 20:21:36 +00009663 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9664 // If a list item is an array section, it must specify contiguous storage.
9665 //
9666 // For this restriction it is sufficient that we make sure only references
9667 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009668 // exist except in the rightmost expression (unless they cover the whole
9669 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009670 //
9671 // r.ArrS[3:5].Arr[6:7]
9672 //
9673 // r.ArrS[3:5].x
9674 //
9675 // but these would be valid:
9676 // r.ArrS[3].Arr[6:7]
9677 //
9678 // r.ArrS[3].x
9679
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009680 bool AllowUnitySizeArraySection = true;
9681 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009682
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009683 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009684 E = E->IgnoreParenImpCasts();
9685
9686 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9687 if (!isa<VarDecl>(CurE->getDecl()))
9688 break;
9689
9690 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009691
9692 // If we got a reference to a declaration, we should not expect any array
9693 // section before that.
9694 AllowUnitySizeArraySection = false;
9695 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009696 continue;
9697 }
9698
9699 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9700 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9701
9702 if (isa<CXXThisExpr>(BaseE))
9703 // We found a base expression: this->Val.
9704 RelevantExpr = CurE;
9705 else
9706 E = BaseE;
9707
9708 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9709 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9710 << CurE->getSourceRange();
9711 break;
9712 }
9713
9714 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9715
9716 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9717 // A bit-field cannot appear in a map clause.
9718 //
9719 if (FD->isBitField()) {
9720 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9721 << CurE->getSourceRange();
9722 break;
9723 }
9724
9725 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9726 // If the type of a list item is a reference to a type T then the type
9727 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009728 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009729
9730 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9731 // A list item cannot be a variable that is a member of a structure with
9732 // a union type.
9733 //
9734 if (auto *RT = CurType->getAs<RecordType>())
9735 if (RT->isUnionType()) {
9736 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9737 << CurE->getSourceRange();
9738 break;
9739 }
9740
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009741 // If we got a member expression, we should not expect any array section
9742 // before that:
9743 //
9744 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9745 // If a list item is an element of a structure, only the rightmost symbol
9746 // of the variable reference can be an array section.
9747 //
9748 AllowUnitySizeArraySection = false;
9749 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009750 continue;
9751 }
9752
9753 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9754 E = CurE->getBase()->IgnoreParenImpCasts();
9755
9756 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9757 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9758 << 0 << CurE->getSourceRange();
9759 break;
9760 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009761
9762 // If we got an array subscript that express the whole dimension we
9763 // can have any array expressions before. If it only expressing part of
9764 // the dimension, we can only have unitary-size array expressions.
9765 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9766 E->getType()))
9767 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009768 continue;
9769 }
9770
9771 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009772 E = CurE->getBase()->IgnoreParenImpCasts();
9773
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009774 auto CurType =
9775 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9776
Samuel Antao5de996e2016-01-22 20:21:36 +00009777 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9778 // If the type of a list item is a reference to a type T then the type
9779 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009780 if (CurType->isReferenceType())
9781 CurType = CurType->getPointeeType();
9782
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009783 bool IsPointer = CurType->isAnyPointerType();
9784
9785 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009786 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9787 << 0 << CurE->getSourceRange();
9788 break;
9789 }
9790
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009791 bool NotWhole =
9792 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9793 bool NotUnity =
9794 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9795
9796 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9797 // Any array section is currently allowed.
9798 //
9799 // If this array section refers to the whole dimension we can still
9800 // accept other array sections before this one, except if the base is a
9801 // pointer. Otherwise, only unitary sections are accepted.
9802 if (NotWhole || IsPointer)
9803 AllowWholeSizeArraySection = false;
9804 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9805 (AllowWholeSizeArraySection && NotWhole)) {
9806 // A unity or whole array section is not allowed and that is not
9807 // compatible with the properties of the current array section.
9808 SemaRef.Diag(
9809 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9810 << CurE->getSourceRange();
9811 break;
9812 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009813 continue;
9814 }
9815
9816 // If nothing else worked, this is not a valid map clause expression.
9817 SemaRef.Diag(ELoc,
9818 diag::err_omp_expected_named_var_member_or_array_expression)
9819 << ERange;
9820 break;
9821 }
9822
9823 return RelevantExpr;
9824}
9825
9826// Return true if expression E associated with value VD has conflicts with other
9827// map information.
9828static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9829 Expr *E, bool CurrentRegionOnly) {
9830 assert(VD && E);
9831
9832 // Types used to organize the components of a valid map clause.
9833 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9834 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9835
9836 // Helper to extract the components in the map clause expression E and store
9837 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9838 // it has already passed the single clause checks.
9839 auto ExtractMapExpressionComponents = [](Expr *TE,
9840 MapExpressionComponents &MEC) {
9841 while (true) {
9842 TE = TE->IgnoreParenImpCasts();
9843
9844 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9845 MEC.push_back(
9846 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9847 break;
9848 }
9849
9850 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9851 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9852
9853 MEC.push_back(MapExpressionComponent(
9854 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9855 if (isa<CXXThisExpr>(BaseE))
9856 break;
9857
9858 TE = BaseE;
9859 continue;
9860 }
9861
9862 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9863 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9864 TE = CurE->getBase()->IgnoreParenImpCasts();
9865 continue;
9866 }
9867
9868 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9869 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9870 TE = CurE->getBase()->IgnoreParenImpCasts();
9871 continue;
9872 }
9873
9874 llvm_unreachable(
9875 "Expecting only valid map clause expressions at this point!");
9876 }
9877 };
9878
9879 SourceLocation ELoc = E->getExprLoc();
9880 SourceRange ERange = E->getSourceRange();
9881
9882 // In order to easily check the conflicts we need to match each component of
9883 // the expression under test with the components of the expressions that are
9884 // already in the stack.
9885
9886 MapExpressionComponents CurComponents;
9887 ExtractMapExpressionComponents(E, CurComponents);
9888
9889 assert(!CurComponents.empty() && "Map clause expression with no components!");
9890 assert(CurComponents.back().second == VD &&
9891 "Map clause expression with unexpected base!");
9892
9893 // Variables to help detecting enclosing problems in data environment nests.
9894 bool IsEnclosedByDataEnvironmentExpr = false;
9895 Expr *EnclosingExpr = nullptr;
9896
9897 bool FoundError =
9898 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9899 MapExpressionComponents StackComponents;
9900 ExtractMapExpressionComponents(RE, StackComponents);
9901 assert(!StackComponents.empty() &&
9902 "Map clause expression with no components!");
9903 assert(StackComponents.back().second == VD &&
9904 "Map clause expression with unexpected base!");
9905
9906 // Expressions must start from the same base. Here we detect at which
9907 // point both expressions diverge from each other and see if we can
9908 // detect if the memory referred to both expressions is contiguous and
9909 // do not overlap.
9910 auto CI = CurComponents.rbegin();
9911 auto CE = CurComponents.rend();
9912 auto SI = StackComponents.rbegin();
9913 auto SE = StackComponents.rend();
9914 for (; CI != CE && SI != SE; ++CI, ++SI) {
9915
9916 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9917 // At most one list item can be an array item derived from a given
9918 // variable in map clauses of the same construct.
9919 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9920 isa<OMPArraySectionExpr>(CI->first)) &&
9921 (isa<ArraySubscriptExpr>(SI->first) ||
9922 isa<OMPArraySectionExpr>(SI->first))) {
9923 SemaRef.Diag(CI->first->getExprLoc(),
9924 diag::err_omp_multiple_array_items_in_map_clause)
9925 << CI->first->getSourceRange();
9926 ;
9927 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9928 << SI->first->getSourceRange();
9929 return true;
9930 }
9931
9932 // Do both expressions have the same kind?
9933 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9934 break;
9935
9936 // Are we dealing with different variables/fields?
9937 if (CI->second != SI->second)
9938 break;
9939 }
9940
9941 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9942 // List items of map clauses in the same construct must not share
9943 // original storage.
9944 //
9945 // If the expressions are exactly the same or one is a subset of the
9946 // other, it means they are sharing storage.
9947 if (CI == CE && SI == SE) {
9948 if (CurrentRegionOnly) {
9949 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9950 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9951 << RE->getSourceRange();
9952 return true;
9953 } else {
9954 // If we find the same expression in the enclosing data environment,
9955 // that is legal.
9956 IsEnclosedByDataEnvironmentExpr = true;
9957 return false;
9958 }
9959 }
9960
9961 QualType DerivedType = std::prev(CI)->first->getType();
9962 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
9963
9964 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9965 // If the type of a list item is a reference to a type T then the type
9966 // will be considered to be T for all purposes of this clause.
9967 if (DerivedType->isReferenceType())
9968 DerivedType = DerivedType->getPointeeType();
9969
9970 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
9971 // A variable for which the type is pointer and an array section
9972 // derived from that variable must not appear as list items of map
9973 // clauses of the same construct.
9974 //
9975 // Also, cover one of the cases in:
9976 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
9977 // If any part of the original storage of a list item has corresponding
9978 // storage in the device data environment, all of the original storage
9979 // must have corresponding storage in the device data environment.
9980 //
9981 if (DerivedType->isAnyPointerType()) {
9982 if (CI == CE || SI == SE) {
9983 SemaRef.Diag(
9984 DerivedLoc,
9985 diag::err_omp_pointer_mapped_along_with_derived_section)
9986 << DerivedLoc;
9987 } else {
9988 assert(CI != CE && SI != SE);
9989 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
9990 << DerivedLoc;
9991 }
9992 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9993 << RE->getSourceRange();
9994 return true;
9995 }
9996
9997 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9998 // List items of map clauses in the same construct must not share
9999 // original storage.
10000 //
10001 // An expression is a subset of the other.
10002 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10003 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10004 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10005 << RE->getSourceRange();
10006 return true;
10007 }
10008
10009 // The current expression uses the same base as other expression in the
10010 // data environment but does not contain it completelly.
10011 if (!CurrentRegionOnly && SI != SE)
10012 EnclosingExpr = RE;
10013
10014 // The current expression is a subset of the expression in the data
10015 // environment.
10016 IsEnclosedByDataEnvironmentExpr |=
10017 (!CurrentRegionOnly && CI != CE && SI == SE);
10018
10019 return false;
10020 });
10021
10022 if (CurrentRegionOnly)
10023 return FoundError;
10024
10025 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10026 // If any part of the original storage of a list item has corresponding
10027 // storage in the device data environment, all of the original storage must
10028 // have corresponding storage in the device data environment.
10029 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10030 // If a list item is an element of a structure, and a different element of
10031 // the structure has a corresponding list item in the device data environment
10032 // prior to a task encountering the construct associated with the map clause,
10033 // then the list item must also have a correspnding list item in the device
10034 // data environment prior to the task encountering the construct.
10035 //
10036 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10037 SemaRef.Diag(ELoc,
10038 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10039 << ERange;
10040 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10041 << EnclosingExpr->getSourceRange();
10042 return true;
10043 }
10044
10045 return FoundError;
10046}
10047
Samuel Antao23abd722016-01-19 20:40:49 +000010048OMPClause *
10049Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10050 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10051 SourceLocation MapLoc, SourceLocation ColonLoc,
10052 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10053 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010054 SmallVector<Expr *, 4> Vars;
10055
10056 for (auto &RE : VarList) {
10057 assert(RE && "Null expr in omp map");
10058 if (isa<DependentScopeDeclRefExpr>(RE)) {
10059 // It will be analyzed later.
10060 Vars.push_back(RE);
10061 continue;
10062 }
10063 SourceLocation ELoc = RE->getExprLoc();
10064
Kelvin Li0bff7af2015-11-23 05:32:03 +000010065 auto *VE = RE->IgnoreParenLValueCasts();
10066
10067 if (VE->isValueDependent() || VE->isTypeDependent() ||
10068 VE->isInstantiationDependent() ||
10069 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010070 // We can only analyze this information once the missing information is
10071 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010072 Vars.push_back(RE);
10073 continue;
10074 }
10075
10076 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010077
Samuel Antao5de996e2016-01-22 20:21:36 +000010078 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10079 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10080 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010081 continue;
10082 }
10083
Samuel Antao5de996e2016-01-22 20:21:36 +000010084 // Obtain the array or member expression bases if required.
10085 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
10086 if (!BE)
10087 continue;
10088
10089 // If the base is a reference to a variable, we rely on that variable for
10090 // the following checks. If it is a 'this' expression we rely on the field.
10091 ValueDecl *D = nullptr;
10092 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
10093 D = DRE->getDecl();
10094 } else {
10095 auto *ME = cast<MemberExpr>(BE);
10096 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
10097 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010098 }
10099 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010100
Samuel Antao5de996e2016-01-22 20:21:36 +000010101 auto *VD = dyn_cast<VarDecl>(D);
10102 auto *FD = dyn_cast<FieldDecl>(D);
10103
10104 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010105 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010106
10107 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10108 // threadprivate variables cannot appear in a map clause.
10109 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010110 auto DVar = DSAStack->getTopDSA(VD, false);
10111 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10112 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10113 continue;
10114 }
10115
Samuel Antao5de996e2016-01-22 20:21:36 +000010116 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10117 // A list item cannot appear in both a map clause and a data-sharing
10118 // attribute clause on the same construct.
10119 //
10120 // TODO: Implement this check - it cannot currently be tested because of
10121 // missing implementation of the other data sharing clauses in target
10122 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010123
Samuel Antao5de996e2016-01-22 20:21:36 +000010124 // Check conflicts with other map clause expressions. We check the conflicts
10125 // with the current construct separately from the enclosing data
10126 // environment, because the restrictions are different.
10127 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
10128 /*CurrentRegionOnly=*/true))
10129 break;
10130 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
10131 /*CurrentRegionOnly=*/false))
10132 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010133
Samuel Antao5de996e2016-01-22 20:21:36 +000010134 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10135 // If the type of a list item is a reference to a type T then the type will
10136 // be considered to be T for all purposes of this clause.
10137 QualType Type = D->getType();
10138 if (Type->isReferenceType())
10139 Type = Type->getPointeeType();
10140
10141 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010142 // A list item must have a mappable type.
10143 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10144 DSAStack, Type))
10145 continue;
10146
Samuel Antaodf67fc42016-01-19 19:15:56 +000010147 // target enter data
10148 // OpenMP [2.10.2, Restrictions, p. 99]
10149 // A map-type must be specified in all map clauses and must be either
10150 // to or alloc.
10151 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10152 if (DKind == OMPD_target_enter_data &&
10153 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10154 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010155 << (IsMapTypeImplicit ? 1 : 0)
10156 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010157 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010158 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010159 }
10160
Samuel Antao72590762016-01-19 20:04:50 +000010161 // target exit_data
10162 // OpenMP [2.10.3, Restrictions, p. 102]
10163 // A map-type must be specified in all map clauses and must be either
10164 // from, release, or delete.
10165 DKind = DSAStack->getCurrentDirective();
10166 if (DKind == OMPD_target_exit_data &&
10167 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10168 MapType == OMPC_MAP_delete)) {
10169 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010170 << (IsMapTypeImplicit ? 1 : 0)
10171 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010172 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010173 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010174 }
10175
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010176 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10177 // A list item cannot appear in both a map clause and a data-sharing
10178 // attribute clause on the same construct
10179 if (DKind == OMPD_target && VD) {
10180 auto DVar = DSAStack->getTopDSA(VD, false);
10181 if (isOpenMPPrivate(DVar.CKind)) {
10182 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10183 << getOpenMPClauseName(DVar.CKind)
10184 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10185 ReportOriginalDSA(*this, DSAStack, D, DVar);
10186 continue;
10187 }
10188 }
10189
Kelvin Li0bff7af2015-11-23 05:32:03 +000010190 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +000010191 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010192 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010193
Samuel Antao5de996e2016-01-22 20:21:36 +000010194 // We need to produce a map clause even if we don't have variables so that
10195 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010196 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +000010197 MapTypeModifier, MapType, IsMapTypeImplicit,
10198 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010199}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010200
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010201QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10202 TypeResult ParsedType) {
10203 assert(ParsedType.isUsable());
10204
10205 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10206 if (ReductionType.isNull())
10207 return QualType();
10208
10209 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10210 // A type name in a declare reduction directive cannot be a function type, an
10211 // array type, a reference type, or a type qualified with const, volatile or
10212 // restrict.
10213 if (ReductionType.hasQualifiers()) {
10214 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10215 return QualType();
10216 }
10217
10218 if (ReductionType->isFunctionType()) {
10219 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10220 return QualType();
10221 }
10222 if (ReductionType->isReferenceType()) {
10223 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10224 return QualType();
10225 }
10226 if (ReductionType->isArrayType()) {
10227 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10228 return QualType();
10229 }
10230 return ReductionType;
10231}
10232
10233Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10234 Scope *S, DeclContext *DC, DeclarationName Name,
10235 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10236 AccessSpecifier AS, Decl *PrevDeclInScope) {
10237 SmallVector<Decl *, 8> Decls;
10238 Decls.reserve(ReductionTypes.size());
10239
10240 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10241 ForRedeclaration);
10242 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10243 // A reduction-identifier may not be re-declared in the current scope for the
10244 // same type or for a type that is compatible according to the base language
10245 // rules.
10246 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10247 OMPDeclareReductionDecl *PrevDRD = nullptr;
10248 bool InCompoundScope = true;
10249 if (S != nullptr) {
10250 // Find previous declaration with the same name not referenced in other
10251 // declarations.
10252 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10253 InCompoundScope =
10254 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10255 LookupName(Lookup, S);
10256 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10257 /*AllowInlineNamespace=*/false);
10258 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10259 auto Filter = Lookup.makeFilter();
10260 while (Filter.hasNext()) {
10261 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10262 if (InCompoundScope) {
10263 auto I = UsedAsPrevious.find(PrevDecl);
10264 if (I == UsedAsPrevious.end())
10265 UsedAsPrevious[PrevDecl] = false;
10266 if (auto *D = PrevDecl->getPrevDeclInScope())
10267 UsedAsPrevious[D] = true;
10268 }
10269 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10270 PrevDecl->getLocation();
10271 }
10272 Filter.done();
10273 if (InCompoundScope) {
10274 for (auto &PrevData : UsedAsPrevious) {
10275 if (!PrevData.second) {
10276 PrevDRD = PrevData.first;
10277 break;
10278 }
10279 }
10280 }
10281 } else if (PrevDeclInScope != nullptr) {
10282 auto *PrevDRDInScope = PrevDRD =
10283 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10284 do {
10285 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10286 PrevDRDInScope->getLocation();
10287 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10288 } while (PrevDRDInScope != nullptr);
10289 }
10290 for (auto &TyData : ReductionTypes) {
10291 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10292 bool Invalid = false;
10293 if (I != PreviousRedeclTypes.end()) {
10294 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10295 << TyData.first;
10296 Diag(I->second, diag::note_previous_definition);
10297 Invalid = true;
10298 }
10299 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10300 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10301 Name, TyData.first, PrevDRD);
10302 DC->addDecl(DRD);
10303 DRD->setAccess(AS);
10304 Decls.push_back(DRD);
10305 if (Invalid)
10306 DRD->setInvalidDecl();
10307 else
10308 PrevDRD = DRD;
10309 }
10310
10311 return DeclGroupPtrTy::make(
10312 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10313}
10314
10315void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10316 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10317
10318 // Enter new function scope.
10319 PushFunctionScope();
10320 getCurFunction()->setHasBranchProtectedScope();
10321 getCurFunction()->setHasOMPDeclareReductionCombiner();
10322
10323 if (S != nullptr)
10324 PushDeclContext(S, DRD);
10325 else
10326 CurContext = DRD;
10327
10328 PushExpressionEvaluationContext(PotentiallyEvaluated);
10329
10330 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010331 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10332 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10333 // uses semantics of argument handles by value, but it should be passed by
10334 // reference. C lang does not support references, so pass all parameters as
10335 // pointers.
10336 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010337 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010338 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010339 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10340 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10341 // uses semantics of argument handles by value, but it should be passed by
10342 // reference. C lang does not support references, so pass all parameters as
10343 // pointers.
10344 // Create 'T omp_out;' variable.
10345 auto *OmpOutParm =
10346 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10347 if (S != nullptr) {
10348 PushOnScopeChains(OmpInParm, S);
10349 PushOnScopeChains(OmpOutParm, S);
10350 } else {
10351 DRD->addDecl(OmpInParm);
10352 DRD->addDecl(OmpOutParm);
10353 }
10354}
10355
10356void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10357 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10358 DiscardCleanupsInEvaluationContext();
10359 PopExpressionEvaluationContext();
10360
10361 PopDeclContext();
10362 PopFunctionScopeInfo();
10363
10364 if (Combiner != nullptr)
10365 DRD->setCombiner(Combiner);
10366 else
10367 DRD->setInvalidDecl();
10368}
10369
10370void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10371 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10372
10373 // Enter new function scope.
10374 PushFunctionScope();
10375 getCurFunction()->setHasBranchProtectedScope();
10376
10377 if (S != nullptr)
10378 PushDeclContext(S, DRD);
10379 else
10380 CurContext = DRD;
10381
10382 PushExpressionEvaluationContext(PotentiallyEvaluated);
10383
10384 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010385 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10386 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10387 // uses semantics of argument handles by value, but it should be passed by
10388 // reference. C lang does not support references, so pass all parameters as
10389 // pointers.
10390 // Create 'T omp_priv;' variable.
10391 auto *OmpPrivParm =
10392 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010393 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10394 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10395 // uses semantics of argument handles by value, but it should be passed by
10396 // reference. C lang does not support references, so pass all parameters as
10397 // pointers.
10398 // Create 'T omp_orig;' variable.
10399 auto *OmpOrigParm =
10400 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010401 if (S != nullptr) {
10402 PushOnScopeChains(OmpPrivParm, S);
10403 PushOnScopeChains(OmpOrigParm, S);
10404 } else {
10405 DRD->addDecl(OmpPrivParm);
10406 DRD->addDecl(OmpOrigParm);
10407 }
10408}
10409
10410void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10411 Expr *Initializer) {
10412 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10413 DiscardCleanupsInEvaluationContext();
10414 PopExpressionEvaluationContext();
10415
10416 PopDeclContext();
10417 PopFunctionScopeInfo();
10418
10419 if (Initializer != nullptr)
10420 DRD->setInitializer(Initializer);
10421 else
10422 DRD->setInvalidDecl();
10423}
10424
10425Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10426 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10427 for (auto *D : DeclReductions.get()) {
10428 if (IsValid) {
10429 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10430 if (S != nullptr)
10431 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10432 } else
10433 D->setInvalidDecl();
10434 }
10435 return DeclReductions;
10436}
10437
Kelvin Li099bb8c2015-11-24 20:50:12 +000010438OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10439 SourceLocation StartLoc,
10440 SourceLocation LParenLoc,
10441 SourceLocation EndLoc) {
10442 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010443
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010444 // OpenMP [teams Constrcut, Restrictions]
10445 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010446 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10447 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010448 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010449
10450 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10451}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010452
10453OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10454 SourceLocation StartLoc,
10455 SourceLocation LParenLoc,
10456 SourceLocation EndLoc) {
10457 Expr *ValExpr = ThreadLimit;
10458
10459 // OpenMP [teams Constrcut, Restrictions]
10460 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010461 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10462 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010463 return nullptr;
10464
10465 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10466 EndLoc);
10467}
Alexey Bataeva0569352015-12-01 10:17:31 +000010468
10469OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10470 SourceLocation StartLoc,
10471 SourceLocation LParenLoc,
10472 SourceLocation EndLoc) {
10473 Expr *ValExpr = Priority;
10474
10475 // OpenMP [2.9.1, task Constrcut]
10476 // The priority-value is a non-negative numerical scalar expression.
10477 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10478 /*StrictlyPositive=*/false))
10479 return nullptr;
10480
10481 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10482}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010483
10484OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10485 SourceLocation StartLoc,
10486 SourceLocation LParenLoc,
10487 SourceLocation EndLoc) {
10488 Expr *ValExpr = Grainsize;
10489
10490 // OpenMP [2.9.2, taskloop Constrcut]
10491 // The parameter of the grainsize clause must be a positive integer
10492 // expression.
10493 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10494 /*StrictlyPositive=*/true))
10495 return nullptr;
10496
10497 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10498}
Alexey Bataev382967a2015-12-08 12:06:20 +000010499
10500OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10501 SourceLocation StartLoc,
10502 SourceLocation LParenLoc,
10503 SourceLocation EndLoc) {
10504 Expr *ValExpr = NumTasks;
10505
10506 // OpenMP [2.9.2, taskloop Constrcut]
10507 // The parameter of the num_tasks clause must be a positive integer
10508 // expression.
10509 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10510 /*StrictlyPositive=*/true))
10511 return nullptr;
10512
10513 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10514}
10515
Alexey Bataev28c75412015-12-15 08:19:24 +000010516OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10517 SourceLocation LParenLoc,
10518 SourceLocation EndLoc) {
10519 // OpenMP [2.13.2, critical construct, Description]
10520 // ... where hint-expression is an integer constant expression that evaluates
10521 // to a valid lock hint.
10522 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10523 if (HintExpr.isInvalid())
10524 return nullptr;
10525 return new (Context)
10526 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10527}
10528
Carlo Bertollib4adf552016-01-15 18:50:31 +000010529OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10530 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10531 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10532 SourceLocation EndLoc) {
10533 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10534 std::string Values;
10535 Values += "'";
10536 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10537 Values += "'";
10538 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10539 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10540 return nullptr;
10541 }
10542 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010543 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010544 if (ChunkSize) {
10545 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10546 !ChunkSize->isInstantiationDependent() &&
10547 !ChunkSize->containsUnexpandedParameterPack()) {
10548 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10549 ExprResult Val =
10550 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10551 if (Val.isInvalid())
10552 return nullptr;
10553
10554 ValExpr = Val.get();
10555
10556 // OpenMP [2.7.1, Restrictions]
10557 // chunk_size must be a loop invariant integer expression with a positive
10558 // value.
10559 llvm::APSInt Result;
10560 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10561 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10562 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10563 << "dist_schedule" << ChunkSize->getSourceRange();
10564 return nullptr;
10565 }
10566 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010567 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10568 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10569 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010570 }
10571 }
10572 }
10573
10574 return new (Context)
10575 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010576 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010577}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010578
10579OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10580 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10581 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10582 SourceLocation KindLoc, SourceLocation EndLoc) {
10583 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10584 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10585 Kind != OMPC_DEFAULTMAP_scalar) {
10586 std::string Value;
10587 SourceLocation Loc;
10588 Value += "'";
10589 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10590 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10591 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10592 Loc = MLoc;
10593 } else {
10594 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10595 OMPC_DEFAULTMAP_scalar);
10596 Loc = KindLoc;
10597 }
10598 Value += "'";
10599 Diag(Loc, diag::err_omp_unexpected_clause_value)
10600 << Value << getOpenMPClauseName(OMPC_defaultmap);
10601 return nullptr;
10602 }
10603
10604 return new (Context)
10605 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10606}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010607
10608bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10609 DeclContext *CurLexicalContext = getCurLexicalContext();
10610 if (!CurLexicalContext->isFileContext() &&
10611 !CurLexicalContext->isExternCContext() &&
10612 !CurLexicalContext->isExternCXXContext()) {
10613 Diag(Loc, diag::err_omp_region_not_file_context);
10614 return false;
10615 }
10616 if (IsInOpenMPDeclareTargetContext) {
10617 Diag(Loc, diag::err_omp_enclosed_declare_target);
10618 return false;
10619 }
10620
10621 IsInOpenMPDeclareTargetContext = true;
10622 return true;
10623}
10624
10625void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10626 assert(IsInOpenMPDeclareTargetContext &&
10627 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10628
10629 IsInOpenMPDeclareTargetContext = false;
10630}
10631
10632static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10633 Sema &SemaRef, Decl *D) {
10634 if (!D)
10635 return;
10636 Decl *LD = nullptr;
10637 if (isa<TagDecl>(D)) {
10638 LD = cast<TagDecl>(D)->getDefinition();
10639 } else if (isa<VarDecl>(D)) {
10640 LD = cast<VarDecl>(D)->getDefinition();
10641
10642 // If this is an implicit variable that is legal and we do not need to do
10643 // anything.
10644 if (cast<VarDecl>(D)->isImplicit()) {
10645 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10646 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10647 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10648 return;
10649 }
10650
10651 } else if (isa<FunctionDecl>(D)) {
10652 const FunctionDecl *FD = nullptr;
10653 if (cast<FunctionDecl>(D)->hasBody(FD))
10654 LD = const_cast<FunctionDecl *>(FD);
10655
10656 // If the definition is associated with the current declaration in the
10657 // target region (it can be e.g. a lambda) that is legal and we do not need
10658 // to do anything else.
10659 if (LD == D) {
10660 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10661 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10662 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10663 return;
10664 }
10665 }
10666 if (!LD)
10667 LD = D;
10668 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10669 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10670 // Outlined declaration is not declared target.
10671 if (LD->isOutOfLine()) {
10672 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10673 SemaRef.Diag(SL, diag::note_used_here) << SR;
10674 } else {
10675 DeclContext *DC = LD->getDeclContext();
10676 while (DC) {
10677 if (isa<FunctionDecl>(DC) &&
10678 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10679 break;
10680 DC = DC->getParent();
10681 }
10682 if (DC)
10683 return;
10684
10685 // Is not declared in target context.
10686 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10687 SemaRef.Diag(SL, diag::note_used_here) << SR;
10688 }
10689 // Mark decl as declared target to prevent further diagnostic.
10690 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10691 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10692 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10693 }
10694}
10695
10696static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10697 Sema &SemaRef, DSAStackTy *Stack,
10698 ValueDecl *VD) {
10699 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10700 return true;
10701 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10702 return false;
10703 return true;
10704}
10705
10706void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10707 if (!D || D->isInvalidDecl())
10708 return;
10709 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10710 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10711 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10712 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10713 if (DSAStack->isThreadPrivate(VD)) {
10714 Diag(SL, diag::err_omp_threadprivate_in_target);
10715 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10716 return;
10717 }
10718 }
10719 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10720 // Problem if any with var declared with incomplete type will be reported
10721 // as normal, so no need to check it here.
10722 if ((E || !VD->getType()->isIncompleteType()) &&
10723 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10724 // Mark decl as declared target to prevent further diagnostic.
10725 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10726 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10727 if (ASTMutationListener *ML = Context.getASTMutationListener())
10728 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10729 }
10730 return;
10731 }
10732 }
10733 if (!E) {
10734 // Checking declaration inside declare target region.
10735 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10736 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10737 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10738 if (ASTMutationListener *ML = Context.getASTMutationListener())
10739 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10740 }
10741 return;
10742 }
10743 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10744}