blob: a57bbfc1d530bfd1531f3fee57159f304e98bc12 [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:
84 struct DSAInfo {
85 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000086 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000087 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000091 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
92 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Samuel Antao90927002016-04-26 14:54:23 +000093 typedef llvm::DenseMap<
94 ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
95 MappedExprComponentsTy;
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;
Samuel Antao90927002016-04-26 14:54:23 +0000102 MappedExprComponentsTy MappedExprComponents;
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 Antao90927002016-04-26 14:54:23 +0000343 // Do the check specified in \a Check to all component lists and return true
344 // if any issue is found.
345 bool checkMappableExprComponentListsForDecl(
346 ValueDecl *VD, bool CurrentRegionOnly,
347 const llvm::function_ref<bool(
348 OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000349 auto SI = Stack.rbegin();
350 auto SE = Stack.rend();
351
352 if (SI == SE)
353 return false;
354
355 if (CurrentRegionOnly) {
356 SE = std::next(SI);
357 } else {
358 ++SI;
359 }
360
361 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000362 auto MI = SI->MappedExprComponents.find(VD);
363 if (MI != SI->MappedExprComponents.end())
364 for (auto &L : MI->second)
365 if (Check(L))
Samuel Antao5de996e2016-01-22 20:21:36 +0000366 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000368 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000369 }
370
Samuel Antao90927002016-04-26 14:54:23 +0000371 // Create a new mappable expression component list associated with a given
372 // declaration and initialize it with the provided list of components.
373 void addMappableExpressionComponents(
374 ValueDecl *VD,
375 OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
376 assert(Stack.size() > 1 &&
377 "Not expecting to retrieve components from a empty stack!");
378 auto &MEC = Stack.back().MappedExprComponents[VD];
379 // Create new entry and append the new components there.
380 MEC.resize(MEC.size() + 1);
381 MEC.back().append(Components.begin(), Components.end());
Kelvin Li0bff7af2015-11-23 05:32:03 +0000382 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000384bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000385 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
386 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387}
Alexey Bataeved09d242014-05-28 05:53:51 +0000388} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000390static ValueDecl *getCanonicalDecl(ValueDecl *D) {
391 auto *VD = dyn_cast<VarDecl>(D);
392 auto *FD = dyn_cast<FieldDecl>(D);
393 if (VD != nullptr) {
394 VD = VD->getCanonicalDecl();
395 D = VD;
396 } else {
397 assert(FD);
398 FD = FD->getCanonicalDecl();
399 D = FD;
400 }
401 return D;
402}
403
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000404DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000405 ValueDecl *D) {
406 D = getCanonicalDecl(D);
407 auto *VD = dyn_cast<VarDecl>(D);
408 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000409 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000410 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000411 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
412 // in a region but not in construct]
413 // File-scope or namespace-scope variables referenced in called routines
414 // in the region are shared unless they appear in a threadprivate
415 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000416 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000417 DVar.CKind = OMPC_shared;
418
419 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
420 // in a region but not in construct]
421 // Variables with static storage duration that are declared in called
422 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000423 if (VD && VD->hasGlobalStorage())
424 DVar.CKind = OMPC_shared;
425
426 // Non-static data members are shared by default.
427 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000428 DVar.CKind = OMPC_shared;
429
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 return DVar;
431 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000432
Alexey Bataev758e55e2013-09-06 18:03:48 +0000433 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
435 // in a Construct, C/C++, predetermined, p.1]
436 // Variables with automatic storage duration that are declared in a scope
437 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000438 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
439 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000440 DVar.CKind = OMPC_private;
441 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000442 }
443
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 // Explicitly specified attributes and local variables with predetermined
445 // attributes.
446 if (Iter->SharingMap.count(D)) {
447 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000448 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000450 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451 return DVar;
452 }
453
454 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
455 // in a Construct, C/C++, implicitly determined, p.1]
456 // In a parallel or task construct, the data-sharing attributes of these
457 // variables are determined by the default clause, if present.
458 switch (Iter->DefaultAttr) {
459 case DSA_shared:
460 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000461 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000462 return DVar;
463 case DSA_none:
464 return DVar;
465 case DSA_unspecified:
466 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
467 // in a Construct, implicitly determined, p.2]
468 // In a parallel construct, if no default clause is present, these
469 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000470 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000471 if (isOpenMPParallelDirective(DVar.DKind) ||
472 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473 DVar.CKind = OMPC_shared;
474 return DVar;
475 }
476
477 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
478 // in a Construct, implicitly determined, p.4]
479 // In a task construct, if no default clause is present, a variable that in
480 // the enclosing context is determined to be shared by all implicit tasks
481 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000482 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000483 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000484 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000486 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000487 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000488 // In a task construct, if no default clause is present, a variable
489 // whose data-sharing attribute is not determined by the rules above is
490 // firstprivate.
491 DVarTemp = getDSA(I, D);
492 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000493 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000494 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000495 return DVar;
496 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000497 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000498 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000499 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000500 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000501 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000502 return DVar;
503 }
504 }
505 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
506 // in a Construct, implicitly determined, p.3]
507 // For constructs other than task, if no default clause is present, these
508 // variables inherit their data-sharing attributes from the enclosing
509 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000510 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000511}
512
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000513Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000514 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000515 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000516 auto It = Stack.back().AlignedMap.find(D);
517 if (It == Stack.back().AlignedMap.end()) {
518 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
519 Stack.back().AlignedMap[D] = NewDE;
520 return nullptr;
521 } else {
522 assert(It->second && "Unexpected nullptr expr in the aligned map");
523 return It->second;
524 }
525 return nullptr;
526}
527
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000528void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000529 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000530 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000531 Stack.back().LCVMap.insert(
532 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000533}
534
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000535DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000536 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000537 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000538 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
539 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000540}
541
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000542DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000543 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000544 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000545 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
546 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000547 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000548}
549
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000550ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000551 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
552 if (Stack[Stack.size() - 2].LCVMap.size() < I)
553 return nullptr;
554 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000555 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000556 return Pair.first;
557 }
558 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000559}
560
Alexey Bataev90c228f2016-02-08 09:29:13 +0000561void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
562 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000563 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000564 if (A == OMPC_threadprivate) {
565 Stack[0].SharingMap[D].Attributes = A;
566 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000567 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568 } else {
569 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
570 Stack.back().SharingMap[D].Attributes = A;
571 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000572 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
573 if (PrivateCopy)
574 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000575 }
576}
577
Alexey Bataeved09d242014-05-28 05:53:51 +0000578bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000579 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000580 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000581 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000582 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000583 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000584 ++I;
585 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000586 if (I == E)
587 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000588 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000589 Scope *CurScope = getCurScope();
590 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000591 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000592 }
593 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000595 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000596}
597
Alexey Bataev39f915b82015-05-08 10:41:21 +0000598/// \brief Build a variable declaration for OpenMP loop iteration variable.
599static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000600 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000601 DeclContext *DC = SemaRef.CurContext;
602 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
603 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
604 VarDecl *Decl =
605 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000606 if (Attrs) {
607 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
608 I != E; ++I)
609 Decl->addAttr(*I);
610 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000611 Decl->setImplicit();
612 return Decl;
613}
614
615static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
616 SourceLocation Loc,
617 bool RefersToCapture = false) {
618 D->setReferenced();
619 D->markUsed(S.Context);
620 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
621 SourceLocation(), D, RefersToCapture, Loc, Ty,
622 VK_LValue);
623}
624
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000625DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
626 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 DSAVarData DVar;
628
629 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
630 // in a Construct, C/C++, predetermined, p.1]
631 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000632 auto *VD = dyn_cast<VarDecl>(D);
633 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
634 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000635 SemaRef.getLangOpts().OpenMPUseTLS &&
636 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000637 (VD && VD->getStorageClass() == SC_Register &&
638 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
639 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000640 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000641 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000642 }
643 if (Stack[0].SharingMap.count(D)) {
644 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
645 DVar.CKind = OMPC_threadprivate;
646 return DVar;
647 }
648
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000649 if (Stack.size() == 1) {
650 // Not in OpenMP execution region and top scope was already checked.
651 return DVar;
652 }
653
Alexey Bataev758e55e2013-09-06 18:03:48 +0000654 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000655 // in a Construct, C/C++, predetermined, p.4]
656 // Static data members are shared.
657 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
658 // in a Construct, C/C++, predetermined, p.7]
659 // Variables with static storage duration that are declared in a scope
660 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000661 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000662 DSAVarData DVarTemp =
663 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
664 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000665 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000666
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000667 DVar.CKind = OMPC_shared;
668 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000669 }
670
671 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000672 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
673 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000674 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
675 // in a Construct, C/C++, predetermined, p.6]
676 // Variables with const qualified type having no mutable member are
677 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000678 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000679 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000680 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
681 if (auto *CTD = CTSD->getSpecializedTemplate())
682 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000684 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
685 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000686 // Variables with const-qualified type having no mutable member may be
687 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000688 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
689 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000690 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
691 return DVar;
692
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693 DVar.CKind = OMPC_shared;
694 return DVar;
695 }
696
Alexey Bataev758e55e2013-09-06 18:03:48 +0000697 // Explicitly specified attributes and local variables with predetermined
698 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000699 auto StartI = std::next(Stack.rbegin());
700 auto EndI = std::prev(Stack.rend());
701 if (FromParent && StartI != EndI) {
702 StartI = std::next(StartI);
703 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000704 auto I = std::prev(StartI);
705 if (I->SharingMap.count(D)) {
706 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000707 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000708 DVar.CKind = I->SharingMap[D].Attributes;
709 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000710 }
711
712 return DVar;
713}
714
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
716 bool FromParent) {
717 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718 auto StartI = Stack.rbegin();
719 auto EndI = std::prev(Stack.rend());
720 if (FromParent && StartI != EndI) {
721 StartI = std::next(StartI);
722 }
723 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000724}
725
Alexey Bataevf29276e2014-06-18 04:14:57 +0000726template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000727DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000728 DirectivesPredicate DPred,
729 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000730 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000731 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000732 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000733 if (FromParent && StartI != EndI) {
734 StartI = std::next(StartI);
735 }
736 for (auto I = StartI, EE = EndI; I != EE; ++I) {
737 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000738 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000739 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000740 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000741 return DVar;
742 }
743 return DSAVarData();
744}
745
Alexey Bataevf29276e2014-06-18 04:14:57 +0000746template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000747DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000748DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000749 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000750 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000751 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000752 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000753 if (FromParent && StartI != EndI) {
754 StartI = std::next(StartI);
755 }
756 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000757 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000758 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000759 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000760 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000761 return DVar;
762 return DSAVarData();
763 }
764 return DSAVarData();
765}
766
Alexey Bataevaac108a2015-06-23 04:51:00 +0000767bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000768 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000769 unsigned Level) {
770 if (CPred(ClauseKindMode))
771 return true;
772 if (isClauseParsingMode())
773 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000774 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000775 auto StartI = Stack.rbegin();
776 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000777 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000778 return false;
779 std::advance(StartI, Level);
780 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
781 CPred(StartI->SharingMap[D].Attributes);
782}
783
Samuel Antao4be30e92015-10-02 17:14:03 +0000784bool DSAStackTy::hasExplicitDirective(
785 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
786 unsigned Level) {
787 if (isClauseParsingMode())
788 ++Level;
789 auto StartI = Stack.rbegin();
790 auto EndI = std::prev(Stack.rend());
791 if (std::distance(StartI, EndI) <= (int)Level)
792 return false;
793 std::advance(StartI, Level);
794 return DPred(StartI->Directive);
795}
796
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000797template <class NamedDirectivesPredicate>
798bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
799 auto StartI = std::next(Stack.rbegin());
800 auto EndI = std::prev(Stack.rend());
801 if (FromParent && StartI != EndI) {
802 StartI = std::next(StartI);
803 }
804 for (auto I = StartI, EE = EndI; I != EE; ++I) {
805 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
806 return true;
807 }
808 return false;
809}
810
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000811OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
812 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
813 if (I->CurScope == S)
814 return I->Directive;
815 return OMPD_unknown;
816}
817
Alexey Bataev758e55e2013-09-06 18:03:48 +0000818void Sema::InitDataSharingAttributesStack() {
819 VarDataSharingAttributesStack = new DSAStackTy(*this);
820}
821
822#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
823
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000824bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000825 const CapturedRegionScopeInfo *RSI) {
826 assert(LangOpts.OpenMP && "OpenMP is not allowed");
827
828 auto &Ctx = getASTContext();
829 bool IsByRef = true;
830
831 // Find the directive that is associated with the provided scope.
832 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000833 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000834
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000835 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000836 // This table summarizes how a given variable should be passed to the device
837 // given its type and the clauses where it appears. This table is based on
838 // the description in OpenMP 4.5 [2.10.4, target Construct] and
839 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
840 //
841 // =========================================================================
842 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
843 // | |(tofrom:scalar)| | pvt | | | |
844 // =========================================================================
845 // | scl | | | | - | | bycopy|
846 // | scl | | - | x | - | - | bycopy|
847 // | scl | | x | - | - | - | null |
848 // | scl | x | | | - | | byref |
849 // | scl | x | - | x | - | - | bycopy|
850 // | scl | x | x | - | - | - | null |
851 // | scl | | - | - | - | x | byref |
852 // | scl | x | - | - | - | x | byref |
853 //
854 // | agg | n.a. | | | - | | byref |
855 // | agg | n.a. | - | x | - | - | byref |
856 // | agg | n.a. | x | - | - | - | null |
857 // | agg | n.a. | - | - | - | x | byref |
858 // | agg | n.a. | - | - | - | x[] | byref |
859 //
860 // | ptr | n.a. | | | - | | bycopy|
861 // | ptr | n.a. | - | x | - | - | bycopy|
862 // | ptr | n.a. | x | - | - | - | null |
863 // | ptr | n.a. | - | - | - | x | byref |
864 // | ptr | n.a. | - | - | - | x[] | bycopy|
865 // | ptr | n.a. | - | - | x | | bycopy|
866 // | ptr | n.a. | - | - | x | x | bycopy|
867 // | ptr | n.a. | - | - | x | x[] | bycopy|
868 // =========================================================================
869 // Legend:
870 // scl - scalar
871 // ptr - pointer
872 // agg - aggregate
873 // x - applies
874 // - - invalid in this combination
875 // [] - mapped with an array section
876 // byref - should be mapped by reference
877 // byval - should be mapped by value
878 // null - initialize a local variable to null on the device
879 //
880 // Observations:
881 // - All scalar declarations that show up in a map clause have to be passed
882 // by reference, because they may have been mapped in the enclosing data
883 // environment.
884 // - If the scalar value does not fit the size of uintptr, it has to be
885 // passed by reference, regardless the result in the table above.
886 // - For pointers mapped by value that have either an implicit map or an
887 // array section, the runtime library may pass the NULL value to the
888 // device instead of the value passed to it by the compiler.
889
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000890
891 if (Ty->isReferenceType())
892 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +0000893
894 // Locate map clauses and see if the variable being captured is referred to
895 // in any of those clauses. Here we only care about variables, not fields,
896 // because fields are part of aggregates.
897 bool IsVariableUsedInMapClause = false;
898 bool IsVariableAssociatedWithSection = false;
899
900 DSAStack->checkMappableExprComponentListsForDecl(
901 D, /*CurrentRegionOnly=*/true,
902 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
903 MapExprComponents) {
904
905 auto EI = MapExprComponents.rbegin();
906 auto EE = MapExprComponents.rend();
907
908 assert(EI != EE && "Invalid map expression!");
909
910 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
911 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
912
913 ++EI;
914 if (EI == EE)
915 return false;
916
917 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
918 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
919 isa<MemberExpr>(EI->getAssociatedExpression())) {
920 IsVariableAssociatedWithSection = true;
921 // There is nothing more we need to know about this variable.
922 return true;
923 }
924
925 // Keep looking for more map info.
926 return false;
927 });
928
929 if (IsVariableUsedInMapClause) {
930 // If variable is identified in a map clause it is always captured by
931 // reference except if it is a pointer that is dereferenced somehow.
932 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
933 } else {
934 // By default, all the data that has a scalar type is mapped by copy.
935 IsByRef = !Ty->isScalarType();
936 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000937 }
938
Samuel Antao86ace552016-04-27 22:40:57 +0000939 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000940 // and alignment, because the runtime library only deals with uintptr types.
941 // If it does not fit the uintptr size, we need to pass the data by reference
942 // instead.
943 if (!IsByRef &&
944 (Ctx.getTypeSizeInChars(Ty) >
945 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000946 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000947 IsByRef = true;
948
949 return IsByRef;
950}
951
Alexey Bataev90c228f2016-02-08 09:29:13 +0000952VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000953 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000954 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000955
956 // If we are attempting to capture a global variable in a directive with
957 // 'target' we return true so that this global is also mapped to the device.
958 //
959 // FIXME: If the declaration is enclosed in a 'declare target' directive,
960 // then it should not be captured. Therefore, an extra check has to be
961 // inserted here once support for 'declare target' is added.
962 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000963 auto *VD = dyn_cast<VarDecl>(D);
964 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000965 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000966 !DSAStack->isClauseParsingMode())
967 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000968 if (DSAStack->getCurScope() &&
969 DSAStack->hasDirective(
970 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
971 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000972 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000973 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000974 false))
975 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000976 }
977
Alexey Bataev48977c32015-08-04 08:10:48 +0000978 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
979 (!DSAStack->isClauseParsingMode() ||
980 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000981 auto &&Info = DSAStack->isLoopControlVariable(D);
982 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000983 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000984 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000985 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000986 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000987 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000988 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000989 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000990 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000991 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000992 if (DVarPrivate.CKind != OMPC_unknown)
993 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000994 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000995 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000996}
997
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000998bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000999 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1000 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001001 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +00001002}
1003
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001004bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +00001005 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1006 // Return true if the current level is no longer enclosed in a target region.
1007
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001008 auto *VD = dyn_cast<VarDecl>(D);
1009 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001010 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1011 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001012}
1013
Alexey Bataeved09d242014-05-28 05:53:51 +00001014void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001015
1016void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1017 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001018 Scope *CurScope, SourceLocation Loc) {
1019 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001020 PushExpressionEvaluationContext(PotentiallyEvaluated);
1021}
1022
Alexey Bataevaac108a2015-06-23 04:51:00 +00001023void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1024 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001025}
1026
Alexey Bataevaac108a2015-06-23 04:51:00 +00001027void Sema::EndOpenMPClause() {
1028 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001029}
1030
Alexey Bataev758e55e2013-09-06 18:03:48 +00001031void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001032 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1033 // A variable of class type (or array thereof) that appears in a lastprivate
1034 // clause requires an accessible, unambiguous default constructor for the
1035 // class type, unless the list item is also specified in a firstprivate
1036 // clause.
1037 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001038 for (auto *C : D->clauses()) {
1039 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1040 SmallVector<Expr *, 8> PrivateCopies;
1041 for (auto *DE : Clause->varlists()) {
1042 if (DE->isValueDependent() || DE->isTypeDependent()) {
1043 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001044 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001045 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001046 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001047 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1048 QualType Type = VD->getType().getNonReferenceType();
1049 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001050 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001051 // Generate helper private variable and initialize it with the
1052 // default value. The address of the original variable is replaced
1053 // by the address of the new private variable in CodeGen. This new
1054 // variable is not added to IdResolver, so the code in the OpenMP
1055 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001056 auto *VDPrivate = buildVarDecl(
1057 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001058 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001059 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1060 if (VDPrivate->isInvalidDecl())
1061 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001062 PrivateCopies.push_back(buildDeclRefExpr(
1063 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001064 } else {
1065 // The variable is also a firstprivate, so initialization sequence
1066 // for private copy is generated already.
1067 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001068 }
1069 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001070 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001071 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001072 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001073 }
1074 }
1075 }
1076
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077 DSAStack->pop();
1078 DiscardCleanupsInEvaluationContext();
1079 PopExpressionEvaluationContext();
1080}
1081
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001082static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1083 Expr *NumIterations, Sema &SemaRef,
1084 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001085
Alexey Bataeva769e072013-03-22 06:34:35 +00001086namespace {
1087
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001088class VarDeclFilterCCC : public CorrectionCandidateCallback {
1089private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001090 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001091
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001093 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001094 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095 NamedDecl *ND = Candidate.getCorrectionDecl();
1096 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1097 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001098 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1099 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001100 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001101 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001102 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001103};
Alexey Bataeved09d242014-05-28 05:53:51 +00001104} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105
1106ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1107 CXXScopeSpec &ScopeSpec,
1108 const DeclarationNameInfo &Id) {
1109 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1110 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1111
1112 if (Lookup.isAmbiguous())
1113 return ExprError();
1114
1115 VarDecl *VD;
1116 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001117 if (TypoCorrection Corrected = CorrectTypo(
1118 Id, LookupOrdinaryName, CurScope, nullptr,
1119 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001120 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001121 PDiag(Lookup.empty()
1122 ? diag::err_undeclared_var_use_suggest
1123 : diag::err_omp_expected_var_arg_suggest)
1124 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001125 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001126 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001127 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1128 : diag::err_omp_expected_var_arg)
1129 << Id.getName();
1130 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001131 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001132 } else {
1133 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001134 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001135 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1136 return ExprError();
1137 }
1138 }
1139 Lookup.suppressDiagnostics();
1140
1141 // OpenMP [2.9.2, Syntax, C/C++]
1142 // Variables must be file-scope, namespace-scope, or static block-scope.
1143 if (!VD->hasGlobalStorage()) {
1144 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001145 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1146 bool IsDecl =
1147 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001149 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1150 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 return ExprError();
1152 }
1153
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001154 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1155 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001156 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1157 // A threadprivate directive for file-scope variables must appear outside
1158 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001159 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1160 !getCurLexicalContext()->isTranslationUnit()) {
1161 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001162 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1163 bool IsDecl =
1164 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1165 Diag(VD->getLocation(),
1166 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1167 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001168 return ExprError();
1169 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001170 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1171 // A threadprivate directive for static class member variables must appear
1172 // in the class definition, in the same scope in which the member
1173 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001174 if (CanonicalVD->isStaticDataMember() &&
1175 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1176 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001177 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1178 bool IsDecl =
1179 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1180 Diag(VD->getLocation(),
1181 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1182 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001183 return ExprError();
1184 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001185 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1186 // A threadprivate directive for namespace-scope variables must appear
1187 // outside any definition or declaration other than the namespace
1188 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001189 if (CanonicalVD->getDeclContext()->isNamespace() &&
1190 (!getCurLexicalContext()->isFileContext() ||
1191 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1192 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001193 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1194 bool IsDecl =
1195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1196 Diag(VD->getLocation(),
1197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1198 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001199 return ExprError();
1200 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001201 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1202 // A threadprivate directive for static block-scope variables must appear
1203 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001204 if (CanonicalVD->isStaticLocal() && CurScope &&
1205 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001206 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208 bool IsDecl =
1209 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210 Diag(VD->getLocation(),
1211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001213 return ExprError();
1214 }
1215
1216 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1217 // A threadprivate directive must lexically precede all references to any
1218 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001219 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001220 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001221 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001222 return ExprError();
1223 }
1224
1225 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001226 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1227 SourceLocation(), VD,
1228 /*RefersToEnclosingVariableOrCapture=*/false,
1229 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230}
1231
Alexey Bataeved09d242014-05-28 05:53:51 +00001232Sema::DeclGroupPtrTy
1233Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1234 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001235 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001236 CurContext->addDecl(D);
1237 return DeclGroupPtrTy::make(DeclGroupRef(D));
1238 }
David Blaikie0403cb12016-01-15 23:43:25 +00001239 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001240}
1241
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001242namespace {
1243class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1244 Sema &SemaRef;
1245
1246public:
1247 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1248 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1249 if (VD->hasLocalStorage()) {
1250 SemaRef.Diag(E->getLocStart(),
1251 diag::err_omp_local_var_in_threadprivate_init)
1252 << E->getSourceRange();
1253 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1254 << VD << VD->getSourceRange();
1255 return true;
1256 }
1257 }
1258 return false;
1259 }
1260 bool VisitStmt(const Stmt *S) {
1261 for (auto Child : S->children()) {
1262 if (Child && Visit(Child))
1263 return true;
1264 }
1265 return false;
1266 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001267 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001268};
1269} // namespace
1270
Alexey Bataeved09d242014-05-28 05:53:51 +00001271OMPThreadPrivateDecl *
1272Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001273 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001274 for (auto &RefExpr : VarList) {
1275 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001276 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1277 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001278
Alexey Bataev376b4a42016-02-09 09:41:09 +00001279 // Mark variable as used.
1280 VD->setReferenced();
1281 VD->markUsed(Context);
1282
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001283 QualType QType = VD->getType();
1284 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1285 // It will be analyzed later.
1286 Vars.push_back(DE);
1287 continue;
1288 }
1289
Alexey Bataeva769e072013-03-22 06:34:35 +00001290 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1291 // A threadprivate variable must not have an incomplete type.
1292 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001293 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001294 continue;
1295 }
1296
1297 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1298 // A threadprivate variable must not have a reference type.
1299 if (VD->getType()->isReferenceType()) {
1300 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001301 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1302 bool IsDecl =
1303 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1304 Diag(VD->getLocation(),
1305 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1306 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001307 continue;
1308 }
1309
Samuel Antaof8b50122015-07-13 22:54:53 +00001310 // Check if this is a TLS variable. If TLS is not being supported, produce
1311 // the corresponding diagnostic.
1312 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1313 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1314 getLangOpts().OpenMPUseTLS &&
1315 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001316 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1317 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001318 Diag(ILoc, diag::err_omp_var_thread_local)
1319 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001320 bool IsDecl =
1321 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1322 Diag(VD->getLocation(),
1323 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1324 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001325 continue;
1326 }
1327
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001328 // Check if initial value of threadprivate variable reference variable with
1329 // local storage (it is not supported by runtime).
1330 if (auto Init = VD->getAnyInitializer()) {
1331 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001332 if (Checker.Visit(Init))
1333 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001334 }
1335
Alexey Bataeved09d242014-05-28 05:53:51 +00001336 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001337 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001338 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1339 Context, SourceRange(Loc, Loc)));
1340 if (auto *ML = Context.getASTMutationListener())
1341 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001342 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001343 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001344 if (!Vars.empty()) {
1345 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1346 Vars);
1347 D->setAccess(AS_public);
1348 }
1349 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001350}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001351
Alexey Bataev7ff55242014-06-19 09:13:45 +00001352static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001353 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001354 bool IsLoopIterVar = false) {
1355 if (DVar.RefExpr) {
1356 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1357 << getOpenMPClauseName(DVar.CKind);
1358 return;
1359 }
1360 enum {
1361 PDSA_StaticMemberShared,
1362 PDSA_StaticLocalVarShared,
1363 PDSA_LoopIterVarPrivate,
1364 PDSA_LoopIterVarLinear,
1365 PDSA_LoopIterVarLastprivate,
1366 PDSA_ConstVarShared,
1367 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001369 PDSA_LocalVarPrivate,
1370 PDSA_Implicit
1371 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001372 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001373 auto ReportLoc = D->getLocation();
1374 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001375 if (IsLoopIterVar) {
1376 if (DVar.CKind == OMPC_private)
1377 Reason = PDSA_LoopIterVarPrivate;
1378 else if (DVar.CKind == OMPC_lastprivate)
1379 Reason = PDSA_LoopIterVarLastprivate;
1380 else
1381 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001382 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1383 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001384 Reason = PDSA_TaskVarFirstprivate;
1385 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001386 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001387 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001388 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001389 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001390 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001391 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001392 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001393 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001394 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001395 ReportHint = true;
1396 Reason = PDSA_LocalVarPrivate;
1397 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001398 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001399 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001400 << Reason << ReportHint
1401 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1402 } else if (DVar.ImplicitDSALoc.isValid()) {
1403 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1404 << getOpenMPClauseName(DVar.CKind);
1405 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001406}
1407
Alexey Bataev758e55e2013-09-06 18:03:48 +00001408namespace {
1409class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1410 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001411 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001412 bool ErrorFound;
1413 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001414 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001415 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001416
Alexey Bataev758e55e2013-09-06 18:03:48 +00001417public:
1418 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001419 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001420 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001421 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1422 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001423
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001424 auto DVar = Stack->getTopDSA(VD, false);
1425 // Check if the variable has explicit DSA set and stop analysis if it so.
1426 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001427
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001428 auto ELoc = E->getExprLoc();
1429 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001430 // The default(none) clause requires that each variable that is referenced
1431 // in the construct, and does not have a predetermined data-sharing
1432 // attribute, must have its data-sharing attribute explicitly determined
1433 // by being listed in a data-sharing attribute clause.
1434 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001435 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001436 VarsWithInheritedDSA.count(VD) == 0) {
1437 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001438 return;
1439 }
1440
1441 // OpenMP [2.9.3.6, Restrictions, p.2]
1442 // A list item that appears in a reduction clause of the innermost
1443 // enclosing worksharing or parallel construct may not be accessed in an
1444 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001445 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001446 [](OpenMPDirectiveKind K) -> bool {
1447 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001448 isOpenMPWorksharingDirective(K) ||
1449 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001450 },
1451 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001452 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001453 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001454 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1455 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001456 return;
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458
1459 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001460 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001461 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1462 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001463 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001464 }
1465 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001466 void VisitMemberExpr(MemberExpr *E) {
1467 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1468 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1469 auto DVar = Stack->getTopDSA(FD, false);
1470 // Check if the variable has explicit DSA set and stop analysis if it
1471 // so.
1472 if (DVar.RefExpr)
1473 return;
1474
1475 auto ELoc = E->getExprLoc();
1476 auto DKind = Stack->getCurrentDirective();
1477 // OpenMP [2.9.3.6, Restrictions, p.2]
1478 // A list item that appears in a reduction clause of the innermost
1479 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001480 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001481 DVar =
1482 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1483 [](OpenMPDirectiveKind K) -> bool {
1484 return isOpenMPParallelDirective(K) ||
1485 isOpenMPWorksharingDirective(K) ||
1486 isOpenMPTeamsDirective(K);
1487 },
1488 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001489 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001490 ErrorFound = true;
1491 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1492 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1493 return;
1494 }
1495
1496 // Define implicit data-sharing attributes for task.
1497 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001498 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1499 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001500 ImplicitFirstprivate.push_back(E);
1501 }
1502 }
1503 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001504 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001505 for (auto *C : S->clauses()) {
1506 // Skip analysis of arguments of implicitly defined firstprivate clause
1507 // for task directives.
1508 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1509 for (auto *CC : C->children()) {
1510 if (CC)
1511 Visit(CC);
1512 }
1513 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001514 }
1515 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001516 for (auto *C : S->children()) {
1517 if (C && !isa<OMPExecutableDirective>(C))
1518 Visit(C);
1519 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001520 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001521
1522 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001523 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001524 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001525 return VarsWithInheritedDSA;
1526 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001527
Alexey Bataev7ff55242014-06-19 09:13:45 +00001528 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1529 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001530};
Alexey Bataeved09d242014-05-28 05:53:51 +00001531} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001532
Alexey Bataevbae9a792014-06-27 10:37:06 +00001533void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001534 switch (DKind) {
1535 case OMPD_parallel: {
1536 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001537 QualType KmpInt32PtrTy =
1538 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001539 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001540 std::make_pair(".global_tid.", KmpInt32PtrTy),
1541 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1542 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001543 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001544 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1545 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001546 break;
1547 }
1548 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001549 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001550 std::make_pair(StringRef(), QualType()) // __context with shared vars
1551 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001552 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1553 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001554 break;
1555 }
1556 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001557 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001558 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001559 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001560 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1561 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001562 break;
1563 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001564 case OMPD_for_simd: {
1565 Sema::CapturedParamNameType Params[] = {
1566 std::make_pair(StringRef(), QualType()) // __context with shared vars
1567 };
1568 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1569 Params);
1570 break;
1571 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001572 case OMPD_sections: {
1573 Sema::CapturedParamNameType Params[] = {
1574 std::make_pair(StringRef(), QualType()) // __context with shared vars
1575 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001578 break;
1579 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001580 case OMPD_section: {
1581 Sema::CapturedParamNameType Params[] = {
1582 std::make_pair(StringRef(), QualType()) // __context with shared vars
1583 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001584 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1585 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001586 break;
1587 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001588 case OMPD_single: {
1589 Sema::CapturedParamNameType Params[] = {
1590 std::make_pair(StringRef(), QualType()) // __context with shared vars
1591 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001592 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1593 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001594 break;
1595 }
Alexander Musman80c22892014-07-17 08:54:58 +00001596 case OMPD_master: {
1597 Sema::CapturedParamNameType Params[] = {
1598 std::make_pair(StringRef(), QualType()) // __context with shared vars
1599 };
1600 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1601 Params);
1602 break;
1603 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001604 case OMPD_critical: {
1605 Sema::CapturedParamNameType Params[] = {
1606 std::make_pair(StringRef(), QualType()) // __context with shared vars
1607 };
1608 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1609 Params);
1610 break;
1611 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001612 case OMPD_parallel_for: {
1613 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001614 QualType KmpInt32PtrTy =
1615 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001616 Sema::CapturedParamNameType Params[] = {
1617 std::make_pair(".global_tid.", KmpInt32PtrTy),
1618 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1619 std::make_pair(StringRef(), QualType()) // __context with shared vars
1620 };
1621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1622 Params);
1623 break;
1624 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001625 case OMPD_parallel_for_simd: {
1626 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001627 QualType KmpInt32PtrTy =
1628 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001629 Sema::CapturedParamNameType Params[] = {
1630 std::make_pair(".global_tid.", KmpInt32PtrTy),
1631 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1632 std::make_pair(StringRef(), QualType()) // __context with shared vars
1633 };
1634 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635 Params);
1636 break;
1637 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001638 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001639 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001640 QualType KmpInt32PtrTy =
1641 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001642 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001643 std::make_pair(".global_tid.", KmpInt32PtrTy),
1644 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001645 std::make_pair(StringRef(), QualType()) // __context with shared vars
1646 };
1647 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1648 Params);
1649 break;
1650 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001651 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001652 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001653 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1654 FunctionProtoType::ExtProtoInfo EPI;
1655 EPI.Variadic = true;
1656 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001657 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001658 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001659 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1660 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1661 std::make_pair(".copy_fn.",
1662 Context.getPointerType(CopyFnType).withConst()),
1663 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001664 std::make_pair(StringRef(), QualType()) // __context with shared vars
1665 };
1666 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001668 // Mark this captured region as inlined, because we don't use outlined
1669 // function directly.
1670 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1671 AlwaysInlineAttr::CreateImplicit(
1672 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001673 break;
1674 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001675 case OMPD_ordered: {
1676 Sema::CapturedParamNameType Params[] = {
1677 std::make_pair(StringRef(), QualType()) // __context with shared vars
1678 };
1679 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1680 Params);
1681 break;
1682 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001683 case OMPD_atomic: {
1684 Sema::CapturedParamNameType Params[] = {
1685 std::make_pair(StringRef(), QualType()) // __context with shared vars
1686 };
1687 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1688 Params);
1689 break;
1690 }
Michael Wong65f367f2015-07-21 13:44:28 +00001691 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001692 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001693 case OMPD_target_parallel:
1694 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001695 Sema::CapturedParamNameType Params[] = {
1696 std::make_pair(StringRef(), QualType()) // __context with shared vars
1697 };
1698 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699 Params);
1700 break;
1701 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001702 case OMPD_teams: {
1703 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001704 QualType KmpInt32PtrTy =
1705 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001706 Sema::CapturedParamNameType Params[] = {
1707 std::make_pair(".global_tid.", KmpInt32PtrTy),
1708 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1709 std::make_pair(StringRef(), QualType()) // __context with shared vars
1710 };
1711 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1712 Params);
1713 break;
1714 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001715 case OMPD_taskgroup: {
1716 Sema::CapturedParamNameType Params[] = {
1717 std::make_pair(StringRef(), QualType()) // __context with shared vars
1718 };
1719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1720 Params);
1721 break;
1722 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001723 case OMPD_taskloop: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001724 QualType KmpInt32Ty =
1725 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1726 QualType KmpUInt64Ty =
1727 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1728 QualType KmpInt64Ty =
1729 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1730 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1731 FunctionProtoType::ExtProtoInfo EPI;
1732 EPI.Variadic = true;
1733 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001734 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001735 std::make_pair(".global_tid.", KmpInt32Ty),
1736 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1737 std::make_pair(".privates.",
1738 Context.VoidPtrTy.withConst().withRestrict()),
1739 std::make_pair(
1740 ".copy_fn.",
1741 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1742 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1743 std::make_pair(".lb.", KmpUInt64Ty),
1744 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1745 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001746 std::make_pair(StringRef(), QualType()) // __context with shared vars
1747 };
1748 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1749 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001750 // Mark this captured region as inlined, because we don't use outlined
1751 // function directly.
1752 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1753 AlwaysInlineAttr::CreateImplicit(
1754 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001755 break;
1756 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001757 case OMPD_taskloop_simd: {
1758 Sema::CapturedParamNameType Params[] = {
1759 std::make_pair(StringRef(), QualType()) // __context with shared vars
1760 };
1761 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1762 Params);
1763 break;
1764 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001765 case OMPD_distribute: {
1766 Sema::CapturedParamNameType Params[] = {
1767 std::make_pair(StringRef(), QualType()) // __context with shared vars
1768 };
1769 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1770 Params);
1771 break;
1772 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001773 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001774 case OMPD_taskyield:
1775 case OMPD_barrier:
1776 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001777 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001778 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001779 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001780 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001781 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001782 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001783 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001784 case OMPD_declare_target:
1785 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001786 llvm_unreachable("OpenMP Directive is not allowed");
1787 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001788 llvm_unreachable("Unknown OpenMP directive");
1789 }
1790}
1791
Alexey Bataev3392d762016-02-16 11:18:12 +00001792static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001793 Expr *CaptureExpr, bool WithInit,
1794 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001795 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001796 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001797 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001798 QualType Ty = Init->getType();
1799 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1800 if (S.getLangOpts().CPlusPlus)
1801 Ty = C.getLValueReferenceType(Ty);
1802 else {
1803 Ty = C.getPointerType(Ty);
1804 ExprResult Res =
1805 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1806 if (!Res.isUsable())
1807 return nullptr;
1808 Init = Res.get();
1809 }
Alexey Bataev61205072016-03-02 04:57:40 +00001810 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001811 }
1812 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001813 if (!WithInit)
1814 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001815 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001816 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1817 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001818 return CED;
1819}
1820
Alexey Bataev61205072016-03-02 04:57:40 +00001821static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1822 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001823 OMPCapturedExprDecl *CD;
1824 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1825 CD = cast<OMPCapturedExprDecl>(VD);
1826 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001827 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1828 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001829 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001830 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001831}
1832
Alexey Bataev5a3af132016-03-29 08:58:54 +00001833static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1834 if (!Ref) {
1835 auto *CD =
1836 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1837 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1838 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1839 CaptureExpr->getExprLoc());
1840 }
1841 ExprResult Res = Ref;
1842 if (!S.getLangOpts().CPlusPlus &&
1843 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1844 Ref->getType()->isPointerType())
1845 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1846 if (!Res.isUsable())
1847 return ExprError();
1848 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001849}
1850
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001851StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1852 ArrayRef<OMPClause *> Clauses) {
1853 if (!S.isUsable()) {
1854 ActOnCapturedRegionError();
1855 return StmtError();
1856 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001857
1858 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001859 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001860 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001861 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001862 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001863 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001864 Clause->getClauseKind() == OMPC_copyprivate ||
1865 (getLangOpts().OpenMPUseTLS &&
1866 getASTContext().getTargetInfo().isTLSSupported() &&
1867 Clause->getClauseKind() == OMPC_copyin)) {
1868 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001869 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001870 for (auto *VarRef : Clause->children()) {
1871 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001872 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001873 }
1874 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001875 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001876 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001877 // Mark all variables in private list clauses as used in inner region.
1878 // Required for proper codegen of combined directives.
1879 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001880 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001881 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1882 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001883 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1884 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001885 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001886 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1887 if (auto *E = C->getPostUpdateExpr())
1888 MarkDeclarationsReferencedInExpr(E);
1889 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001890 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001891 if (Clause->getClauseKind() == OMPC_schedule)
1892 SC = cast<OMPScheduleClause>(Clause);
1893 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001894 OC = cast<OMPOrderedClause>(Clause);
1895 else if (Clause->getClauseKind() == OMPC_linear)
1896 LCs.push_back(cast<OMPLinearClause>(Clause));
1897 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001898 bool ErrorFound = false;
1899 // OpenMP, 2.7.1 Loop Construct, Restrictions
1900 // The nonmonotonic modifier cannot be specified if an ordered clause is
1901 // specified.
1902 if (SC &&
1903 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1904 SC->getSecondScheduleModifier() ==
1905 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1906 OC) {
1907 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1908 ? SC->getFirstScheduleModifierLoc()
1909 : SC->getSecondScheduleModifierLoc(),
1910 diag::err_omp_schedule_nonmonotonic_ordered)
1911 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1912 ErrorFound = true;
1913 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001914 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1915 for (auto *C : LCs) {
1916 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1917 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1918 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001919 ErrorFound = true;
1920 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001921 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1922 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1923 OC->getNumForLoops()) {
1924 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1925 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1926 ErrorFound = true;
1927 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001928 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001929 ActOnCapturedRegionError();
1930 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001931 }
1932 return ActOnCapturedRegionEnd(S.get());
1933}
1934
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001935static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1936 OpenMPDirectiveKind CurrentRegion,
1937 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001938 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001939 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001940 // Allowed nesting of constructs
1941 // +------------------+-----------------+------------------------------------+
1942 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1943 // +------------------+-----------------+------------------------------------+
1944 // | parallel | parallel | * |
1945 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001946 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001947 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001948 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001949 // | parallel | simd | * |
1950 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001951 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001952 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001953 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001954 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001955 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001956 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001957 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001958 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001959 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001960 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001961 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001962 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001963 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001964 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001965 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001966 // | parallel | target parallel | * |
1967 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001968 // | parallel | target enter | * |
1969 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001970 // | parallel | target exit | * |
1971 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001972 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001973 // | parallel | cancellation | |
1974 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001975 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001976 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001977 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001978 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001979 // +------------------+-----------------+------------------------------------+
1980 // | for | parallel | * |
1981 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001982 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001983 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001984 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001985 // | for | simd | * |
1986 // | for | sections | + |
1987 // | for | section | + |
1988 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001989 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001990 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001991 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001992 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001993 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001994 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001995 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001996 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001997 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001998 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001999 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002000 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002001 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002002 // | for | target parallel | * |
2003 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002004 // | for | target enter | * |
2005 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002006 // | for | target exit | * |
2007 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002008 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002009 // | for | cancellation | |
2010 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002011 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002012 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002013 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002014 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002015 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002016 // | master | parallel | * |
2017 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002018 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002019 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002020 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002021 // | master | simd | * |
2022 // | master | sections | + |
2023 // | master | section | + |
2024 // | master | single | + |
2025 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002026 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002027 // | master |parallel sections| * |
2028 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002029 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002030 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002031 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002032 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002033 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002034 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002035 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002036 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002037 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002038 // | master | target parallel | * |
2039 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002040 // | master | target enter | * |
2041 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002042 // | master | target exit | * |
2043 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002044 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002045 // | master | cancellation | |
2046 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002047 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002048 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002049 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002050 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002051 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002052 // | critical | parallel | * |
2053 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002054 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002055 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002056 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002057 // | critical | simd | * |
2058 // | critical | sections | + |
2059 // | critical | section | + |
2060 // | critical | single | + |
2061 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002062 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002063 // | critical |parallel sections| * |
2064 // | critical | task | * |
2065 // | critical | taskyield | * |
2066 // | critical | barrier | + |
2067 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002068 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002069 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002070 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002071 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002072 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002073 // | critical | target parallel | * |
2074 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002075 // | critical | target enter | * |
2076 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002077 // | critical | target exit | * |
2078 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002079 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002080 // | critical | cancellation | |
2081 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002082 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002083 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002084 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002085 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002086 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002087 // | simd | parallel | |
2088 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002089 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002090 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002091 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002093 // | simd | sections | |
2094 // | simd | section | |
2095 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002096 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002097 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002098 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002099 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002100 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002101 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002102 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002104 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002106 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002107 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | simd | target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | simd | target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | simd | target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | simd | cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002122 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002123 // | for simd | parallel | |
2124 // | for simd | for | |
2125 // | for simd | for simd | |
2126 // | for simd | master | |
2127 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002128 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002129 // | for simd | sections | |
2130 // | for simd | section | |
2131 // | for simd | single | |
2132 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002133 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002134 // | for simd |parallel sections| |
2135 // | for simd | task | |
2136 // | for simd | taskyield | |
2137 // | for simd | barrier | |
2138 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002139 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002140 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002141 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002142 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002143 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002144 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002145 // | for simd | target parallel | |
2146 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002147 // | for simd | target enter | |
2148 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002149 // | for simd | target exit | |
2150 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002151 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002152 // | for simd | cancellation | |
2153 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002154 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002155 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002156 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002157 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002158 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002159 // | parallel for simd| parallel | |
2160 // | parallel for simd| for | |
2161 // | parallel for simd| for simd | |
2162 // | parallel for simd| master | |
2163 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002164 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002165 // | parallel for simd| sections | |
2166 // | parallel for simd| section | |
2167 // | parallel for simd| single | |
2168 // | parallel for simd| parallel for | |
2169 // | parallel for simd|parallel for simd| |
2170 // | parallel for simd|parallel sections| |
2171 // | parallel for simd| task | |
2172 // | parallel for simd| taskyield | |
2173 // | parallel for simd| barrier | |
2174 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002175 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002176 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002177 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002178 // | parallel for simd| atomic | |
2179 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002180 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002181 // | parallel for simd| target parallel | |
2182 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002183 // | parallel for simd| target enter | |
2184 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002185 // | parallel for simd| target exit | |
2186 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002187 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002188 // | parallel for simd| cancellation | |
2189 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002190 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002191 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002192 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002193 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002194 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002195 // | sections | parallel | * |
2196 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002197 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002198 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002199 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002200 // | sections | simd | * |
2201 // | sections | sections | + |
2202 // | sections | section | * |
2203 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002204 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002205 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002206 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002207 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002208 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002209 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002210 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002211 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002212 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002214 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002216 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002217 // | sections | target parallel | * |
2218 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002219 // | sections | target enter | * |
2220 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002221 // | sections | target exit | * |
2222 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002223 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002224 // | sections | cancellation | |
2225 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002226 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002227 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002228 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002229 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002230 // +------------------+-----------------+------------------------------------+
2231 // | section | parallel | * |
2232 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002233 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002234 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002235 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002236 // | section | simd | * |
2237 // | section | sections | + |
2238 // | section | section | + |
2239 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002240 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002241 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002242 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002243 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002244 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002245 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002246 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002247 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002248 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002249 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002250 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002251 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002252 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002253 // | section | target parallel | * |
2254 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002255 // | section | target enter | * |
2256 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002257 // | section | target exit | * |
2258 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002259 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002260 // | section | cancellation | |
2261 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002262 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002263 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002264 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002265 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002266 // +------------------+-----------------+------------------------------------+
2267 // | single | parallel | * |
2268 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002269 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002270 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002271 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002272 // | single | simd | * |
2273 // | single | sections | + |
2274 // | single | section | + |
2275 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002276 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002277 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002278 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002279 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002280 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002281 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002282 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002283 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002284 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002285 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002286 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002287 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002288 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002289 // | single | target parallel | * |
2290 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002291 // | single | target enter | * |
2292 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002293 // | single | target exit | * |
2294 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002295 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002296 // | single | cancellation | |
2297 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002298 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002299 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002300 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002301 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002302 // +------------------+-----------------+------------------------------------+
2303 // | parallel for | parallel | * |
2304 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002305 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002306 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002307 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002308 // | parallel for | simd | * |
2309 // | parallel for | sections | + |
2310 // | parallel for | section | + |
2311 // | parallel for | single | + |
2312 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002313 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002314 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002315 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002316 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002317 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002318 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002319 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002320 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002321 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002322 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002323 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002324 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002325 // | parallel for | target parallel | * |
2326 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002327 // | parallel for | target enter | * |
2328 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002329 // | parallel for | target exit | * |
2330 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002331 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002332 // | parallel for | cancellation | |
2333 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002334 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002335 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002336 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002337 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002338 // +------------------+-----------------+------------------------------------+
2339 // | parallel sections| parallel | * |
2340 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002341 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002342 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002343 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002344 // | parallel sections| simd | * |
2345 // | parallel sections| sections | + |
2346 // | parallel sections| section | * |
2347 // | parallel sections| single | + |
2348 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002349 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002350 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002351 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002352 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002353 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002354 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002355 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002356 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002357 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002358 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002359 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002360 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002361 // | parallel sections| target parallel | * |
2362 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002363 // | parallel sections| target enter | * |
2364 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002365 // | parallel sections| target exit | * |
2366 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002367 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002368 // | parallel sections| cancellation | |
2369 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002370 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002371 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002372 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002373 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002374 // +------------------+-----------------+------------------------------------+
2375 // | task | parallel | * |
2376 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002377 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002378 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002379 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002380 // | task | simd | * |
2381 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002382 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002383 // | task | single | + |
2384 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002385 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002386 // | task |parallel sections| * |
2387 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002388 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002389 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002390 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002391 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002392 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002393 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002394 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002395 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002396 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002397 // | task | target parallel | * |
2398 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | task | target enter | * |
2400 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002401 // | task | target exit | * |
2402 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002403 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002404 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002405 // | | point | ! |
2406 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002407 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002408 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002409 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002410 // +------------------+-----------------+------------------------------------+
2411 // | ordered | parallel | * |
2412 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002413 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002414 // | ordered | master | * |
2415 // | ordered | critical | * |
2416 // | ordered | simd | * |
2417 // | ordered | sections | + |
2418 // | ordered | section | + |
2419 // | ordered | single | + |
2420 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002421 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002422 // | ordered |parallel sections| * |
2423 // | ordered | task | * |
2424 // | ordered | taskyield | * |
2425 // | ordered | barrier | + |
2426 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002427 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002428 // | ordered | flush | * |
2429 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002430 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002431 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002432 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002433 // | ordered | target parallel | * |
2434 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002435 // | ordered | target enter | * |
2436 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002437 // | ordered | target exit | * |
2438 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002439 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002440 // | ordered | cancellation | |
2441 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002442 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002443 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002444 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002445 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002446 // +------------------+-----------------+------------------------------------+
2447 // | atomic | parallel | |
2448 // | atomic | for | |
2449 // | atomic | for simd | |
2450 // | atomic | master | |
2451 // | atomic | critical | |
2452 // | atomic | simd | |
2453 // | atomic | sections | |
2454 // | atomic | section | |
2455 // | atomic | single | |
2456 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002457 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002458 // | atomic |parallel sections| |
2459 // | atomic | task | |
2460 // | atomic | taskyield | |
2461 // | atomic | barrier | |
2462 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002463 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002464 // | atomic | flush | |
2465 // | atomic | ordered | |
2466 // | atomic | atomic | |
2467 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002468 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002469 // | atomic | target parallel | |
2470 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002471 // | atomic | target enter | |
2472 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002473 // | atomic | target exit | |
2474 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002475 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002476 // | atomic | cancellation | |
2477 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002478 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002479 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002480 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002481 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002482 // +------------------+-----------------+------------------------------------+
2483 // | target | parallel | * |
2484 // | target | for | * |
2485 // | target | for simd | * |
2486 // | target | master | * |
2487 // | target | critical | * |
2488 // | target | simd | * |
2489 // | target | sections | * |
2490 // | target | section | * |
2491 // | target | single | * |
2492 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002493 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002494 // | target |parallel sections| * |
2495 // | target | task | * |
2496 // | target | taskyield | * |
2497 // | target | barrier | * |
2498 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002499 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002500 // | target | flush | * |
2501 // | target | ordered | * |
2502 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002503 // | target | target | |
2504 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002505 // | target | target parallel | |
2506 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002507 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002508 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002509 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002510 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002511 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002512 // | target | cancellation | |
2513 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002514 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002515 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002516 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002517 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002518 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002519 // | target parallel | parallel | * |
2520 // | target parallel | for | * |
2521 // | target parallel | for simd | * |
2522 // | target parallel | master | * |
2523 // | target parallel | critical | * |
2524 // | target parallel | simd | * |
2525 // | target parallel | sections | * |
2526 // | target parallel | section | * |
2527 // | target parallel | single | * |
2528 // | target parallel | parallel for | * |
2529 // | target parallel |parallel for simd| * |
2530 // | target parallel |parallel sections| * |
2531 // | target parallel | task | * |
2532 // | target parallel | taskyield | * |
2533 // | target parallel | barrier | * |
2534 // | target parallel | taskwait | * |
2535 // | target parallel | taskgroup | * |
2536 // | target parallel | flush | * |
2537 // | target parallel | ordered | * |
2538 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002539 // | target parallel | target | |
2540 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002541 // | target parallel | target parallel | |
2542 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002543 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002544 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002545 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002546 // | | data | |
2547 // | target parallel | teams | |
2548 // | target parallel | cancellation | |
2549 // | | point | ! |
2550 // | target parallel | cancel | ! |
2551 // | target parallel | taskloop | * |
2552 // | target parallel | taskloop simd | * |
2553 // | target parallel | distribute | |
2554 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002555 // | target parallel | parallel | * |
2556 // | for | | |
2557 // | target parallel | for | * |
2558 // | for | | |
2559 // | target parallel | for simd | * |
2560 // | for | | |
2561 // | target parallel | master | * |
2562 // | for | | |
2563 // | target parallel | critical | * |
2564 // | for | | |
2565 // | target parallel | simd | * |
2566 // | for | | |
2567 // | target parallel | sections | * |
2568 // | for | | |
2569 // | target parallel | section | * |
2570 // | for | | |
2571 // | target parallel | single | * |
2572 // | for | | |
2573 // | target parallel | parallel for | * |
2574 // | for | | |
2575 // | target parallel |parallel for simd| * |
2576 // | for | | |
2577 // | target parallel |parallel sections| * |
2578 // | for | | |
2579 // | target parallel | task | * |
2580 // | for | | |
2581 // | target parallel | taskyield | * |
2582 // | for | | |
2583 // | target parallel | barrier | * |
2584 // | for | | |
2585 // | target parallel | taskwait | * |
2586 // | for | | |
2587 // | target parallel | taskgroup | * |
2588 // | for | | |
2589 // | target parallel | flush | * |
2590 // | for | | |
2591 // | target parallel | ordered | * |
2592 // | for | | |
2593 // | target parallel | atomic | * |
2594 // | for | | |
2595 // | target parallel | target | |
2596 // | for | | |
2597 // | target parallel | target parallel | |
2598 // | for | | |
2599 // | target parallel | target parallel | |
2600 // | for | for | |
2601 // | target parallel | target enter | |
2602 // | for | data | |
2603 // | target parallel | target exit | |
2604 // | for | data | |
2605 // | target parallel | teams | |
2606 // | for | | |
2607 // | target parallel | cancellation | |
2608 // | for | point | ! |
2609 // | target parallel | cancel | ! |
2610 // | for | | |
2611 // | target parallel | taskloop | * |
2612 // | for | | |
2613 // | target parallel | taskloop simd | * |
2614 // | for | | |
2615 // | target parallel | distribute | |
2616 // | for | | |
2617 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002618 // | teams | parallel | * |
2619 // | teams | for | + |
2620 // | teams | for simd | + |
2621 // | teams | master | + |
2622 // | teams | critical | + |
2623 // | teams | simd | + |
2624 // | teams | sections | + |
2625 // | teams | section | + |
2626 // | teams | single | + |
2627 // | teams | parallel for | * |
2628 // | teams |parallel for simd| * |
2629 // | teams |parallel sections| * |
2630 // | teams | task | + |
2631 // | teams | taskyield | + |
2632 // | teams | barrier | + |
2633 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002634 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002635 // | teams | flush | + |
2636 // | teams | ordered | + |
2637 // | teams | atomic | + |
2638 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002639 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002640 // | teams | target parallel | + |
2641 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002642 // | teams | target enter | + |
2643 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002644 // | teams | target exit | + |
2645 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002646 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002647 // | teams | cancellation | |
2648 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002649 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002650 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002651 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002652 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002653 // +------------------+-----------------+------------------------------------+
2654 // | taskloop | parallel | * |
2655 // | taskloop | for | + |
2656 // | taskloop | for simd | + |
2657 // | taskloop | master | + |
2658 // | taskloop | critical | * |
2659 // | taskloop | simd | * |
2660 // | taskloop | sections | + |
2661 // | taskloop | section | + |
2662 // | taskloop | single | + |
2663 // | taskloop | parallel for | * |
2664 // | taskloop |parallel for simd| * |
2665 // | taskloop |parallel sections| * |
2666 // | taskloop | task | * |
2667 // | taskloop | taskyield | * |
2668 // | taskloop | barrier | + |
2669 // | taskloop | taskwait | * |
2670 // | taskloop | taskgroup | * |
2671 // | taskloop | flush | * |
2672 // | taskloop | ordered | + |
2673 // | taskloop | atomic | * |
2674 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002675 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002676 // | taskloop | target parallel | * |
2677 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002678 // | taskloop | target enter | * |
2679 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002680 // | taskloop | target exit | * |
2681 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002682 // | taskloop | teams | + |
2683 // | taskloop | cancellation | |
2684 // | | point | |
2685 // | taskloop | cancel | |
2686 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002687 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002688 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002689 // | taskloop simd | parallel | |
2690 // | taskloop simd | for | |
2691 // | taskloop simd | for simd | |
2692 // | taskloop simd | master | |
2693 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002694 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002695 // | taskloop simd | sections | |
2696 // | taskloop simd | section | |
2697 // | taskloop simd | single | |
2698 // | taskloop simd | parallel for | |
2699 // | taskloop simd |parallel for simd| |
2700 // | taskloop simd |parallel sections| |
2701 // | taskloop simd | task | |
2702 // | taskloop simd | taskyield | |
2703 // | taskloop simd | barrier | |
2704 // | taskloop simd | taskwait | |
2705 // | taskloop simd | taskgroup | |
2706 // | taskloop simd | flush | |
2707 // | taskloop simd | ordered | + (with simd clause) |
2708 // | taskloop simd | atomic | |
2709 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002710 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002711 // | taskloop simd | target parallel | |
2712 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002713 // | taskloop simd | target enter | |
2714 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002715 // | taskloop simd | target exit | |
2716 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002717 // | taskloop simd | teams | |
2718 // | taskloop simd | cancellation | |
2719 // | | point | |
2720 // | taskloop simd | cancel | |
2721 // | taskloop simd | taskloop | |
2722 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002723 // | taskloop simd | distribute | |
2724 // +------------------+-----------------+------------------------------------+
2725 // | distribute | parallel | * |
2726 // | distribute | for | * |
2727 // | distribute | for simd | * |
2728 // | distribute | master | * |
2729 // | distribute | critical | * |
2730 // | distribute | simd | * |
2731 // | distribute | sections | * |
2732 // | distribute | section | * |
2733 // | distribute | single | * |
2734 // | distribute | parallel for | * |
2735 // | distribute |parallel for simd| * |
2736 // | distribute |parallel sections| * |
2737 // | distribute | task | * |
2738 // | distribute | taskyield | * |
2739 // | distribute | barrier | * |
2740 // | distribute | taskwait | * |
2741 // | distribute | taskgroup | * |
2742 // | distribute | flush | * |
2743 // | distribute | ordered | + |
2744 // | distribute | atomic | * |
2745 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002746 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002747 // | distribute | target parallel | |
2748 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002749 // | distribute | target enter | |
2750 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002751 // | distribute | target exit | |
2752 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002753 // | distribute | teams | |
2754 // | distribute | cancellation | + |
2755 // | | point | |
2756 // | distribute | cancel | + |
2757 // | distribute | taskloop | * |
2758 // | distribute | taskloop simd | * |
2759 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002760 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002761 if (Stack->getCurScope()) {
2762 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002763 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002764 bool NestingProhibited = false;
2765 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002766 enum {
2767 NoRecommend,
2768 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002769 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002770 ShouldBeInTargetRegion,
2771 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002772 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002773 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2774 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002775 // OpenMP [2.16, Nesting of Regions]
2776 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002777 // OpenMP [2.8.1,simd Construct, Restrictions]
2778 // An ordered construct with the simd clause is the only OpenMP construct
2779 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002780 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2781 return true;
2782 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002783 if (ParentRegion == OMPD_atomic) {
2784 // OpenMP [2.16, Nesting of Regions]
2785 // OpenMP constructs may not be nested inside an atomic region.
2786 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2787 return true;
2788 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002789 if (CurrentRegion == OMPD_section) {
2790 // OpenMP [2.7.2, sections Construct, Restrictions]
2791 // Orphaned section directives are prohibited. That is, the section
2792 // directives must appear within the sections construct and must not be
2793 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002794 if (ParentRegion != OMPD_sections &&
2795 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002796 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2797 << (ParentRegion != OMPD_unknown)
2798 << getOpenMPDirectiveName(ParentRegion);
2799 return true;
2800 }
2801 return false;
2802 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002803 // Allow some constructs to be orphaned (they could be used in functions,
2804 // called from OpenMP regions with the required preconditions).
2805 if (ParentRegion == OMPD_unknown)
2806 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002807 if (CurrentRegion == OMPD_cancellation_point ||
2808 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002809 // OpenMP [2.16, Nesting of Regions]
2810 // A cancellation point construct for which construct-type-clause is
2811 // taskgroup must be nested inside a task construct. A cancellation
2812 // point construct for which construct-type-clause is not taskgroup must
2813 // be closely nested inside an OpenMP construct that matches the type
2814 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002815 // A cancel construct for which construct-type-clause is taskgroup must be
2816 // nested inside a task construct. A cancel construct for which
2817 // construct-type-clause is not taskgroup must be closely nested inside an
2818 // OpenMP construct that matches the type specified in
2819 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002820 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002821 !((CancelRegion == OMPD_parallel &&
2822 (ParentRegion == OMPD_parallel ||
2823 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002824 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002825 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2826 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002827 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2828 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002829 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2830 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002831 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002832 // OpenMP [2.16, Nesting of Regions]
2833 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002834 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002835 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002836 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002837 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2838 // OpenMP [2.16, Nesting of Regions]
2839 // A critical region may not be nested (closely or otherwise) inside a
2840 // critical region with the same name. Note that this restriction is not
2841 // sufficient to prevent deadlock.
2842 SourceLocation PreviousCriticalLoc;
2843 bool DeadLock =
2844 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2845 OpenMPDirectiveKind K,
2846 const DeclarationNameInfo &DNI,
2847 SourceLocation Loc)
2848 ->bool {
2849 if (K == OMPD_critical &&
2850 DNI.getName() == CurrentName.getName()) {
2851 PreviousCriticalLoc = Loc;
2852 return true;
2853 } else
2854 return false;
2855 },
2856 false /* skip top directive */);
2857 if (DeadLock) {
2858 SemaRef.Diag(StartLoc,
2859 diag::err_omp_prohibited_region_critical_same_name)
2860 << CurrentName.getName();
2861 if (PreviousCriticalLoc.isValid())
2862 SemaRef.Diag(PreviousCriticalLoc,
2863 diag::note_omp_previous_critical_region);
2864 return true;
2865 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002866 } else if (CurrentRegion == OMPD_barrier) {
2867 // OpenMP [2.16, Nesting of Regions]
2868 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002869 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002870 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2871 isOpenMPTaskingDirective(ParentRegion) ||
2872 ParentRegion == OMPD_master ||
2873 ParentRegion == OMPD_critical ||
2874 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002875 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002876 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002877 // OpenMP [2.16, Nesting of Regions]
2878 // A worksharing region may not be closely nested inside a worksharing,
2879 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002880 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2881 isOpenMPTaskingDirective(ParentRegion) ||
2882 ParentRegion == OMPD_master ||
2883 ParentRegion == OMPD_critical ||
2884 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002885 Recommend = ShouldBeInParallelRegion;
2886 } else if (CurrentRegion == OMPD_ordered) {
2887 // OpenMP [2.16, Nesting of Regions]
2888 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002889 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002890 // An ordered region must be closely nested inside a loop region (or
2891 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002892 // OpenMP [2.8.1,simd Construct, Restrictions]
2893 // An ordered construct with the simd clause is the only OpenMP construct
2894 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002895 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002896 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002897 !(isOpenMPSimdDirective(ParentRegion) ||
2898 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002899 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002900 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2901 // OpenMP [2.16, Nesting of Regions]
2902 // If specified, a teams construct must be contained within a target
2903 // construct.
2904 NestingProhibited = ParentRegion != OMPD_target;
2905 Recommend = ShouldBeInTargetRegion;
2906 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2907 }
2908 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2909 // OpenMP [2.16, Nesting of Regions]
2910 // distribute, parallel, parallel sections, parallel workshare, and the
2911 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2912 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002913 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2914 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002915 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002916 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002917 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2918 // OpenMP 4.5 [2.17 Nesting of Regions]
2919 // The region associated with the distribute construct must be strictly
2920 // nested inside a teams region
2921 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2922 Recommend = ShouldBeInTeamsRegion;
2923 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002924 if (!NestingProhibited &&
2925 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2926 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2927 // OpenMP 4.5 [2.17 Nesting of Regions]
2928 // If a target, target update, target data, target enter data, or
2929 // target exit data construct is encountered during execution of a
2930 // target region, the behavior is unspecified.
2931 NestingProhibited = Stack->hasDirective(
2932 [&OffendingRegion](OpenMPDirectiveKind K,
2933 const DeclarationNameInfo &DNI,
2934 SourceLocation Loc) -> bool {
2935 if (isOpenMPTargetExecutionDirective(K)) {
2936 OffendingRegion = K;
2937 return true;
2938 } else
2939 return false;
2940 },
2941 false /* don't skip top directive */);
2942 CloseNesting = false;
2943 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002944 if (NestingProhibited) {
2945 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002946 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2947 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002948 return true;
2949 }
2950 }
2951 return false;
2952}
2953
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002954static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2955 ArrayRef<OMPClause *> Clauses,
2956 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2957 bool ErrorFound = false;
2958 unsigned NamedModifiersNumber = 0;
2959 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2960 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002961 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002962 for (const auto *C : Clauses) {
2963 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2964 // At most one if clause without a directive-name-modifier can appear on
2965 // the directive.
2966 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2967 if (FoundNameModifiers[CurNM]) {
2968 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2969 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2970 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2971 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002972 } else if (CurNM != OMPD_unknown) {
2973 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002974 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002975 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002976 FoundNameModifiers[CurNM] = IC;
2977 if (CurNM == OMPD_unknown)
2978 continue;
2979 // Check if the specified name modifier is allowed for the current
2980 // directive.
2981 // At most one if clause with the particular directive-name-modifier can
2982 // appear on the directive.
2983 bool MatchFound = false;
2984 for (auto NM : AllowedNameModifiers) {
2985 if (CurNM == NM) {
2986 MatchFound = true;
2987 break;
2988 }
2989 }
2990 if (!MatchFound) {
2991 S.Diag(IC->getNameModifierLoc(),
2992 diag::err_omp_wrong_if_directive_name_modifier)
2993 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2994 ErrorFound = true;
2995 }
2996 }
2997 }
2998 // If any if clause on the directive includes a directive-name-modifier then
2999 // all if clauses on the directive must include a directive-name-modifier.
3000 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3001 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3002 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3003 diag::err_omp_no_more_if_clause);
3004 } else {
3005 std::string Values;
3006 std::string Sep(", ");
3007 unsigned AllowedCnt = 0;
3008 unsigned TotalAllowedNum =
3009 AllowedNameModifiers.size() - NamedModifiersNumber;
3010 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3011 ++Cnt) {
3012 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3013 if (!FoundNameModifiers[NM]) {
3014 Values += "'";
3015 Values += getOpenMPDirectiveName(NM);
3016 Values += "'";
3017 if (AllowedCnt + 2 == TotalAllowedNum)
3018 Values += " or ";
3019 else if (AllowedCnt + 1 != TotalAllowedNum)
3020 Values += Sep;
3021 ++AllowedCnt;
3022 }
3023 }
3024 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3025 diag::err_omp_unnamed_if_clause)
3026 << (TotalAllowedNum > 1) << Values;
3027 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003028 for (auto Loc : NameModifierLoc) {
3029 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3030 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003031 ErrorFound = true;
3032 }
3033 return ErrorFound;
3034}
3035
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003036StmtResult Sema::ActOnOpenMPExecutableDirective(
3037 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3038 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3039 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003040 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003041 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3042 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003043 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003044
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003045 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003046 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003047 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003048 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003049 if (AStmt) {
3050 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3051
3052 // Check default data sharing attributes for referenced variables.
3053 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3054 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3055 if (DSAChecker.isErrorFound())
3056 return StmtError();
3057 // Generate list of implicitly defined firstprivate variables.
3058 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003059
3060 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3061 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3062 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3063 SourceLocation(), SourceLocation())) {
3064 ClausesWithImplicit.push_back(Implicit);
3065 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3066 DSAChecker.getImplicitFirstprivate().size();
3067 } else
3068 ErrorFound = true;
3069 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003070 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003071
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003072 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003073 switch (Kind) {
3074 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003075 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3076 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003077 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003078 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003079 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003080 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3081 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003082 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003083 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003084 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3085 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003086 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003087 case OMPD_for_simd:
3088 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3089 EndLoc, VarsWithInheritedDSA);
3090 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003091 case OMPD_sections:
3092 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3093 EndLoc);
3094 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003095 case OMPD_section:
3096 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003097 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003098 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3099 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003100 case OMPD_single:
3101 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3102 EndLoc);
3103 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003104 case OMPD_master:
3105 assert(ClausesWithImplicit.empty() &&
3106 "No clauses are allowed for 'omp master' directive");
3107 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3108 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003109 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003110 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3111 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003112 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003113 case OMPD_parallel_for:
3114 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3115 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003116 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003117 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003118 case OMPD_parallel_for_simd:
3119 Res = ActOnOpenMPParallelForSimdDirective(
3120 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003121 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003122 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003123 case OMPD_parallel_sections:
3124 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3125 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003126 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003127 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003128 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003129 Res =
3130 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003131 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003132 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003133 case OMPD_taskyield:
3134 assert(ClausesWithImplicit.empty() &&
3135 "No clauses are allowed for 'omp taskyield' directive");
3136 assert(AStmt == nullptr &&
3137 "No associated statement allowed for 'omp taskyield' directive");
3138 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3139 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003140 case OMPD_barrier:
3141 assert(ClausesWithImplicit.empty() &&
3142 "No clauses are allowed for 'omp barrier' directive");
3143 assert(AStmt == nullptr &&
3144 "No associated statement allowed for 'omp barrier' directive");
3145 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3146 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003147 case OMPD_taskwait:
3148 assert(ClausesWithImplicit.empty() &&
3149 "No clauses are allowed for 'omp taskwait' directive");
3150 assert(AStmt == nullptr &&
3151 "No associated statement allowed for 'omp taskwait' directive");
3152 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3153 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003154 case OMPD_taskgroup:
3155 assert(ClausesWithImplicit.empty() &&
3156 "No clauses are allowed for 'omp taskgroup' directive");
3157 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3158 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003159 case OMPD_flush:
3160 assert(AStmt == nullptr &&
3161 "No associated statement allowed for 'omp flush' directive");
3162 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3163 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003164 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003165 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3166 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003167 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003168 case OMPD_atomic:
3169 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3170 EndLoc);
3171 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003172 case OMPD_teams:
3173 Res =
3174 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3175 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003176 case OMPD_target:
3177 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3178 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003179 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003180 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003181 case OMPD_target_parallel:
3182 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3183 StartLoc, EndLoc);
3184 AllowedNameModifiers.push_back(OMPD_target);
3185 AllowedNameModifiers.push_back(OMPD_parallel);
3186 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003187 case OMPD_target_parallel_for:
3188 Res = ActOnOpenMPTargetParallelForDirective(
3189 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3190 AllowedNameModifiers.push_back(OMPD_target);
3191 AllowedNameModifiers.push_back(OMPD_parallel);
3192 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003193 case OMPD_cancellation_point:
3194 assert(ClausesWithImplicit.empty() &&
3195 "No clauses are allowed for 'omp cancellation point' directive");
3196 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3197 "cancellation point' directive");
3198 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3199 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003200 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003201 assert(AStmt == nullptr &&
3202 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003203 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3204 CancelRegion);
3205 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003206 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003207 case OMPD_target_data:
3208 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3209 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003210 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003211 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003212 case OMPD_target_enter_data:
3213 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3214 EndLoc);
3215 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3216 break;
Samuel Antao72590762016-01-19 20:04:50 +00003217 case OMPD_target_exit_data:
3218 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3219 EndLoc);
3220 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3221 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003222 case OMPD_taskloop:
3223 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3224 EndLoc, VarsWithInheritedDSA);
3225 AllowedNameModifiers.push_back(OMPD_taskloop);
3226 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003227 case OMPD_taskloop_simd:
3228 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3229 EndLoc, VarsWithInheritedDSA);
3230 AllowedNameModifiers.push_back(OMPD_taskloop);
3231 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003232 case OMPD_distribute:
3233 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3234 EndLoc, VarsWithInheritedDSA);
3235 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003236 case OMPD_declare_target:
3237 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003238 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003239 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003240 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003241 llvm_unreachable("OpenMP Directive is not allowed");
3242 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003243 llvm_unreachable("Unknown OpenMP directive");
3244 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003245
Alexey Bataev4acb8592014-07-07 13:01:15 +00003246 for (auto P : VarsWithInheritedDSA) {
3247 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3248 << P.first << P.second->getSourceRange();
3249 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003250 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3251
3252 if (!AllowedNameModifiers.empty())
3253 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3254 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003255
Alexey Bataeved09d242014-05-28 05:53:51 +00003256 if (ErrorFound)
3257 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003258 return Res;
3259}
3260
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003261Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3262 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003263 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003264 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3265 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003266 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003267 assert(Linears.size() == LinModifiers.size());
3268 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003269 if (!DG || DG.get().isNull())
3270 return DeclGroupPtrTy();
3271
3272 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003273 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003274 return DG;
3275 }
3276 auto *ADecl = DG.get().getSingleDecl();
3277 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3278 ADecl = FTD->getTemplatedDecl();
3279
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003280 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3281 if (!FD) {
3282 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003283 return DeclGroupPtrTy();
3284 }
3285
Alexey Bataev2af33e32016-04-07 12:45:37 +00003286 // OpenMP [2.8.2, declare simd construct, Description]
3287 // The parameter of the simdlen clause must be a constant positive integer
3288 // expression.
3289 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003290 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003291 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003292 // OpenMP [2.8.2, declare simd construct, Description]
3293 // The special this pointer can be used as if was one of the arguments to the
3294 // function in any of the linear, aligned, or uniform clauses.
3295 // The uniform clause declares one or more arguments to have an invariant
3296 // value for all concurrent invocations of the function in the execution of a
3297 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003298 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3299 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003300 for (auto *E : Uniforms) {
3301 E = E->IgnoreParenImpCasts();
3302 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3303 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3304 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3305 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003306 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3307 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003308 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003309 }
3310 if (isa<CXXThisExpr>(E)) {
3311 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003312 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003313 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003314 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3315 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003316 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003317 // OpenMP [2.8.2, declare simd construct, Description]
3318 // The aligned clause declares that the object to which each list item points
3319 // is aligned to the number of bytes expressed in the optional parameter of
3320 // the aligned clause.
3321 // The special this pointer can be used as if was one of the arguments to the
3322 // function in any of the linear, aligned, or uniform clauses.
3323 // The type of list items appearing in the aligned clause must be array,
3324 // pointer, reference to array, or reference to pointer.
3325 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3326 Expr *AlignedThis = nullptr;
3327 for (auto *E : Aligneds) {
3328 E = E->IgnoreParenImpCasts();
3329 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3330 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3331 auto *CanonPVD = PVD->getCanonicalDecl();
3332 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3333 FD->getParamDecl(PVD->getFunctionScopeIndex())
3334 ->getCanonicalDecl() == CanonPVD) {
3335 // OpenMP [2.8.1, simd construct, Restrictions]
3336 // A list-item cannot appear in more than one aligned clause.
3337 if (AlignedArgs.count(CanonPVD) > 0) {
3338 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3339 << 1 << E->getSourceRange();
3340 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3341 diag::note_omp_explicit_dsa)
3342 << getOpenMPClauseName(OMPC_aligned);
3343 continue;
3344 }
3345 AlignedArgs[CanonPVD] = E;
3346 QualType QTy = PVD->getType()
3347 .getNonReferenceType()
3348 .getUnqualifiedType()
3349 .getCanonicalType();
3350 const Type *Ty = QTy.getTypePtrOrNull();
3351 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3352 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3353 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3354 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3355 }
3356 continue;
3357 }
3358 }
3359 if (isa<CXXThisExpr>(E)) {
3360 if (AlignedThis) {
3361 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3362 << 2 << E->getSourceRange();
3363 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3364 << getOpenMPClauseName(OMPC_aligned);
3365 }
3366 AlignedThis = E;
3367 continue;
3368 }
3369 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3370 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3371 }
3372 // The optional parameter of the aligned clause, alignment, must be a constant
3373 // positive integer expression. If no optional parameter is specified,
3374 // implementation-defined default alignments for SIMD instructions on the
3375 // target platforms are assumed.
3376 SmallVector<Expr *, 4> NewAligns;
3377 for (auto *E : Alignments) {
3378 ExprResult Align;
3379 if (E)
3380 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3381 NewAligns.push_back(Align.get());
3382 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003383 // OpenMP [2.8.2, declare simd construct, Description]
3384 // The linear clause declares one or more list items to be private to a SIMD
3385 // lane and to have a linear relationship with respect to the iteration space
3386 // of a loop.
3387 // The special this pointer can be used as if was one of the arguments to the
3388 // function in any of the linear, aligned, or uniform clauses.
3389 // When a linear-step expression is specified in a linear clause it must be
3390 // either a constant integer expression or an integer-typed parameter that is
3391 // specified in a uniform clause on the directive.
3392 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3393 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3394 auto MI = LinModifiers.begin();
3395 for (auto *E : Linears) {
3396 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3397 ++MI;
3398 E = E->IgnoreParenImpCasts();
3399 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3400 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3401 auto *CanonPVD = PVD->getCanonicalDecl();
3402 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3403 FD->getParamDecl(PVD->getFunctionScopeIndex())
3404 ->getCanonicalDecl() == CanonPVD) {
3405 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3406 // A list-item cannot appear in more than one linear clause.
3407 if (LinearArgs.count(CanonPVD) > 0) {
3408 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3409 << getOpenMPClauseName(OMPC_linear)
3410 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3411 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3412 diag::note_omp_explicit_dsa)
3413 << getOpenMPClauseName(OMPC_linear);
3414 continue;
3415 }
3416 // Each argument can appear in at most one uniform or linear clause.
3417 if (UniformedArgs.count(CanonPVD) > 0) {
3418 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3419 << getOpenMPClauseName(OMPC_linear)
3420 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3421 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3422 diag::note_omp_explicit_dsa)
3423 << getOpenMPClauseName(OMPC_uniform);
3424 continue;
3425 }
3426 LinearArgs[CanonPVD] = E;
3427 if (E->isValueDependent() || E->isTypeDependent() ||
3428 E->isInstantiationDependent() ||
3429 E->containsUnexpandedParameterPack())
3430 continue;
3431 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3432 PVD->getOriginalType());
3433 continue;
3434 }
3435 }
3436 if (isa<CXXThisExpr>(E)) {
3437 if (UniformedLinearThis) {
3438 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3439 << getOpenMPClauseName(OMPC_linear)
3440 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3441 << E->getSourceRange();
3442 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3443 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3444 : OMPC_linear);
3445 continue;
3446 }
3447 UniformedLinearThis = E;
3448 if (E->isValueDependent() || E->isTypeDependent() ||
3449 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3450 continue;
3451 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3452 E->getType());
3453 continue;
3454 }
3455 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3456 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3457 }
3458 Expr *Step = nullptr;
3459 Expr *NewStep = nullptr;
3460 SmallVector<Expr *, 4> NewSteps;
3461 for (auto *E : Steps) {
3462 // Skip the same step expression, it was checked already.
3463 if (Step == E || !E) {
3464 NewSteps.push_back(E ? NewStep : nullptr);
3465 continue;
3466 }
3467 Step = E;
3468 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3469 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3470 auto *CanonPVD = PVD->getCanonicalDecl();
3471 if (UniformedArgs.count(CanonPVD) == 0) {
3472 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3473 << Step->getSourceRange();
3474 } else if (E->isValueDependent() || E->isTypeDependent() ||
3475 E->isInstantiationDependent() ||
3476 E->containsUnexpandedParameterPack() ||
3477 CanonPVD->getType()->hasIntegerRepresentation())
3478 NewSteps.push_back(Step);
3479 else {
3480 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3481 << Step->getSourceRange();
3482 }
3483 continue;
3484 }
3485 NewStep = Step;
3486 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3487 !Step->isInstantiationDependent() &&
3488 !Step->containsUnexpandedParameterPack()) {
3489 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3490 .get();
3491 if (NewStep)
3492 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3493 }
3494 NewSteps.push_back(NewStep);
3495 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003496 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3497 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003498 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003499 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3500 const_cast<Expr **>(Linears.data()), Linears.size(),
3501 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3502 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003503 ADecl->addAttr(NewAttr);
3504 return ConvertDeclToDeclGroup(ADecl);
3505}
3506
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003507StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3508 Stmt *AStmt,
3509 SourceLocation StartLoc,
3510 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003511 if (!AStmt)
3512 return StmtError();
3513
Alexey Bataev9959db52014-05-06 10:08:46 +00003514 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3515 // 1.2.2 OpenMP Language Terminology
3516 // Structured block - An executable statement with a single entry at the
3517 // top and a single exit at the bottom.
3518 // The point of exit cannot be a branch out of the structured block.
3519 // longjmp() and throw() must not violate the entry/exit criteria.
3520 CS->getCapturedDecl()->setNothrow();
3521
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003522 getCurFunction()->setHasBranchProtectedScope();
3523
Alexey Bataev25e5b442015-09-15 12:52:43 +00003524 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3525 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003526}
3527
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003528namespace {
3529/// \brief Helper class for checking canonical form of the OpenMP loops and
3530/// extracting iteration space of each loop in the loop nest, that will be used
3531/// for IR generation.
3532class OpenMPIterationSpaceChecker {
3533 /// \brief Reference to Sema.
3534 Sema &SemaRef;
3535 /// \brief A location for diagnostics (when there is no some better location).
3536 SourceLocation DefaultLoc;
3537 /// \brief A location for diagnostics (when increment is not compatible).
3538 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003539 /// \brief A source location for referring to loop init later.
3540 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003541 /// \brief A source location for referring to condition later.
3542 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003543 /// \brief A source location for referring to increment later.
3544 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003545 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003546 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003547 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003548 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003549 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003550 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003551 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003552 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003553 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003554 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003555 /// \brief This flag is true when condition is one of:
3556 /// Var < UB
3557 /// Var <= UB
3558 /// UB > Var
3559 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003560 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003561 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003562 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003563 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003564 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003565
3566public:
3567 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003568 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003569 /// \brief Check init-expr for canonical loop form and save loop counter
3570 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003571 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003572 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3573 /// for less/greater and for strict/non-strict comparison.
3574 bool CheckCond(Expr *S);
3575 /// \brief Check incr-expr for canonical loop form and return true if it
3576 /// does not conform, otherwise save loop step (#Step).
3577 bool CheckInc(Expr *S);
3578 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003579 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003580 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003581 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003582 /// \brief Source range of the loop init.
3583 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3584 /// \brief Source range of the loop condition.
3585 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3586 /// \brief Source range of the loop increment.
3587 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3588 /// \brief True if the step should be subtracted.
3589 bool ShouldSubtractStep() const { return SubtractStep; }
3590 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003591 Expr *
3592 BuildNumIterations(Scope *S, const bool LimitedType,
3593 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003594 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003595 Expr *BuildPreCond(Scope *S, Expr *Cond,
3596 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003597 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003598 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3599 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003600 /// \brief Build reference expression to the private counter be used for
3601 /// codegen.
3602 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003603 /// \brief Build initization of the counter be used for codegen.
3604 Expr *BuildCounterInit() const;
3605 /// \brief Build step of the counter be used for codegen.
3606 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003607 /// \brief Return true if any expression is dependent.
3608 bool Dependent() const;
3609
3610private:
3611 /// \brief Check the right-hand side of an assignment in the increment
3612 /// expression.
3613 bool CheckIncRHS(Expr *RHS);
3614 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003615 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003616 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003617 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003618 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003619 /// \brief Helper to set loop increment.
3620 bool SetStep(Expr *NewStep, bool Subtract);
3621};
3622
3623bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003624 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003625 assert(!LB && !UB && !Step);
3626 return false;
3627 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003628 return LCDecl->getType()->isDependentType() ||
3629 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3630 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003631}
3632
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003633static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003634 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3635 E = ExprTemp->getSubExpr();
3636
3637 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3638 E = MTE->GetTemporaryExpr();
3639
3640 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3641 E = Binder->getSubExpr();
3642
3643 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3644 E = ICE->getSubExprAsWritten();
3645 return E->IgnoreParens();
3646}
3647
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003648bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3649 Expr *NewLCRefExpr,
3650 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003651 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003652 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003653 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003654 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003655 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003656 LCDecl = getCanonicalDecl(NewLCDecl);
3657 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003658 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3659 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003660 if ((Ctor->isCopyOrMoveConstructor() ||
3661 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3662 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003663 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003664 LB = NewLB;
3665 return false;
3666}
3667
3668bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003669 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003670 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003671 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3672 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003673 if (!NewUB)
3674 return true;
3675 UB = NewUB;
3676 TestIsLessOp = LessOp;
3677 TestIsStrictOp = StrictOp;
3678 ConditionSrcRange = SR;
3679 ConditionLoc = SL;
3680 return false;
3681}
3682
3683bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3684 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003685 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003686 if (!NewStep)
3687 return true;
3688 if (!NewStep->isValueDependent()) {
3689 // Check that the step is integer expression.
3690 SourceLocation StepLoc = NewStep->getLocStart();
3691 ExprResult Val =
3692 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3693 if (Val.isInvalid())
3694 return true;
3695 NewStep = Val.get();
3696
3697 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3698 // If test-expr is of form var relational-op b and relational-op is < or
3699 // <= then incr-expr must cause var to increase on each iteration of the
3700 // loop. If test-expr is of form var relational-op b and relational-op is
3701 // > or >= then incr-expr must cause var to decrease on each iteration of
3702 // the loop.
3703 // If test-expr is of form b relational-op var and relational-op is < or
3704 // <= then incr-expr must cause var to decrease on each iteration of the
3705 // loop. If test-expr is of form b relational-op var and relational-op is
3706 // > or >= then incr-expr must cause var to increase on each iteration of
3707 // the loop.
3708 llvm::APSInt Result;
3709 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3710 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3711 bool IsConstNeg =
3712 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003713 bool IsConstPos =
3714 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003715 bool IsConstZero = IsConstant && !Result.getBoolValue();
3716 if (UB && (IsConstZero ||
3717 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003718 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003719 SemaRef.Diag(NewStep->getExprLoc(),
3720 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003721 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003722 SemaRef.Diag(ConditionLoc,
3723 diag::note_omp_loop_cond_requres_compatible_incr)
3724 << TestIsLessOp << ConditionSrcRange;
3725 return true;
3726 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003727 if (TestIsLessOp == Subtract) {
3728 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3729 NewStep).get();
3730 Subtract = !Subtract;
3731 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003732 }
3733
3734 Step = NewStep;
3735 SubtractStep = Subtract;
3736 return false;
3737}
3738
Alexey Bataev9c821032015-04-30 04:23:23 +00003739bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003740 // Check init-expr for canonical loop form and save loop counter
3741 // variable - #Var and its initialization value - #LB.
3742 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3743 // var = lb
3744 // integer-type var = lb
3745 // random-access-iterator-type var = lb
3746 // pointer-type var = lb
3747 //
3748 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003749 if (EmitDiags) {
3750 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3751 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003752 return true;
3753 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003754 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003755 if (Expr *E = dyn_cast<Expr>(S))
3756 S = E->IgnoreParens();
3757 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003758 if (BO->getOpcode() == BO_Assign) {
3759 auto *LHS = BO->getLHS()->IgnoreParens();
3760 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3761 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3762 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3763 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3764 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3765 }
3766 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3767 if (ME->isArrow() &&
3768 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3769 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3770 }
3771 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003772 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3773 if (DS->isSingleDecl()) {
3774 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003775 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003776 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003777 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003778 SemaRef.Diag(S->getLocStart(),
3779 diag::ext_omp_loop_not_canonical_init)
3780 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003781 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003782 }
3783 }
3784 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003785 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3786 if (CE->getOperator() == OO_Equal) {
3787 auto *LHS = CE->getArg(0);
3788 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3789 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3790 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3791 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3792 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3793 }
3794 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3795 if (ME->isArrow() &&
3796 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3797 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3798 }
3799 }
3800 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003801
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003802 if (Dependent() || SemaRef.CurContext->isDependentContext())
3803 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003804 if (EmitDiags) {
3805 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3806 << S->getSourceRange();
3807 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003808 return true;
3809}
3810
Alexey Bataev23b69422014-06-18 07:08:49 +00003811/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003812/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003813static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003814 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003815 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003816 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3818 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003819 if ((Ctor->isCopyOrMoveConstructor() ||
3820 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3821 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003822 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003823 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3824 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3825 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3826 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3827 return getCanonicalDecl(ME->getMemberDecl());
3828 return getCanonicalDecl(VD);
3829 }
3830 }
3831 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3832 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3833 return getCanonicalDecl(ME->getMemberDecl());
3834 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003835}
3836
3837bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3838 // Check test-expr for canonical form, save upper-bound UB, flags for
3839 // less/greater and for strict/non-strict comparison.
3840 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3841 // var relational-op b
3842 // b relational-op var
3843 //
3844 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003845 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003846 return true;
3847 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003848 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003849 SourceLocation CondLoc = S->getLocStart();
3850 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3851 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003852 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003853 return SetUB(BO->getRHS(),
3854 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3855 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3856 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003857 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003858 return SetUB(BO->getLHS(),
3859 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3860 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3861 BO->getSourceRange(), BO->getOperatorLoc());
3862 }
3863 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3864 if (CE->getNumArgs() == 2) {
3865 auto Op = CE->getOperator();
3866 switch (Op) {
3867 case OO_Greater:
3868 case OO_GreaterEqual:
3869 case OO_Less:
3870 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003871 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003872 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3873 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3874 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003875 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003876 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3877 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3878 CE->getOperatorLoc());
3879 break;
3880 default:
3881 break;
3882 }
3883 }
3884 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003885 if (Dependent() || SemaRef.CurContext->isDependentContext())
3886 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003887 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003888 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003889 return true;
3890}
3891
3892bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3893 // RHS of canonical loop form increment can be:
3894 // var + incr
3895 // incr + var
3896 // var - incr
3897 //
3898 RHS = RHS->IgnoreParenImpCasts();
3899 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3900 if (BO->isAdditiveOp()) {
3901 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003902 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003903 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003904 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003905 return SetStep(BO->getLHS(), false);
3906 }
3907 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3908 bool IsAdd = CE->getOperator() == OO_Plus;
3909 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003910 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003911 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003912 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003913 return SetStep(CE->getArg(0), false);
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(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920 return true;
3921}
3922
3923bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3924 // Check incr-expr for canonical loop form and return true if it
3925 // does not conform.
3926 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3927 // ++var
3928 // var++
3929 // --var
3930 // var--
3931 // var += incr
3932 // var -= incr
3933 // var = var + incr
3934 // var = incr + var
3935 // var = var - incr
3936 //
3937 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003938 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003939 return true;
3940 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003941 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942 S = S->IgnoreParens();
3943 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944 if (UO->isIncrementDecrementOp() &&
3945 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003946 return SetStep(
3947 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3948 (UO->isDecrementOp() ? -1 : 1)).get(),
3949 false);
3950 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3951 switch (BO->getOpcode()) {
3952 case BO_AddAssign:
3953 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003954 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003955 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3956 break;
3957 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 return CheckIncRHS(BO->getRHS());
3960 break;
3961 default:
3962 break;
3963 }
3964 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3965 switch (CE->getOperator()) {
3966 case OO_PlusPlus:
3967 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003968 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003969 return SetStep(
3970 SemaRef.ActOnIntegerConstant(
3971 CE->getLocStart(),
3972 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3973 false);
3974 break;
3975 case OO_PlusEqual:
3976 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003977 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003978 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3979 break;
3980 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003981 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003982 return CheckIncRHS(CE->getArg(1));
3983 break;
3984 default:
3985 break;
3986 }
3987 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003988 if (Dependent() || SemaRef.CurContext->isDependentContext())
3989 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003992 return true;
3993}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003994
Alexey Bataev5a3af132016-03-29 08:58:54 +00003995static ExprResult
3996tryBuildCapture(Sema &SemaRef, Expr *Capture,
3997 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3998 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3999 return SemaRef.PerformImplicitConversion(
4000 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4001 /*AllowExplicit=*/true);
4002 auto I = Captures.find(Capture);
4003 if (I != Captures.end())
4004 return buildCapture(SemaRef, Capture, I->second);
4005 DeclRefExpr *Ref = nullptr;
4006 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4007 Captures[Capture] = Ref;
4008 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004009}
4010
Alexander Musmana5f070a2014-10-01 06:03:56 +00004011/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004012Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4013 Scope *S, const bool LimitedType,
4014 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004015 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004016 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004017 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004018 SemaRef.getLangOpts().CPlusPlus) {
4019 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004020 auto *UBExpr = TestIsLessOp ? UB : LB;
4021 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004022 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4023 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004024 if (!Upper || !Lower)
4025 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004026
4027 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4028
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004029 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004030 // BuildBinOp already emitted error, this one is to point user to upper
4031 // and lower bound, and to tell what is passed to 'operator-'.
4032 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4033 << Upper->getSourceRange() << Lower->getSourceRange();
4034 return nullptr;
4035 }
4036 }
4037
4038 if (!Diff.isUsable())
4039 return nullptr;
4040
4041 // Upper - Lower [- 1]
4042 if (TestIsStrictOp)
4043 Diff = SemaRef.BuildBinOp(
4044 S, DefaultLoc, BO_Sub, Diff.get(),
4045 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4046 if (!Diff.isUsable())
4047 return nullptr;
4048
4049 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004050 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4051 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004052 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004053 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004054 if (!Diff.isUsable())
4055 return nullptr;
4056
4057 // Parentheses (for dumping/debugging purposes only).
4058 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4059 if (!Diff.isUsable())
4060 return nullptr;
4061
4062 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004063 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064 if (!Diff.isUsable())
4065 return nullptr;
4066
Alexander Musman174b3ca2014-10-06 11:16:29 +00004067 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004068 QualType Type = Diff.get()->getType();
4069 auto &C = SemaRef.Context;
4070 bool UseVarType = VarType->hasIntegerRepresentation() &&
4071 C.getTypeSize(Type) > C.getTypeSize(VarType);
4072 if (!Type->isIntegerType() || UseVarType) {
4073 unsigned NewSize =
4074 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4075 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4076 : Type->hasSignedIntegerRepresentation();
4077 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004078 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4079 Diff = SemaRef.PerformImplicitConversion(
4080 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4081 if (!Diff.isUsable())
4082 return nullptr;
4083 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004084 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004085 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004086 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4087 if (NewSize != C.getTypeSize(Type)) {
4088 if (NewSize < C.getTypeSize(Type)) {
4089 assert(NewSize == 64 && "incorrect loop var size");
4090 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4091 << InitSrcRange << ConditionSrcRange;
4092 }
4093 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004094 NewSize, Type->hasSignedIntegerRepresentation() ||
4095 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004096 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4097 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4098 Sema::AA_Converting, true);
4099 if (!Diff.isUsable())
4100 return nullptr;
4101 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004102 }
4103 }
4104
Alexander Musmana5f070a2014-10-01 06:03:56 +00004105 return Diff.get();
4106}
4107
Alexey Bataev5a3af132016-03-29 08:58:54 +00004108Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4109 Scope *S, Expr *Cond,
4110 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004111 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4112 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4113 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004114
Alexey Bataev5a3af132016-03-29 08:58:54 +00004115 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4116 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4117 if (!NewLB.isUsable() || !NewUB.isUsable())
4118 return nullptr;
4119
Alexey Bataev62dbb972015-04-22 11:59:37 +00004120 auto CondExpr = SemaRef.BuildBinOp(
4121 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4122 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004123 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004124 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004125 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4126 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004127 CondExpr = SemaRef.PerformImplicitConversion(
4128 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4129 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004130 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004131 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4132 // Otherwise use original loop conditon and evaluate it in runtime.
4133 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4134}
4135
Alexander Musmana5f070a2014-10-01 06:03:56 +00004136/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004137DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004138 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004139 auto *VD = dyn_cast<VarDecl>(LCDecl);
4140 if (!VD) {
4141 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4142 auto *Ref = buildDeclRefExpr(
4143 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004144 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4145 // If the loop control decl is explicitly marked as private, do not mark it
4146 // as captured again.
4147 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4148 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004149 return Ref;
4150 }
4151 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004152 DefaultLoc);
4153}
4154
4155Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004156 if (LCDecl && !LCDecl->isInvalidDecl()) {
4157 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004158 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004159 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4160 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004161 if (PrivateVar->isInvalidDecl())
4162 return nullptr;
4163 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4164 }
4165 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004166}
4167
4168/// \brief Build initization of the counter be used for codegen.
4169Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4170
4171/// \brief Build step of the counter be used for codegen.
4172Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4173
4174/// \brief Iteration space of a single for loop.
4175struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004176 /// \brief Condition of the loop.
4177 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004178 /// \brief This expression calculates the number of iterations in the loop.
4179 /// It is always possible to calculate it before starting the loop.
4180 Expr *NumIterations;
4181 /// \brief The loop counter variable.
4182 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004183 /// \brief Private loop counter variable.
4184 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004185 /// \brief This is initializer for the initial value of #CounterVar.
4186 Expr *CounterInit;
4187 /// \brief This is step for the #CounterVar used to generate its update:
4188 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4189 Expr *CounterStep;
4190 /// \brief Should step be subtracted?
4191 bool Subtract;
4192 /// \brief Source range of the loop init.
4193 SourceRange InitSrcRange;
4194 /// \brief Source range of the loop condition.
4195 SourceRange CondSrcRange;
4196 /// \brief Source range of the loop increment.
4197 SourceRange IncSrcRange;
4198};
4199
Alexey Bataev23b69422014-06-18 07:08:49 +00004200} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004201
Alexey Bataev9c821032015-04-30 04:23:23 +00004202void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4203 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4204 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004205 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4206 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004207 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4208 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004209 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4210 if (auto *D = ISC.GetLoopDecl()) {
4211 auto *VD = dyn_cast<VarDecl>(D);
4212 if (!VD) {
4213 if (auto *Private = IsOpenMPCapturedDecl(D))
4214 VD = Private;
4215 else {
4216 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4217 /*WithInit=*/false);
4218 VD = cast<VarDecl>(Ref->getDecl());
4219 }
4220 }
4221 DSAStack->addLoopControlVariable(D, VD);
4222 }
4223 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004224 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004225 }
4226}
4227
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004228/// \brief Called on a for stmt to check and extract its iteration space
4229/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004230static bool CheckOpenMPIterationSpace(
4231 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4232 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004233 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004234 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004235 LoopIterationSpace &ResultIterSpace,
4236 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004237 // OpenMP [2.6, Canonical Loop Form]
4238 // for (init-expr; test-expr; incr-expr) structured-block
4239 auto For = dyn_cast_or_null<ForStmt>(S);
4240 if (!For) {
4241 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004242 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4243 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4244 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4245 if (NestedLoopCount > 1) {
4246 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4247 SemaRef.Diag(DSA.getConstructLoc(),
4248 diag::note_omp_collapse_ordered_expr)
4249 << 2 << CollapseLoopCountExpr->getSourceRange()
4250 << OrderedLoopCountExpr->getSourceRange();
4251 else if (CollapseLoopCountExpr)
4252 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4253 diag::note_omp_collapse_ordered_expr)
4254 << 0 << CollapseLoopCountExpr->getSourceRange();
4255 else
4256 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4257 diag::note_omp_collapse_ordered_expr)
4258 << 1 << OrderedLoopCountExpr->getSourceRange();
4259 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004260 return true;
4261 }
4262 assert(For->getBody());
4263
4264 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4265
4266 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004267 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004268 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004270
4271 bool HasErrors = false;
4272
4273 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004274 if (auto *LCDecl = ISC.GetLoopDecl()) {
4275 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004276
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004277 // OpenMP [2.6, Canonical Loop Form]
4278 // Var is one of the following:
4279 // A variable of signed or unsigned integer type.
4280 // For C++, a variable of a random access iterator type.
4281 // For C, a variable of a pointer type.
4282 auto VarType = LCDecl->getType().getNonReferenceType();
4283 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4284 !VarType->isPointerType() &&
4285 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4286 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4287 << SemaRef.getLangOpts().CPlusPlus;
4288 HasErrors = true;
4289 }
4290
4291 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4292 // a Construct
4293 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4294 // parallel for construct is (are) private.
4295 // The loop iteration variable in the associated for-loop of a simd
4296 // construct with just one associated for-loop is linear with a
4297 // constant-linear-step that is the increment of the associated for-loop.
4298 // Exclude loop var from the list of variables with implicitly defined data
4299 // sharing attributes.
4300 VarsWithImplicitDSA.erase(LCDecl);
4301
4302 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4303 // in a Construct, C/C++].
4304 // The loop iteration variable in the associated for-loop of a simd
4305 // construct with just one associated for-loop may be listed in a linear
4306 // clause with a constant-linear-step that is the increment of the
4307 // associated for-loop.
4308 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4309 // parallel for construct may be listed in a private or lastprivate clause.
4310 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4311 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4312 // declared in the loop and it is predetermined as a private.
4313 auto PredeterminedCKind =
4314 isOpenMPSimdDirective(DKind)
4315 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4316 : OMPC_private;
4317 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4318 DVar.CKind != PredeterminedCKind) ||
4319 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4320 isOpenMPDistributeDirective(DKind)) &&
4321 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4322 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4323 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4324 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4325 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4326 << getOpenMPClauseName(PredeterminedCKind);
4327 if (DVar.RefExpr == nullptr)
4328 DVar.CKind = PredeterminedCKind;
4329 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4330 HasErrors = true;
4331 } else if (LoopDeclRefExpr != nullptr) {
4332 // Make the loop iteration variable private (for worksharing constructs),
4333 // linear (for simd directives with the only one associated loop) or
4334 // lastprivate (for simd directives with several collapsed or ordered
4335 // loops).
4336 if (DVar.CKind == OMPC_unknown)
4337 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4338 /*FromParent=*/false);
4339 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4340 }
4341
4342 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4343
4344 // Check test-expr.
4345 HasErrors |= ISC.CheckCond(For->getCond());
4346
4347 // Check incr-expr.
4348 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004349 }
4350
Alexander Musmana5f070a2014-10-01 06:03:56 +00004351 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004352 return HasErrors;
4353
Alexander Musmana5f070a2014-10-01 06:03:56 +00004354 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004355 ResultIterSpace.PreCond =
4356 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004357 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004358 DSA.getCurScope(),
4359 (isOpenMPWorksharingDirective(DKind) ||
4360 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4361 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004362 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004363 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4365 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4366 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4367 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4368 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4369 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4370
Alexey Bataev62dbb972015-04-22 11:59:37 +00004371 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4372 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004373 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004374 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004375 ResultIterSpace.CounterInit == nullptr ||
4376 ResultIterSpace.CounterStep == nullptr);
4377
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004378 return HasErrors;
4379}
4380
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004381/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004382static ExprResult
4383BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4384 ExprResult Start,
4385 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004386 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004387 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4388 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004389 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004390 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004391 VarRef.get()->getType())) {
4392 NewStart = SemaRef.PerformImplicitConversion(
4393 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4394 /*AllowExplicit=*/true);
4395 if (!NewStart.isUsable())
4396 return ExprError();
4397 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004398
4399 auto Init =
4400 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4401 return Init;
4402}
4403
Alexander Musmana5f070a2014-10-01 06:03:56 +00004404/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004405static ExprResult
4406BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4407 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4408 ExprResult Step, bool Subtract,
4409 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004410 // Add parentheses (for debugging purposes only).
4411 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4412 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4413 !Step.isUsable())
4414 return ExprError();
4415
Alexey Bataev5a3af132016-03-29 08:58:54 +00004416 ExprResult NewStep = Step;
4417 if (Captures)
4418 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004419 if (NewStep.isInvalid())
4420 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004421 ExprResult Update =
4422 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004423 if (!Update.isUsable())
4424 return ExprError();
4425
Alexey Bataevc0214e02016-02-16 12:13:49 +00004426 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4427 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004428 ExprResult NewStart = Start;
4429 if (Captures)
4430 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004431 if (NewStart.isInvalid())
4432 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433
Alexey Bataevc0214e02016-02-16 12:13:49 +00004434 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4435 ExprResult SavedUpdate = Update;
4436 ExprResult UpdateVal;
4437 if (VarRef.get()->getType()->isOverloadableType() ||
4438 NewStart.get()->getType()->isOverloadableType() ||
4439 Update.get()->getType()->isOverloadableType()) {
4440 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4441 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4442 Update =
4443 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4444 if (Update.isUsable()) {
4445 UpdateVal =
4446 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4447 VarRef.get(), SavedUpdate.get());
4448 if (UpdateVal.isUsable()) {
4449 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4450 UpdateVal.get());
4451 }
4452 }
4453 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4454 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004455
Alexey Bataevc0214e02016-02-16 12:13:49 +00004456 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4457 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4458 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4459 NewStart.get(), SavedUpdate.get());
4460 if (!Update.isUsable())
4461 return ExprError();
4462
Alexey Bataev11481f52016-02-17 10:29:05 +00004463 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4464 VarRef.get()->getType())) {
4465 Update = SemaRef.PerformImplicitConversion(
4466 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4467 if (!Update.isUsable())
4468 return ExprError();
4469 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004470
4471 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4472 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004473 return Update;
4474}
4475
4476/// \brief Convert integer expression \a E to make it have at least \a Bits
4477/// bits.
4478static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4479 Sema &SemaRef) {
4480 if (E == nullptr)
4481 return ExprError();
4482 auto &C = SemaRef.Context;
4483 QualType OldType = E->getType();
4484 unsigned HasBits = C.getTypeSize(OldType);
4485 if (HasBits >= Bits)
4486 return ExprResult(E);
4487 // OK to convert to signed, because new type has more bits than old.
4488 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4489 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4490 true);
4491}
4492
4493/// \brief Check if the given expression \a E is a constant integer that fits
4494/// into \a Bits bits.
4495static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4496 if (E == nullptr)
4497 return false;
4498 llvm::APSInt Result;
4499 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4500 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4501 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004502}
4503
Alexey Bataev5a3af132016-03-29 08:58:54 +00004504/// Build preinits statement for the given declarations.
4505static Stmt *buildPreInits(ASTContext &Context,
4506 SmallVectorImpl<Decl *> &PreInits) {
4507 if (!PreInits.empty()) {
4508 return new (Context) DeclStmt(
4509 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4510 SourceLocation(), SourceLocation());
4511 }
4512 return nullptr;
4513}
4514
4515/// Build preinits statement for the given declarations.
4516static Stmt *buildPreInits(ASTContext &Context,
4517 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4518 if (!Captures.empty()) {
4519 SmallVector<Decl *, 16> PreInits;
4520 for (auto &Pair : Captures)
4521 PreInits.push_back(Pair.second->getDecl());
4522 return buildPreInits(Context, PreInits);
4523 }
4524 return nullptr;
4525}
4526
4527/// Build postupdate expression for the given list of postupdates expressions.
4528static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4529 Expr *PostUpdate = nullptr;
4530 if (!PostUpdates.empty()) {
4531 for (auto *E : PostUpdates) {
4532 Expr *ConvE = S.BuildCStyleCastExpr(
4533 E->getExprLoc(),
4534 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4535 E->getExprLoc(), E)
4536 .get();
4537 PostUpdate = PostUpdate
4538 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4539 PostUpdate, ConvE)
4540 .get()
4541 : ConvE;
4542 }
4543 }
4544 return PostUpdate;
4545}
4546
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004547/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004548/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4549/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004550static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004551CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4552 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4553 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004554 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004555 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004556 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004557 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004558 // Found 'collapse' clause - calculate collapse number.
4559 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004560 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004561 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004562 }
4563 if (OrderedLoopCountExpr) {
4564 // Found 'ordered' clause - calculate collapse number.
4565 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004566 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4567 if (Result.getLimitedValue() < NestedLoopCount) {
4568 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4569 diag::err_omp_wrong_ordered_loop_count)
4570 << OrderedLoopCountExpr->getSourceRange();
4571 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4572 diag::note_collapse_loop_count)
4573 << CollapseLoopCountExpr->getSourceRange();
4574 }
4575 NestedLoopCount = Result.getLimitedValue();
4576 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004577 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004578 // This is helper routine for loop directives (e.g., 'for', 'simd',
4579 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004580 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004581 SmallVector<LoopIterationSpace, 4> IterSpaces;
4582 IterSpaces.resize(NestedLoopCount);
4583 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004584 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004585 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004586 NestedLoopCount, CollapseLoopCountExpr,
4587 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004588 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004589 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004590 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004591 // OpenMP [2.8.1, simd construct, Restrictions]
4592 // All loops associated with the construct must be perfectly nested; that
4593 // is, there must be no intervening code nor any OpenMP directive between
4594 // any two loops.
4595 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004596 }
4597
Alexander Musmana5f070a2014-10-01 06:03:56 +00004598 Built.clear(/* size */ NestedLoopCount);
4599
4600 if (SemaRef.CurContext->isDependentContext())
4601 return NestedLoopCount;
4602
4603 // An example of what is generated for the following code:
4604 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004605 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004606 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004607 // for (k = 0; k < NK; ++k)
4608 // for (j = J0; j < NJ; j+=2) {
4609 // <loop body>
4610 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004611 //
4612 // We generate the code below.
4613 // Note: the loop body may be outlined in CodeGen.
4614 // Note: some counters may be C++ classes, operator- is used to find number of
4615 // iterations and operator+= to calculate counter value.
4616 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4617 // or i64 is currently supported).
4618 //
4619 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4620 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4621 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4622 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4623 // // similar updates for vars in clauses (e.g. 'linear')
4624 // <loop body (using local i and j)>
4625 // }
4626 // i = NI; // assign final values of counters
4627 // j = NJ;
4628 //
4629
4630 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4631 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004632 // Precondition tests if there is at least one iteration (all conditions are
4633 // true).
4634 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004635 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004636 ExprResult LastIteration32 = WidenIterationCount(
4637 32 /* Bits */, SemaRef.PerformImplicitConversion(
4638 N0->IgnoreImpCasts(), N0->getType(),
4639 Sema::AA_Converting, /*AllowExplicit=*/true)
4640 .get(),
4641 SemaRef);
4642 ExprResult LastIteration64 = WidenIterationCount(
4643 64 /* Bits */, SemaRef.PerformImplicitConversion(
4644 N0->IgnoreImpCasts(), N0->getType(),
4645 Sema::AA_Converting, /*AllowExplicit=*/true)
4646 .get(),
4647 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004648
4649 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4650 return NestedLoopCount;
4651
4652 auto &C = SemaRef.Context;
4653 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4654
4655 Scope *CurScope = DSA.getCurScope();
4656 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004657 if (PreCond.isUsable()) {
4658 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4659 PreCond.get(), IterSpaces[Cnt].PreCond);
4660 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004661 auto N = IterSpaces[Cnt].NumIterations;
4662 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4663 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004664 LastIteration32 = SemaRef.BuildBinOp(
4665 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4666 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4667 Sema::AA_Converting,
4668 /*AllowExplicit=*/true)
4669 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004670 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004671 LastIteration64 = SemaRef.BuildBinOp(
4672 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4673 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4674 Sema::AA_Converting,
4675 /*AllowExplicit=*/true)
4676 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004677 }
4678
4679 // Choose either the 32-bit or 64-bit version.
4680 ExprResult LastIteration = LastIteration64;
4681 if (LastIteration32.isUsable() &&
4682 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4683 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4684 FitsInto(
4685 32 /* Bits */,
4686 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4687 LastIteration64.get(), SemaRef)))
4688 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004689 QualType VType = LastIteration.get()->getType();
4690 QualType RealVType = VType;
4691 QualType StrideVType = VType;
4692 if (isOpenMPTaskLoopDirective(DKind)) {
4693 VType =
4694 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4695 StrideVType =
4696 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4697 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004698
4699 if (!LastIteration.isUsable())
4700 return 0;
4701
4702 // Save the number of iterations.
4703 ExprResult NumIterations = LastIteration;
4704 {
4705 LastIteration = SemaRef.BuildBinOp(
4706 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4707 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4708 if (!LastIteration.isUsable())
4709 return 0;
4710 }
4711
4712 // Calculate the last iteration number beforehand instead of doing this on
4713 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4714 llvm::APSInt Result;
4715 bool IsConstant =
4716 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4717 ExprResult CalcLastIteration;
4718 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004719 ExprResult SaveRef =
4720 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004721 LastIteration = SaveRef;
4722
4723 // Prepare SaveRef + 1.
4724 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004725 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004726 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4727 if (!NumIterations.isUsable())
4728 return 0;
4729 }
4730
4731 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4732
Alexander Musmanc6388682014-12-15 07:07:06 +00004733 // Build variables passed into runtime, nesessary for worksharing directives.
4734 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004735 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4736 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004737 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004738 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4739 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004740 SemaRef.AddInitializerToDecl(
4741 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4742 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4743
4744 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004745 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4746 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004747 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4748 /*DirectInit*/ false,
4749 /*TypeMayContainAuto*/ false);
4750
4751 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4752 // This will be used to implement clause 'lastprivate'.
4753 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004754 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4755 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004756 SemaRef.AddInitializerToDecl(
4757 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4759
4760 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004761 VarDecl *STDecl =
4762 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4763 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004764 SemaRef.AddInitializerToDecl(
4765 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4766 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4767
4768 // Build expression: UB = min(UB, LastIteration)
4769 // It is nesessary for CodeGen of directives with static scheduling.
4770 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4771 UB.get(), LastIteration.get());
4772 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4773 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4774 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4775 CondOp.get());
4776 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4777 }
4778
4779 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004780 ExprResult IV;
4781 ExprResult Init;
4782 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004783 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4784 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004785 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004786 isOpenMPTaskLoopDirective(DKind) ||
4787 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004788 ? LB.get()
4789 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4790 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4791 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004792 }
4793
Alexander Musmanc6388682014-12-15 07:07:06 +00004794 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004795 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004796 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004797 (isOpenMPWorksharingDirective(DKind) ||
4798 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004799 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4800 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4801 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004802
4803 // Loop increment (IV = IV + 1)
4804 SourceLocation IncLoc;
4805 ExprResult Inc =
4806 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4807 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4808 if (!Inc.isUsable())
4809 return 0;
4810 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004811 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4812 if (!Inc.isUsable())
4813 return 0;
4814
4815 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4816 // Used for directives with static scheduling.
4817 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004818 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4819 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004820 // LB + ST
4821 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4822 if (!NextLB.isUsable())
4823 return 0;
4824 // LB = LB + ST
4825 NextLB =
4826 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4827 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4828 if (!NextLB.isUsable())
4829 return 0;
4830 // UB + ST
4831 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4832 if (!NextUB.isUsable())
4833 return 0;
4834 // UB = UB + ST
4835 NextUB =
4836 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4837 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4838 if (!NextUB.isUsable())
4839 return 0;
4840 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004841
4842 // Build updates and final values of the loop counters.
4843 bool HasErrors = false;
4844 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004845 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004846 Built.Updates.resize(NestedLoopCount);
4847 Built.Finals.resize(NestedLoopCount);
4848 {
4849 ExprResult Div;
4850 // Go from inner nested loop to outer.
4851 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4852 LoopIterationSpace &IS = IterSpaces[Cnt];
4853 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4854 // Build: Iter = (IV / Div) % IS.NumIters
4855 // where Div is product of previous iterations' IS.NumIters.
4856 ExprResult Iter;
4857 if (Div.isUsable()) {
4858 Iter =
4859 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4860 } else {
4861 Iter = IV;
4862 assert((Cnt == (int)NestedLoopCount - 1) &&
4863 "unusable div expected on first iteration only");
4864 }
4865
4866 if (Cnt != 0 && Iter.isUsable())
4867 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4868 IS.NumIterations);
4869 if (!Iter.isUsable()) {
4870 HasErrors = true;
4871 break;
4872 }
4873
Alexey Bataev39f915b82015-05-08 10:41:21 +00004874 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004875 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4876 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4877 IS.CounterVar->getExprLoc(),
4878 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004879 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004880 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004881 if (!Init.isUsable()) {
4882 HasErrors = true;
4883 break;
4884 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004885 ExprResult Update = BuildCounterUpdate(
4886 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4887 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004888 if (!Update.isUsable()) {
4889 HasErrors = true;
4890 break;
4891 }
4892
4893 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4894 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004895 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004896 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004897 if (!Final.isUsable()) {
4898 HasErrors = true;
4899 break;
4900 }
4901
4902 // Build Div for the next iteration: Div <- Div * IS.NumIters
4903 if (Cnt != 0) {
4904 if (Div.isUnset())
4905 Div = IS.NumIterations;
4906 else
4907 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4908 IS.NumIterations);
4909
4910 // Add parentheses (for debugging purposes only).
4911 if (Div.isUsable())
4912 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4913 if (!Div.isUsable()) {
4914 HasErrors = true;
4915 break;
4916 }
4917 }
4918 if (!Update.isUsable() || !Final.isUsable()) {
4919 HasErrors = true;
4920 break;
4921 }
4922 // Save results
4923 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004924 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004925 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004926 Built.Updates[Cnt] = Update.get();
4927 Built.Finals[Cnt] = Final.get();
4928 }
4929 }
4930
4931 if (HasErrors)
4932 return 0;
4933
4934 // Save results
4935 Built.IterationVarRef = IV.get();
4936 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004937 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004938 Built.CalcLastIteration =
4939 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004940 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004941 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004942 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004943 Built.Init = Init.get();
4944 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004945 Built.LB = LB.get();
4946 Built.UB = UB.get();
4947 Built.IL = IL.get();
4948 Built.ST = ST.get();
4949 Built.EUB = EUB.get();
4950 Built.NLB = NextLB.get();
4951 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004952
Alexey Bataevabfc0692014-06-25 06:52:00 +00004953 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004954}
4955
Alexey Bataev10e775f2015-07-30 11:36:16 +00004956static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004957 auto CollapseClauses =
4958 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4959 if (CollapseClauses.begin() != CollapseClauses.end())
4960 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004961 return nullptr;
4962}
4963
Alexey Bataev10e775f2015-07-30 11:36:16 +00004964static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004965 auto OrderedClauses =
4966 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4967 if (OrderedClauses.begin() != OrderedClauses.end())
4968 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004969 return nullptr;
4970}
4971
Alexey Bataev66b15b52015-08-21 11:14:16 +00004972static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4973 const Expr *Safelen) {
4974 llvm::APSInt SimdlenRes, SafelenRes;
4975 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4976 Simdlen->isInstantiationDependent() ||
4977 Simdlen->containsUnexpandedParameterPack())
4978 return false;
4979 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4980 Safelen->isInstantiationDependent() ||
4981 Safelen->containsUnexpandedParameterPack())
4982 return false;
4983 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4984 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4985 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4986 // If both simdlen and safelen clauses are specified, the value of the simdlen
4987 // parameter must be less than or equal to the value of the safelen parameter.
4988 if (SimdlenRes > SafelenRes) {
4989 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4990 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4991 return true;
4992 }
4993 return false;
4994}
4995
Alexey Bataev4acb8592014-07-07 13:01:15 +00004996StmtResult Sema::ActOnOpenMPSimdDirective(
4997 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4998 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004999 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005000 if (!AStmt)
5001 return StmtError();
5002
5003 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005004 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005005 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5006 // define the nested loops number.
5007 unsigned NestedLoopCount = CheckOpenMPLoop(
5008 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5009 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005010 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005011 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005012
Alexander Musmana5f070a2014-10-01 06:03:56 +00005013 assert((CurContext->isDependentContext() || B.builtAll()) &&
5014 "omp simd loop exprs were not built");
5015
Alexander Musman3276a272015-03-21 10:12:56 +00005016 if (!CurContext->isDependentContext()) {
5017 // Finalize the clauses that need pre-built expressions for CodeGen.
5018 for (auto C : Clauses) {
5019 if (auto LC = dyn_cast<OMPLinearClause>(C))
5020 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005021 B.NumIterations, *this, CurScope,
5022 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005023 return StmtError();
5024 }
5025 }
5026
Alexey Bataev66b15b52015-08-21 11:14:16 +00005027 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5028 // If both simdlen and safelen clauses are specified, the value of the simdlen
5029 // parameter must be less than or equal to the value of the safelen parameter.
5030 OMPSafelenClause *Safelen = nullptr;
5031 OMPSimdlenClause *Simdlen = nullptr;
5032 for (auto *Clause : Clauses) {
5033 if (Clause->getClauseKind() == OMPC_safelen)
5034 Safelen = cast<OMPSafelenClause>(Clause);
5035 else if (Clause->getClauseKind() == OMPC_simdlen)
5036 Simdlen = cast<OMPSimdlenClause>(Clause);
5037 if (Safelen && Simdlen)
5038 break;
5039 }
5040 if (Simdlen && Safelen &&
5041 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5042 Safelen->getSafelen()))
5043 return StmtError();
5044
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005045 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005046 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5047 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005048}
5049
Alexey Bataev4acb8592014-07-07 13:01:15 +00005050StmtResult Sema::ActOnOpenMPForDirective(
5051 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5052 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005053 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005054 if (!AStmt)
5055 return StmtError();
5056
5057 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005058 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005059 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5060 // define the nested loops number.
5061 unsigned NestedLoopCount = CheckOpenMPLoop(
5062 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5063 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005064 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005065 return StmtError();
5066
Alexander Musmana5f070a2014-10-01 06:03:56 +00005067 assert((CurContext->isDependentContext() || B.builtAll()) &&
5068 "omp for loop exprs were not built");
5069
Alexey Bataev54acd402015-08-04 11:18:19 +00005070 if (!CurContext->isDependentContext()) {
5071 // Finalize the clauses that need pre-built expressions for CodeGen.
5072 for (auto C : Clauses) {
5073 if (auto LC = dyn_cast<OMPLinearClause>(C))
5074 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005075 B.NumIterations, *this, CurScope,
5076 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005077 return StmtError();
5078 }
5079 }
5080
Alexey Bataevf29276e2014-06-18 04:14:57 +00005081 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005082 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005083 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005084}
5085
Alexander Musmanf82886e2014-09-18 05:12:34 +00005086StmtResult Sema::ActOnOpenMPForSimdDirective(
5087 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5088 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005089 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005090 if (!AStmt)
5091 return StmtError();
5092
5093 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005094 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005095 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5096 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005097 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005098 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5099 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5100 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005101 if (NestedLoopCount == 0)
5102 return StmtError();
5103
Alexander Musmanc6388682014-12-15 07:07:06 +00005104 assert((CurContext->isDependentContext() || B.builtAll()) &&
5105 "omp for simd loop exprs were not built");
5106
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005107 if (!CurContext->isDependentContext()) {
5108 // Finalize the clauses that need pre-built expressions for CodeGen.
5109 for (auto C : Clauses) {
5110 if (auto LC = dyn_cast<OMPLinearClause>(C))
5111 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005112 B.NumIterations, *this, CurScope,
5113 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005114 return StmtError();
5115 }
5116 }
5117
Alexey Bataev66b15b52015-08-21 11:14:16 +00005118 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5119 // If both simdlen and safelen clauses are specified, the value of the simdlen
5120 // parameter must be less than or equal to the value of the safelen parameter.
5121 OMPSafelenClause *Safelen = nullptr;
5122 OMPSimdlenClause *Simdlen = nullptr;
5123 for (auto *Clause : Clauses) {
5124 if (Clause->getClauseKind() == OMPC_safelen)
5125 Safelen = cast<OMPSafelenClause>(Clause);
5126 else if (Clause->getClauseKind() == OMPC_simdlen)
5127 Simdlen = cast<OMPSimdlenClause>(Clause);
5128 if (Safelen && Simdlen)
5129 break;
5130 }
5131 if (Simdlen && Safelen &&
5132 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5133 Safelen->getSafelen()))
5134 return StmtError();
5135
Alexander Musmanf82886e2014-09-18 05:12:34 +00005136 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005137 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5138 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005139}
5140
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005141StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5142 Stmt *AStmt,
5143 SourceLocation StartLoc,
5144 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005145 if (!AStmt)
5146 return StmtError();
5147
5148 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005149 auto BaseStmt = AStmt;
5150 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5151 BaseStmt = CS->getCapturedStmt();
5152 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5153 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005154 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005155 return StmtError();
5156 // All associated statements must be '#pragma omp section' except for
5157 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005158 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005159 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5160 if (SectionStmt)
5161 Diag(SectionStmt->getLocStart(),
5162 diag::err_omp_sections_substmt_not_section);
5163 return StmtError();
5164 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005165 cast<OMPSectionDirective>(SectionStmt)
5166 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005167 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005168 } else {
5169 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5170 return StmtError();
5171 }
5172
5173 getCurFunction()->setHasBranchProtectedScope();
5174
Alexey Bataev25e5b442015-09-15 12:52:43 +00005175 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5176 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005177}
5178
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005179StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5180 SourceLocation StartLoc,
5181 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005182 if (!AStmt)
5183 return StmtError();
5184
5185 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005186
5187 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005188 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005189
Alexey Bataev25e5b442015-09-15 12:52:43 +00005190 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5191 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005192}
5193
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005194StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5195 Stmt *AStmt,
5196 SourceLocation StartLoc,
5197 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005198 if (!AStmt)
5199 return StmtError();
5200
5201 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005202
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005203 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005204
Alexey Bataev3255bf32015-01-19 05:20:46 +00005205 // OpenMP [2.7.3, single Construct, Restrictions]
5206 // The copyprivate clause must not be used with the nowait clause.
5207 OMPClause *Nowait = nullptr;
5208 OMPClause *Copyprivate = nullptr;
5209 for (auto *Clause : Clauses) {
5210 if (Clause->getClauseKind() == OMPC_nowait)
5211 Nowait = Clause;
5212 else if (Clause->getClauseKind() == OMPC_copyprivate)
5213 Copyprivate = Clause;
5214 if (Copyprivate && Nowait) {
5215 Diag(Copyprivate->getLocStart(),
5216 diag::err_omp_single_copyprivate_with_nowait);
5217 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5218 return StmtError();
5219 }
5220 }
5221
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005222 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5223}
5224
Alexander Musman80c22892014-07-17 08:54:58 +00005225StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5226 SourceLocation StartLoc,
5227 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005228 if (!AStmt)
5229 return StmtError();
5230
5231 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005232
5233 getCurFunction()->setHasBranchProtectedScope();
5234
5235 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5236}
5237
Alexey Bataev28c75412015-12-15 08:19:24 +00005238StmtResult Sema::ActOnOpenMPCriticalDirective(
5239 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5240 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005241 if (!AStmt)
5242 return StmtError();
5243
5244 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005245
Alexey Bataev28c75412015-12-15 08:19:24 +00005246 bool ErrorFound = false;
5247 llvm::APSInt Hint;
5248 SourceLocation HintLoc;
5249 bool DependentHint = false;
5250 for (auto *C : Clauses) {
5251 if (C->getClauseKind() == OMPC_hint) {
5252 if (!DirName.getName()) {
5253 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5254 ErrorFound = true;
5255 }
5256 Expr *E = cast<OMPHintClause>(C)->getHint();
5257 if (E->isTypeDependent() || E->isValueDependent() ||
5258 E->isInstantiationDependent())
5259 DependentHint = true;
5260 else {
5261 Hint = E->EvaluateKnownConstInt(Context);
5262 HintLoc = C->getLocStart();
5263 }
5264 }
5265 }
5266 if (ErrorFound)
5267 return StmtError();
5268 auto Pair = DSAStack->getCriticalWithHint(DirName);
5269 if (Pair.first && DirName.getName() && !DependentHint) {
5270 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5271 Diag(StartLoc, diag::err_omp_critical_with_hint);
5272 if (HintLoc.isValid()) {
5273 Diag(HintLoc, diag::note_omp_critical_hint_here)
5274 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5275 } else
5276 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5277 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5278 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5279 << 1
5280 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5281 /*Radix=*/10, /*Signed=*/false);
5282 } else
5283 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5284 }
5285 }
5286
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005287 getCurFunction()->setHasBranchProtectedScope();
5288
Alexey Bataev28c75412015-12-15 08:19:24 +00005289 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5290 Clauses, AStmt);
5291 if (!Pair.first && DirName.getName() && !DependentHint)
5292 DSAStack->addCriticalWithHint(Dir, Hint);
5293 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005294}
5295
Alexey Bataev4acb8592014-07-07 13:01:15 +00005296StmtResult Sema::ActOnOpenMPParallelForDirective(
5297 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5298 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005299 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005300 if (!AStmt)
5301 return StmtError();
5302
Alexey Bataev4acb8592014-07-07 13:01:15 +00005303 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5304 // 1.2.2 OpenMP Language Terminology
5305 // Structured block - An executable statement with a single entry at the
5306 // top and a single exit at the bottom.
5307 // The point of exit cannot be a branch out of the structured block.
5308 // longjmp() and throw() must not violate the entry/exit criteria.
5309 CS->getCapturedDecl()->setNothrow();
5310
Alexander Musmanc6388682014-12-15 07:07:06 +00005311 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005312 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5313 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005314 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005315 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5316 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5317 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005318 if (NestedLoopCount == 0)
5319 return StmtError();
5320
Alexander Musmana5f070a2014-10-01 06:03:56 +00005321 assert((CurContext->isDependentContext() || B.builtAll()) &&
5322 "omp parallel for loop exprs were not built");
5323
Alexey Bataev54acd402015-08-04 11:18:19 +00005324 if (!CurContext->isDependentContext()) {
5325 // Finalize the clauses that need pre-built expressions for CodeGen.
5326 for (auto C : Clauses) {
5327 if (auto LC = dyn_cast<OMPLinearClause>(C))
5328 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005329 B.NumIterations, *this, CurScope,
5330 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005331 return StmtError();
5332 }
5333 }
5334
Alexey Bataev4acb8592014-07-07 13:01:15 +00005335 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005336 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005337 NestedLoopCount, Clauses, AStmt, B,
5338 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005339}
5340
Alexander Musmane4e893b2014-09-23 09:33:00 +00005341StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5342 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5343 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005344 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005345 if (!AStmt)
5346 return StmtError();
5347
Alexander Musmane4e893b2014-09-23 09:33:00 +00005348 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5349 // 1.2.2 OpenMP Language Terminology
5350 // Structured block - An executable statement with a single entry at the
5351 // top and a single exit at the bottom.
5352 // The point of exit cannot be a branch out of the structured block.
5353 // longjmp() and throw() must not violate the entry/exit criteria.
5354 CS->getCapturedDecl()->setNothrow();
5355
Alexander Musmanc6388682014-12-15 07:07:06 +00005356 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005357 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5358 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005359 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005360 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5361 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5362 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005363 if (NestedLoopCount == 0)
5364 return StmtError();
5365
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005366 if (!CurContext->isDependentContext()) {
5367 // Finalize the clauses that need pre-built expressions for CodeGen.
5368 for (auto C : Clauses) {
5369 if (auto LC = dyn_cast<OMPLinearClause>(C))
5370 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005371 B.NumIterations, *this, CurScope,
5372 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005373 return StmtError();
5374 }
5375 }
5376
Alexey Bataev66b15b52015-08-21 11:14:16 +00005377 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5378 // If both simdlen and safelen clauses are specified, the value of the simdlen
5379 // parameter must be less than or equal to the value of the safelen parameter.
5380 OMPSafelenClause *Safelen = nullptr;
5381 OMPSimdlenClause *Simdlen = nullptr;
5382 for (auto *Clause : Clauses) {
5383 if (Clause->getClauseKind() == OMPC_safelen)
5384 Safelen = cast<OMPSafelenClause>(Clause);
5385 else if (Clause->getClauseKind() == OMPC_simdlen)
5386 Simdlen = cast<OMPSimdlenClause>(Clause);
5387 if (Safelen && Simdlen)
5388 break;
5389 }
5390 if (Simdlen && Safelen &&
5391 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5392 Safelen->getSafelen()))
5393 return StmtError();
5394
Alexander Musmane4e893b2014-09-23 09:33:00 +00005395 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005396 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005397 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005398}
5399
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005400StmtResult
5401Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5402 Stmt *AStmt, SourceLocation StartLoc,
5403 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005404 if (!AStmt)
5405 return StmtError();
5406
5407 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005408 auto BaseStmt = AStmt;
5409 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5410 BaseStmt = CS->getCapturedStmt();
5411 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5412 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005413 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005414 return StmtError();
5415 // All associated statements must be '#pragma omp section' except for
5416 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005417 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005418 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5419 if (SectionStmt)
5420 Diag(SectionStmt->getLocStart(),
5421 diag::err_omp_parallel_sections_substmt_not_section);
5422 return StmtError();
5423 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005424 cast<OMPSectionDirective>(SectionStmt)
5425 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005426 }
5427 } else {
5428 Diag(AStmt->getLocStart(),
5429 diag::err_omp_parallel_sections_not_compound_stmt);
5430 return StmtError();
5431 }
5432
5433 getCurFunction()->setHasBranchProtectedScope();
5434
Alexey Bataev25e5b442015-09-15 12:52:43 +00005435 return OMPParallelSectionsDirective::Create(
5436 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005437}
5438
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005439StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5440 Stmt *AStmt, SourceLocation StartLoc,
5441 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005442 if (!AStmt)
5443 return StmtError();
5444
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005445 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5446 // 1.2.2 OpenMP Language Terminology
5447 // Structured block - An executable statement with a single entry at the
5448 // top and a single exit at the bottom.
5449 // The point of exit cannot be a branch out of the structured block.
5450 // longjmp() and throw() must not violate the entry/exit criteria.
5451 CS->getCapturedDecl()->setNothrow();
5452
5453 getCurFunction()->setHasBranchProtectedScope();
5454
Alexey Bataev25e5b442015-09-15 12:52:43 +00005455 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5456 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005457}
5458
Alexey Bataev68446b72014-07-18 07:47:19 +00005459StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5460 SourceLocation EndLoc) {
5461 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5462}
5463
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005464StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5465 SourceLocation EndLoc) {
5466 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5467}
5468
Alexey Bataev2df347a2014-07-18 10:17:07 +00005469StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5470 SourceLocation EndLoc) {
5471 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5472}
5473
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005474StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5475 SourceLocation StartLoc,
5476 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005477 if (!AStmt)
5478 return StmtError();
5479
5480 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005481
5482 getCurFunction()->setHasBranchProtectedScope();
5483
5484 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5485}
5486
Alexey Bataev6125da92014-07-21 11:26:11 +00005487StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5488 SourceLocation StartLoc,
5489 SourceLocation EndLoc) {
5490 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5491 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5492}
5493
Alexey Bataev346265e2015-09-25 10:37:12 +00005494StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5495 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005496 SourceLocation StartLoc,
5497 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005498 OMPClause *DependFound = nullptr;
5499 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005500 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005501 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005502 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005503 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005504 for (auto *C : Clauses) {
5505 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5506 DependFound = C;
5507 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5508 if (DependSourceClause) {
5509 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5510 << getOpenMPDirectiveName(OMPD_ordered)
5511 << getOpenMPClauseName(OMPC_depend) << 2;
5512 ErrorFound = true;
5513 } else
5514 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005515 if (DependSinkClause) {
5516 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5517 << 0;
5518 ErrorFound = true;
5519 }
5520 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5521 if (DependSourceClause) {
5522 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5523 << 1;
5524 ErrorFound = true;
5525 }
5526 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005527 }
5528 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005529 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005530 else if (C->getClauseKind() == OMPC_simd)
5531 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005532 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005533 if (!ErrorFound && !SC &&
5534 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005535 // OpenMP [2.8.1,simd Construct, Restrictions]
5536 // An ordered construct with the simd clause is the only OpenMP construct
5537 // that can appear in the simd region.
5538 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005539 ErrorFound = true;
5540 } else if (DependFound && (TC || SC)) {
5541 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5542 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5543 ErrorFound = true;
5544 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5545 Diag(DependFound->getLocStart(),
5546 diag::err_omp_ordered_directive_without_param);
5547 ErrorFound = true;
5548 } else if (TC || Clauses.empty()) {
5549 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5550 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5551 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5552 << (TC != nullptr);
5553 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5554 ErrorFound = true;
5555 }
5556 }
5557 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005558 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005559
5560 if (AStmt) {
5561 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5562
5563 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005564 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005565
5566 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005567}
5568
Alexey Bataev1d160b12015-03-13 12:27:31 +00005569namespace {
5570/// \brief Helper class for checking expression in 'omp atomic [update]'
5571/// construct.
5572class OpenMPAtomicUpdateChecker {
5573 /// \brief Error results for atomic update expressions.
5574 enum ExprAnalysisErrorCode {
5575 /// \brief A statement is not an expression statement.
5576 NotAnExpression,
5577 /// \brief Expression is not builtin binary or unary operation.
5578 NotABinaryOrUnaryExpression,
5579 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5580 NotAnUnaryIncDecExpression,
5581 /// \brief An expression is not of scalar type.
5582 NotAScalarType,
5583 /// \brief A binary operation is not an assignment operation.
5584 NotAnAssignmentOp,
5585 /// \brief RHS part of the binary operation is not a binary expression.
5586 NotABinaryExpression,
5587 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5588 /// expression.
5589 NotABinaryOperator,
5590 /// \brief RHS binary operation does not have reference to the updated LHS
5591 /// part.
5592 NotAnUpdateExpression,
5593 /// \brief No errors is found.
5594 NoError
5595 };
5596 /// \brief Reference to Sema.
5597 Sema &SemaRef;
5598 /// \brief A location for note diagnostics (when error is found).
5599 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005600 /// \brief 'x' lvalue part of the source atomic expression.
5601 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005602 /// \brief 'expr' rvalue part of the source atomic expression.
5603 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005604 /// \brief Helper expression of the form
5605 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5606 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5607 Expr *UpdateExpr;
5608 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5609 /// important for non-associative operations.
5610 bool IsXLHSInRHSPart;
5611 BinaryOperatorKind Op;
5612 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005613 /// \brief true if the source expression is a postfix unary operation, false
5614 /// if it is a prefix unary operation.
5615 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005616
5617public:
5618 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005619 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005620 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005621 /// \brief Check specified statement that it is suitable for 'atomic update'
5622 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005623 /// expression. If DiagId and NoteId == 0, then only check is performed
5624 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005625 /// \param DiagId Diagnostic which should be emitted if error is found.
5626 /// \param NoteId Diagnostic note for the main error message.
5627 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005628 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005629 /// \brief Return the 'x' lvalue part of the source atomic expression.
5630 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005631 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5632 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005633 /// \brief Return the update expression used in calculation of the updated
5634 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5635 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5636 Expr *getUpdateExpr() const { return UpdateExpr; }
5637 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5638 /// false otherwise.
5639 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5640
Alexey Bataevb78ca832015-04-01 03:33:17 +00005641 /// \brief true if the source expression is a postfix unary operation, false
5642 /// if it is a prefix unary operation.
5643 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5644
Alexey Bataev1d160b12015-03-13 12:27:31 +00005645private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005646 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5647 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005648};
5649} // namespace
5650
5651bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5652 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5653 ExprAnalysisErrorCode ErrorFound = NoError;
5654 SourceLocation ErrorLoc, NoteLoc;
5655 SourceRange ErrorRange, NoteRange;
5656 // Allowed constructs are:
5657 // x = x binop expr;
5658 // x = expr binop x;
5659 if (AtomicBinOp->getOpcode() == BO_Assign) {
5660 X = AtomicBinOp->getLHS();
5661 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5662 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5663 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5664 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5665 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005666 Op = AtomicInnerBinOp->getOpcode();
5667 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005668 auto *LHS = AtomicInnerBinOp->getLHS();
5669 auto *RHS = AtomicInnerBinOp->getRHS();
5670 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5671 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5672 /*Canonical=*/true);
5673 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5674 /*Canonical=*/true);
5675 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5676 /*Canonical=*/true);
5677 if (XId == LHSId) {
5678 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005679 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005680 } else if (XId == RHSId) {
5681 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005682 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005683 } else {
5684 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5685 ErrorRange = AtomicInnerBinOp->getSourceRange();
5686 NoteLoc = X->getExprLoc();
5687 NoteRange = X->getSourceRange();
5688 ErrorFound = NotAnUpdateExpression;
5689 }
5690 } else {
5691 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5692 ErrorRange = AtomicInnerBinOp->getSourceRange();
5693 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5694 NoteRange = SourceRange(NoteLoc, NoteLoc);
5695 ErrorFound = NotABinaryOperator;
5696 }
5697 } else {
5698 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5699 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5700 ErrorFound = NotABinaryExpression;
5701 }
5702 } else {
5703 ErrorLoc = AtomicBinOp->getExprLoc();
5704 ErrorRange = AtomicBinOp->getSourceRange();
5705 NoteLoc = AtomicBinOp->getOperatorLoc();
5706 NoteRange = SourceRange(NoteLoc, NoteLoc);
5707 ErrorFound = NotAnAssignmentOp;
5708 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005709 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005710 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5711 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5712 return true;
5713 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005714 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005715 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005716}
5717
5718bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5719 unsigned NoteId) {
5720 ExprAnalysisErrorCode ErrorFound = NoError;
5721 SourceLocation ErrorLoc, NoteLoc;
5722 SourceRange ErrorRange, NoteRange;
5723 // Allowed constructs are:
5724 // x++;
5725 // x--;
5726 // ++x;
5727 // --x;
5728 // x binop= expr;
5729 // x = x binop expr;
5730 // x = expr binop x;
5731 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5732 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5733 if (AtomicBody->getType()->isScalarType() ||
5734 AtomicBody->isInstantiationDependent()) {
5735 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5736 AtomicBody->IgnoreParenImpCasts())) {
5737 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005738 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005739 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005740 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005741 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005742 X = AtomicCompAssignOp->getLHS();
5743 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005744 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5745 AtomicBody->IgnoreParenImpCasts())) {
5746 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005747 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5748 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005749 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005750 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5751 // Check for Unary Operation
5752 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005753 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005754 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5755 OpLoc = AtomicUnaryOp->getOperatorLoc();
5756 X = AtomicUnaryOp->getSubExpr();
5757 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5758 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005759 } else {
5760 ErrorFound = NotAnUnaryIncDecExpression;
5761 ErrorLoc = AtomicUnaryOp->getExprLoc();
5762 ErrorRange = AtomicUnaryOp->getSourceRange();
5763 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5764 NoteRange = SourceRange(NoteLoc, NoteLoc);
5765 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005766 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005767 ErrorFound = NotABinaryOrUnaryExpression;
5768 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5769 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5770 }
5771 } else {
5772 ErrorFound = NotAScalarType;
5773 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5774 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5775 }
5776 } else {
5777 ErrorFound = NotAnExpression;
5778 NoteLoc = ErrorLoc = S->getLocStart();
5779 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5780 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005781 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005782 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5783 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5784 return true;
5785 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005786 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005787 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005788 // Build an update expression of form 'OpaqueValueExpr(x) binop
5789 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5790 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5791 auto *OVEX = new (SemaRef.getASTContext())
5792 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5793 auto *OVEExpr = new (SemaRef.getASTContext())
5794 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5795 auto Update =
5796 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5797 IsXLHSInRHSPart ? OVEExpr : OVEX);
5798 if (Update.isInvalid())
5799 return true;
5800 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5801 Sema::AA_Casting);
5802 if (Update.isInvalid())
5803 return true;
5804 UpdateExpr = Update.get();
5805 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005806 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005807}
5808
Alexey Bataev0162e452014-07-22 10:10:35 +00005809StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5810 Stmt *AStmt,
5811 SourceLocation StartLoc,
5812 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005813 if (!AStmt)
5814 return StmtError();
5815
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005816 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005817 // 1.2.2 OpenMP Language Terminology
5818 // Structured block - An executable statement with a single entry at the
5819 // top and a single exit at the bottom.
5820 // The point of exit cannot be a branch out of the structured block.
5821 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005822 OpenMPClauseKind AtomicKind = OMPC_unknown;
5823 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005824 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005825 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005826 C->getClauseKind() == OMPC_update ||
5827 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005828 if (AtomicKind != OMPC_unknown) {
5829 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5830 << SourceRange(C->getLocStart(), C->getLocEnd());
5831 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5832 << getOpenMPClauseName(AtomicKind);
5833 } else {
5834 AtomicKind = C->getClauseKind();
5835 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005836 }
5837 }
5838 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005839
Alexey Bataev459dec02014-07-24 06:46:57 +00005840 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005841 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5842 Body = EWC->getSubExpr();
5843
Alexey Bataev62cec442014-11-18 10:14:22 +00005844 Expr *X = nullptr;
5845 Expr *V = nullptr;
5846 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005847 Expr *UE = nullptr;
5848 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005849 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005850 // OpenMP [2.12.6, atomic Construct]
5851 // In the next expressions:
5852 // * x and v (as applicable) are both l-value expressions with scalar type.
5853 // * During the execution of an atomic region, multiple syntactic
5854 // occurrences of x must designate the same storage location.
5855 // * Neither of v and expr (as applicable) may access the storage location
5856 // designated by x.
5857 // * Neither of x and expr (as applicable) may access the storage location
5858 // designated by v.
5859 // * expr is an expression with scalar type.
5860 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5861 // * binop, binop=, ++, and -- are not overloaded operators.
5862 // * The expression x binop expr must be numerically equivalent to x binop
5863 // (expr). This requirement is satisfied if the operators in expr have
5864 // precedence greater than binop, or by using parentheses around expr or
5865 // subexpressions of expr.
5866 // * The expression expr binop x must be numerically equivalent to (expr)
5867 // binop x. This requirement is satisfied if the operators in expr have
5868 // precedence equal to or greater than binop, or by using parentheses around
5869 // expr or subexpressions of expr.
5870 // * For forms that allow multiple occurrences of x, the number of times
5871 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005872 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005873 enum {
5874 NotAnExpression,
5875 NotAnAssignmentOp,
5876 NotAScalarType,
5877 NotAnLValue,
5878 NoError
5879 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005880 SourceLocation ErrorLoc, NoteLoc;
5881 SourceRange ErrorRange, NoteRange;
5882 // If clause is read:
5883 // v = x;
5884 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5885 auto AtomicBinOp =
5886 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5887 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5888 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5889 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5890 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5891 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5892 if (!X->isLValue() || !V->isLValue()) {
5893 auto NotLValueExpr = X->isLValue() ? V : X;
5894 ErrorFound = NotAnLValue;
5895 ErrorLoc = AtomicBinOp->getExprLoc();
5896 ErrorRange = AtomicBinOp->getSourceRange();
5897 NoteLoc = NotLValueExpr->getExprLoc();
5898 NoteRange = NotLValueExpr->getSourceRange();
5899 }
5900 } else if (!X->isInstantiationDependent() ||
5901 !V->isInstantiationDependent()) {
5902 auto NotScalarExpr =
5903 (X->isInstantiationDependent() || X->getType()->isScalarType())
5904 ? V
5905 : X;
5906 ErrorFound = NotAScalarType;
5907 ErrorLoc = AtomicBinOp->getExprLoc();
5908 ErrorRange = AtomicBinOp->getSourceRange();
5909 NoteLoc = NotScalarExpr->getExprLoc();
5910 NoteRange = NotScalarExpr->getSourceRange();
5911 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005912 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005913 ErrorFound = NotAnAssignmentOp;
5914 ErrorLoc = AtomicBody->getExprLoc();
5915 ErrorRange = AtomicBody->getSourceRange();
5916 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5917 : AtomicBody->getExprLoc();
5918 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5919 : AtomicBody->getSourceRange();
5920 }
5921 } else {
5922 ErrorFound = NotAnExpression;
5923 NoteLoc = ErrorLoc = Body->getLocStart();
5924 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005925 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005926 if (ErrorFound != NoError) {
5927 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5928 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005929 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5930 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005931 return StmtError();
5932 } else if (CurContext->isDependentContext())
5933 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005934 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005935 enum {
5936 NotAnExpression,
5937 NotAnAssignmentOp,
5938 NotAScalarType,
5939 NotAnLValue,
5940 NoError
5941 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005942 SourceLocation ErrorLoc, NoteLoc;
5943 SourceRange ErrorRange, NoteRange;
5944 // If clause is write:
5945 // x = expr;
5946 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5947 auto AtomicBinOp =
5948 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5949 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005950 X = AtomicBinOp->getLHS();
5951 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005952 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5953 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5954 if (!X->isLValue()) {
5955 ErrorFound = NotAnLValue;
5956 ErrorLoc = AtomicBinOp->getExprLoc();
5957 ErrorRange = AtomicBinOp->getSourceRange();
5958 NoteLoc = X->getExprLoc();
5959 NoteRange = X->getSourceRange();
5960 }
5961 } else if (!X->isInstantiationDependent() ||
5962 !E->isInstantiationDependent()) {
5963 auto NotScalarExpr =
5964 (X->isInstantiationDependent() || X->getType()->isScalarType())
5965 ? E
5966 : X;
5967 ErrorFound = NotAScalarType;
5968 ErrorLoc = AtomicBinOp->getExprLoc();
5969 ErrorRange = AtomicBinOp->getSourceRange();
5970 NoteLoc = NotScalarExpr->getExprLoc();
5971 NoteRange = NotScalarExpr->getSourceRange();
5972 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005973 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005974 ErrorFound = NotAnAssignmentOp;
5975 ErrorLoc = AtomicBody->getExprLoc();
5976 ErrorRange = AtomicBody->getSourceRange();
5977 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5978 : AtomicBody->getExprLoc();
5979 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5980 : AtomicBody->getSourceRange();
5981 }
5982 } else {
5983 ErrorFound = NotAnExpression;
5984 NoteLoc = ErrorLoc = Body->getLocStart();
5985 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005986 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005987 if (ErrorFound != NoError) {
5988 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5989 << ErrorRange;
5990 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5991 << NoteRange;
5992 return StmtError();
5993 } else if (CurContext->isDependentContext())
5994 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005995 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005996 // If clause is update:
5997 // x++;
5998 // x--;
5999 // ++x;
6000 // --x;
6001 // x binop= expr;
6002 // x = x binop expr;
6003 // x = expr binop x;
6004 OpenMPAtomicUpdateChecker Checker(*this);
6005 if (Checker.checkStatement(
6006 Body, (AtomicKind == OMPC_update)
6007 ? diag::err_omp_atomic_update_not_expression_statement
6008 : diag::err_omp_atomic_not_expression_statement,
6009 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006010 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006011 if (!CurContext->isDependentContext()) {
6012 E = Checker.getExpr();
6013 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006014 UE = Checker.getUpdateExpr();
6015 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006016 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006017 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006018 enum {
6019 NotAnAssignmentOp,
6020 NotACompoundStatement,
6021 NotTwoSubstatements,
6022 NotASpecificExpression,
6023 NoError
6024 } ErrorFound = NoError;
6025 SourceLocation ErrorLoc, NoteLoc;
6026 SourceRange ErrorRange, NoteRange;
6027 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6028 // If clause is a capture:
6029 // v = x++;
6030 // v = x--;
6031 // v = ++x;
6032 // v = --x;
6033 // v = x binop= expr;
6034 // v = x = x binop expr;
6035 // v = x = expr binop x;
6036 auto *AtomicBinOp =
6037 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6038 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6039 V = AtomicBinOp->getLHS();
6040 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6041 OpenMPAtomicUpdateChecker Checker(*this);
6042 if (Checker.checkStatement(
6043 Body, diag::err_omp_atomic_capture_not_expression_statement,
6044 diag::note_omp_atomic_update))
6045 return StmtError();
6046 E = Checker.getExpr();
6047 X = Checker.getX();
6048 UE = Checker.getUpdateExpr();
6049 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6050 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006051 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006052 ErrorLoc = AtomicBody->getExprLoc();
6053 ErrorRange = AtomicBody->getSourceRange();
6054 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6055 : AtomicBody->getExprLoc();
6056 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6057 : AtomicBody->getSourceRange();
6058 ErrorFound = NotAnAssignmentOp;
6059 }
6060 if (ErrorFound != NoError) {
6061 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6062 << ErrorRange;
6063 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6064 return StmtError();
6065 } else if (CurContext->isDependentContext()) {
6066 UE = V = E = X = nullptr;
6067 }
6068 } else {
6069 // If clause is a capture:
6070 // { v = x; x = expr; }
6071 // { v = x; x++; }
6072 // { v = x; x--; }
6073 // { v = x; ++x; }
6074 // { v = x; --x; }
6075 // { v = x; x binop= expr; }
6076 // { v = x; x = x binop expr; }
6077 // { v = x; x = expr binop x; }
6078 // { x++; v = x; }
6079 // { x--; v = x; }
6080 // { ++x; v = x; }
6081 // { --x; v = x; }
6082 // { x binop= expr; v = x; }
6083 // { x = x binop expr; v = x; }
6084 // { x = expr binop x; v = x; }
6085 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6086 // Check that this is { expr1; expr2; }
6087 if (CS->size() == 2) {
6088 auto *First = CS->body_front();
6089 auto *Second = CS->body_back();
6090 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6091 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6092 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6093 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6094 // Need to find what subexpression is 'v' and what is 'x'.
6095 OpenMPAtomicUpdateChecker Checker(*this);
6096 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6097 BinaryOperator *BinOp = nullptr;
6098 if (IsUpdateExprFound) {
6099 BinOp = dyn_cast<BinaryOperator>(First);
6100 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6101 }
6102 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6103 // { v = x; x++; }
6104 // { v = x; x--; }
6105 // { v = x; ++x; }
6106 // { v = x; --x; }
6107 // { v = x; x binop= expr; }
6108 // { v = x; x = x binop expr; }
6109 // { v = x; x = expr binop x; }
6110 // Check that the first expression has form v = x.
6111 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6112 llvm::FoldingSetNodeID XId, PossibleXId;
6113 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6114 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6115 IsUpdateExprFound = XId == PossibleXId;
6116 if (IsUpdateExprFound) {
6117 V = BinOp->getLHS();
6118 X = Checker.getX();
6119 E = Checker.getExpr();
6120 UE = Checker.getUpdateExpr();
6121 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006122 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006123 }
6124 }
6125 if (!IsUpdateExprFound) {
6126 IsUpdateExprFound = !Checker.checkStatement(First);
6127 BinOp = nullptr;
6128 if (IsUpdateExprFound) {
6129 BinOp = dyn_cast<BinaryOperator>(Second);
6130 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6131 }
6132 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6133 // { x++; v = x; }
6134 // { x--; v = x; }
6135 // { ++x; v = x; }
6136 // { --x; v = x; }
6137 // { x binop= expr; v = x; }
6138 // { x = x binop expr; v = x; }
6139 // { x = expr binop x; v = x; }
6140 // Check that the second expression has form v = x.
6141 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6142 llvm::FoldingSetNodeID XId, PossibleXId;
6143 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6144 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6145 IsUpdateExprFound = XId == PossibleXId;
6146 if (IsUpdateExprFound) {
6147 V = BinOp->getLHS();
6148 X = Checker.getX();
6149 E = Checker.getExpr();
6150 UE = Checker.getUpdateExpr();
6151 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006152 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006153 }
6154 }
6155 }
6156 if (!IsUpdateExprFound) {
6157 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006158 auto *FirstExpr = dyn_cast<Expr>(First);
6159 auto *SecondExpr = dyn_cast<Expr>(Second);
6160 if (!FirstExpr || !SecondExpr ||
6161 !(FirstExpr->isInstantiationDependent() ||
6162 SecondExpr->isInstantiationDependent())) {
6163 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6164 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006165 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006166 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6167 : First->getLocStart();
6168 NoteRange = ErrorRange = FirstBinOp
6169 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006170 : SourceRange(ErrorLoc, ErrorLoc);
6171 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006172 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6173 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6174 ErrorFound = NotAnAssignmentOp;
6175 NoteLoc = ErrorLoc = SecondBinOp
6176 ? SecondBinOp->getOperatorLoc()
6177 : Second->getLocStart();
6178 NoteRange = ErrorRange =
6179 SecondBinOp ? SecondBinOp->getSourceRange()
6180 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006181 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006182 auto *PossibleXRHSInFirst =
6183 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6184 auto *PossibleXLHSInSecond =
6185 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6186 llvm::FoldingSetNodeID X1Id, X2Id;
6187 PossibleXRHSInFirst->Profile(X1Id, Context,
6188 /*Canonical=*/true);
6189 PossibleXLHSInSecond->Profile(X2Id, Context,
6190 /*Canonical=*/true);
6191 IsUpdateExprFound = X1Id == X2Id;
6192 if (IsUpdateExprFound) {
6193 V = FirstBinOp->getLHS();
6194 X = SecondBinOp->getLHS();
6195 E = SecondBinOp->getRHS();
6196 UE = nullptr;
6197 IsXLHSInRHSPart = false;
6198 IsPostfixUpdate = true;
6199 } else {
6200 ErrorFound = NotASpecificExpression;
6201 ErrorLoc = FirstBinOp->getExprLoc();
6202 ErrorRange = FirstBinOp->getSourceRange();
6203 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6204 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6205 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006206 }
6207 }
6208 }
6209 }
6210 } else {
6211 NoteLoc = ErrorLoc = Body->getLocStart();
6212 NoteRange = ErrorRange =
6213 SourceRange(Body->getLocStart(), Body->getLocStart());
6214 ErrorFound = NotTwoSubstatements;
6215 }
6216 } else {
6217 NoteLoc = ErrorLoc = Body->getLocStart();
6218 NoteRange = ErrorRange =
6219 SourceRange(Body->getLocStart(), Body->getLocStart());
6220 ErrorFound = NotACompoundStatement;
6221 }
6222 if (ErrorFound != NoError) {
6223 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6224 << ErrorRange;
6225 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6226 return StmtError();
6227 } else if (CurContext->isDependentContext()) {
6228 UE = V = E = X = nullptr;
6229 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006230 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006231 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006232
6233 getCurFunction()->setHasBranchProtectedScope();
6234
Alexey Bataev62cec442014-11-18 10:14:22 +00006235 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006236 X, V, E, UE, IsXLHSInRHSPart,
6237 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006238}
6239
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006240StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6241 Stmt *AStmt,
6242 SourceLocation StartLoc,
6243 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006244 if (!AStmt)
6245 return StmtError();
6246
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006247 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6248 // 1.2.2 OpenMP Language Terminology
6249 // Structured block - An executable statement with a single entry at the
6250 // top and a single exit at the bottom.
6251 // The point of exit cannot be a branch out of the structured block.
6252 // longjmp() and throw() must not violate the entry/exit criteria.
6253 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006254
Alexey Bataev13314bf2014-10-09 04:18:56 +00006255 // OpenMP [2.16, Nesting of Regions]
6256 // If specified, a teams construct must be contained within a target
6257 // construct. That target construct must contain no statements or directives
6258 // outside of the teams construct.
6259 if (DSAStack->hasInnerTeamsRegion()) {
6260 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6261 bool OMPTeamsFound = true;
6262 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6263 auto I = CS->body_begin();
6264 while (I != CS->body_end()) {
6265 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6266 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6267 OMPTeamsFound = false;
6268 break;
6269 }
6270 ++I;
6271 }
6272 assert(I != CS->body_end() && "Not found statement");
6273 S = *I;
6274 }
6275 if (!OMPTeamsFound) {
6276 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6277 Diag(DSAStack->getInnerTeamsRegionLoc(),
6278 diag::note_omp_nested_teams_construct_here);
6279 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6280 << isa<OMPExecutableDirective>(S);
6281 return StmtError();
6282 }
6283 }
6284
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006285 getCurFunction()->setHasBranchProtectedScope();
6286
6287 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6288}
6289
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006290StmtResult
6291Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6292 Stmt *AStmt, SourceLocation StartLoc,
6293 SourceLocation EndLoc) {
6294 if (!AStmt)
6295 return StmtError();
6296
6297 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6298 // 1.2.2 OpenMP Language Terminology
6299 // Structured block - An executable statement with a single entry at the
6300 // top and a single exit at the bottom.
6301 // The point of exit cannot be a branch out of the structured block.
6302 // longjmp() and throw() must not violate the entry/exit criteria.
6303 CS->getCapturedDecl()->setNothrow();
6304
6305 getCurFunction()->setHasBranchProtectedScope();
6306
6307 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6308 AStmt);
6309}
6310
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006311StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6312 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6313 SourceLocation EndLoc,
6314 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6315 if (!AStmt)
6316 return StmtError();
6317
6318 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6319 // 1.2.2 OpenMP Language Terminology
6320 // Structured block - An executable statement with a single entry at the
6321 // top and a single exit at the bottom.
6322 // The point of exit cannot be a branch out of the structured block.
6323 // longjmp() and throw() must not violate the entry/exit criteria.
6324 CS->getCapturedDecl()->setNothrow();
6325
6326 OMPLoopDirective::HelperExprs B;
6327 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6328 // define the nested loops number.
6329 unsigned NestedLoopCount =
6330 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6331 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6332 VarsWithImplicitDSA, B);
6333 if (NestedLoopCount == 0)
6334 return StmtError();
6335
6336 assert((CurContext->isDependentContext() || B.builtAll()) &&
6337 "omp target parallel for loop exprs were not built");
6338
6339 if (!CurContext->isDependentContext()) {
6340 // Finalize the clauses that need pre-built expressions for CodeGen.
6341 for (auto C : Clauses) {
6342 if (auto LC = dyn_cast<OMPLinearClause>(C))
6343 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006344 B.NumIterations, *this, CurScope,
6345 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006346 return StmtError();
6347 }
6348 }
6349
6350 getCurFunction()->setHasBranchProtectedScope();
6351 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6352 NestedLoopCount, Clauses, AStmt,
6353 B, DSAStack->isCancelRegion());
6354}
6355
Samuel Antaodf67fc42016-01-19 19:15:56 +00006356/// \brief Check for existence of a map clause in the list of clauses.
6357static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6358 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6359 I != E; ++I) {
6360 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6361 return true;
6362 }
6363 }
6364
6365 return false;
6366}
6367
Michael Wong65f367f2015-07-21 13:44:28 +00006368StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6369 Stmt *AStmt,
6370 SourceLocation StartLoc,
6371 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006372 if (!AStmt)
6373 return StmtError();
6374
6375 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6376
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006377 // OpenMP [2.10.1, Restrictions, p. 97]
6378 // At least one map clause must appear on the directive.
6379 if (!HasMapClause(Clauses)) {
6380 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6381 getOpenMPDirectiveName(OMPD_target_data);
6382 return StmtError();
6383 }
6384
Michael Wong65f367f2015-07-21 13:44:28 +00006385 getCurFunction()->setHasBranchProtectedScope();
6386
6387 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6388 AStmt);
6389}
6390
Samuel Antaodf67fc42016-01-19 19:15:56 +00006391StmtResult
6392Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6393 SourceLocation StartLoc,
6394 SourceLocation EndLoc) {
6395 // OpenMP [2.10.2, Restrictions, p. 99]
6396 // At least one map clause must appear on the directive.
6397 if (!HasMapClause(Clauses)) {
6398 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6399 << getOpenMPDirectiveName(OMPD_target_enter_data);
6400 return StmtError();
6401 }
6402
6403 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6404 Clauses);
6405}
6406
Samuel Antao72590762016-01-19 20:04:50 +00006407StmtResult
6408Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6409 SourceLocation StartLoc,
6410 SourceLocation EndLoc) {
6411 // OpenMP [2.10.3, Restrictions, p. 102]
6412 // At least one map clause must appear on the directive.
6413 if (!HasMapClause(Clauses)) {
6414 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6415 << getOpenMPDirectiveName(OMPD_target_exit_data);
6416 return StmtError();
6417 }
6418
6419 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6420}
6421
Alexey Bataev13314bf2014-10-09 04:18:56 +00006422StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6423 Stmt *AStmt, SourceLocation StartLoc,
6424 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006425 if (!AStmt)
6426 return StmtError();
6427
Alexey Bataev13314bf2014-10-09 04:18:56 +00006428 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6429 // 1.2.2 OpenMP Language Terminology
6430 // Structured block - An executable statement with a single entry at the
6431 // top and a single exit at the bottom.
6432 // The point of exit cannot be a branch out of the structured block.
6433 // longjmp() and throw() must not violate the entry/exit criteria.
6434 CS->getCapturedDecl()->setNothrow();
6435
6436 getCurFunction()->setHasBranchProtectedScope();
6437
6438 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6439}
6440
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006441StmtResult
6442Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6443 SourceLocation EndLoc,
6444 OpenMPDirectiveKind CancelRegion) {
6445 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6446 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6447 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6448 << getOpenMPDirectiveName(CancelRegion);
6449 return StmtError();
6450 }
6451 if (DSAStack->isParentNowaitRegion()) {
6452 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6453 return StmtError();
6454 }
6455 if (DSAStack->isParentOrderedRegion()) {
6456 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6457 return StmtError();
6458 }
6459 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6460 CancelRegion);
6461}
6462
Alexey Bataev87933c72015-09-18 08:07:34 +00006463StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6464 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006465 SourceLocation EndLoc,
6466 OpenMPDirectiveKind CancelRegion) {
6467 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6468 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6469 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6470 << getOpenMPDirectiveName(CancelRegion);
6471 return StmtError();
6472 }
6473 if (DSAStack->isParentNowaitRegion()) {
6474 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6475 return StmtError();
6476 }
6477 if (DSAStack->isParentOrderedRegion()) {
6478 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6479 return StmtError();
6480 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006481 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006482 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6483 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006484}
6485
Alexey Bataev382967a2015-12-08 12:06:20 +00006486static bool checkGrainsizeNumTasksClauses(Sema &S,
6487 ArrayRef<OMPClause *> Clauses) {
6488 OMPClause *PrevClause = nullptr;
6489 bool ErrorFound = false;
6490 for (auto *C : Clauses) {
6491 if (C->getClauseKind() == OMPC_grainsize ||
6492 C->getClauseKind() == OMPC_num_tasks) {
6493 if (!PrevClause)
6494 PrevClause = C;
6495 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6496 S.Diag(C->getLocStart(),
6497 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6498 << getOpenMPClauseName(C->getClauseKind())
6499 << getOpenMPClauseName(PrevClause->getClauseKind());
6500 S.Diag(PrevClause->getLocStart(),
6501 diag::note_omp_previous_grainsize_num_tasks)
6502 << getOpenMPClauseName(PrevClause->getClauseKind());
6503 ErrorFound = true;
6504 }
6505 }
6506 }
6507 return ErrorFound;
6508}
6509
Alexey Bataev49f6e782015-12-01 04:18:41 +00006510StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6511 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6512 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006513 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006514 if (!AStmt)
6515 return StmtError();
6516
6517 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6518 OMPLoopDirective::HelperExprs B;
6519 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6520 // define the nested loops number.
6521 unsigned NestedLoopCount =
6522 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006523 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006524 VarsWithImplicitDSA, B);
6525 if (NestedLoopCount == 0)
6526 return StmtError();
6527
6528 assert((CurContext->isDependentContext() || B.builtAll()) &&
6529 "omp for loop exprs were not built");
6530
Alexey Bataev382967a2015-12-08 12:06:20 +00006531 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6532 // The grainsize clause and num_tasks clause are mutually exclusive and may
6533 // not appear on the same taskloop directive.
6534 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6535 return StmtError();
6536
Alexey Bataev49f6e782015-12-01 04:18:41 +00006537 getCurFunction()->setHasBranchProtectedScope();
6538 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6539 NestedLoopCount, Clauses, AStmt, B);
6540}
6541
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006542StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6543 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6544 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006545 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006546 if (!AStmt)
6547 return StmtError();
6548
6549 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6550 OMPLoopDirective::HelperExprs B;
6551 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6552 // define the nested loops number.
6553 unsigned NestedLoopCount =
6554 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6555 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6556 VarsWithImplicitDSA, B);
6557 if (NestedLoopCount == 0)
6558 return StmtError();
6559
6560 assert((CurContext->isDependentContext() || B.builtAll()) &&
6561 "omp for loop exprs were not built");
6562
Alexey Bataev5a3af132016-03-29 08:58:54 +00006563 if (!CurContext->isDependentContext()) {
6564 // Finalize the clauses that need pre-built expressions for CodeGen.
6565 for (auto C : Clauses) {
6566 if (auto LC = dyn_cast<OMPLinearClause>(C))
6567 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006568 B.NumIterations, *this, CurScope,
6569 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006570 return StmtError();
6571 }
6572 }
6573
Alexey Bataev382967a2015-12-08 12:06:20 +00006574 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6575 // The grainsize clause and num_tasks clause are mutually exclusive and may
6576 // not appear on the same taskloop directive.
6577 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6578 return StmtError();
6579
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006580 getCurFunction()->setHasBranchProtectedScope();
6581 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6582 NestedLoopCount, Clauses, AStmt, B);
6583}
6584
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006585StmtResult Sema::ActOnOpenMPDistributeDirective(
6586 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6587 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006588 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006589 if (!AStmt)
6590 return StmtError();
6591
6592 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6593 OMPLoopDirective::HelperExprs B;
6594 // In presence of clause 'collapse' with number of loops, it will
6595 // define the nested loops number.
6596 unsigned NestedLoopCount =
6597 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6598 nullptr /*ordered not a clause on distribute*/, AStmt,
6599 *this, *DSAStack, VarsWithImplicitDSA, B);
6600 if (NestedLoopCount == 0)
6601 return StmtError();
6602
6603 assert((CurContext->isDependentContext() || B.builtAll()) &&
6604 "omp for loop exprs were not built");
6605
6606 getCurFunction()->setHasBranchProtectedScope();
6607 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6608 NestedLoopCount, Clauses, AStmt, B);
6609}
6610
Alexey Bataeved09d242014-05-28 05:53:51 +00006611OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006612 SourceLocation StartLoc,
6613 SourceLocation LParenLoc,
6614 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006615 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006616 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006617 case OMPC_final:
6618 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6619 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006620 case OMPC_num_threads:
6621 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6622 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006623 case OMPC_safelen:
6624 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6625 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006626 case OMPC_simdlen:
6627 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6628 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006629 case OMPC_collapse:
6630 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6631 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006632 case OMPC_ordered:
6633 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6634 break;
Michael Wonge710d542015-08-07 16:16:36 +00006635 case OMPC_device:
6636 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6637 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006638 case OMPC_num_teams:
6639 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6640 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006641 case OMPC_thread_limit:
6642 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6643 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006644 case OMPC_priority:
6645 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6646 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006647 case OMPC_grainsize:
6648 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6649 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006650 case OMPC_num_tasks:
6651 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6652 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006653 case OMPC_hint:
6654 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6655 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006656 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006657 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006658 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006659 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006660 case OMPC_private:
6661 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006662 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006663 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006664 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006665 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006666 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006667 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006668 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006669 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006670 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006671 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006672 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006673 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006674 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006675 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006676 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006677 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006678 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006679 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006680 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006681 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006682 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006683 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006684 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006685 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006686 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006687 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006688 llvm_unreachable("Clause is not allowed.");
6689 }
6690 return Res;
6691}
6692
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006693OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6694 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006695 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006696 SourceLocation NameModifierLoc,
6697 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006698 SourceLocation EndLoc) {
6699 Expr *ValExpr = Condition;
6700 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6701 !Condition->isInstantiationDependent() &&
6702 !Condition->containsUnexpandedParameterPack()) {
6703 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006704 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006705 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006706 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006707
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006708 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006709 }
6710
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006711 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6712 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006713}
6714
Alexey Bataev3778b602014-07-17 07:32:53 +00006715OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6716 SourceLocation StartLoc,
6717 SourceLocation LParenLoc,
6718 SourceLocation EndLoc) {
6719 Expr *ValExpr = Condition;
6720 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6721 !Condition->isInstantiationDependent() &&
6722 !Condition->containsUnexpandedParameterPack()) {
6723 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6724 Condition->getExprLoc(), Condition);
6725 if (Val.isInvalid())
6726 return nullptr;
6727
6728 ValExpr = Val.get();
6729 }
6730
6731 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6732}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006733ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6734 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006735 if (!Op)
6736 return ExprError();
6737
6738 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6739 public:
6740 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006741 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006742 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6743 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006744 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6745 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006746 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6747 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006748 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6749 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006750 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6751 QualType T,
6752 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006753 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6754 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006755 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6756 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006757 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006758 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006759 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006760 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6761 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006762 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6763 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006764 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6765 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006766 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006767 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006768 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006769 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6770 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006771 llvm_unreachable("conversion functions are permitted");
6772 }
6773 } ConvertDiagnoser;
6774 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6775}
6776
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006777static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006778 OpenMPClauseKind CKind,
6779 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006780 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6781 !ValExpr->isInstantiationDependent()) {
6782 SourceLocation Loc = ValExpr->getExprLoc();
6783 ExprResult Value =
6784 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6785 if (Value.isInvalid())
6786 return false;
6787
6788 ValExpr = Value.get();
6789 // The expression must evaluate to a non-negative integer value.
6790 llvm::APSInt Result;
6791 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006792 Result.isSigned() &&
6793 !((!StrictlyPositive && Result.isNonNegative()) ||
6794 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006795 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006796 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6797 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006798 return false;
6799 }
6800 }
6801 return true;
6802}
6803
Alexey Bataev568a8332014-03-06 06:15:19 +00006804OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6805 SourceLocation StartLoc,
6806 SourceLocation LParenLoc,
6807 SourceLocation EndLoc) {
6808 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006809
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006810 // OpenMP [2.5, Restrictions]
6811 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006812 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6813 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006814 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006815
Alexey Bataeved09d242014-05-28 05:53:51 +00006816 return new (Context)
6817 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006818}
6819
Alexey Bataev62c87d22014-03-21 04:51:18 +00006820ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006821 OpenMPClauseKind CKind,
6822 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006823 if (!E)
6824 return ExprError();
6825 if (E->isValueDependent() || E->isTypeDependent() ||
6826 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006827 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006828 llvm::APSInt Result;
6829 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6830 if (ICE.isInvalid())
6831 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006832 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6833 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006834 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006835 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6836 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006837 return ExprError();
6838 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006839 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6840 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6841 << E->getSourceRange();
6842 return ExprError();
6843 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006844 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6845 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006846 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006847 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006848 return ICE;
6849}
6850
6851OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6852 SourceLocation LParenLoc,
6853 SourceLocation EndLoc) {
6854 // OpenMP [2.8.1, simd construct, Description]
6855 // The parameter of the safelen clause must be a constant
6856 // positive integer expression.
6857 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6858 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006859 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006860 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006861 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006862}
6863
Alexey Bataev66b15b52015-08-21 11:14:16 +00006864OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6865 SourceLocation LParenLoc,
6866 SourceLocation EndLoc) {
6867 // OpenMP [2.8.1, simd construct, Description]
6868 // The parameter of the simdlen clause must be a constant
6869 // positive integer expression.
6870 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6871 if (Simdlen.isInvalid())
6872 return nullptr;
6873 return new (Context)
6874 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6875}
6876
Alexander Musman64d33f12014-06-04 07:53:32 +00006877OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6878 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006879 SourceLocation LParenLoc,
6880 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006881 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006882 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006883 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006884 // The parameter of the collapse clause must be a constant
6885 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006886 ExprResult NumForLoopsResult =
6887 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6888 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006889 return nullptr;
6890 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006891 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006892}
6893
Alexey Bataev10e775f2015-07-30 11:36:16 +00006894OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6895 SourceLocation EndLoc,
6896 SourceLocation LParenLoc,
6897 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006898 // OpenMP [2.7.1, loop construct, Description]
6899 // OpenMP [2.8.1, simd construct, Description]
6900 // OpenMP [2.9.6, distribute construct, Description]
6901 // The parameter of the ordered clause must be a constant
6902 // positive integer expression if any.
6903 if (NumForLoops && LParenLoc.isValid()) {
6904 ExprResult NumForLoopsResult =
6905 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6906 if (NumForLoopsResult.isInvalid())
6907 return nullptr;
6908 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006909 } else
6910 NumForLoops = nullptr;
6911 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006912 return new (Context)
6913 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6914}
6915
Alexey Bataeved09d242014-05-28 05:53:51 +00006916OMPClause *Sema::ActOnOpenMPSimpleClause(
6917 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6918 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006919 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006920 switch (Kind) {
6921 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006922 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006923 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6924 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006925 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006926 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006927 Res = ActOnOpenMPProcBindClause(
6928 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6929 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006930 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006931 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006932 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006933 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006934 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006935 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006936 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006937 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006938 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006939 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006940 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006941 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006942 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006943 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006944 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006945 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006946 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006947 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006948 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006949 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006950 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006951 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006952 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006953 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006954 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006955 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006956 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006957 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006958 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006959 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006960 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006961 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006962 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006963 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006964 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006965 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006966 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006967 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006968 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006969 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006970 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006971 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006972 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006973 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006974 llvm_unreachable("Clause is not allowed.");
6975 }
6976 return Res;
6977}
6978
Alexey Bataev6402bca2015-12-28 07:25:51 +00006979static std::string
6980getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6981 ArrayRef<unsigned> Exclude = llvm::None) {
6982 std::string Values;
6983 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6984 unsigned Skipped = Exclude.size();
6985 auto S = Exclude.begin(), E = Exclude.end();
6986 for (unsigned i = First; i < Last; ++i) {
6987 if (std::find(S, E, i) != E) {
6988 --Skipped;
6989 continue;
6990 }
6991 Values += "'";
6992 Values += getOpenMPSimpleClauseTypeName(K, i);
6993 Values += "'";
6994 if (i == Bound - Skipped)
6995 Values += " or ";
6996 else if (i != Bound + 1 - Skipped)
6997 Values += ", ";
6998 }
6999 return Values;
7000}
7001
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007002OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7003 SourceLocation KindKwLoc,
7004 SourceLocation StartLoc,
7005 SourceLocation LParenLoc,
7006 SourceLocation EndLoc) {
7007 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007008 static_assert(OMPC_DEFAULT_unknown > 0,
7009 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007010 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007011 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7012 /*Last=*/OMPC_DEFAULT_unknown)
7013 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007014 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007015 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007016 switch (Kind) {
7017 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007018 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007019 break;
7020 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007021 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007022 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007023 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007024 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007025 break;
7026 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007027 return new (Context)
7028 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007029}
7030
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007031OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7032 SourceLocation KindKwLoc,
7033 SourceLocation StartLoc,
7034 SourceLocation LParenLoc,
7035 SourceLocation EndLoc) {
7036 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007037 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007038 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7039 /*Last=*/OMPC_PROC_BIND_unknown)
7040 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007041 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007042 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007043 return new (Context)
7044 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007045}
7046
Alexey Bataev56dafe82014-06-20 07:16:17 +00007047OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007048 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007049 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007050 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007051 SourceLocation EndLoc) {
7052 OMPClause *Res = nullptr;
7053 switch (Kind) {
7054 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007055 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7056 assert(Argument.size() == NumberOfElements &&
7057 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007058 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007059 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7060 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7061 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7062 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7063 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007064 break;
7065 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007066 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7067 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7068 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7069 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007070 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007071 case OMPC_dist_schedule:
7072 Res = ActOnOpenMPDistScheduleClause(
7073 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7074 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7075 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007076 case OMPC_defaultmap:
7077 enum { Modifier, DefaultmapKind };
7078 Res = ActOnOpenMPDefaultmapClause(
7079 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7080 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7081 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7082 ArgumentLoc[DefaultmapKind], EndLoc);
7083 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007084 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007085 case OMPC_num_threads:
7086 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007087 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007088 case OMPC_collapse:
7089 case OMPC_default:
7090 case OMPC_proc_bind:
7091 case OMPC_private:
7092 case OMPC_firstprivate:
7093 case OMPC_lastprivate:
7094 case OMPC_shared:
7095 case OMPC_reduction:
7096 case OMPC_linear:
7097 case OMPC_aligned:
7098 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007099 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007100 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007101 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007102 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007103 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007104 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007105 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007106 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007107 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007108 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007109 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007110 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007111 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007112 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007113 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007114 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007115 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007116 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007117 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007118 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007119 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007120 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007121 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007122 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007123 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007124 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007125 llvm_unreachable("Clause is not allowed.");
7126 }
7127 return Res;
7128}
7129
Alexey Bataev6402bca2015-12-28 07:25:51 +00007130static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7131 OpenMPScheduleClauseModifier M2,
7132 SourceLocation M1Loc, SourceLocation M2Loc) {
7133 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7134 SmallVector<unsigned, 2> Excluded;
7135 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7136 Excluded.push_back(M2);
7137 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7138 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7139 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7140 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7141 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7142 << getListOfPossibleValues(OMPC_schedule,
7143 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7144 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7145 Excluded)
7146 << getOpenMPClauseName(OMPC_schedule);
7147 return true;
7148 }
7149 return false;
7150}
7151
Alexey Bataev56dafe82014-06-20 07:16:17 +00007152OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007153 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007154 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007155 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7156 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7157 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7158 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7159 return nullptr;
7160 // OpenMP, 2.7.1, Loop Construct, Restrictions
7161 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7162 // but not both.
7163 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7164 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7165 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7166 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7167 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7168 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7169 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7170 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7171 return nullptr;
7172 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007173 if (Kind == OMPC_SCHEDULE_unknown) {
7174 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007175 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7176 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7177 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7178 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7179 Exclude);
7180 } else {
7181 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7182 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007183 }
7184 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7185 << Values << getOpenMPClauseName(OMPC_schedule);
7186 return nullptr;
7187 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007188 // OpenMP, 2.7.1, Loop Construct, Restrictions
7189 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7190 // schedule(guided).
7191 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7192 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7193 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7194 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7195 diag::err_omp_schedule_nonmonotonic_static);
7196 return nullptr;
7197 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007198 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007199 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007200 if (ChunkSize) {
7201 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7202 !ChunkSize->isInstantiationDependent() &&
7203 !ChunkSize->containsUnexpandedParameterPack()) {
7204 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7205 ExprResult Val =
7206 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7207 if (Val.isInvalid())
7208 return nullptr;
7209
7210 ValExpr = Val.get();
7211
7212 // OpenMP [2.7.1, Restrictions]
7213 // chunk_size must be a loop invariant integer expression with a positive
7214 // value.
7215 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007216 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7217 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7218 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007219 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007220 return nullptr;
7221 }
7222 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007223 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7224 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7225 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007226 }
7227 }
7228 }
7229
Alexey Bataev6402bca2015-12-28 07:25:51 +00007230 return new (Context)
7231 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007232 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007233}
7234
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007235OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7236 SourceLocation StartLoc,
7237 SourceLocation EndLoc) {
7238 OMPClause *Res = nullptr;
7239 switch (Kind) {
7240 case OMPC_ordered:
7241 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7242 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007243 case OMPC_nowait:
7244 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7245 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007246 case OMPC_untied:
7247 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7248 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007249 case OMPC_mergeable:
7250 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7251 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007252 case OMPC_read:
7253 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7254 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007255 case OMPC_write:
7256 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7257 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007258 case OMPC_update:
7259 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7260 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007261 case OMPC_capture:
7262 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7263 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007264 case OMPC_seq_cst:
7265 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7266 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007267 case OMPC_threads:
7268 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7269 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007270 case OMPC_simd:
7271 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7272 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007273 case OMPC_nogroup:
7274 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7275 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007276 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007277 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007278 case OMPC_num_threads:
7279 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007280 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007281 case OMPC_collapse:
7282 case OMPC_schedule:
7283 case OMPC_private:
7284 case OMPC_firstprivate:
7285 case OMPC_lastprivate:
7286 case OMPC_shared:
7287 case OMPC_reduction:
7288 case OMPC_linear:
7289 case OMPC_aligned:
7290 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007291 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007292 case OMPC_default:
7293 case OMPC_proc_bind:
7294 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007295 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007296 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007297 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007298 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007299 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007300 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007301 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007302 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007303 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007304 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007305 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007306 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007307 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007308 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007309 llvm_unreachable("Clause is not allowed.");
7310 }
7311 return Res;
7312}
7313
Alexey Bataev236070f2014-06-20 11:19:47 +00007314OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7315 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007316 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007317 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7318}
7319
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007320OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7321 SourceLocation EndLoc) {
7322 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7323}
7324
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007325OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7326 SourceLocation EndLoc) {
7327 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7328}
7329
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007330OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7331 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007332 return new (Context) OMPReadClause(StartLoc, EndLoc);
7333}
7334
Alexey Bataevdea47612014-07-23 07:46:59 +00007335OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7336 SourceLocation EndLoc) {
7337 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7338}
7339
Alexey Bataev67a4f222014-07-23 10:25:33 +00007340OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7341 SourceLocation EndLoc) {
7342 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7343}
7344
Alexey Bataev459dec02014-07-24 06:46:57 +00007345OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7346 SourceLocation EndLoc) {
7347 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7348}
7349
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007350OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7351 SourceLocation EndLoc) {
7352 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7353}
7354
Alexey Bataev346265e2015-09-25 10:37:12 +00007355OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7356 SourceLocation EndLoc) {
7357 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7358}
7359
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007360OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7361 SourceLocation EndLoc) {
7362 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7363}
7364
Alexey Bataevb825de12015-12-07 10:51:44 +00007365OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7366 SourceLocation EndLoc) {
7367 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7368}
7369
Alexey Bataevc5e02582014-06-16 07:08:35 +00007370OMPClause *Sema::ActOnOpenMPVarListClause(
7371 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7372 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7373 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007374 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007375 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7376 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7377 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007378 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007379 switch (Kind) {
7380 case OMPC_private:
7381 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7382 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007383 case OMPC_firstprivate:
7384 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7385 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007386 case OMPC_lastprivate:
7387 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7388 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007389 case OMPC_shared:
7390 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7391 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007392 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007393 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7394 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007395 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007396 case OMPC_linear:
7397 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007398 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007399 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007400 case OMPC_aligned:
7401 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7402 ColonLoc, EndLoc);
7403 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007404 case OMPC_copyin:
7405 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7406 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007407 case OMPC_copyprivate:
7408 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7409 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007410 case OMPC_flush:
7411 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7412 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007413 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007414 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7415 StartLoc, LParenLoc, EndLoc);
7416 break;
7417 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007418 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7419 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7420 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007421 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007422 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007423 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007424 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007425 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007426 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007427 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007428 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007429 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007430 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007431 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007432 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007433 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007434 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007435 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007436 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007437 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007438 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007439 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007440 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007441 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007442 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007443 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007444 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007445 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007446 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007447 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007448 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007449 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007450 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007451 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007452 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007453 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007454 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007455 llvm_unreachable("Clause is not allowed.");
7456 }
7457 return Res;
7458}
7459
Alexey Bataev90c228f2016-02-08 09:29:13 +00007460ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007461 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007462 ExprResult Res = BuildDeclRefExpr(
7463 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7464 if (!Res.isUsable())
7465 return ExprError();
7466 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7467 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7468 if (!Res.isUsable())
7469 return ExprError();
7470 }
7471 if (VK != VK_LValue && Res.get()->isGLValue()) {
7472 Res = DefaultLvalueConversion(Res.get());
7473 if (!Res.isUsable())
7474 return ExprError();
7475 }
7476 return Res;
7477}
7478
Alexey Bataev60da77e2016-02-29 05:54:20 +00007479static std::pair<ValueDecl *, bool>
7480getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7481 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007482 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7483 RefExpr->containsUnexpandedParameterPack())
7484 return std::make_pair(nullptr, true);
7485
Alexey Bataevd985eda2016-02-10 11:29:16 +00007486 // OpenMP [3.1, C/C++]
7487 // A list item is a variable name.
7488 // OpenMP [2.9.3.3, Restrictions, p.1]
7489 // A variable that is part of another variable (as an array or
7490 // structure element) cannot appear in a private clause.
7491 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007492 enum {
7493 NoArrayExpr = -1,
7494 ArraySubscript = 0,
7495 OMPArraySection = 1
7496 } IsArrayExpr = NoArrayExpr;
7497 if (AllowArraySection) {
7498 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7499 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7500 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7501 Base = TempASE->getBase()->IgnoreParenImpCasts();
7502 RefExpr = Base;
7503 IsArrayExpr = ArraySubscript;
7504 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7505 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7506 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7507 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7508 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7509 Base = TempASE->getBase()->IgnoreParenImpCasts();
7510 RefExpr = Base;
7511 IsArrayExpr = OMPArraySection;
7512 }
7513 }
7514 ELoc = RefExpr->getExprLoc();
7515 ERange = RefExpr->getSourceRange();
7516 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007517 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7518 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7519 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7520 (S.getCurrentThisType().isNull() || !ME ||
7521 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7522 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007523 if (IsArrayExpr != NoArrayExpr)
7524 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7525 << ERange;
7526 else {
7527 S.Diag(ELoc,
7528 AllowArraySection
7529 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7530 : diag::err_omp_expected_var_name_member_expr)
7531 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7532 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007533 return std::make_pair(nullptr, false);
7534 }
7535 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7536}
7537
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007538OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7539 SourceLocation StartLoc,
7540 SourceLocation LParenLoc,
7541 SourceLocation EndLoc) {
7542 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007543 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007544 for (auto &RefExpr : VarList) {
7545 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007546 SourceLocation ELoc;
7547 SourceRange ERange;
7548 Expr *SimpleRefExpr = RefExpr;
7549 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007550 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007551 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007552 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007553 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007554 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007555 ValueDecl *D = Res.first;
7556 if (!D)
7557 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007558
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007559 QualType Type = D->getType();
7560 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007561
7562 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7563 // A variable that appears in a private clause must not have an incomplete
7564 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007565 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007566 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007567 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007568
Alexey Bataev758e55e2013-09-06 18:03:48 +00007569 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7570 // in a Construct]
7571 // Variables with the predetermined data-sharing attributes may not be
7572 // listed in data-sharing attributes clauses, except for the cases
7573 // listed below. For these exceptions only, listing a predetermined
7574 // variable in a data-sharing attribute clause is allowed and overrides
7575 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007576 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007577 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007578 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7579 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007580 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007581 continue;
7582 }
7583
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007584 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007585 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007586 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007587 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7588 << getOpenMPClauseName(OMPC_private) << Type
7589 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7590 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007591 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007592 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007593 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007594 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007595 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007596 continue;
7597 }
7598
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007599 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7600 // A list item cannot appear in both a map clause and a data-sharing
7601 // attribute clause on the same construct
7602 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007603 if (DSAStack->checkMappableExprComponentListsForDecl(
7604 VD, /* CurrentRegionOnly = */ true,
7605 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7606 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007607 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7608 << getOpenMPClauseName(OMPC_private)
7609 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7610 ReportOriginalDSA(*this, DSAStack, D, DVar);
7611 continue;
7612 }
7613 }
7614
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007615 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7616 // A variable of class type (or array thereof) that appears in a private
7617 // clause requires an accessible, unambiguous default constructor for the
7618 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007619 // Generate helper private variable and initialize it with the default
7620 // value. The address of the original variable is replaced by the address of
7621 // the new private variable in CodeGen. This new variable is not added to
7622 // IdResolver, so the code in the OpenMP region uses original variable for
7623 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007624 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007625 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7626 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007627 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007628 if (VDPrivate->isInvalidDecl())
7629 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007630 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007631 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007632
Alexey Bataev90c228f2016-02-08 09:29:13 +00007633 DeclRefExpr *Ref = nullptr;
7634 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007635 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007636 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7637 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007638 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007639 }
7640
Alexey Bataeved09d242014-05-28 05:53:51 +00007641 if (Vars.empty())
7642 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007643
Alexey Bataev03b340a2014-10-21 03:16:40 +00007644 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7645 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007646}
7647
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007648namespace {
7649class DiagsUninitializedSeveretyRAII {
7650private:
7651 DiagnosticsEngine &Diags;
7652 SourceLocation SavedLoc;
7653 bool IsIgnored;
7654
7655public:
7656 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7657 bool IsIgnored)
7658 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7659 if (!IsIgnored) {
7660 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7661 /*Map*/ diag::Severity::Ignored, Loc);
7662 }
7663 }
7664 ~DiagsUninitializedSeveretyRAII() {
7665 if (!IsIgnored)
7666 Diags.popMappings(SavedLoc);
7667 }
7668};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007669}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007670
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007671OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7672 SourceLocation StartLoc,
7673 SourceLocation LParenLoc,
7674 SourceLocation EndLoc) {
7675 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007676 SmallVector<Expr *, 8> PrivateCopies;
7677 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007678 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007679 bool IsImplicitClause =
7680 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7681 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7682
Alexey Bataeved09d242014-05-28 05:53:51 +00007683 for (auto &RefExpr : VarList) {
7684 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007685 SourceLocation ELoc;
7686 SourceRange ERange;
7687 Expr *SimpleRefExpr = RefExpr;
7688 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007689 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007690 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007691 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007692 PrivateCopies.push_back(nullptr);
7693 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007694 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007695 ValueDecl *D = Res.first;
7696 if (!D)
7697 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007698
Alexey Bataev60da77e2016-02-29 05:54:20 +00007699 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007700 QualType Type = D->getType();
7701 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007702
7703 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7704 // A variable that appears in a private clause must not have an incomplete
7705 // type or a reference type.
7706 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007707 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007708 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007709 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007710
7711 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7712 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007713 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007714 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007715 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007716
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007717 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007718 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007719 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007720 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007721 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007722 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007723 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7724 // A list item that specifies a given variable may not appear in more
7725 // than one clause on the same directive, except that a variable may be
7726 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007727 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007728 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007729 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007730 << getOpenMPClauseName(DVar.CKind)
7731 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007732 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007733 continue;
7734 }
7735
7736 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7737 // in a Construct]
7738 // Variables with the predetermined data-sharing attributes may not be
7739 // listed in data-sharing attributes clauses, except for the cases
7740 // listed below. For these exceptions only, listing a predetermined
7741 // variable in a data-sharing attribute clause is allowed and overrides
7742 // the variable's predetermined data-sharing attributes.
7743 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7744 // in a Construct, C/C++, p.2]
7745 // Variables with const-qualified type having no mutable member may be
7746 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007747 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007748 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7749 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007750 << getOpenMPClauseName(DVar.CKind)
7751 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007752 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007753 continue;
7754 }
7755
Alexey Bataevf29276e2014-06-18 04:14:57 +00007756 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007757 // OpenMP [2.9.3.4, Restrictions, p.2]
7758 // A list item that is private within a parallel region must not appear
7759 // in a firstprivate clause on a worksharing construct if any of the
7760 // worksharing regions arising from the worksharing construct ever bind
7761 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007762 if (isOpenMPWorksharingDirective(CurrDir) &&
7763 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007764 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007765 if (DVar.CKind != OMPC_shared &&
7766 (isOpenMPParallelDirective(DVar.DKind) ||
7767 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007768 Diag(ELoc, diag::err_omp_required_access)
7769 << getOpenMPClauseName(OMPC_firstprivate)
7770 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007771 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007772 continue;
7773 }
7774 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007775 // OpenMP [2.9.3.4, Restrictions, p.3]
7776 // A list item that appears in a reduction clause of a parallel construct
7777 // must not appear in a firstprivate clause on a worksharing or task
7778 // construct if any of the worksharing or task regions arising from the
7779 // worksharing or task construct ever bind to any of the parallel regions
7780 // arising from the parallel construct.
7781 // OpenMP [2.9.3.4, Restrictions, p.4]
7782 // A list item that appears in a reduction clause in worksharing
7783 // construct must not appear in a firstprivate clause in a task construct
7784 // encountered during execution of any of the worksharing regions arising
7785 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007786 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007787 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007788 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007789 [](OpenMPDirectiveKind K) -> bool {
7790 return isOpenMPParallelDirective(K) ||
7791 isOpenMPWorksharingDirective(K);
7792 },
7793 false);
7794 if (DVar.CKind == OMPC_reduction &&
7795 (isOpenMPParallelDirective(DVar.DKind) ||
7796 isOpenMPWorksharingDirective(DVar.DKind))) {
7797 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7798 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007799 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007800 continue;
7801 }
7802 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007803
7804 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7805 // A list item that is private within a teams region must not appear in a
7806 // firstprivate clause on a distribute construct if any of the distribute
7807 // regions arising from the distribute construct ever bind to any of the
7808 // teams regions arising from the teams construct.
7809 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7810 // A list item that appears in a reduction clause of a teams construct
7811 // must not appear in a firstprivate clause on a distribute construct if
7812 // any of the distribute regions arising from the distribute construct
7813 // ever bind to any of the teams regions arising from the teams construct.
7814 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7815 // A list item may appear in a firstprivate or lastprivate clause but not
7816 // both.
7817 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007818 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007819 [](OpenMPDirectiveKind K) -> bool {
7820 return isOpenMPTeamsDirective(K);
7821 },
7822 false);
7823 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7824 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007825 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007826 continue;
7827 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007828 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007829 [](OpenMPDirectiveKind K) -> bool {
7830 return isOpenMPTeamsDirective(K);
7831 },
7832 false);
7833 if (DVar.CKind == OMPC_reduction &&
7834 isOpenMPTeamsDirective(DVar.DKind)) {
7835 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007836 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007837 continue;
7838 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007839 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007840 if (DVar.CKind == OMPC_lastprivate) {
7841 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007842 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007843 continue;
7844 }
7845 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007846 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7847 // A list item cannot appear in both a map clause and a data-sharing
7848 // attribute clause on the same construct
7849 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007850 if (DSAStack->checkMappableExprComponentListsForDecl(
7851 VD, /* CurrentRegionOnly = */ true,
7852 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7853 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007854 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7855 << getOpenMPClauseName(OMPC_firstprivate)
7856 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7857 ReportOriginalDSA(*this, DSAStack, D, DVar);
7858 continue;
7859 }
7860 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007861 }
7862
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007863 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007864 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007865 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007866 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7867 << getOpenMPClauseName(OMPC_firstprivate) << Type
7868 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7869 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007870 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007872 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007874 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007875 continue;
7876 }
7877
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007878 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007879 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7880 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007881 // Generate helper private variable and initialize it with the value of the
7882 // original variable. The address of the original variable is replaced by
7883 // the address of the new private variable in the CodeGen. This new variable
7884 // is not added to IdResolver, so the code in the OpenMP region uses
7885 // original variable for proper diagnostics and variable capturing.
7886 Expr *VDInitRefExpr = nullptr;
7887 // For arrays generate initializer for single element and replace it by the
7888 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007889 if (Type->isArrayType()) {
7890 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007891 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007892 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007893 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007894 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007895 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007896 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007897 InitializedEntity Entity =
7898 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007899 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7900
7901 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7902 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7903 if (Result.isInvalid())
7904 VDPrivate->setInvalidDecl();
7905 else
7906 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007907 // Remove temp variable declaration.
7908 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007909 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007910 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7911 ".firstprivate.temp");
7912 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7913 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007914 AddInitializerToDecl(VDPrivate,
7915 DefaultLvalueConversion(VDInitRefExpr).get(),
7916 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007917 }
7918 if (VDPrivate->isInvalidDecl()) {
7919 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007920 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007921 diag::note_omp_task_predetermined_firstprivate_here);
7922 }
7923 continue;
7924 }
7925 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007926 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007927 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7928 RefExpr->getExprLoc());
7929 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007930 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007931 if (TopDVar.CKind == OMPC_lastprivate)
7932 Ref = TopDVar.PrivateCopy;
7933 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007934 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007935 if (!IsOpenMPCapturedDecl(D))
7936 ExprCaptures.push_back(Ref->getDecl());
7937 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007938 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007939 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7940 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007941 PrivateCopies.push_back(VDPrivateRefExpr);
7942 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007943 }
7944
Alexey Bataeved09d242014-05-28 05:53:51 +00007945 if (Vars.empty())
7946 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007947
7948 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007949 Vars, PrivateCopies, Inits,
7950 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007951}
7952
Alexander Musman1bb328c2014-06-04 13:06:39 +00007953OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7954 SourceLocation StartLoc,
7955 SourceLocation LParenLoc,
7956 SourceLocation EndLoc) {
7957 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007958 SmallVector<Expr *, 8> SrcExprs;
7959 SmallVector<Expr *, 8> DstExprs;
7960 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007961 SmallVector<Decl *, 4> ExprCaptures;
7962 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007963 for (auto &RefExpr : VarList) {
7964 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007965 SourceLocation ELoc;
7966 SourceRange ERange;
7967 Expr *SimpleRefExpr = RefExpr;
7968 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007969 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007970 // It will be analyzed later.
7971 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007972 SrcExprs.push_back(nullptr);
7973 DstExprs.push_back(nullptr);
7974 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007975 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007976 ValueDecl *D = Res.first;
7977 if (!D)
7978 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007979
Alexey Bataev74caaf22016-02-20 04:09:36 +00007980 QualType Type = D->getType();
7981 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007982
7983 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7984 // A variable that appears in a lastprivate clause must not have an
7985 // incomplete type or a reference type.
7986 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007987 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007988 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007989 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007990
7991 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7992 // in a Construct]
7993 // Variables with the predetermined data-sharing attributes may not be
7994 // listed in data-sharing attributes clauses, except for the cases
7995 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007996 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007997 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7998 DVar.CKind != OMPC_firstprivate &&
7999 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8000 Diag(ELoc, diag::err_omp_wrong_dsa)
8001 << getOpenMPClauseName(DVar.CKind)
8002 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008003 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008004 continue;
8005 }
8006
Alexey Bataevf29276e2014-06-18 04:14:57 +00008007 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8008 // OpenMP [2.14.3.5, Restrictions, p.2]
8009 // A list item that is private within a parallel region, or that appears in
8010 // the reduction clause of a parallel construct, must not appear in a
8011 // lastprivate clause on a worksharing construct if any of the corresponding
8012 // worksharing regions ever binds to any of the corresponding parallel
8013 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008014 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008015 if (isOpenMPWorksharingDirective(CurrDir) &&
8016 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008017 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008018 if (DVar.CKind != OMPC_shared) {
8019 Diag(ELoc, diag::err_omp_required_access)
8020 << getOpenMPClauseName(OMPC_lastprivate)
8021 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008022 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008023 continue;
8024 }
8025 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008026
8027 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8028 // A list item may appear in a firstprivate or lastprivate clause but not
8029 // both.
8030 if (CurrDir == OMPD_distribute) {
8031 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8032 if (DVar.CKind == OMPC_firstprivate) {
8033 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8034 ReportOriginalDSA(*this, DSAStack, D, DVar);
8035 continue;
8036 }
8037 }
8038
Alexander Musman1bb328c2014-06-04 13:06:39 +00008039 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008040 // A variable of class type (or array thereof) that appears in a
8041 // lastprivate clause requires an accessible, unambiguous default
8042 // constructor for the class type, unless the list item is also specified
8043 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008044 // A variable of class type (or array thereof) that appears in a
8045 // lastprivate clause requires an accessible, unambiguous copy assignment
8046 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008047 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008048 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008049 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008050 D->hasAttrs() ? &D->getAttrs() : nullptr);
8051 auto *PseudoSrcExpr =
8052 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008053 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008054 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008055 D->hasAttrs() ? &D->getAttrs() : nullptr);
8056 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008057 // For arrays generate assignment operation for single element and replace
8058 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008059 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008060 PseudoDstExpr, PseudoSrcExpr);
8061 if (AssignmentOp.isInvalid())
8062 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008063 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008064 /*DiscardedValue=*/true);
8065 if (AssignmentOp.isInvalid())
8066 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008067
Alexey Bataev74caaf22016-02-20 04:09:36 +00008068 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008069 if (!VD) {
8070 if (TopDVar.CKind == OMPC_firstprivate)
8071 Ref = TopDVar.PrivateCopy;
8072 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008073 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008074 if (!IsOpenMPCapturedDecl(D))
8075 ExprCaptures.push_back(Ref->getDecl());
8076 }
8077 if (TopDVar.CKind == OMPC_firstprivate ||
8078 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008079 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008080 ExprResult RefRes = DefaultLvalueConversion(Ref);
8081 if (!RefRes.isUsable())
8082 continue;
8083 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008084 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8085 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008086 if (!PostUpdateRes.isUsable())
8087 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008088 ExprPostUpdates.push_back(
8089 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008090 }
8091 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00008092 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00008093 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8094 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008095 SrcExprs.push_back(PseudoSrcExpr);
8096 DstExprs.push_back(PseudoDstExpr);
8097 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008098 }
8099
8100 if (Vars.empty())
8101 return nullptr;
8102
8103 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008104 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008105 buildPreInits(Context, ExprCaptures),
8106 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008107}
8108
Alexey Bataev758e55e2013-09-06 18:03:48 +00008109OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8110 SourceLocation StartLoc,
8111 SourceLocation LParenLoc,
8112 SourceLocation EndLoc) {
8113 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008114 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008115 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008116 SourceLocation ELoc;
8117 SourceRange ERange;
8118 Expr *SimpleRefExpr = RefExpr;
8119 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008120 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008121 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008122 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008123 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008124 ValueDecl *D = Res.first;
8125 if (!D)
8126 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008127
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008128 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008129 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8130 // in a Construct]
8131 // Variables with the predetermined data-sharing attributes may not be
8132 // listed in data-sharing attributes clauses, except for the cases
8133 // listed below. For these exceptions only, listing a predetermined
8134 // variable in a data-sharing attribute clause is allowed and overrides
8135 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008136 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008137 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8138 DVar.RefExpr) {
8139 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8140 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008141 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008142 continue;
8143 }
8144
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008145 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008146 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008147 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008148 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008149 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008150 }
8151
Alexey Bataeved09d242014-05-28 05:53:51 +00008152 if (Vars.empty())
8153 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008154
8155 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8156}
8157
Alexey Bataevc5e02582014-06-16 07:08:35 +00008158namespace {
8159class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8160 DSAStackTy *Stack;
8161
8162public:
8163 bool VisitDeclRefExpr(DeclRefExpr *E) {
8164 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008165 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008166 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8167 return false;
8168 if (DVar.CKind != OMPC_unknown)
8169 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008170 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008171 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008172 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008173 return true;
8174 return false;
8175 }
8176 return false;
8177 }
8178 bool VisitStmt(Stmt *S) {
8179 for (auto Child : S->children()) {
8180 if (Child && Visit(Child))
8181 return true;
8182 }
8183 return false;
8184 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008185 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008186};
Alexey Bataev23b69422014-06-18 07:08:49 +00008187} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008188
Alexey Bataev60da77e2016-02-29 05:54:20 +00008189namespace {
8190// Transform MemberExpression for specified FieldDecl of current class to
8191// DeclRefExpr to specified OMPCapturedExprDecl.
8192class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8193 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8194 ValueDecl *Field;
8195 DeclRefExpr *CapturedExpr;
8196
8197public:
8198 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8199 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8200
8201 ExprResult TransformMemberExpr(MemberExpr *E) {
8202 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8203 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008204 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008205 return CapturedExpr;
8206 }
8207 return BaseTransform::TransformMemberExpr(E);
8208 }
8209 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8210};
8211} // namespace
8212
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008213template <typename T>
8214static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8215 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8216 for (auto &Set : Lookups) {
8217 for (auto *D : Set) {
8218 if (auto Res = Gen(cast<ValueDecl>(D)))
8219 return Res;
8220 }
8221 }
8222 return T();
8223}
8224
8225static ExprResult
8226buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8227 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8228 const DeclarationNameInfo &ReductionId, QualType Ty,
8229 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8230 if (ReductionIdScopeSpec.isInvalid())
8231 return ExprError();
8232 SmallVector<UnresolvedSet<8>, 4> Lookups;
8233 if (S) {
8234 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8235 Lookup.suppressDiagnostics();
8236 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8237 auto *D = Lookup.getRepresentativeDecl();
8238 do {
8239 S = S->getParent();
8240 } while (S && !S->isDeclScope(D));
8241 if (S)
8242 S = S->getParent();
8243 Lookups.push_back(UnresolvedSet<8>());
8244 Lookups.back().append(Lookup.begin(), Lookup.end());
8245 Lookup.clear();
8246 }
8247 } else if (auto *ULE =
8248 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8249 Lookups.push_back(UnresolvedSet<8>());
8250 Decl *PrevD = nullptr;
8251 for(auto *D : ULE->decls()) {
8252 if (D == PrevD)
8253 Lookups.push_back(UnresolvedSet<8>());
8254 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8255 Lookups.back().addDecl(DRD);
8256 PrevD = D;
8257 }
8258 }
8259 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8260 Ty->containsUnexpandedParameterPack() ||
8261 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8262 return !D->isInvalidDecl() &&
8263 (D->getType()->isDependentType() ||
8264 D->getType()->isInstantiationDependentType() ||
8265 D->getType()->containsUnexpandedParameterPack());
8266 })) {
8267 UnresolvedSet<8> ResSet;
8268 for (auto &Set : Lookups) {
8269 ResSet.append(Set.begin(), Set.end());
8270 // The last item marks the end of all declarations at the specified scope.
8271 ResSet.addDecl(Set[Set.size() - 1]);
8272 }
8273 return UnresolvedLookupExpr::Create(
8274 SemaRef.Context, /*NamingClass=*/nullptr,
8275 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8276 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8277 }
8278 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8279 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8280 if (!D->isInvalidDecl() &&
8281 SemaRef.Context.hasSameType(D->getType(), Ty))
8282 return D;
8283 return nullptr;
8284 }))
8285 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8286 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8287 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8288 if (!D->isInvalidDecl() &&
8289 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8290 !Ty.isMoreQualifiedThan(D->getType()))
8291 return D;
8292 return nullptr;
8293 })) {
8294 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8295 /*DetectVirtual=*/false);
8296 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8297 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8298 VD->getType().getUnqualifiedType()))) {
8299 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8300 /*DiagID=*/0) !=
8301 Sema::AR_inaccessible) {
8302 SemaRef.BuildBasePathArray(Paths, BasePath);
8303 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8304 }
8305 }
8306 }
8307 }
8308 if (ReductionIdScopeSpec.isSet()) {
8309 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8310 return ExprError();
8311 }
8312 return ExprEmpty();
8313}
8314
Alexey Bataevc5e02582014-06-16 07:08:35 +00008315OMPClause *Sema::ActOnOpenMPReductionClause(
8316 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8317 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008318 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8319 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008320 auto DN = ReductionId.getName();
8321 auto OOK = DN.getCXXOverloadedOperator();
8322 BinaryOperatorKind BOK = BO_Comma;
8323
8324 // OpenMP [2.14.3.6, reduction clause]
8325 // C
8326 // reduction-identifier is either an identifier or one of the following
8327 // operators: +, -, *, &, |, ^, && and ||
8328 // C++
8329 // reduction-identifier is either an id-expression or one of the following
8330 // operators: +, -, *, &, |, ^, && and ||
8331 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8332 switch (OOK) {
8333 case OO_Plus:
8334 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008335 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008336 break;
8337 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008338 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008339 break;
8340 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008341 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008342 break;
8343 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008344 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008345 break;
8346 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008347 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008348 break;
8349 case OO_AmpAmp:
8350 BOK = BO_LAnd;
8351 break;
8352 case OO_PipePipe:
8353 BOK = BO_LOr;
8354 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008355 case OO_New:
8356 case OO_Delete:
8357 case OO_Array_New:
8358 case OO_Array_Delete:
8359 case OO_Slash:
8360 case OO_Percent:
8361 case OO_Tilde:
8362 case OO_Exclaim:
8363 case OO_Equal:
8364 case OO_Less:
8365 case OO_Greater:
8366 case OO_LessEqual:
8367 case OO_GreaterEqual:
8368 case OO_PlusEqual:
8369 case OO_MinusEqual:
8370 case OO_StarEqual:
8371 case OO_SlashEqual:
8372 case OO_PercentEqual:
8373 case OO_CaretEqual:
8374 case OO_AmpEqual:
8375 case OO_PipeEqual:
8376 case OO_LessLess:
8377 case OO_GreaterGreater:
8378 case OO_LessLessEqual:
8379 case OO_GreaterGreaterEqual:
8380 case OO_EqualEqual:
8381 case OO_ExclaimEqual:
8382 case OO_PlusPlus:
8383 case OO_MinusMinus:
8384 case OO_Comma:
8385 case OO_ArrowStar:
8386 case OO_Arrow:
8387 case OO_Call:
8388 case OO_Subscript:
8389 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008390 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008391 case NUM_OVERLOADED_OPERATORS:
8392 llvm_unreachable("Unexpected reduction identifier");
8393 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008394 if (auto II = DN.getAsIdentifierInfo()) {
8395 if (II->isStr("max"))
8396 BOK = BO_GT;
8397 else if (II->isStr("min"))
8398 BOK = BO_LT;
8399 }
8400 break;
8401 }
8402 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008403 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008404 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008405 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008406
8407 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008408 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008409 SmallVector<Expr *, 8> LHSs;
8410 SmallVector<Expr *, 8> RHSs;
8411 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008412 SmallVector<Decl *, 4> ExprCaptures;
8413 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008414 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8415 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008416 for (auto RefExpr : VarList) {
8417 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008418 // OpenMP [2.1, C/C++]
8419 // A list item is a variable or array section, subject to the restrictions
8420 // specified in Section 2.4 on page 42 and in each of the sections
8421 // describing clauses and directives for which a list appears.
8422 // OpenMP [2.14.3.3, Restrictions, p.1]
8423 // A variable that is part of another variable (as an array or
8424 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008425 if (!FirstIter && IR != ER)
8426 ++IR;
8427 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008428 SourceLocation ELoc;
8429 SourceRange ERange;
8430 Expr *SimpleRefExpr = RefExpr;
8431 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8432 /*AllowArraySection=*/true);
8433 if (Res.second) {
8434 // It will be analyzed later.
8435 Vars.push_back(RefExpr);
8436 Privates.push_back(nullptr);
8437 LHSs.push_back(nullptr);
8438 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008439 // Try to find 'declare reduction' corresponding construct before using
8440 // builtin/overloaded operators.
8441 QualType Type = Context.DependentTy;
8442 CXXCastPath BasePath;
8443 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8444 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8445 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8446 if (CurContext->isDependentContext() &&
8447 (DeclareReductionRef.isUnset() ||
8448 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8449 ReductionOps.push_back(DeclareReductionRef.get());
8450 else
8451 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008452 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008453 ValueDecl *D = Res.first;
8454 if (!D)
8455 continue;
8456
Alexey Bataeva1764212015-09-30 09:22:36 +00008457 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008458 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8459 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8460 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008461 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008462 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008463 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8464 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8465 Type = ATy->getElementType();
8466 else
8467 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008468 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008469 } else
8470 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8471 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008472
Alexey Bataevc5e02582014-06-16 07:08:35 +00008473 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8474 // A variable that appears in a private clause must not have an incomplete
8475 // type or a reference type.
8476 if (RequireCompleteType(ELoc, Type,
8477 diag::err_omp_reduction_incomplete_type))
8478 continue;
8479 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008480 // A list item that appears in a reduction clause must not be
8481 // const-qualified.
8482 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008483 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008484 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008485 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008486 bool IsDecl = !VD ||
8487 VD->isThisDeclarationADefinition(Context) ==
8488 VarDecl::DeclarationOnly;
8489 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008490 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008491 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008492 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008493 continue;
8494 }
8495 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8496 // If a list-item is a reference type then it must bind to the same object
8497 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008498 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008499 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008500 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008501 DSARefChecker Check(DSAStack);
8502 if (Check.Visit(VDDef->getInit())) {
8503 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8504 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8505 continue;
8506 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008507 }
8508 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008509
Alexey Bataevc5e02582014-06-16 07:08:35 +00008510 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8511 // in a Construct]
8512 // Variables with the predetermined data-sharing attributes may not be
8513 // listed in data-sharing attributes clauses, except for the cases
8514 // listed below. For these exceptions only, listing a predetermined
8515 // variable in a data-sharing attribute clause is allowed and overrides
8516 // the variable's predetermined data-sharing attributes.
8517 // OpenMP [2.14.3.6, Restrictions, p.3]
8518 // Any number of reduction clauses can be specified on the directive,
8519 // but a list item can appear only once in the reduction clauses for that
8520 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008521 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008522 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008523 if (DVar.CKind == OMPC_reduction) {
8524 Diag(ELoc, diag::err_omp_once_referenced)
8525 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008526 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008527 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008528 } else if (DVar.CKind != OMPC_unknown) {
8529 Diag(ELoc, diag::err_omp_wrong_dsa)
8530 << getOpenMPClauseName(DVar.CKind)
8531 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008532 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008533 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008534 }
8535
8536 // OpenMP [2.14.3.6, Restrictions, p.1]
8537 // A list item that appears in a reduction clause of a worksharing
8538 // construct must be shared in the parallel regions to which any of the
8539 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008540 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8541 if (isOpenMPWorksharingDirective(CurrDir) &&
8542 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008543 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008544 if (DVar.CKind != OMPC_shared) {
8545 Diag(ELoc, diag::err_omp_required_access)
8546 << getOpenMPClauseName(OMPC_reduction)
8547 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008548 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008549 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008550 }
8551 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008552
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008553 // Try to find 'declare reduction' corresponding construct before using
8554 // builtin/overloaded operators.
8555 CXXCastPath BasePath;
8556 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8557 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8558 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8559 if (DeclareReductionRef.isInvalid())
8560 continue;
8561 if (CurContext->isDependentContext() &&
8562 (DeclareReductionRef.isUnset() ||
8563 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8564 Vars.push_back(RefExpr);
8565 Privates.push_back(nullptr);
8566 LHSs.push_back(nullptr);
8567 RHSs.push_back(nullptr);
8568 ReductionOps.push_back(DeclareReductionRef.get());
8569 continue;
8570 }
8571 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8572 // Not allowed reduction identifier is found.
8573 Diag(ReductionId.getLocStart(),
8574 diag::err_omp_unknown_reduction_identifier)
8575 << Type << ReductionIdRange;
8576 continue;
8577 }
8578
8579 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8580 // The type of a list item that appears in a reduction clause must be valid
8581 // for the reduction-identifier. For a max or min reduction in C, the type
8582 // of the list item must be an allowed arithmetic data type: char, int,
8583 // float, double, or _Bool, possibly modified with long, short, signed, or
8584 // unsigned. For a max or min reduction in C++, the type of the list item
8585 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8586 // double, or bool, possibly modified with long, short, signed, or unsigned.
8587 if (DeclareReductionRef.isUnset()) {
8588 if ((BOK == BO_GT || BOK == BO_LT) &&
8589 !(Type->isScalarType() ||
8590 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8591 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8592 << getLangOpts().CPlusPlus;
8593 if (!ASE && !OASE) {
8594 bool IsDecl = !VD ||
8595 VD->isThisDeclarationADefinition(Context) ==
8596 VarDecl::DeclarationOnly;
8597 Diag(D->getLocation(),
8598 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8599 << D;
8600 }
8601 continue;
8602 }
8603 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8604 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8605 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8606 if (!ASE && !OASE) {
8607 bool IsDecl = !VD ||
8608 VD->isThisDeclarationADefinition(Context) ==
8609 VarDecl::DeclarationOnly;
8610 Diag(D->getLocation(),
8611 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8612 << D;
8613 }
8614 continue;
8615 }
8616 }
8617
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008618 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008619 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008620 D->hasAttrs() ? &D->getAttrs() : nullptr);
8621 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8622 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008623 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008624 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008625 (!ASE &&
8626 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008627 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008628 // Create pseudo array type for private copy. The size for this array will
8629 // be generated during codegen.
8630 // For array subscripts or single variables Private Ty is the same as Type
8631 // (type of the variable or single array element).
8632 PrivateTy = Context.getVariableArrayType(
8633 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8634 Context.getSizeType(), VK_RValue),
8635 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008636 } else if (!ASE && !OASE &&
8637 Context.getAsArrayType(D->getType().getNonReferenceType()))
8638 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008639 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008640 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8641 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008642 // Add initializer for private variable.
8643 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008644 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8645 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8646 if (DeclareReductionRef.isUsable()) {
8647 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8648 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8649 if (DRD->getInitializer()) {
8650 Init = DRDRef;
8651 RHSVD->setInit(DRDRef);
8652 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008653 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008654 } else {
8655 switch (BOK) {
8656 case BO_Add:
8657 case BO_Xor:
8658 case BO_Or:
8659 case BO_LOr:
8660 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8661 if (Type->isScalarType() || Type->isAnyComplexType())
8662 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8663 break;
8664 case BO_Mul:
8665 case BO_LAnd:
8666 if (Type->isScalarType() || Type->isAnyComplexType()) {
8667 // '*' and '&&' reduction ops - initializer is '1'.
8668 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008669 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008670 break;
8671 case BO_And: {
8672 // '&' reduction op - initializer is '~0'.
8673 QualType OrigType = Type;
8674 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8675 Type = ComplexTy->getElementType();
8676 if (Type->isRealFloatingType()) {
8677 llvm::APFloat InitValue =
8678 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8679 /*isIEEE=*/true);
8680 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8681 Type, ELoc);
8682 } else if (Type->isScalarType()) {
8683 auto Size = Context.getTypeSize(Type);
8684 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8685 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8686 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8687 }
8688 if (Init && OrigType->isAnyComplexType()) {
8689 // Init = 0xFFFF + 0xFFFFi;
8690 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8691 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8692 }
8693 Type = OrigType;
8694 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008695 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008696 case BO_LT:
8697 case BO_GT: {
8698 // 'min' reduction op - initializer is 'Largest representable number in
8699 // the reduction list item type'.
8700 // 'max' reduction op - initializer is 'Least representable number in
8701 // the reduction list item type'.
8702 if (Type->isIntegerType() || Type->isPointerType()) {
8703 bool IsSigned = Type->hasSignedIntegerRepresentation();
8704 auto Size = Context.getTypeSize(Type);
8705 QualType IntTy =
8706 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8707 llvm::APInt InitValue =
8708 (BOK != BO_LT)
8709 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8710 : llvm::APInt::getMinValue(Size)
8711 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8712 : llvm::APInt::getMaxValue(Size);
8713 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8714 if (Type->isPointerType()) {
8715 // Cast to pointer type.
8716 auto CastExpr = BuildCStyleCastExpr(
8717 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8718 SourceLocation(), Init);
8719 if (CastExpr.isInvalid())
8720 continue;
8721 Init = CastExpr.get();
8722 }
8723 } else if (Type->isRealFloatingType()) {
8724 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8725 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8726 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8727 Type, ELoc);
8728 }
8729 break;
8730 }
8731 case BO_PtrMemD:
8732 case BO_PtrMemI:
8733 case BO_MulAssign:
8734 case BO_Div:
8735 case BO_Rem:
8736 case BO_Sub:
8737 case BO_Shl:
8738 case BO_Shr:
8739 case BO_LE:
8740 case BO_GE:
8741 case BO_EQ:
8742 case BO_NE:
8743 case BO_AndAssign:
8744 case BO_XorAssign:
8745 case BO_OrAssign:
8746 case BO_Assign:
8747 case BO_AddAssign:
8748 case BO_SubAssign:
8749 case BO_DivAssign:
8750 case BO_RemAssign:
8751 case BO_ShlAssign:
8752 case BO_ShrAssign:
8753 case BO_Comma:
8754 llvm_unreachable("Unexpected reduction operation");
8755 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008756 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008757 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008758 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8759 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008760 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008761 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008762 if (RHSVD->isInvalidDecl())
8763 continue;
8764 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008765 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8766 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008767 bool IsDecl =
8768 !VD ||
8769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8770 Diag(D->getLocation(),
8771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8772 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008773 continue;
8774 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008775 // Store initializer for single element in private copy. Will be used during
8776 // codegen.
8777 PrivateVD->setInit(RHSVD->getInit());
8778 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008779 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008780 ExprResult ReductionOp;
8781 if (DeclareReductionRef.isUsable()) {
8782 QualType RedTy = DeclareReductionRef.get()->getType();
8783 QualType PtrRedTy = Context.getPointerType(RedTy);
8784 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8785 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8786 if (!BasePath.empty()) {
8787 LHS = DefaultLvalueConversion(LHS.get());
8788 RHS = DefaultLvalueConversion(RHS.get());
8789 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8790 CK_UncheckedDerivedToBase, LHS.get(),
8791 &BasePath, LHS.get()->getValueKind());
8792 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8793 CK_UncheckedDerivedToBase, RHS.get(),
8794 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008795 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008796 FunctionProtoType::ExtProtoInfo EPI;
8797 QualType Params[] = {PtrRedTy, PtrRedTy};
8798 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8799 auto *OVE = new (Context) OpaqueValueExpr(
8800 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8801 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8802 Expr *Args[] = {LHS.get(), RHS.get()};
8803 ReductionOp = new (Context)
8804 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8805 } else {
8806 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8807 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8808 if (ReductionOp.isUsable()) {
8809 if (BOK != BO_LT && BOK != BO_GT) {
8810 ReductionOp =
8811 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8812 BO_Assign, LHSDRE, ReductionOp.get());
8813 } else {
8814 auto *ConditionalOp = new (Context) ConditionalOperator(
8815 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8816 RHSDRE, Type, VK_LValue, OK_Ordinary);
8817 ReductionOp =
8818 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8819 BO_Assign, LHSDRE, ConditionalOp);
8820 }
8821 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8822 }
8823 if (ReductionOp.isInvalid())
8824 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008825 }
8826
Alexey Bataev60da77e2016-02-29 05:54:20 +00008827 DeclRefExpr *Ref = nullptr;
8828 Expr *VarsExpr = RefExpr->IgnoreParens();
8829 if (!VD) {
8830 if (ASE || OASE) {
8831 TransformExprToCaptures RebuildToCapture(*this, D);
8832 VarsExpr =
8833 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8834 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008835 } else {
8836 VarsExpr = Ref =
8837 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008838 }
8839 if (!IsOpenMPCapturedDecl(D)) {
8840 ExprCaptures.push_back(Ref->getDecl());
8841 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8842 ExprResult RefRes = DefaultLvalueConversion(Ref);
8843 if (!RefRes.isUsable())
8844 continue;
8845 ExprResult PostUpdateRes =
8846 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8847 SimpleRefExpr, RefRes.get());
8848 if (!PostUpdateRes.isUsable())
8849 continue;
8850 ExprPostUpdates.push_back(
8851 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008852 }
8853 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008854 }
8855 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8856 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008857 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008858 LHSs.push_back(LHSDRE);
8859 RHSs.push_back(RHSDRE);
8860 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008861 }
8862
8863 if (Vars.empty())
8864 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008865
Alexey Bataevc5e02582014-06-16 07:08:35 +00008866 return OMPReductionClause::Create(
8867 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008868 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008869 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8870 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008871}
8872
Alexey Bataevecba70f2016-04-12 11:02:11 +00008873bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8874 SourceLocation LinLoc) {
8875 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8876 LinKind == OMPC_LINEAR_unknown) {
8877 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8878 return true;
8879 }
8880 return false;
8881}
8882
8883bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8884 OpenMPLinearClauseKind LinKind,
8885 QualType Type) {
8886 auto *VD = dyn_cast_or_null<VarDecl>(D);
8887 // A variable must not have an incomplete type or a reference type.
8888 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8889 return true;
8890 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8891 !Type->isReferenceType()) {
8892 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8893 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8894 return true;
8895 }
8896 Type = Type.getNonReferenceType();
8897
8898 // A list item must not be const-qualified.
8899 if (Type.isConstant(Context)) {
8900 Diag(ELoc, diag::err_omp_const_variable)
8901 << getOpenMPClauseName(OMPC_linear);
8902 if (D) {
8903 bool IsDecl =
8904 !VD ||
8905 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8906 Diag(D->getLocation(),
8907 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8908 << D;
8909 }
8910 return true;
8911 }
8912
8913 // A list item must be of integral or pointer type.
8914 Type = Type.getUnqualifiedType().getCanonicalType();
8915 const auto *Ty = Type.getTypePtrOrNull();
8916 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8917 !Ty->isPointerType())) {
8918 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8919 if (D) {
8920 bool IsDecl =
8921 !VD ||
8922 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8923 Diag(D->getLocation(),
8924 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8925 << D;
8926 }
8927 return true;
8928 }
8929 return false;
8930}
8931
Alexey Bataev182227b2015-08-20 10:54:39 +00008932OMPClause *Sema::ActOnOpenMPLinearClause(
8933 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8934 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8935 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008936 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008937 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008938 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008939 SmallVector<Decl *, 4> ExprCaptures;
8940 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008941 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008942 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008943 for (auto &RefExpr : VarList) {
8944 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008945 SourceLocation ELoc;
8946 SourceRange ERange;
8947 Expr *SimpleRefExpr = RefExpr;
8948 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8949 /*AllowArraySection=*/false);
8950 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008951 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008952 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008953 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008954 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008955 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008956 ValueDecl *D = Res.first;
8957 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008958 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008959
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008960 QualType Type = D->getType();
8961 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008962
8963 // OpenMP [2.14.3.7, linear clause]
8964 // A list-item cannot appear in more than one linear clause.
8965 // A list-item that appears in a linear clause cannot appear in any
8966 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008967 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008968 if (DVar.RefExpr) {
8969 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8970 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008971 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008972 continue;
8973 }
8974
Alexey Bataevecba70f2016-04-12 11:02:11 +00008975 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008976 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008977 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008978
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008979 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008980 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8981 D->hasAttrs() ? &D->getAttrs() : nullptr);
8982 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008983 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008984 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008985 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008986 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008987 if (!VD) {
8988 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8989 if (!IsOpenMPCapturedDecl(D)) {
8990 ExprCaptures.push_back(Ref->getDecl());
8991 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8992 ExprResult RefRes = DefaultLvalueConversion(Ref);
8993 if (!RefRes.isUsable())
8994 continue;
8995 ExprResult PostUpdateRes =
8996 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8997 SimpleRefExpr, RefRes.get());
8998 if (!PostUpdateRes.isUsable())
8999 continue;
9000 ExprPostUpdates.push_back(
9001 IgnoredValueConversions(PostUpdateRes.get()).get());
9002 }
9003 }
9004 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009005 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009006 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009007 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009008 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009009 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009010 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9011 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9012
9013 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9014 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009015 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009016 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009017 }
9018
9019 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009020 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009021
9022 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009023 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009024 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9025 !Step->isInstantiationDependent() &&
9026 !Step->containsUnexpandedParameterPack()) {
9027 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009028 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009029 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009030 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009031 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009032
Alexander Musman3276a272015-03-21 10:12:56 +00009033 // Build var to save the step value.
9034 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009035 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009036 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009037 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009038 ExprResult CalcStep =
9039 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009040 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009041
Alexander Musman8dba6642014-04-22 13:09:42 +00009042 // Warn about zero linear step (it would be probably better specified as
9043 // making corresponding variables 'const').
9044 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009045 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9046 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009047 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9048 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009049 if (!IsConstant && CalcStep.isUsable()) {
9050 // Calculate the step beforehand instead of doing this on each iteration.
9051 // (This is not used if the number of iterations may be kfold-ed).
9052 CalcStepExpr = CalcStep.get();
9053 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009054 }
9055
Alexey Bataev182227b2015-08-20 10:54:39 +00009056 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9057 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009058 StepExpr, CalcStepExpr,
9059 buildPreInits(Context, ExprCaptures),
9060 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009061}
9062
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009063static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9064 Expr *NumIterations, Sema &SemaRef,
9065 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009066 // Walk the vars and build update/final expressions for the CodeGen.
9067 SmallVector<Expr *, 8> Updates;
9068 SmallVector<Expr *, 8> Finals;
9069 Expr *Step = Clause.getStep();
9070 Expr *CalcStep = Clause.getCalcStep();
9071 // OpenMP [2.14.3.7, linear clause]
9072 // If linear-step is not specified it is assumed to be 1.
9073 if (Step == nullptr)
9074 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009075 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009076 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009077 }
Alexander Musman3276a272015-03-21 10:12:56 +00009078 bool HasErrors = false;
9079 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009080 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009081 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009082 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009083 SourceLocation ELoc;
9084 SourceRange ERange;
9085 Expr *SimpleRefExpr = RefExpr;
9086 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9087 /*AllowArraySection=*/false);
9088 ValueDecl *D = Res.first;
9089 if (Res.second || !D) {
9090 Updates.push_back(nullptr);
9091 Finals.push_back(nullptr);
9092 HasErrors = true;
9093 continue;
9094 }
9095 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9096 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9097 ->getMemberDecl();
9098 }
9099 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009100 Expr *InitExpr = *CurInit;
9101
9102 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009103 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009104 Expr *CapturedRef;
9105 if (LinKind == OMPC_LINEAR_uval)
9106 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9107 else
9108 CapturedRef =
9109 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9110 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9111 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009112
9113 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009114 ExprResult Update;
9115 if (!Info.first) {
9116 Update =
9117 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9118 InitExpr, IV, Step, /* Subtract */ false);
9119 } else
9120 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009121 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9122 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009123
9124 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009125 ExprResult Final;
9126 if (!Info.first) {
9127 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9128 InitExpr, NumIterations, Step,
9129 /* Subtract */ false);
9130 } else
9131 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009132 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9133 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009134
Alexander Musman3276a272015-03-21 10:12:56 +00009135 if (!Update.isUsable() || !Final.isUsable()) {
9136 Updates.push_back(nullptr);
9137 Finals.push_back(nullptr);
9138 HasErrors = true;
9139 } else {
9140 Updates.push_back(Update.get());
9141 Finals.push_back(Final.get());
9142 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009143 ++CurInit;
9144 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009145 }
9146 Clause.setUpdates(Updates);
9147 Clause.setFinals(Finals);
9148 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009149}
9150
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009151OMPClause *Sema::ActOnOpenMPAlignedClause(
9152 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9153 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9154
9155 SmallVector<Expr *, 8> Vars;
9156 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009157 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9158 SourceLocation ELoc;
9159 SourceRange ERange;
9160 Expr *SimpleRefExpr = RefExpr;
9161 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9162 /*AllowArraySection=*/false);
9163 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009164 // It will be analyzed later.
9165 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009166 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009167 ValueDecl *D = Res.first;
9168 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009169 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009170
Alexey Bataev1efd1662016-03-29 10:59:56 +00009171 QualType QType = D->getType();
9172 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009173
9174 // OpenMP [2.8.1, simd construct, Restrictions]
9175 // The type of list items appearing in the aligned clause must be
9176 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009177 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009178 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009179 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009180 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009181 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009182 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009183 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009184 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009185 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009186 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009187 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009188 continue;
9189 }
9190
9191 // OpenMP [2.8.1, simd construct, Restrictions]
9192 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009193 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009194 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009195 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9196 << getOpenMPClauseName(OMPC_aligned);
9197 continue;
9198 }
9199
Alexey Bataev1efd1662016-03-29 10:59:56 +00009200 DeclRefExpr *Ref = nullptr;
9201 if (!VD && IsOpenMPCapturedDecl(D))
9202 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9203 Vars.push_back(DefaultFunctionArrayConversion(
9204 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9205 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009206 }
9207
9208 // OpenMP [2.8.1, simd construct, Description]
9209 // The parameter of the aligned clause, alignment, must be a constant
9210 // positive integer expression.
9211 // If no optional parameter is specified, implementation-defined default
9212 // alignments for SIMD instructions on the target platforms are assumed.
9213 if (Alignment != nullptr) {
9214 ExprResult AlignResult =
9215 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9216 if (AlignResult.isInvalid())
9217 return nullptr;
9218 Alignment = AlignResult.get();
9219 }
9220 if (Vars.empty())
9221 return nullptr;
9222
9223 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9224 EndLoc, Vars, Alignment);
9225}
9226
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009227OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9228 SourceLocation StartLoc,
9229 SourceLocation LParenLoc,
9230 SourceLocation EndLoc) {
9231 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009232 SmallVector<Expr *, 8> SrcExprs;
9233 SmallVector<Expr *, 8> DstExprs;
9234 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009235 for (auto &RefExpr : VarList) {
9236 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9237 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009238 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009239 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009240 SrcExprs.push_back(nullptr);
9241 DstExprs.push_back(nullptr);
9242 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009243 continue;
9244 }
9245
Alexey Bataeved09d242014-05-28 05:53:51 +00009246 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009247 // OpenMP [2.1, C/C++]
9248 // A list item is a variable name.
9249 // OpenMP [2.14.4.1, Restrictions, p.1]
9250 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009251 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009252 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009253 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9254 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009255 continue;
9256 }
9257
9258 Decl *D = DE->getDecl();
9259 VarDecl *VD = cast<VarDecl>(D);
9260
9261 QualType Type = VD->getType();
9262 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9263 // It will be analyzed later.
9264 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009265 SrcExprs.push_back(nullptr);
9266 DstExprs.push_back(nullptr);
9267 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009268 continue;
9269 }
9270
9271 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9272 // A list item that appears in a copyin clause must be threadprivate.
9273 if (!DSAStack->isThreadPrivate(VD)) {
9274 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009275 << getOpenMPClauseName(OMPC_copyin)
9276 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009277 continue;
9278 }
9279
9280 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9281 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009282 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009283 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009284 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009285 auto *SrcVD =
9286 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9287 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009288 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009289 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9290 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009291 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9292 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009293 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009294 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009295 // For arrays generate assignment operation for single element and replace
9296 // it by the original array element in CodeGen.
9297 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9298 PseudoDstExpr, PseudoSrcExpr);
9299 if (AssignmentOp.isInvalid())
9300 continue;
9301 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9302 /*DiscardedValue=*/true);
9303 if (AssignmentOp.isInvalid())
9304 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009305
9306 DSAStack->addDSA(VD, DE, OMPC_copyin);
9307 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009308 SrcExprs.push_back(PseudoSrcExpr);
9309 DstExprs.push_back(PseudoDstExpr);
9310 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009311 }
9312
Alexey Bataeved09d242014-05-28 05:53:51 +00009313 if (Vars.empty())
9314 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009315
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009316 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9317 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009318}
9319
Alexey Bataevbae9a792014-06-27 10:37:06 +00009320OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9321 SourceLocation StartLoc,
9322 SourceLocation LParenLoc,
9323 SourceLocation EndLoc) {
9324 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009325 SmallVector<Expr *, 8> SrcExprs;
9326 SmallVector<Expr *, 8> DstExprs;
9327 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009328 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009329 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9330 SourceLocation ELoc;
9331 SourceRange ERange;
9332 Expr *SimpleRefExpr = RefExpr;
9333 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9334 /*AllowArraySection=*/false);
9335 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009336 // It will be analyzed later.
9337 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009338 SrcExprs.push_back(nullptr);
9339 DstExprs.push_back(nullptr);
9340 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009341 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009342 ValueDecl *D = Res.first;
9343 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009344 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009345
Alexey Bataeve122da12016-03-17 10:50:17 +00009346 QualType Type = D->getType();
9347 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009348
9349 // OpenMP [2.14.4.2, Restrictions, p.2]
9350 // A list item that appears in a copyprivate clause may not appear in a
9351 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009352 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9353 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009354 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9355 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009356 Diag(ELoc, diag::err_omp_wrong_dsa)
9357 << getOpenMPClauseName(DVar.CKind)
9358 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009359 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009360 continue;
9361 }
9362
9363 // OpenMP [2.11.4.2, Restrictions, p.1]
9364 // All list items that appear in a copyprivate clause must be either
9365 // threadprivate or private in the enclosing context.
9366 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009367 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009368 if (DVar.CKind == OMPC_shared) {
9369 Diag(ELoc, diag::err_omp_required_access)
9370 << getOpenMPClauseName(OMPC_copyprivate)
9371 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009372 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009373 continue;
9374 }
9375 }
9376 }
9377
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009378 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009379 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009380 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009381 << getOpenMPClauseName(OMPC_copyprivate) << Type
9382 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009383 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009384 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009385 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009386 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009387 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009388 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009389 continue;
9390 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009391
Alexey Bataevbae9a792014-06-27 10:37:06 +00009392 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9393 // A variable of class type (or array thereof) that appears in a
9394 // copyin clause requires an accessible, unambiguous copy assignment
9395 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009396 Type = Context.getBaseElementType(Type.getNonReferenceType())
9397 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009398 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009399 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9400 D->hasAttrs() ? &D->getAttrs() : nullptr);
9401 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009402 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009403 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9404 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009405 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009406 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9407 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009408 PseudoDstExpr, PseudoSrcExpr);
9409 if (AssignmentOp.isInvalid())
9410 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009411 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009412 /*DiscardedValue=*/true);
9413 if (AssignmentOp.isInvalid())
9414 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009415
9416 // No need to mark vars as copyprivate, they are already threadprivate or
9417 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009418 assert(VD || IsOpenMPCapturedDecl(D));
9419 Vars.push_back(
9420 VD ? RefExpr->IgnoreParens()
9421 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009422 SrcExprs.push_back(PseudoSrcExpr);
9423 DstExprs.push_back(PseudoDstExpr);
9424 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009425 }
9426
9427 if (Vars.empty())
9428 return nullptr;
9429
Alexey Bataeva63048e2015-03-23 06:18:07 +00009430 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9431 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009432}
9433
Alexey Bataev6125da92014-07-21 11:26:11 +00009434OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9435 SourceLocation StartLoc,
9436 SourceLocation LParenLoc,
9437 SourceLocation EndLoc) {
9438 if (VarList.empty())
9439 return nullptr;
9440
9441 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9442}
Alexey Bataevdea47612014-07-23 07:46:59 +00009443
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009444OMPClause *
9445Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9446 SourceLocation DepLoc, SourceLocation ColonLoc,
9447 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9448 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009449 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009450 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009451 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009452 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009453 return nullptr;
9454 }
9455 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009456 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9457 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009458 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009459 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009460 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9461 /*Last=*/OMPC_DEPEND_unknown, Except)
9462 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009463 return nullptr;
9464 }
9465 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009466 llvm::APSInt DepCounter(/*BitWidth=*/32);
9467 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9468 if (DepKind == OMPC_DEPEND_sink) {
9469 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9470 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9471 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009472 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009473 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009474 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9475 DSAStack->getParentOrderedRegionParam()) {
9476 for (auto &RefExpr : VarList) {
9477 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9478 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9479 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9480 // It will be analyzed later.
9481 Vars.push_back(RefExpr);
9482 continue;
9483 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009484
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009485 SourceLocation ELoc = RefExpr->getExprLoc();
9486 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9487 if (DepKind == OMPC_DEPEND_sink) {
9488 if (DepCounter >= TotalDepCount) {
9489 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9490 continue;
9491 }
9492 ++DepCounter;
9493 // OpenMP [2.13.9, Summary]
9494 // depend(dependence-type : vec), where dependence-type is:
9495 // 'sink' and where vec is the iteration vector, which has the form:
9496 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9497 // where n is the value specified by the ordered clause in the loop
9498 // directive, xi denotes the loop iteration variable of the i-th nested
9499 // loop associated with the loop directive, and di is a constant
9500 // non-negative integer.
9501 SimpleExpr = SimpleExpr->IgnoreImplicit();
9502 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9503 if (!DE) {
9504 OverloadedOperatorKind OOK = OO_None;
9505 SourceLocation OOLoc;
9506 Expr *LHS, *RHS;
9507 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9508 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9509 OOLoc = BO->getOperatorLoc();
9510 LHS = BO->getLHS()->IgnoreParenImpCasts();
9511 RHS = BO->getRHS()->IgnoreParenImpCasts();
9512 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9513 OOK = OCE->getOperator();
9514 OOLoc = OCE->getOperatorLoc();
9515 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9516 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9517 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9518 OOK = MCE->getMethodDecl()
9519 ->getNameInfo()
9520 .getName()
9521 .getCXXOverloadedOperator();
9522 OOLoc = MCE->getCallee()->getExprLoc();
9523 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9524 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9525 } else {
9526 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9527 continue;
9528 }
9529 DE = dyn_cast<DeclRefExpr>(LHS);
9530 if (!DE) {
9531 Diag(LHS->getExprLoc(),
9532 diag::err_omp_depend_sink_expected_loop_iteration)
9533 << DSAStack->getParentLoopControlVariable(
9534 DepCounter.getZExtValue());
9535 continue;
9536 }
9537 if (OOK != OO_Plus && OOK != OO_Minus) {
9538 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9539 continue;
9540 }
9541 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9542 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9543 if (Res.isInvalid())
9544 continue;
9545 }
9546 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9547 if (!CurContext->isDependentContext() &&
9548 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009549 (!VD ||
9550 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009551 Diag(DE->getExprLoc(),
9552 diag::err_omp_depend_sink_expected_loop_iteration)
9553 << DSAStack->getParentLoopControlVariable(
9554 DepCounter.getZExtValue());
9555 continue;
9556 }
9557 } else {
9558 // OpenMP [2.11.1.1, Restrictions, p.3]
9559 // A variable that is part of another variable (such as a field of a
9560 // structure) but is not an array element or an array section cannot
9561 // appear in a depend clause.
9562 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9563 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9564 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9565 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9566 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009567 (ASE &&
9568 !ASE->getBase()
9569 ->getType()
9570 .getNonReferenceType()
9571 ->isPointerType() &&
9572 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009573 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9574 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009575 continue;
9576 }
9577 }
9578
9579 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9580 }
9581
9582 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9583 TotalDepCount > VarList.size() &&
9584 DSAStack->getParentOrderedRegionParam()) {
9585 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9586 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9587 }
9588 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9589 Vars.empty())
9590 return nullptr;
9591 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009592
9593 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9594 DepLoc, ColonLoc, Vars);
9595}
Michael Wonge710d542015-08-07 16:16:36 +00009596
9597OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9598 SourceLocation LParenLoc,
9599 SourceLocation EndLoc) {
9600 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009601
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009602 // OpenMP [2.9.1, Restrictions]
9603 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009604 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9605 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009606 return nullptr;
9607
Michael Wonge710d542015-08-07 16:16:36 +00009608 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9609}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009610
9611static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9612 DSAStackTy *Stack, CXXRecordDecl *RD) {
9613 if (!RD || RD->isInvalidDecl())
9614 return true;
9615
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009616 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9617 if (auto *CTD = CTSD->getSpecializedTemplate())
9618 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009619 auto QTy = SemaRef.Context.getRecordType(RD);
9620 if (RD->isDynamicClass()) {
9621 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9622 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9623 return false;
9624 }
9625 auto *DC = RD;
9626 bool IsCorrect = true;
9627 for (auto *I : DC->decls()) {
9628 if (I) {
9629 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9630 if (MD->isStatic()) {
9631 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9632 SemaRef.Diag(MD->getLocation(),
9633 diag::note_omp_static_member_in_target);
9634 IsCorrect = false;
9635 }
9636 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9637 if (VD->isStaticDataMember()) {
9638 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9639 SemaRef.Diag(VD->getLocation(),
9640 diag::note_omp_static_member_in_target);
9641 IsCorrect = false;
9642 }
9643 }
9644 }
9645 }
9646
9647 for (auto &I : RD->bases()) {
9648 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9649 I.getType()->getAsCXXRecordDecl()))
9650 IsCorrect = false;
9651 }
9652 return IsCorrect;
9653}
9654
9655static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9656 DSAStackTy *Stack, QualType QTy) {
9657 NamedDecl *ND;
9658 if (QTy->isIncompleteType(&ND)) {
9659 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9660 return false;
9661 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9662 if (!RD->isInvalidDecl() &&
9663 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9664 return false;
9665 }
9666 return true;
9667}
9668
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009669/// \brief Return true if it can be proven that the provided array expression
9670/// (array section or array subscript) does NOT specify the whole size of the
9671/// array whose base type is \a BaseQTy.
9672static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9673 const Expr *E,
9674 QualType BaseQTy) {
9675 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9676
9677 // If this is an array subscript, it refers to the whole size if the size of
9678 // the dimension is constant and equals 1. Also, an array section assumes the
9679 // format of an array subscript if no colon is used.
9680 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9681 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9682 return ATy->getSize().getSExtValue() != 1;
9683 // Size can't be evaluated statically.
9684 return false;
9685 }
9686
9687 assert(OASE && "Expecting array section if not an array subscript.");
9688 auto *LowerBound = OASE->getLowerBound();
9689 auto *Length = OASE->getLength();
9690
9691 // If there is a lower bound that does not evaluates to zero, we are not
9692 // convering the whole dimension.
9693 if (LowerBound) {
9694 llvm::APSInt ConstLowerBound;
9695 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9696 return false; // Can't get the integer value as a constant.
9697 if (ConstLowerBound.getSExtValue())
9698 return true;
9699 }
9700
9701 // If we don't have a length we covering the whole dimension.
9702 if (!Length)
9703 return false;
9704
9705 // If the base is a pointer, we don't have a way to get the size of the
9706 // pointee.
9707 if (BaseQTy->isPointerType())
9708 return false;
9709
9710 // We can only check if the length is the same as the size of the dimension
9711 // if we have a constant array.
9712 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9713 if (!CATy)
9714 return false;
9715
9716 llvm::APSInt ConstLength;
9717 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9718 return false; // Can't get the integer value as a constant.
9719
9720 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9721}
9722
9723// Return true if it can be proven that the provided array expression (array
9724// section or array subscript) does NOT specify a single element of the array
9725// whose base type is \a BaseQTy.
9726static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9727 const Expr *E,
9728 QualType BaseQTy) {
9729 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9730
9731 // An array subscript always refer to a single element. Also, an array section
9732 // assumes the format of an array subscript if no colon is used.
9733 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9734 return false;
9735
9736 assert(OASE && "Expecting array section if not an array subscript.");
9737 auto *Length = OASE->getLength();
9738
9739 // If we don't have a length we have to check if the array has unitary size
9740 // for this dimension. Also, we should always expect a length if the base type
9741 // is pointer.
9742 if (!Length) {
9743 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9744 return ATy->getSize().getSExtValue() != 1;
9745 // We cannot assume anything.
9746 return false;
9747 }
9748
9749 // Check if the length evaluates to 1.
9750 llvm::APSInt ConstLength;
9751 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9752 return false; // Can't get the integer value as a constant.
9753
9754 return ConstLength.getSExtValue() != 1;
9755}
9756
Samuel Antao5de996e2016-01-22 20:21:36 +00009757// Return the expression of the base of the map clause or null if it cannot
9758// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009759// valid as a standalone map clause expression. In the process, record all the
9760// components of the expression.
9761static Expr *CheckMapClauseExpressionBase(
9762 Sema &SemaRef, Expr *E,
9763 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009764 SourceLocation ELoc = E->getExprLoc();
9765 SourceRange ERange = E->getSourceRange();
9766
9767 // The base of elements of list in a map clause have to be either:
9768 // - a reference to variable or field.
9769 // - a member expression.
9770 // - an array expression.
9771 //
9772 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9773 // reference to 'r'.
9774 //
9775 // If we have:
9776 //
9777 // struct SS {
9778 // Bla S;
9779 // foo() {
9780 // #pragma omp target map (S.Arr[:12]);
9781 // }
9782 // }
9783 //
9784 // We want to retrieve the member expression 'this->S';
9785
9786 Expr *RelevantExpr = nullptr;
9787
Samuel Antao5de996e2016-01-22 20:21:36 +00009788 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9789 // If a list item is an array section, it must specify contiguous storage.
9790 //
9791 // For this restriction it is sufficient that we make sure only references
9792 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009793 // exist except in the rightmost expression (unless they cover the whole
9794 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009795 //
9796 // r.ArrS[3:5].Arr[6:7]
9797 //
9798 // r.ArrS[3:5].x
9799 //
9800 // but these would be valid:
9801 // r.ArrS[3].Arr[6:7]
9802 //
9803 // r.ArrS[3].x
9804
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009805 bool AllowUnitySizeArraySection = true;
9806 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009807
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009808 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009809 E = E->IgnoreParenImpCasts();
9810
9811 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9812 if (!isa<VarDecl>(CurE->getDecl()))
9813 break;
9814
9815 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009816
9817 // If we got a reference to a declaration, we should not expect any array
9818 // section before that.
9819 AllowUnitySizeArraySection = false;
9820 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009821
9822 // Record the component.
9823 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9824 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009825 continue;
9826 }
9827
9828 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9829 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9830
9831 if (isa<CXXThisExpr>(BaseE))
9832 // We found a base expression: this->Val.
9833 RelevantExpr = CurE;
9834 else
9835 E = BaseE;
9836
9837 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9838 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9839 << CurE->getSourceRange();
9840 break;
9841 }
9842
9843 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9844
9845 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9846 // A bit-field cannot appear in a map clause.
9847 //
9848 if (FD->isBitField()) {
9849 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9850 << CurE->getSourceRange();
9851 break;
9852 }
9853
9854 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9855 // If the type of a list item is a reference to a type T then the type
9856 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009857 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009858
9859 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9860 // A list item cannot be a variable that is a member of a structure with
9861 // a union type.
9862 //
9863 if (auto *RT = CurType->getAs<RecordType>())
9864 if (RT->isUnionType()) {
9865 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9866 << CurE->getSourceRange();
9867 break;
9868 }
9869
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009870 // If we got a member expression, we should not expect any array section
9871 // before that:
9872 //
9873 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9874 // If a list item is an element of a structure, only the rightmost symbol
9875 // of the variable reference can be an array section.
9876 //
9877 AllowUnitySizeArraySection = false;
9878 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009879
9880 // Record the component.
9881 CurComponents.push_back(
9882 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009883 continue;
9884 }
9885
9886 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9887 E = CurE->getBase()->IgnoreParenImpCasts();
9888
9889 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9890 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9891 << 0 << CurE->getSourceRange();
9892 break;
9893 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009894
9895 // If we got an array subscript that express the whole dimension we
9896 // can have any array expressions before. If it only expressing part of
9897 // the dimension, we can only have unitary-size array expressions.
9898 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9899 E->getType()))
9900 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009901
9902 // Record the component - we don't have any declaration associated.
9903 CurComponents.push_back(
9904 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009905 continue;
9906 }
9907
9908 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009909 E = CurE->getBase()->IgnoreParenImpCasts();
9910
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009911 auto CurType =
9912 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9913
Samuel Antao5de996e2016-01-22 20:21:36 +00009914 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9915 // If the type of a list item is a reference to a type T then the type
9916 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009917 if (CurType->isReferenceType())
9918 CurType = CurType->getPointeeType();
9919
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009920 bool IsPointer = CurType->isAnyPointerType();
9921
9922 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009923 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9924 << 0 << CurE->getSourceRange();
9925 break;
9926 }
9927
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009928 bool NotWhole =
9929 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9930 bool NotUnity =
9931 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9932
9933 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9934 // Any array section is currently allowed.
9935 //
9936 // If this array section refers to the whole dimension we can still
9937 // accept other array sections before this one, except if the base is a
9938 // pointer. Otherwise, only unitary sections are accepted.
9939 if (NotWhole || IsPointer)
9940 AllowWholeSizeArraySection = false;
9941 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9942 (AllowWholeSizeArraySection && NotWhole)) {
9943 // A unity or whole array section is not allowed and that is not
9944 // compatible with the properties of the current array section.
9945 SemaRef.Diag(
9946 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9947 << CurE->getSourceRange();
9948 break;
9949 }
Samuel Antao90927002016-04-26 14:54:23 +00009950
9951 // Record the component - we don't have any declaration associated.
9952 CurComponents.push_back(
9953 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009954 continue;
9955 }
9956
9957 // If nothing else worked, this is not a valid map clause expression.
9958 SemaRef.Diag(ELoc,
9959 diag::err_omp_expected_named_var_member_or_array_expression)
9960 << ERange;
9961 break;
9962 }
9963
9964 return RelevantExpr;
9965}
9966
9967// Return true if expression E associated with value VD has conflicts with other
9968// map information.
Samuel Antao90927002016-04-26 14:54:23 +00009969static bool CheckMapConflicts(
9970 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9971 bool CurrentRegionOnly,
9972 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009973 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +00009974 SourceLocation ELoc = E->getExprLoc();
9975 SourceRange ERange = E->getSourceRange();
9976
9977 // In order to easily check the conflicts we need to match each component of
9978 // the expression under test with the components of the expressions that are
9979 // already in the stack.
9980
Samuel Antao5de996e2016-01-22 20:21:36 +00009981 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009982 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009983 "Map clause expression with unexpected base!");
9984
9985 // Variables to help detecting enclosing problems in data environment nests.
9986 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +00009987 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +00009988
Samuel Antao90927002016-04-26 14:54:23 +00009989 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9990 VD, CurrentRegionOnly,
9991 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
9992 StackComponents) -> bool {
9993
Samuel Antao5de996e2016-01-22 20:21:36 +00009994 assert(!StackComponents.empty() &&
9995 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009996 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009997 "Map clause expression with unexpected base!");
9998
Samuel Antao90927002016-04-26 14:54:23 +00009999 // The whole expression in the stack.
10000 auto *RE = StackComponents.front().getAssociatedExpression();
10001
Samuel Antao5de996e2016-01-22 20:21:36 +000010002 // Expressions must start from the same base. Here we detect at which
10003 // point both expressions diverge from each other and see if we can
10004 // detect if the memory referred to both expressions is contiguous and
10005 // do not overlap.
10006 auto CI = CurComponents.rbegin();
10007 auto CE = CurComponents.rend();
10008 auto SI = StackComponents.rbegin();
10009 auto SE = StackComponents.rend();
10010 for (; CI != CE && SI != SE; ++CI, ++SI) {
10011
10012 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10013 // At most one list item can be an array item derived from a given
10014 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010015 if (CurrentRegionOnly &&
10016 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10017 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10018 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10019 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10020 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010021 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010022 << CI->getAssociatedExpression()->getSourceRange();
10023 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10024 diag::note_used_here)
10025 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010026 return true;
10027 }
10028
10029 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010030 if (CI->getAssociatedExpression()->getStmtClass() !=
10031 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010032 break;
10033
10034 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010035 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010036 break;
10037 }
10038
10039 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10040 // List items of map clauses in the same construct must not share
10041 // original storage.
10042 //
10043 // If the expressions are exactly the same or one is a subset of the
10044 // other, it means they are sharing storage.
10045 if (CI == CE && SI == SE) {
10046 if (CurrentRegionOnly) {
10047 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10048 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10049 << RE->getSourceRange();
10050 return true;
10051 } else {
10052 // If we find the same expression in the enclosing data environment,
10053 // that is legal.
10054 IsEnclosedByDataEnvironmentExpr = true;
10055 return false;
10056 }
10057 }
10058
Samuel Antao90927002016-04-26 14:54:23 +000010059 QualType DerivedType =
10060 std::prev(CI)->getAssociatedDeclaration()->getType();
10061 SourceLocation DerivedLoc =
10062 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010063
10064 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10065 // If the type of a list item is a reference to a type T then the type
10066 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010067 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010068
10069 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10070 // A variable for which the type is pointer and an array section
10071 // derived from that variable must not appear as list items of map
10072 // clauses of the same construct.
10073 //
10074 // Also, cover one of the cases in:
10075 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10076 // If any part of the original storage of a list item has corresponding
10077 // storage in the device data environment, all of the original storage
10078 // must have corresponding storage in the device data environment.
10079 //
10080 if (DerivedType->isAnyPointerType()) {
10081 if (CI == CE || SI == SE) {
10082 SemaRef.Diag(
10083 DerivedLoc,
10084 diag::err_omp_pointer_mapped_along_with_derived_section)
10085 << DerivedLoc;
10086 } else {
10087 assert(CI != CE && SI != SE);
10088 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10089 << DerivedLoc;
10090 }
10091 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10092 << RE->getSourceRange();
10093 return true;
10094 }
10095
10096 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10097 // List items of map clauses in the same construct must not share
10098 // original storage.
10099 //
10100 // An expression is a subset of the other.
10101 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10102 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10103 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10104 << RE->getSourceRange();
10105 return true;
10106 }
10107
10108 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010109 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010110 if (!CurrentRegionOnly && SI != SE)
10111 EnclosingExpr = RE;
10112
10113 // The current expression is a subset of the expression in the data
10114 // environment.
10115 IsEnclosedByDataEnvironmentExpr |=
10116 (!CurrentRegionOnly && CI != CE && SI == SE);
10117
10118 return false;
10119 });
10120
10121 if (CurrentRegionOnly)
10122 return FoundError;
10123
10124 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10125 // If any part of the original storage of a list item has corresponding
10126 // storage in the device data environment, all of the original storage must
10127 // have corresponding storage in the device data environment.
10128 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10129 // If a list item is an element of a structure, and a different element of
10130 // the structure has a corresponding list item in the device data environment
10131 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010132 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010133 // data environment prior to the task encountering the construct.
10134 //
10135 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10136 SemaRef.Diag(ELoc,
10137 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10138 << ERange;
10139 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10140 << EnclosingExpr->getSourceRange();
10141 return true;
10142 }
10143
10144 return FoundError;
10145}
10146
Samuel Antao23abd722016-01-19 20:40:49 +000010147OMPClause *
10148Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10149 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10150 SourceLocation MapLoc, SourceLocation ColonLoc,
10151 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10152 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010153 SmallVector<Expr *, 4> Vars;
10154
Samuel Antao90927002016-04-26 14:54:23 +000010155 // Keep track of the mappable components and base declarations in this clause.
10156 // Each entry in the list is going to have a list of components associated. We
10157 // record each set of the components so that we can build the clause later on.
10158 // In the end we should have the same amount of declarations and component
10159 // lists.
10160 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10161 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10162
10163 ClauseComponents.reserve(VarList.size());
10164 ClauseBaseDeclarations.reserve(VarList.size());
10165
Kelvin Li0bff7af2015-11-23 05:32:03 +000010166 for (auto &RE : VarList) {
10167 assert(RE && "Null expr in omp map");
10168 if (isa<DependentScopeDeclRefExpr>(RE)) {
10169 // It will be analyzed later.
10170 Vars.push_back(RE);
10171 continue;
10172 }
10173 SourceLocation ELoc = RE->getExprLoc();
10174
Kelvin Li0bff7af2015-11-23 05:32:03 +000010175 auto *VE = RE->IgnoreParenLValueCasts();
10176
10177 if (VE->isValueDependent() || VE->isTypeDependent() ||
10178 VE->isInstantiationDependent() ||
10179 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010180 // We can only analyze this information once the missing information is
10181 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010182 Vars.push_back(RE);
10183 continue;
10184 }
10185
10186 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010187
Samuel Antao5de996e2016-01-22 20:21:36 +000010188 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10189 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10190 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010191 continue;
10192 }
10193
Samuel Antao90927002016-04-26 14:54:23 +000010194 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10195 ValueDecl *CurDeclaration = nullptr;
10196
10197 // Obtain the array or member expression bases if required. Also, fill the
10198 // components array with all the components identified in the process.
10199 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010200 if (!BE)
10201 continue;
10202
Samuel Antao90927002016-04-26 14:54:23 +000010203 assert(!CurComponents.empty() &&
10204 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010205
Samuel Antao90927002016-04-26 14:54:23 +000010206 // For the following checks, we rely on the base declaration which is
10207 // expected to be associated with the last component. The declaration is
10208 // expected to be a variable or a field (if 'this' is being mapped).
10209 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10210 assert(CurDeclaration && "Null decl on map clause.");
10211 assert(
10212 CurDeclaration->isCanonicalDecl() &&
10213 "Expecting components to have associated only canonical declarations.");
10214
10215 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10216 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010217
10218 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010219 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010220
10221 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10222 // threadprivate variables cannot appear in a map clause.
10223 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010224 auto DVar = DSAStack->getTopDSA(VD, false);
10225 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10226 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10227 continue;
10228 }
10229
Samuel Antao5de996e2016-01-22 20:21:36 +000010230 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10231 // A list item cannot appear in both a map clause and a data-sharing
10232 // attribute clause on the same construct.
10233 //
10234 // TODO: Implement this check - it cannot currently be tested because of
10235 // missing implementation of the other data sharing clauses in target
10236 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010237
Samuel Antao5de996e2016-01-22 20:21:36 +000010238 // Check conflicts with other map clause expressions. We check the conflicts
10239 // with the current construct separately from the enclosing data
10240 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010241 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10242 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010243 break;
Samuel Antao90927002016-04-26 14:54:23 +000010244 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10245 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010246 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010247
Samuel Antao5de996e2016-01-22 20:21:36 +000010248 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10249 // If the type of a list item is a reference to a type T then the type will
10250 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010251 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010252
10253 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010254 // A list item must have a mappable type.
10255 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10256 DSAStack, Type))
10257 continue;
10258
Samuel Antaodf67fc42016-01-19 19:15:56 +000010259 // target enter data
10260 // OpenMP [2.10.2, Restrictions, p. 99]
10261 // A map-type must be specified in all map clauses and must be either
10262 // to or alloc.
10263 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10264 if (DKind == OMPD_target_enter_data &&
10265 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10266 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010267 << (IsMapTypeImplicit ? 1 : 0)
10268 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010269 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010270 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010271 }
10272
Samuel Antao72590762016-01-19 20:04:50 +000010273 // target exit_data
10274 // OpenMP [2.10.3, Restrictions, p. 102]
10275 // A map-type must be specified in all map clauses and must be either
10276 // from, release, or delete.
10277 DKind = DSAStack->getCurrentDirective();
10278 if (DKind == OMPD_target_exit_data &&
10279 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10280 MapType == OMPC_MAP_delete)) {
10281 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010282 << (IsMapTypeImplicit ? 1 : 0)
10283 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010284 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010285 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010286 }
10287
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010288 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10289 // A list item cannot appear in both a map clause and a data-sharing
10290 // attribute clause on the same construct
10291 if (DKind == OMPD_target && VD) {
10292 auto DVar = DSAStack->getTopDSA(VD, false);
10293 if (isOpenMPPrivate(DVar.CKind)) {
10294 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10295 << getOpenMPClauseName(DVar.CKind)
10296 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010297 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010298 continue;
10299 }
10300 }
10301
Samuel Antao90927002016-04-26 14:54:23 +000010302 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010303 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010304
10305 // Store the components in the stack so that they can be used to check
10306 // against other clauses later on.
10307 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10308
10309 // Save the components and declaration to create the clause. For purposes of
10310 // the clause creation, any component list that has has base 'this' uses
10311 // null has
10312 ClauseComponents.resize(ClauseComponents.size() + 1);
10313 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10314 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10315 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010316 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010317
Samuel Antao5de996e2016-01-22 20:21:36 +000010318 // We need to produce a map clause even if we don't have variables so that
10319 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010320 return OMPMapClause::Create(
10321 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10322 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010323}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010324
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010325QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10326 TypeResult ParsedType) {
10327 assert(ParsedType.isUsable());
10328
10329 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10330 if (ReductionType.isNull())
10331 return QualType();
10332
10333 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10334 // A type name in a declare reduction directive cannot be a function type, an
10335 // array type, a reference type, or a type qualified with const, volatile or
10336 // restrict.
10337 if (ReductionType.hasQualifiers()) {
10338 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10339 return QualType();
10340 }
10341
10342 if (ReductionType->isFunctionType()) {
10343 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10344 return QualType();
10345 }
10346 if (ReductionType->isReferenceType()) {
10347 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10348 return QualType();
10349 }
10350 if (ReductionType->isArrayType()) {
10351 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10352 return QualType();
10353 }
10354 return ReductionType;
10355}
10356
10357Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10358 Scope *S, DeclContext *DC, DeclarationName Name,
10359 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10360 AccessSpecifier AS, Decl *PrevDeclInScope) {
10361 SmallVector<Decl *, 8> Decls;
10362 Decls.reserve(ReductionTypes.size());
10363
10364 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10365 ForRedeclaration);
10366 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10367 // A reduction-identifier may not be re-declared in the current scope for the
10368 // same type or for a type that is compatible according to the base language
10369 // rules.
10370 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10371 OMPDeclareReductionDecl *PrevDRD = nullptr;
10372 bool InCompoundScope = true;
10373 if (S != nullptr) {
10374 // Find previous declaration with the same name not referenced in other
10375 // declarations.
10376 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10377 InCompoundScope =
10378 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10379 LookupName(Lookup, S);
10380 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10381 /*AllowInlineNamespace=*/false);
10382 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10383 auto Filter = Lookup.makeFilter();
10384 while (Filter.hasNext()) {
10385 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10386 if (InCompoundScope) {
10387 auto I = UsedAsPrevious.find(PrevDecl);
10388 if (I == UsedAsPrevious.end())
10389 UsedAsPrevious[PrevDecl] = false;
10390 if (auto *D = PrevDecl->getPrevDeclInScope())
10391 UsedAsPrevious[D] = true;
10392 }
10393 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10394 PrevDecl->getLocation();
10395 }
10396 Filter.done();
10397 if (InCompoundScope) {
10398 for (auto &PrevData : UsedAsPrevious) {
10399 if (!PrevData.second) {
10400 PrevDRD = PrevData.first;
10401 break;
10402 }
10403 }
10404 }
10405 } else if (PrevDeclInScope != nullptr) {
10406 auto *PrevDRDInScope = PrevDRD =
10407 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10408 do {
10409 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10410 PrevDRDInScope->getLocation();
10411 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10412 } while (PrevDRDInScope != nullptr);
10413 }
10414 for (auto &TyData : ReductionTypes) {
10415 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10416 bool Invalid = false;
10417 if (I != PreviousRedeclTypes.end()) {
10418 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10419 << TyData.first;
10420 Diag(I->second, diag::note_previous_definition);
10421 Invalid = true;
10422 }
10423 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10424 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10425 Name, TyData.first, PrevDRD);
10426 DC->addDecl(DRD);
10427 DRD->setAccess(AS);
10428 Decls.push_back(DRD);
10429 if (Invalid)
10430 DRD->setInvalidDecl();
10431 else
10432 PrevDRD = DRD;
10433 }
10434
10435 return DeclGroupPtrTy::make(
10436 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10437}
10438
10439void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10440 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10441
10442 // Enter new function scope.
10443 PushFunctionScope();
10444 getCurFunction()->setHasBranchProtectedScope();
10445 getCurFunction()->setHasOMPDeclareReductionCombiner();
10446
10447 if (S != nullptr)
10448 PushDeclContext(S, DRD);
10449 else
10450 CurContext = DRD;
10451
10452 PushExpressionEvaluationContext(PotentiallyEvaluated);
10453
10454 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010455 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10456 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10457 // uses semantics of argument handles by value, but it should be passed by
10458 // reference. C lang does not support references, so pass all parameters as
10459 // pointers.
10460 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010461 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010462 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010463 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10464 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10465 // uses semantics of argument handles by value, but it should be passed by
10466 // reference. C lang does not support references, so pass all parameters as
10467 // pointers.
10468 // Create 'T omp_out;' variable.
10469 auto *OmpOutParm =
10470 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10471 if (S != nullptr) {
10472 PushOnScopeChains(OmpInParm, S);
10473 PushOnScopeChains(OmpOutParm, S);
10474 } else {
10475 DRD->addDecl(OmpInParm);
10476 DRD->addDecl(OmpOutParm);
10477 }
10478}
10479
10480void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10481 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10482 DiscardCleanupsInEvaluationContext();
10483 PopExpressionEvaluationContext();
10484
10485 PopDeclContext();
10486 PopFunctionScopeInfo();
10487
10488 if (Combiner != nullptr)
10489 DRD->setCombiner(Combiner);
10490 else
10491 DRD->setInvalidDecl();
10492}
10493
10494void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10495 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10496
10497 // Enter new function scope.
10498 PushFunctionScope();
10499 getCurFunction()->setHasBranchProtectedScope();
10500
10501 if (S != nullptr)
10502 PushDeclContext(S, DRD);
10503 else
10504 CurContext = DRD;
10505
10506 PushExpressionEvaluationContext(PotentiallyEvaluated);
10507
10508 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010509 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10510 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10511 // uses semantics of argument handles by value, but it should be passed by
10512 // reference. C lang does not support references, so pass all parameters as
10513 // pointers.
10514 // Create 'T omp_priv;' variable.
10515 auto *OmpPrivParm =
10516 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010517 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10518 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10519 // uses semantics of argument handles by value, but it should be passed by
10520 // reference. C lang does not support references, so pass all parameters as
10521 // pointers.
10522 // Create 'T omp_orig;' variable.
10523 auto *OmpOrigParm =
10524 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010525 if (S != nullptr) {
10526 PushOnScopeChains(OmpPrivParm, S);
10527 PushOnScopeChains(OmpOrigParm, S);
10528 } else {
10529 DRD->addDecl(OmpPrivParm);
10530 DRD->addDecl(OmpOrigParm);
10531 }
10532}
10533
10534void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10535 Expr *Initializer) {
10536 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10537 DiscardCleanupsInEvaluationContext();
10538 PopExpressionEvaluationContext();
10539
10540 PopDeclContext();
10541 PopFunctionScopeInfo();
10542
10543 if (Initializer != nullptr)
10544 DRD->setInitializer(Initializer);
10545 else
10546 DRD->setInvalidDecl();
10547}
10548
10549Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10550 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10551 for (auto *D : DeclReductions.get()) {
10552 if (IsValid) {
10553 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10554 if (S != nullptr)
10555 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10556 } else
10557 D->setInvalidDecl();
10558 }
10559 return DeclReductions;
10560}
10561
Kelvin Li099bb8c2015-11-24 20:50:12 +000010562OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10563 SourceLocation StartLoc,
10564 SourceLocation LParenLoc,
10565 SourceLocation EndLoc) {
10566 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010567
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010568 // OpenMP [teams Constrcut, Restrictions]
10569 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010570 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10571 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010572 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010573
10574 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10575}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010576
10577OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10578 SourceLocation StartLoc,
10579 SourceLocation LParenLoc,
10580 SourceLocation EndLoc) {
10581 Expr *ValExpr = ThreadLimit;
10582
10583 // OpenMP [teams Constrcut, Restrictions]
10584 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010585 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10586 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010587 return nullptr;
10588
10589 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10590 EndLoc);
10591}
Alexey Bataeva0569352015-12-01 10:17:31 +000010592
10593OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10594 SourceLocation StartLoc,
10595 SourceLocation LParenLoc,
10596 SourceLocation EndLoc) {
10597 Expr *ValExpr = Priority;
10598
10599 // OpenMP [2.9.1, task Constrcut]
10600 // The priority-value is a non-negative numerical scalar expression.
10601 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10602 /*StrictlyPositive=*/false))
10603 return nullptr;
10604
10605 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10606}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010607
10608OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10609 SourceLocation StartLoc,
10610 SourceLocation LParenLoc,
10611 SourceLocation EndLoc) {
10612 Expr *ValExpr = Grainsize;
10613
10614 // OpenMP [2.9.2, taskloop Constrcut]
10615 // The parameter of the grainsize clause must be a positive integer
10616 // expression.
10617 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10618 /*StrictlyPositive=*/true))
10619 return nullptr;
10620
10621 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10622}
Alexey Bataev382967a2015-12-08 12:06:20 +000010623
10624OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10625 SourceLocation StartLoc,
10626 SourceLocation LParenLoc,
10627 SourceLocation EndLoc) {
10628 Expr *ValExpr = NumTasks;
10629
10630 // OpenMP [2.9.2, taskloop Constrcut]
10631 // The parameter of the num_tasks clause must be a positive integer
10632 // expression.
10633 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10634 /*StrictlyPositive=*/true))
10635 return nullptr;
10636
10637 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10638}
10639
Alexey Bataev28c75412015-12-15 08:19:24 +000010640OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10641 SourceLocation LParenLoc,
10642 SourceLocation EndLoc) {
10643 // OpenMP [2.13.2, critical construct, Description]
10644 // ... where hint-expression is an integer constant expression that evaluates
10645 // to a valid lock hint.
10646 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10647 if (HintExpr.isInvalid())
10648 return nullptr;
10649 return new (Context)
10650 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10651}
10652
Carlo Bertollib4adf552016-01-15 18:50:31 +000010653OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10654 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10655 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10656 SourceLocation EndLoc) {
10657 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10658 std::string Values;
10659 Values += "'";
10660 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10661 Values += "'";
10662 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10663 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10664 return nullptr;
10665 }
10666 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010667 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010668 if (ChunkSize) {
10669 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10670 !ChunkSize->isInstantiationDependent() &&
10671 !ChunkSize->containsUnexpandedParameterPack()) {
10672 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10673 ExprResult Val =
10674 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10675 if (Val.isInvalid())
10676 return nullptr;
10677
10678 ValExpr = Val.get();
10679
10680 // OpenMP [2.7.1, Restrictions]
10681 // chunk_size must be a loop invariant integer expression with a positive
10682 // value.
10683 llvm::APSInt Result;
10684 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10685 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10686 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10687 << "dist_schedule" << ChunkSize->getSourceRange();
10688 return nullptr;
10689 }
10690 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010691 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10692 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10693 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010694 }
10695 }
10696 }
10697
10698 return new (Context)
10699 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010700 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010701}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010702
10703OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10704 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10705 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10706 SourceLocation KindLoc, SourceLocation EndLoc) {
10707 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10708 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10709 Kind != OMPC_DEFAULTMAP_scalar) {
10710 std::string Value;
10711 SourceLocation Loc;
10712 Value += "'";
10713 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10714 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10715 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10716 Loc = MLoc;
10717 } else {
10718 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10719 OMPC_DEFAULTMAP_scalar);
10720 Loc = KindLoc;
10721 }
10722 Value += "'";
10723 Diag(Loc, diag::err_omp_unexpected_clause_value)
10724 << Value << getOpenMPClauseName(OMPC_defaultmap);
10725 return nullptr;
10726 }
10727
10728 return new (Context)
10729 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10730}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010731
10732bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10733 DeclContext *CurLexicalContext = getCurLexicalContext();
10734 if (!CurLexicalContext->isFileContext() &&
10735 !CurLexicalContext->isExternCContext() &&
10736 !CurLexicalContext->isExternCXXContext()) {
10737 Diag(Loc, diag::err_omp_region_not_file_context);
10738 return false;
10739 }
10740 if (IsInOpenMPDeclareTargetContext) {
10741 Diag(Loc, diag::err_omp_enclosed_declare_target);
10742 return false;
10743 }
10744
10745 IsInOpenMPDeclareTargetContext = true;
10746 return true;
10747}
10748
10749void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10750 assert(IsInOpenMPDeclareTargetContext &&
10751 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10752
10753 IsInOpenMPDeclareTargetContext = false;
10754}
10755
10756static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10757 Sema &SemaRef, Decl *D) {
10758 if (!D)
10759 return;
10760 Decl *LD = nullptr;
10761 if (isa<TagDecl>(D)) {
10762 LD = cast<TagDecl>(D)->getDefinition();
10763 } else if (isa<VarDecl>(D)) {
10764 LD = cast<VarDecl>(D)->getDefinition();
10765
10766 // If this is an implicit variable that is legal and we do not need to do
10767 // anything.
10768 if (cast<VarDecl>(D)->isImplicit()) {
10769 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10770 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10771 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10772 return;
10773 }
10774
10775 } else if (isa<FunctionDecl>(D)) {
10776 const FunctionDecl *FD = nullptr;
10777 if (cast<FunctionDecl>(D)->hasBody(FD))
10778 LD = const_cast<FunctionDecl *>(FD);
10779
10780 // If the definition is associated with the current declaration in the
10781 // target region (it can be e.g. a lambda) that is legal and we do not need
10782 // to do anything else.
10783 if (LD == D) {
10784 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10785 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10786 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10787 return;
10788 }
10789 }
10790 if (!LD)
10791 LD = D;
10792 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10793 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10794 // Outlined declaration is not declared target.
10795 if (LD->isOutOfLine()) {
10796 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10797 SemaRef.Diag(SL, diag::note_used_here) << SR;
10798 } else {
10799 DeclContext *DC = LD->getDeclContext();
10800 while (DC) {
10801 if (isa<FunctionDecl>(DC) &&
10802 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10803 break;
10804 DC = DC->getParent();
10805 }
10806 if (DC)
10807 return;
10808
10809 // Is not declared in target context.
10810 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10811 SemaRef.Diag(SL, diag::note_used_here) << SR;
10812 }
10813 // Mark decl as declared target to prevent further diagnostic.
10814 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10815 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10816 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10817 }
10818}
10819
10820static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10821 Sema &SemaRef, DSAStackTy *Stack,
10822 ValueDecl *VD) {
10823 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10824 return true;
10825 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10826 return false;
10827 return true;
10828}
10829
10830void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10831 if (!D || D->isInvalidDecl())
10832 return;
10833 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10834 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10835 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10836 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10837 if (DSAStack->isThreadPrivate(VD)) {
10838 Diag(SL, diag::err_omp_threadprivate_in_target);
10839 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10840 return;
10841 }
10842 }
10843 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10844 // Problem if any with var declared with incomplete type will be reported
10845 // as normal, so no need to check it here.
10846 if ((E || !VD->getType()->isIncompleteType()) &&
10847 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10848 // Mark decl as declared target to prevent further diagnostic.
10849 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10850 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10851 if (ASTMutationListener *ML = Context.getASTMutationListener())
10852 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10853 }
10854 return;
10855 }
10856 }
10857 if (!E) {
10858 // Checking declaration inside declare target region.
10859 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10860 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10861 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10862 if (ASTMutationListener *ML = Context.getASTMutationListener())
10863 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10864 }
10865 return;
10866 }
10867 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10868}