blob: cbcf70b41fc02a8820ce2bd173069584851ebc71 [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 Bataev07b79c22016-04-29 09:56:11 +00001419 if (E->isTypeDependent() || E->isValueDependent() ||
1420 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1421 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001422 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001423 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001424 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1425 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001426
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001427 auto DVar = Stack->getTopDSA(VD, false);
1428 // Check if the variable has explicit DSA set and stop analysis if it so.
1429 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001430
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001431 auto ELoc = E->getExprLoc();
1432 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001433 // The default(none) clause requires that each variable that is referenced
1434 // in the construct, and does not have a predetermined data-sharing
1435 // attribute, must have its data-sharing attribute explicitly determined
1436 // by being listed in a data-sharing attribute clause.
1437 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001438 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001439 VarsWithInheritedDSA.count(VD) == 0) {
1440 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001441 return;
1442 }
1443
1444 // OpenMP [2.9.3.6, Restrictions, p.2]
1445 // A list item that appears in a reduction clause of the innermost
1446 // enclosing worksharing or parallel construct may not be accessed in an
1447 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001448 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 [](OpenMPDirectiveKind K) -> bool {
1450 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001451 isOpenMPWorksharingDirective(K) ||
1452 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001453 },
1454 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001455 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001456 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001457 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1458 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001459 return;
1460 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001461
1462 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001463 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001464 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1465 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001466 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001467 }
1468 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001469 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00001470 if (E->isTypeDependent() || E->isValueDependent() ||
1471 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1472 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001473 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1474 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1475 auto DVar = Stack->getTopDSA(FD, false);
1476 // Check if the variable has explicit DSA set and stop analysis if it
1477 // so.
1478 if (DVar.RefExpr)
1479 return;
1480
1481 auto ELoc = E->getExprLoc();
1482 auto DKind = Stack->getCurrentDirective();
1483 // OpenMP [2.9.3.6, Restrictions, p.2]
1484 // A list item that appears in a reduction clause of the innermost
1485 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001486 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001487 DVar =
1488 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1489 [](OpenMPDirectiveKind K) -> bool {
1490 return isOpenMPParallelDirective(K) ||
1491 isOpenMPWorksharingDirective(K) ||
1492 isOpenMPTeamsDirective(K);
1493 },
1494 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001495 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001496 ErrorFound = true;
1497 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1498 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1499 return;
1500 }
1501
1502 // Define implicit data-sharing attributes for task.
1503 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001504 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1505 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001506 ImplicitFirstprivate.push_back(E);
1507 }
1508 }
1509 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001510 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001511 for (auto *C : S->clauses()) {
1512 // Skip analysis of arguments of implicitly defined firstprivate clause
1513 // for task directives.
1514 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1515 for (auto *CC : C->children()) {
1516 if (CC)
1517 Visit(CC);
1518 }
1519 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001520 }
1521 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001522 for (auto *C : S->children()) {
1523 if (C && !isa<OMPExecutableDirective>(C))
1524 Visit(C);
1525 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001526 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001527
1528 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001529 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001530 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001531 return VarsWithInheritedDSA;
1532 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001533
Alexey Bataev7ff55242014-06-19 09:13:45 +00001534 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1535 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001536};
Alexey Bataeved09d242014-05-28 05:53:51 +00001537} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001538
Alexey Bataevbae9a792014-06-27 10:37:06 +00001539void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001540 switch (DKind) {
1541 case OMPD_parallel: {
1542 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001543 QualType KmpInt32PtrTy =
1544 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001545 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001546 std::make_pair(".global_tid.", KmpInt32PtrTy),
1547 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1548 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001549 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001550 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1551 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001552 break;
1553 }
1554 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001555 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001556 std::make_pair(StringRef(), QualType()) // __context with shared vars
1557 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001558 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1559 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001560 break;
1561 }
1562 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001563 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001564 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001565 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001566 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1567 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001568 break;
1569 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001570 case OMPD_for_simd: {
1571 Sema::CapturedParamNameType Params[] = {
1572 std::make_pair(StringRef(), QualType()) // __context with shared vars
1573 };
1574 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1575 Params);
1576 break;
1577 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001578 case OMPD_sections: {
1579 Sema::CapturedParamNameType Params[] = {
1580 std::make_pair(StringRef(), QualType()) // __context with shared vars
1581 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001582 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1583 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001584 break;
1585 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001586 case OMPD_section: {
1587 Sema::CapturedParamNameType Params[] = {
1588 std::make_pair(StringRef(), QualType()) // __context with shared vars
1589 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001590 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1591 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001592 break;
1593 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001594 case OMPD_single: {
1595 Sema::CapturedParamNameType Params[] = {
1596 std::make_pair(StringRef(), QualType()) // __context with shared vars
1597 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001598 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1599 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001600 break;
1601 }
Alexander Musman80c22892014-07-17 08:54:58 +00001602 case OMPD_master: {
1603 Sema::CapturedParamNameType Params[] = {
1604 std::make_pair(StringRef(), QualType()) // __context with shared vars
1605 };
1606 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1607 Params);
1608 break;
1609 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001610 case OMPD_critical: {
1611 Sema::CapturedParamNameType Params[] = {
1612 std::make_pair(StringRef(), QualType()) // __context with shared vars
1613 };
1614 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1615 Params);
1616 break;
1617 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001618 case OMPD_parallel_for: {
1619 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001620 QualType KmpInt32PtrTy =
1621 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001622 Sema::CapturedParamNameType Params[] = {
1623 std::make_pair(".global_tid.", KmpInt32PtrTy),
1624 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1625 std::make_pair(StringRef(), QualType()) // __context with shared vars
1626 };
1627 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628 Params);
1629 break;
1630 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001631 case OMPD_parallel_for_simd: {
1632 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001633 QualType KmpInt32PtrTy =
1634 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001635 Sema::CapturedParamNameType Params[] = {
1636 std::make_pair(".global_tid.", KmpInt32PtrTy),
1637 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1638 std::make_pair(StringRef(), QualType()) // __context with shared vars
1639 };
1640 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1641 Params);
1642 break;
1643 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001644 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001645 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001646 QualType KmpInt32PtrTy =
1647 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001648 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001649 std::make_pair(".global_tid.", KmpInt32PtrTy),
1650 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001651 std::make_pair(StringRef(), QualType()) // __context with shared vars
1652 };
1653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1654 Params);
1655 break;
1656 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001657 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001658 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001659 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1660 FunctionProtoType::ExtProtoInfo EPI;
1661 EPI.Variadic = true;
1662 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001663 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001664 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001665 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1666 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1667 std::make_pair(".copy_fn.",
1668 Context.getPointerType(CopyFnType).withConst()),
1669 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001670 std::make_pair(StringRef(), QualType()) // __context with shared vars
1671 };
1672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001674 // Mark this captured region as inlined, because we don't use outlined
1675 // function directly.
1676 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1677 AlwaysInlineAttr::CreateImplicit(
1678 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001679 break;
1680 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001681 case OMPD_ordered: {
1682 Sema::CapturedParamNameType Params[] = {
1683 std::make_pair(StringRef(), QualType()) // __context with shared vars
1684 };
1685 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1686 Params);
1687 break;
1688 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001689 case OMPD_atomic: {
1690 Sema::CapturedParamNameType Params[] = {
1691 std::make_pair(StringRef(), QualType()) // __context with shared vars
1692 };
1693 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1694 Params);
1695 break;
1696 }
Michael Wong65f367f2015-07-21 13:44:28 +00001697 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001698 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001699 case OMPD_target_parallel:
1700 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001701 Sema::CapturedParamNameType Params[] = {
1702 std::make_pair(StringRef(), QualType()) // __context with shared vars
1703 };
1704 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1705 Params);
1706 break;
1707 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001708 case OMPD_teams: {
1709 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001710 QualType KmpInt32PtrTy =
1711 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001712 Sema::CapturedParamNameType Params[] = {
1713 std::make_pair(".global_tid.", KmpInt32PtrTy),
1714 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1715 std::make_pair(StringRef(), QualType()) // __context with shared vars
1716 };
1717 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1718 Params);
1719 break;
1720 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001721 case OMPD_taskgroup: {
1722 Sema::CapturedParamNameType Params[] = {
1723 std::make_pair(StringRef(), QualType()) // __context with shared vars
1724 };
1725 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1726 Params);
1727 break;
1728 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00001729 case OMPD_taskloop:
1730 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001731 QualType KmpInt32Ty =
1732 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1733 QualType KmpUInt64Ty =
1734 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1735 QualType KmpInt64Ty =
1736 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1737 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1738 FunctionProtoType::ExtProtoInfo EPI;
1739 EPI.Variadic = true;
1740 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001741 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001742 std::make_pair(".global_tid.", KmpInt32Ty),
1743 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1744 std::make_pair(".privates.",
1745 Context.VoidPtrTy.withConst().withRestrict()),
1746 std::make_pair(
1747 ".copy_fn.",
1748 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1749 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1750 std::make_pair(".lb.", KmpUInt64Ty),
1751 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1752 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001753 std::make_pair(StringRef(), QualType()) // __context with shared vars
1754 };
1755 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1756 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001757 // Mark this captured region as inlined, because we don't use outlined
1758 // function directly.
1759 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1760 AlwaysInlineAttr::CreateImplicit(
1761 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001762 break;
1763 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001764 case OMPD_distribute: {
1765 Sema::CapturedParamNameType Params[] = {
1766 std::make_pair(StringRef(), QualType()) // __context with shared vars
1767 };
1768 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1769 Params);
1770 break;
1771 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001772 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001773 case OMPD_taskyield:
1774 case OMPD_barrier:
1775 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001776 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001777 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001778 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001779 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001780 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001781 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001782 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001783 case OMPD_declare_target:
1784 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001785 llvm_unreachable("OpenMP Directive is not allowed");
1786 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001787 llvm_unreachable("Unknown OpenMP directive");
1788 }
1789}
1790
Alexey Bataev3392d762016-02-16 11:18:12 +00001791static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001792 Expr *CaptureExpr, bool WithInit,
1793 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001794 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001795 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001796 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001797 QualType Ty = Init->getType();
1798 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1799 if (S.getLangOpts().CPlusPlus)
1800 Ty = C.getLValueReferenceType(Ty);
1801 else {
1802 Ty = C.getPointerType(Ty);
1803 ExprResult Res =
1804 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1805 if (!Res.isUsable())
1806 return nullptr;
1807 Init = Res.get();
1808 }
Alexey Bataev61205072016-03-02 04:57:40 +00001809 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001810 }
1811 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001812 if (!WithInit)
1813 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001814 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001815 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1816 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001817 return CED;
1818}
1819
Alexey Bataev61205072016-03-02 04:57:40 +00001820static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1821 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001822 OMPCapturedExprDecl *CD;
1823 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1824 CD = cast<OMPCapturedExprDecl>(VD);
1825 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001826 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1827 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001828 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001829 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001830}
1831
Alexey Bataev5a3af132016-03-29 08:58:54 +00001832static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1833 if (!Ref) {
1834 auto *CD =
1835 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1836 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1837 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1838 CaptureExpr->getExprLoc());
1839 }
1840 ExprResult Res = Ref;
1841 if (!S.getLangOpts().CPlusPlus &&
1842 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1843 Ref->getType()->isPointerType())
1844 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1845 if (!Res.isUsable())
1846 return ExprError();
1847 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001848}
1849
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001850StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1851 ArrayRef<OMPClause *> Clauses) {
1852 if (!S.isUsable()) {
1853 ActOnCapturedRegionError();
1854 return StmtError();
1855 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001856
1857 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001859 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001860 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001861 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001862 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001863 Clause->getClauseKind() == OMPC_copyprivate ||
1864 (getLangOpts().OpenMPUseTLS &&
1865 getASTContext().getTargetInfo().isTLSSupported() &&
1866 Clause->getClauseKind() == OMPC_copyin)) {
1867 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001868 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001869 for (auto *VarRef : Clause->children()) {
1870 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001871 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001872 }
1873 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001874 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001875 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001876 // Mark all variables in private list clauses as used in inner region.
1877 // Required for proper codegen of combined directives.
1878 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001879 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001880 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1881 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001882 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1883 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001884 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001885 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1886 if (auto *E = C->getPostUpdateExpr())
1887 MarkDeclarationsReferencedInExpr(E);
1888 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001889 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001890 if (Clause->getClauseKind() == OMPC_schedule)
1891 SC = cast<OMPScheduleClause>(Clause);
1892 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001893 OC = cast<OMPOrderedClause>(Clause);
1894 else if (Clause->getClauseKind() == OMPC_linear)
1895 LCs.push_back(cast<OMPLinearClause>(Clause));
1896 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001897 bool ErrorFound = false;
1898 // OpenMP, 2.7.1 Loop Construct, Restrictions
1899 // The nonmonotonic modifier cannot be specified if an ordered clause is
1900 // specified.
1901 if (SC &&
1902 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1903 SC->getSecondScheduleModifier() ==
1904 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1905 OC) {
1906 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1907 ? SC->getFirstScheduleModifierLoc()
1908 : SC->getSecondScheduleModifierLoc(),
1909 diag::err_omp_schedule_nonmonotonic_ordered)
1910 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1911 ErrorFound = true;
1912 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001913 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1914 for (auto *C : LCs) {
1915 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1916 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1917 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001918 ErrorFound = true;
1919 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001920 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1921 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1922 OC->getNumForLoops()) {
1923 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1924 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1925 ErrorFound = true;
1926 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001927 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001928 ActOnCapturedRegionError();
1929 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001930 }
1931 return ActOnCapturedRegionEnd(S.get());
1932}
1933
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001934static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1935 OpenMPDirectiveKind CurrentRegion,
1936 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001937 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001938 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001939 // Allowed nesting of constructs
1940 // +------------------+-----------------+------------------------------------+
1941 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1942 // +------------------+-----------------+------------------------------------+
1943 // | parallel | parallel | * |
1944 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001945 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001946 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001947 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001948 // | parallel | simd | * |
1949 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001950 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001951 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001952 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001953 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001954 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001955 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001956 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001957 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001958 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001959 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001960 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001961 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001962 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001963 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001964 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001965 // | parallel | target parallel | * |
1966 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001967 // | parallel | target enter | * |
1968 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001969 // | parallel | target exit | * |
1970 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001971 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001972 // | parallel | cancellation | |
1973 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001974 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001975 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001976 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001977 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001978 // +------------------+-----------------+------------------------------------+
1979 // | for | parallel | * |
1980 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001981 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001982 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001983 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001984 // | for | simd | * |
1985 // | for | sections | + |
1986 // | for | section | + |
1987 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001988 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001989 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001990 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001991 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001992 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001993 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001994 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001995 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001996 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001997 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001998 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001999 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002000 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002001 // | for | target parallel | * |
2002 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002003 // | for | target enter | * |
2004 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002005 // | for | target exit | * |
2006 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002007 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002008 // | for | cancellation | |
2009 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002010 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002011 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002012 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002013 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002014 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00002015 // | master | parallel | * |
2016 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002017 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002018 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002019 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00002020 // | master | simd | * |
2021 // | master | sections | + |
2022 // | master | section | + |
2023 // | master | single | + |
2024 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002025 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00002026 // | master |parallel sections| * |
2027 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002028 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002029 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002030 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002031 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002032 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002033 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002034 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002035 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002036 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002037 // | master | target parallel | * |
2038 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002039 // | master | target enter | * |
2040 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002041 // | master | target exit | * |
2042 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002043 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002044 // | master | cancellation | |
2045 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002046 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002047 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002048 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002049 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | critical | parallel | * |
2052 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002053 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002054 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002055 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002056 // | critical | simd | * |
2057 // | critical | sections | + |
2058 // | critical | section | + |
2059 // | critical | single | + |
2060 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002061 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002062 // | critical |parallel sections| * |
2063 // | critical | task | * |
2064 // | critical | taskyield | * |
2065 // | critical | barrier | + |
2066 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002067 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002068 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002069 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002070 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002071 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002072 // | critical | target parallel | * |
2073 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002074 // | critical | target enter | * |
2075 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002076 // | critical | target exit | * |
2077 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002078 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002079 // | critical | cancellation | |
2080 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002081 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002082 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002083 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002084 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002085 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002086 // | simd | parallel | |
2087 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002088 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002089 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002090 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002091 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002092 // | simd | sections | |
2093 // | simd | section | |
2094 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002095 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002096 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002097 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002098 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002099 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002100 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002101 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002102 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002103 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002104 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002105 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002106 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002107 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002108 // | simd | target parallel | |
2109 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002110 // | simd | target enter | |
2111 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002112 // | simd | target exit | |
2113 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002114 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002115 // | simd | cancellation | |
2116 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002117 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002118 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002119 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002120 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002121 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002122 // | for simd | parallel | |
2123 // | for simd | for | |
2124 // | for simd | for simd | |
2125 // | for simd | master | |
2126 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002127 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002128 // | for simd | sections | |
2129 // | for simd | section | |
2130 // | for simd | single | |
2131 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002132 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002133 // | for simd |parallel sections| |
2134 // | for simd | task | |
2135 // | for simd | taskyield | |
2136 // | for simd | barrier | |
2137 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002138 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002139 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002140 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002141 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002142 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002143 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002144 // | for simd | target parallel | |
2145 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002146 // | for simd | target enter | |
2147 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002148 // | for simd | target exit | |
2149 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002150 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002151 // | for simd | cancellation | |
2152 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002153 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002154 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002155 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002156 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002157 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002158 // | parallel for simd| parallel | |
2159 // | parallel for simd| for | |
2160 // | parallel for simd| for simd | |
2161 // | parallel for simd| master | |
2162 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002163 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002164 // | parallel for simd| sections | |
2165 // | parallel for simd| section | |
2166 // | parallel for simd| single | |
2167 // | parallel for simd| parallel for | |
2168 // | parallel for simd|parallel for simd| |
2169 // | parallel for simd|parallel sections| |
2170 // | parallel for simd| task | |
2171 // | parallel for simd| taskyield | |
2172 // | parallel for simd| barrier | |
2173 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002174 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002175 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002176 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002177 // | parallel for simd| atomic | |
2178 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002179 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002180 // | parallel for simd| target parallel | |
2181 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002182 // | parallel for simd| target enter | |
2183 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002184 // | parallel for simd| target exit | |
2185 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002186 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002187 // | parallel for simd| cancellation | |
2188 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002189 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002190 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002191 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002192 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002193 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002194 // | sections | parallel | * |
2195 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002196 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002197 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002198 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002199 // | sections | simd | * |
2200 // | sections | sections | + |
2201 // | sections | section | * |
2202 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002203 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002204 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002205 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002206 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002207 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002208 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002209 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002210 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002211 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002212 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002213 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002214 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002215 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002216 // | sections | target parallel | * |
2217 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002218 // | sections | target enter | * |
2219 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002220 // | sections | target exit | * |
2221 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002222 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002223 // | sections | cancellation | |
2224 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002225 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002226 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002227 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002228 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002229 // +------------------+-----------------+------------------------------------+
2230 // | section | parallel | * |
2231 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002232 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002233 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002234 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002235 // | section | simd | * |
2236 // | section | sections | + |
2237 // | section | section | + |
2238 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002239 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002240 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002241 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002242 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002243 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002244 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002245 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002246 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002247 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002248 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002249 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002250 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002251 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002252 // | section | target parallel | * |
2253 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002254 // | section | target enter | * |
2255 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002256 // | section | target exit | * |
2257 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002258 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002259 // | section | cancellation | |
2260 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002261 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002262 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002263 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002264 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002265 // +------------------+-----------------+------------------------------------+
2266 // | single | parallel | * |
2267 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002268 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002269 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002270 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002271 // | single | simd | * |
2272 // | single | sections | + |
2273 // | single | section | + |
2274 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002275 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002276 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002277 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002278 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002279 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002280 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002281 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002282 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002283 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002284 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002285 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002286 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002287 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002288 // | single | target parallel | * |
2289 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002290 // | single | target enter | * |
2291 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002292 // | single | target exit | * |
2293 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002294 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002295 // | single | cancellation | |
2296 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002297 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002298 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002299 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002300 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002301 // +------------------+-----------------+------------------------------------+
2302 // | parallel for | parallel | * |
2303 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002304 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002305 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002306 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002307 // | parallel for | simd | * |
2308 // | parallel for | sections | + |
2309 // | parallel for | section | + |
2310 // | parallel for | single | + |
2311 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002312 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002313 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002314 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002315 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002316 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002317 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002318 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002319 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002320 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002321 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002322 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002323 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002324 // | parallel for | target parallel | * |
2325 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002326 // | parallel for | target enter | * |
2327 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002328 // | parallel for | target exit | * |
2329 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002330 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002331 // | parallel for | cancellation | |
2332 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002333 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002334 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002335 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002336 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002337 // +------------------+-----------------+------------------------------------+
2338 // | parallel sections| parallel | * |
2339 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002340 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002341 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002342 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002343 // | parallel sections| simd | * |
2344 // | parallel sections| sections | + |
2345 // | parallel sections| section | * |
2346 // | parallel sections| single | + |
2347 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002348 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002349 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002350 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002351 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002352 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002353 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002354 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002355 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002356 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002357 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002358 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002359 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002360 // | parallel sections| target parallel | * |
2361 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002362 // | parallel sections| target enter | * |
2363 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002364 // | parallel sections| target exit | * |
2365 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002366 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002367 // | parallel sections| cancellation | |
2368 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002369 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002370 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002371 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002372 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002373 // +------------------+-----------------+------------------------------------+
2374 // | task | parallel | * |
2375 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002376 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002377 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002378 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002379 // | task | simd | * |
2380 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002381 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002382 // | task | single | + |
2383 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002384 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002385 // | task |parallel sections| * |
2386 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002387 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002388 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002389 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002390 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002391 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002392 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002393 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002394 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002395 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002396 // | task | target parallel | * |
2397 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002398 // | task | target enter | * |
2399 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002400 // | task | target exit | * |
2401 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002402 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002403 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002404 // | | point | ! |
2405 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002406 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002407 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002408 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002409 // +------------------+-----------------+------------------------------------+
2410 // | ordered | parallel | * |
2411 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002412 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002413 // | ordered | master | * |
2414 // | ordered | critical | * |
2415 // | ordered | simd | * |
2416 // | ordered | sections | + |
2417 // | ordered | section | + |
2418 // | ordered | single | + |
2419 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002420 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002421 // | ordered |parallel sections| * |
2422 // | ordered | task | * |
2423 // | ordered | taskyield | * |
2424 // | ordered | barrier | + |
2425 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002426 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002427 // | ordered | flush | * |
2428 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002429 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002430 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002431 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002432 // | ordered | target parallel | * |
2433 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002434 // | ordered | target enter | * |
2435 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002436 // | ordered | target exit | * |
2437 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002438 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002439 // | ordered | cancellation | |
2440 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002441 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002442 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002443 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002444 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002445 // +------------------+-----------------+------------------------------------+
2446 // | atomic | parallel | |
2447 // | atomic | for | |
2448 // | atomic | for simd | |
2449 // | atomic | master | |
2450 // | atomic | critical | |
2451 // | atomic | simd | |
2452 // | atomic | sections | |
2453 // | atomic | section | |
2454 // | atomic | single | |
2455 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002456 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002457 // | atomic |parallel sections| |
2458 // | atomic | task | |
2459 // | atomic | taskyield | |
2460 // | atomic | barrier | |
2461 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002462 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002463 // | atomic | flush | |
2464 // | atomic | ordered | |
2465 // | atomic | atomic | |
2466 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002467 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002468 // | atomic | target parallel | |
2469 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002470 // | atomic | target enter | |
2471 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002472 // | atomic | target exit | |
2473 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002474 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002475 // | atomic | cancellation | |
2476 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002477 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002478 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002479 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002480 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002481 // +------------------+-----------------+------------------------------------+
2482 // | target | parallel | * |
2483 // | target | for | * |
2484 // | target | for simd | * |
2485 // | target | master | * |
2486 // | target | critical | * |
2487 // | target | simd | * |
2488 // | target | sections | * |
2489 // | target | section | * |
2490 // | target | single | * |
2491 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002492 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002493 // | target |parallel sections| * |
2494 // | target | task | * |
2495 // | target | taskyield | * |
2496 // | target | barrier | * |
2497 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002498 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002499 // | target | flush | * |
2500 // | target | ordered | * |
2501 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002502 // | target | target | |
2503 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002504 // | target | target parallel | |
2505 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002506 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002507 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002508 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002509 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002510 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002511 // | target | cancellation | |
2512 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002513 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002515 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002516 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002517 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002518 // | target parallel | parallel | * |
2519 // | target parallel | for | * |
2520 // | target parallel | for simd | * |
2521 // | target parallel | master | * |
2522 // | target parallel | critical | * |
2523 // | target parallel | simd | * |
2524 // | target parallel | sections | * |
2525 // | target parallel | section | * |
2526 // | target parallel | single | * |
2527 // | target parallel | parallel for | * |
2528 // | target parallel |parallel for simd| * |
2529 // | target parallel |parallel sections| * |
2530 // | target parallel | task | * |
2531 // | target parallel | taskyield | * |
2532 // | target parallel | barrier | * |
2533 // | target parallel | taskwait | * |
2534 // | target parallel | taskgroup | * |
2535 // | target parallel | flush | * |
2536 // | target parallel | ordered | * |
2537 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002538 // | target parallel | target | |
2539 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002540 // | target parallel | target parallel | |
2541 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002542 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002543 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002544 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002545 // | | data | |
2546 // | target parallel | teams | |
2547 // | target parallel | cancellation | |
2548 // | | point | ! |
2549 // | target parallel | cancel | ! |
2550 // | target parallel | taskloop | * |
2551 // | target parallel | taskloop simd | * |
2552 // | target parallel | distribute | |
2553 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002554 // | target parallel | parallel | * |
2555 // | for | | |
2556 // | target parallel | for | * |
2557 // | for | | |
2558 // | target parallel | for simd | * |
2559 // | for | | |
2560 // | target parallel | master | * |
2561 // | for | | |
2562 // | target parallel | critical | * |
2563 // | for | | |
2564 // | target parallel | simd | * |
2565 // | for | | |
2566 // | target parallel | sections | * |
2567 // | for | | |
2568 // | target parallel | section | * |
2569 // | for | | |
2570 // | target parallel | single | * |
2571 // | for | | |
2572 // | target parallel | parallel for | * |
2573 // | for | | |
2574 // | target parallel |parallel for simd| * |
2575 // | for | | |
2576 // | target parallel |parallel sections| * |
2577 // | for | | |
2578 // | target parallel | task | * |
2579 // | for | | |
2580 // | target parallel | taskyield | * |
2581 // | for | | |
2582 // | target parallel | barrier | * |
2583 // | for | | |
2584 // | target parallel | taskwait | * |
2585 // | for | | |
2586 // | target parallel | taskgroup | * |
2587 // | for | | |
2588 // | target parallel | flush | * |
2589 // | for | | |
2590 // | target parallel | ordered | * |
2591 // | for | | |
2592 // | target parallel | atomic | * |
2593 // | for | | |
2594 // | target parallel | target | |
2595 // | for | | |
2596 // | target parallel | target parallel | |
2597 // | for | | |
2598 // | target parallel | target parallel | |
2599 // | for | for | |
2600 // | target parallel | target enter | |
2601 // | for | data | |
2602 // | target parallel | target exit | |
2603 // | for | data | |
2604 // | target parallel | teams | |
2605 // | for | | |
2606 // | target parallel | cancellation | |
2607 // | for | point | ! |
2608 // | target parallel | cancel | ! |
2609 // | for | | |
2610 // | target parallel | taskloop | * |
2611 // | for | | |
2612 // | target parallel | taskloop simd | * |
2613 // | for | | |
2614 // | target parallel | distribute | |
2615 // | for | | |
2616 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002617 // | teams | parallel | * |
2618 // | teams | for | + |
2619 // | teams | for simd | + |
2620 // | teams | master | + |
2621 // | teams | critical | + |
2622 // | teams | simd | + |
2623 // | teams | sections | + |
2624 // | teams | section | + |
2625 // | teams | single | + |
2626 // | teams | parallel for | * |
2627 // | teams |parallel for simd| * |
2628 // | teams |parallel sections| * |
2629 // | teams | task | + |
2630 // | teams | taskyield | + |
2631 // | teams | barrier | + |
2632 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002633 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002634 // | teams | flush | + |
2635 // | teams | ordered | + |
2636 // | teams | atomic | + |
2637 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002638 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002639 // | teams | target parallel | + |
2640 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002641 // | teams | target enter | + |
2642 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002643 // | teams | target exit | + |
2644 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002645 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002646 // | teams | cancellation | |
2647 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002648 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002649 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002650 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002651 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002652 // +------------------+-----------------+------------------------------------+
2653 // | taskloop | parallel | * |
2654 // | taskloop | for | + |
2655 // | taskloop | for simd | + |
2656 // | taskloop | master | + |
2657 // | taskloop | critical | * |
2658 // | taskloop | simd | * |
2659 // | taskloop | sections | + |
2660 // | taskloop | section | + |
2661 // | taskloop | single | + |
2662 // | taskloop | parallel for | * |
2663 // | taskloop |parallel for simd| * |
2664 // | taskloop |parallel sections| * |
2665 // | taskloop | task | * |
2666 // | taskloop | taskyield | * |
2667 // | taskloop | barrier | + |
2668 // | taskloop | taskwait | * |
2669 // | taskloop | taskgroup | * |
2670 // | taskloop | flush | * |
2671 // | taskloop | ordered | + |
2672 // | taskloop | atomic | * |
2673 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002674 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002675 // | taskloop | target parallel | * |
2676 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002677 // | taskloop | target enter | * |
2678 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002679 // | taskloop | target exit | * |
2680 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002681 // | taskloop | teams | + |
2682 // | taskloop | cancellation | |
2683 // | | point | |
2684 // | taskloop | cancel | |
2685 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002686 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002687 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002688 // | taskloop simd | parallel | |
2689 // | taskloop simd | for | |
2690 // | taskloop simd | for simd | |
2691 // | taskloop simd | master | |
2692 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002693 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002694 // | taskloop simd | sections | |
2695 // | taskloop simd | section | |
2696 // | taskloop simd | single | |
2697 // | taskloop simd | parallel for | |
2698 // | taskloop simd |parallel for simd| |
2699 // | taskloop simd |parallel sections| |
2700 // | taskloop simd | task | |
2701 // | taskloop simd | taskyield | |
2702 // | taskloop simd | barrier | |
2703 // | taskloop simd | taskwait | |
2704 // | taskloop simd | taskgroup | |
2705 // | taskloop simd | flush | |
2706 // | taskloop simd | ordered | + (with simd clause) |
2707 // | taskloop simd | atomic | |
2708 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002709 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002710 // | taskloop simd | target parallel | |
2711 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002712 // | taskloop simd | target enter | |
2713 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002714 // | taskloop simd | target exit | |
2715 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002716 // | taskloop simd | teams | |
2717 // | taskloop simd | cancellation | |
2718 // | | point | |
2719 // | taskloop simd | cancel | |
2720 // | taskloop simd | taskloop | |
2721 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002722 // | taskloop simd | distribute | |
2723 // +------------------+-----------------+------------------------------------+
2724 // | distribute | parallel | * |
2725 // | distribute | for | * |
2726 // | distribute | for simd | * |
2727 // | distribute | master | * |
2728 // | distribute | critical | * |
2729 // | distribute | simd | * |
2730 // | distribute | sections | * |
2731 // | distribute | section | * |
2732 // | distribute | single | * |
2733 // | distribute | parallel for | * |
2734 // | distribute |parallel for simd| * |
2735 // | distribute |parallel sections| * |
2736 // | distribute | task | * |
2737 // | distribute | taskyield | * |
2738 // | distribute | barrier | * |
2739 // | distribute | taskwait | * |
2740 // | distribute | taskgroup | * |
2741 // | distribute | flush | * |
2742 // | distribute | ordered | + |
2743 // | distribute | atomic | * |
2744 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002745 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002746 // | distribute | target parallel | |
2747 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002748 // | distribute | target enter | |
2749 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002750 // | distribute | target exit | |
2751 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002752 // | distribute | teams | |
2753 // | distribute | cancellation | + |
2754 // | | point | |
2755 // | distribute | cancel | + |
2756 // | distribute | taskloop | * |
2757 // | distribute | taskloop simd | * |
2758 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002759 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002760 if (Stack->getCurScope()) {
2761 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002762 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002763 bool NestingProhibited = false;
2764 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002765 enum {
2766 NoRecommend,
2767 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002768 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002769 ShouldBeInTargetRegion,
2770 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002771 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002772 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2773 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002774 // OpenMP [2.16, Nesting of Regions]
2775 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002776 // OpenMP [2.8.1,simd Construct, Restrictions]
2777 // An ordered construct with the simd clause is the only OpenMP construct
2778 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002779 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2780 return true;
2781 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002782 if (ParentRegion == OMPD_atomic) {
2783 // OpenMP [2.16, Nesting of Regions]
2784 // OpenMP constructs may not be nested inside an atomic region.
2785 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2786 return true;
2787 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002788 if (CurrentRegion == OMPD_section) {
2789 // OpenMP [2.7.2, sections Construct, Restrictions]
2790 // Orphaned section directives are prohibited. That is, the section
2791 // directives must appear within the sections construct and must not be
2792 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002793 if (ParentRegion != OMPD_sections &&
2794 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002795 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2796 << (ParentRegion != OMPD_unknown)
2797 << getOpenMPDirectiveName(ParentRegion);
2798 return true;
2799 }
2800 return false;
2801 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002802 // Allow some constructs to be orphaned (they could be used in functions,
2803 // called from OpenMP regions with the required preconditions).
2804 if (ParentRegion == OMPD_unknown)
2805 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002806 if (CurrentRegion == OMPD_cancellation_point ||
2807 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002808 // OpenMP [2.16, Nesting of Regions]
2809 // A cancellation point construct for which construct-type-clause is
2810 // taskgroup must be nested inside a task construct. A cancellation
2811 // point construct for which construct-type-clause is not taskgroup must
2812 // be closely nested inside an OpenMP construct that matches the type
2813 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002814 // A cancel construct for which construct-type-clause is taskgroup must be
2815 // nested inside a task construct. A cancel construct for which
2816 // construct-type-clause is not taskgroup must be closely nested inside an
2817 // OpenMP construct that matches the type specified in
2818 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002819 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002820 !((CancelRegion == OMPD_parallel &&
2821 (ParentRegion == OMPD_parallel ||
2822 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002823 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002824 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2825 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002826 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2827 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002828 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2829 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002830 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002831 // OpenMP [2.16, Nesting of Regions]
2832 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002833 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002834 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002835 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002836 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2837 // OpenMP [2.16, Nesting of Regions]
2838 // A critical region may not be nested (closely or otherwise) inside a
2839 // critical region with the same name. Note that this restriction is not
2840 // sufficient to prevent deadlock.
2841 SourceLocation PreviousCriticalLoc;
2842 bool DeadLock =
2843 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2844 OpenMPDirectiveKind K,
2845 const DeclarationNameInfo &DNI,
2846 SourceLocation Loc)
2847 ->bool {
2848 if (K == OMPD_critical &&
2849 DNI.getName() == CurrentName.getName()) {
2850 PreviousCriticalLoc = Loc;
2851 return true;
2852 } else
2853 return false;
2854 },
2855 false /* skip top directive */);
2856 if (DeadLock) {
2857 SemaRef.Diag(StartLoc,
2858 diag::err_omp_prohibited_region_critical_same_name)
2859 << CurrentName.getName();
2860 if (PreviousCriticalLoc.isValid())
2861 SemaRef.Diag(PreviousCriticalLoc,
2862 diag::note_omp_previous_critical_region);
2863 return true;
2864 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002865 } else if (CurrentRegion == OMPD_barrier) {
2866 // OpenMP [2.16, Nesting of Regions]
2867 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002868 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002869 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2870 isOpenMPTaskingDirective(ParentRegion) ||
2871 ParentRegion == OMPD_master ||
2872 ParentRegion == OMPD_critical ||
2873 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002874 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002875 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002876 // OpenMP [2.16, Nesting of Regions]
2877 // A worksharing region may not be closely nested inside a worksharing,
2878 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002879 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2880 isOpenMPTaskingDirective(ParentRegion) ||
2881 ParentRegion == OMPD_master ||
2882 ParentRegion == OMPD_critical ||
2883 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002884 Recommend = ShouldBeInParallelRegion;
2885 } else if (CurrentRegion == OMPD_ordered) {
2886 // OpenMP [2.16, Nesting of Regions]
2887 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002888 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002889 // An ordered region must be closely nested inside a loop region (or
2890 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002891 // OpenMP [2.8.1,simd Construct, Restrictions]
2892 // An ordered construct with the simd clause is the only OpenMP construct
2893 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002894 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002895 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002896 !(isOpenMPSimdDirective(ParentRegion) ||
2897 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002898 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002899 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2900 // OpenMP [2.16, Nesting of Regions]
2901 // If specified, a teams construct must be contained within a target
2902 // construct.
2903 NestingProhibited = ParentRegion != OMPD_target;
2904 Recommend = ShouldBeInTargetRegion;
2905 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2906 }
2907 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2908 // OpenMP [2.16, Nesting of Regions]
2909 // distribute, parallel, parallel sections, parallel workshare, and the
2910 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2911 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002912 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2913 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002914 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002915 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002916 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2917 // OpenMP 4.5 [2.17 Nesting of Regions]
2918 // The region associated with the distribute construct must be strictly
2919 // nested inside a teams region
2920 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2921 Recommend = ShouldBeInTeamsRegion;
2922 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002923 if (!NestingProhibited &&
2924 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2925 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2926 // OpenMP 4.5 [2.17 Nesting of Regions]
2927 // If a target, target update, target data, target enter data, or
2928 // target exit data construct is encountered during execution of a
2929 // target region, the behavior is unspecified.
2930 NestingProhibited = Stack->hasDirective(
2931 [&OffendingRegion](OpenMPDirectiveKind K,
2932 const DeclarationNameInfo &DNI,
2933 SourceLocation Loc) -> bool {
2934 if (isOpenMPTargetExecutionDirective(K)) {
2935 OffendingRegion = K;
2936 return true;
2937 } else
2938 return false;
2939 },
2940 false /* don't skip top directive */);
2941 CloseNesting = false;
2942 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002943 if (NestingProhibited) {
2944 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002945 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2946 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002947 return true;
2948 }
2949 }
2950 return false;
2951}
2952
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002953static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2954 ArrayRef<OMPClause *> Clauses,
2955 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2956 bool ErrorFound = false;
2957 unsigned NamedModifiersNumber = 0;
2958 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2959 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002960 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002961 for (const auto *C : Clauses) {
2962 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2963 // At most one if clause without a directive-name-modifier can appear on
2964 // the directive.
2965 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2966 if (FoundNameModifiers[CurNM]) {
2967 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2968 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2969 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2970 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002971 } else if (CurNM != OMPD_unknown) {
2972 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002973 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002974 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002975 FoundNameModifiers[CurNM] = IC;
2976 if (CurNM == OMPD_unknown)
2977 continue;
2978 // Check if the specified name modifier is allowed for the current
2979 // directive.
2980 // At most one if clause with the particular directive-name-modifier can
2981 // appear on the directive.
2982 bool MatchFound = false;
2983 for (auto NM : AllowedNameModifiers) {
2984 if (CurNM == NM) {
2985 MatchFound = true;
2986 break;
2987 }
2988 }
2989 if (!MatchFound) {
2990 S.Diag(IC->getNameModifierLoc(),
2991 diag::err_omp_wrong_if_directive_name_modifier)
2992 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2993 ErrorFound = true;
2994 }
2995 }
2996 }
2997 // If any if clause on the directive includes a directive-name-modifier then
2998 // all if clauses on the directive must include a directive-name-modifier.
2999 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3000 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3001 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3002 diag::err_omp_no_more_if_clause);
3003 } else {
3004 std::string Values;
3005 std::string Sep(", ");
3006 unsigned AllowedCnt = 0;
3007 unsigned TotalAllowedNum =
3008 AllowedNameModifiers.size() - NamedModifiersNumber;
3009 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3010 ++Cnt) {
3011 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3012 if (!FoundNameModifiers[NM]) {
3013 Values += "'";
3014 Values += getOpenMPDirectiveName(NM);
3015 Values += "'";
3016 if (AllowedCnt + 2 == TotalAllowedNum)
3017 Values += " or ";
3018 else if (AllowedCnt + 1 != TotalAllowedNum)
3019 Values += Sep;
3020 ++AllowedCnt;
3021 }
3022 }
3023 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3024 diag::err_omp_unnamed_if_clause)
3025 << (TotalAllowedNum > 1) << Values;
3026 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00003027 for (auto Loc : NameModifierLoc) {
3028 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3029 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003030 ErrorFound = true;
3031 }
3032 return ErrorFound;
3033}
3034
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003035StmtResult Sema::ActOnOpenMPExecutableDirective(
3036 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3037 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3038 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003039 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003040 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3041 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003042 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003043
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003044 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003045 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003046 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003047 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003048 if (AStmt) {
3049 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3050
3051 // Check default data sharing attributes for referenced variables.
3052 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3053 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3054 if (DSAChecker.isErrorFound())
3055 return StmtError();
3056 // Generate list of implicitly defined firstprivate variables.
3057 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003058
3059 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3060 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3061 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3062 SourceLocation(), SourceLocation())) {
3063 ClausesWithImplicit.push_back(Implicit);
3064 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3065 DSAChecker.getImplicitFirstprivate().size();
3066 } else
3067 ErrorFound = true;
3068 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003069 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003070
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003071 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003072 switch (Kind) {
3073 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003074 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3075 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003076 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003077 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003078 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003079 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3080 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003081 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003082 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003083 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3084 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003085 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003086 case OMPD_for_simd:
3087 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3088 EndLoc, VarsWithInheritedDSA);
3089 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003090 case OMPD_sections:
3091 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3092 EndLoc);
3093 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003094 case OMPD_section:
3095 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003096 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003097 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3098 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003099 case OMPD_single:
3100 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3101 EndLoc);
3102 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003103 case OMPD_master:
3104 assert(ClausesWithImplicit.empty() &&
3105 "No clauses are allowed for 'omp master' directive");
3106 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3107 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003108 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003109 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3110 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003111 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003112 case OMPD_parallel_for:
3113 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3114 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003115 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003116 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003117 case OMPD_parallel_for_simd:
3118 Res = ActOnOpenMPParallelForSimdDirective(
3119 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003120 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003121 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003122 case OMPD_parallel_sections:
3123 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3124 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003125 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003126 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003127 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003128 Res =
3129 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003130 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003131 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003132 case OMPD_taskyield:
3133 assert(ClausesWithImplicit.empty() &&
3134 "No clauses are allowed for 'omp taskyield' directive");
3135 assert(AStmt == nullptr &&
3136 "No associated statement allowed for 'omp taskyield' directive");
3137 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3138 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003139 case OMPD_barrier:
3140 assert(ClausesWithImplicit.empty() &&
3141 "No clauses are allowed for 'omp barrier' directive");
3142 assert(AStmt == nullptr &&
3143 "No associated statement allowed for 'omp barrier' directive");
3144 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3145 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003146 case OMPD_taskwait:
3147 assert(ClausesWithImplicit.empty() &&
3148 "No clauses are allowed for 'omp taskwait' directive");
3149 assert(AStmt == nullptr &&
3150 "No associated statement allowed for 'omp taskwait' directive");
3151 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3152 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003153 case OMPD_taskgroup:
3154 assert(ClausesWithImplicit.empty() &&
3155 "No clauses are allowed for 'omp taskgroup' directive");
3156 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3157 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003158 case OMPD_flush:
3159 assert(AStmt == nullptr &&
3160 "No associated statement allowed for 'omp flush' directive");
3161 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3162 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003163 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003164 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3165 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003166 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003167 case OMPD_atomic:
3168 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3169 EndLoc);
3170 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003171 case OMPD_teams:
3172 Res =
3173 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3174 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003175 case OMPD_target:
3176 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3177 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003178 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003179 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003180 case OMPD_target_parallel:
3181 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3182 StartLoc, EndLoc);
3183 AllowedNameModifiers.push_back(OMPD_target);
3184 AllowedNameModifiers.push_back(OMPD_parallel);
3185 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003186 case OMPD_target_parallel_for:
3187 Res = ActOnOpenMPTargetParallelForDirective(
3188 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3189 AllowedNameModifiers.push_back(OMPD_target);
3190 AllowedNameModifiers.push_back(OMPD_parallel);
3191 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003192 case OMPD_cancellation_point:
3193 assert(ClausesWithImplicit.empty() &&
3194 "No clauses are allowed for 'omp cancellation point' directive");
3195 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3196 "cancellation point' directive");
3197 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3198 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003199 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003200 assert(AStmt == nullptr &&
3201 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003202 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3203 CancelRegion);
3204 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003205 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003206 case OMPD_target_data:
3207 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3208 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003209 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003210 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003211 case OMPD_target_enter_data:
3212 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3213 EndLoc);
3214 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3215 break;
Samuel Antao72590762016-01-19 20:04:50 +00003216 case OMPD_target_exit_data:
3217 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3218 EndLoc);
3219 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3220 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003221 case OMPD_taskloop:
3222 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3223 EndLoc, VarsWithInheritedDSA);
3224 AllowedNameModifiers.push_back(OMPD_taskloop);
3225 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003226 case OMPD_taskloop_simd:
3227 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3228 EndLoc, VarsWithInheritedDSA);
3229 AllowedNameModifiers.push_back(OMPD_taskloop);
3230 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003231 case OMPD_distribute:
3232 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3233 EndLoc, VarsWithInheritedDSA);
3234 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003235 case OMPD_declare_target:
3236 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003237 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003238 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003239 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003240 llvm_unreachable("OpenMP Directive is not allowed");
3241 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003242 llvm_unreachable("Unknown OpenMP directive");
3243 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003244
Alexey Bataev4acb8592014-07-07 13:01:15 +00003245 for (auto P : VarsWithInheritedDSA) {
3246 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3247 << P.first << P.second->getSourceRange();
3248 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003249 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3250
3251 if (!AllowedNameModifiers.empty())
3252 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3253 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003254
Alexey Bataeved09d242014-05-28 05:53:51 +00003255 if (ErrorFound)
3256 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003257 return Res;
3258}
3259
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003260Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3261 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003262 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003263 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3264 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003265 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003266 assert(Linears.size() == LinModifiers.size());
3267 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003268 if (!DG || DG.get().isNull())
3269 return DeclGroupPtrTy();
3270
3271 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003272 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003273 return DG;
3274 }
3275 auto *ADecl = DG.get().getSingleDecl();
3276 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3277 ADecl = FTD->getTemplatedDecl();
3278
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003279 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3280 if (!FD) {
3281 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003282 return DeclGroupPtrTy();
3283 }
3284
Alexey Bataev2af33e32016-04-07 12:45:37 +00003285 // OpenMP [2.8.2, declare simd construct, Description]
3286 // The parameter of the simdlen clause must be a constant positive integer
3287 // expression.
3288 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003289 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003290 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003291 // OpenMP [2.8.2, declare simd construct, Description]
3292 // The special this pointer can be used as if was one of the arguments to the
3293 // function in any of the linear, aligned, or uniform clauses.
3294 // The uniform clause declares one or more arguments to have an invariant
3295 // value for all concurrent invocations of the function in the execution of a
3296 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003297 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3298 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003299 for (auto *E : Uniforms) {
3300 E = E->IgnoreParenImpCasts();
3301 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3302 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3303 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3304 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003305 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3306 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003307 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003308 }
3309 if (isa<CXXThisExpr>(E)) {
3310 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003311 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003312 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003313 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3314 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003315 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003316 // OpenMP [2.8.2, declare simd construct, Description]
3317 // The aligned clause declares that the object to which each list item points
3318 // is aligned to the number of bytes expressed in the optional parameter of
3319 // the aligned clause.
3320 // The special this pointer can be used as if was one of the arguments to the
3321 // function in any of the linear, aligned, or uniform clauses.
3322 // The type of list items appearing in the aligned clause must be array,
3323 // pointer, reference to array, or reference to pointer.
3324 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3325 Expr *AlignedThis = nullptr;
3326 for (auto *E : Aligneds) {
3327 E = E->IgnoreParenImpCasts();
3328 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3329 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3330 auto *CanonPVD = PVD->getCanonicalDecl();
3331 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3332 FD->getParamDecl(PVD->getFunctionScopeIndex())
3333 ->getCanonicalDecl() == CanonPVD) {
3334 // OpenMP [2.8.1, simd construct, Restrictions]
3335 // A list-item cannot appear in more than one aligned clause.
3336 if (AlignedArgs.count(CanonPVD) > 0) {
3337 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3338 << 1 << E->getSourceRange();
3339 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3340 diag::note_omp_explicit_dsa)
3341 << getOpenMPClauseName(OMPC_aligned);
3342 continue;
3343 }
3344 AlignedArgs[CanonPVD] = E;
3345 QualType QTy = PVD->getType()
3346 .getNonReferenceType()
3347 .getUnqualifiedType()
3348 .getCanonicalType();
3349 const Type *Ty = QTy.getTypePtrOrNull();
3350 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3351 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3352 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3353 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3354 }
3355 continue;
3356 }
3357 }
3358 if (isa<CXXThisExpr>(E)) {
3359 if (AlignedThis) {
3360 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3361 << 2 << E->getSourceRange();
3362 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3363 << getOpenMPClauseName(OMPC_aligned);
3364 }
3365 AlignedThis = E;
3366 continue;
3367 }
3368 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3369 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3370 }
3371 // The optional parameter of the aligned clause, alignment, must be a constant
3372 // positive integer expression. If no optional parameter is specified,
3373 // implementation-defined default alignments for SIMD instructions on the
3374 // target platforms are assumed.
3375 SmallVector<Expr *, 4> NewAligns;
3376 for (auto *E : Alignments) {
3377 ExprResult Align;
3378 if (E)
3379 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3380 NewAligns.push_back(Align.get());
3381 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003382 // OpenMP [2.8.2, declare simd construct, Description]
3383 // The linear clause declares one or more list items to be private to a SIMD
3384 // lane and to have a linear relationship with respect to the iteration space
3385 // of a loop.
3386 // The special this pointer can be used as if was one of the arguments to the
3387 // function in any of the linear, aligned, or uniform clauses.
3388 // When a linear-step expression is specified in a linear clause it must be
3389 // either a constant integer expression or an integer-typed parameter that is
3390 // specified in a uniform clause on the directive.
3391 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3392 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3393 auto MI = LinModifiers.begin();
3394 for (auto *E : Linears) {
3395 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3396 ++MI;
3397 E = E->IgnoreParenImpCasts();
3398 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3399 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3400 auto *CanonPVD = PVD->getCanonicalDecl();
3401 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3402 FD->getParamDecl(PVD->getFunctionScopeIndex())
3403 ->getCanonicalDecl() == CanonPVD) {
3404 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3405 // A list-item cannot appear in more than one linear clause.
3406 if (LinearArgs.count(CanonPVD) > 0) {
3407 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3408 << getOpenMPClauseName(OMPC_linear)
3409 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3410 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3411 diag::note_omp_explicit_dsa)
3412 << getOpenMPClauseName(OMPC_linear);
3413 continue;
3414 }
3415 // Each argument can appear in at most one uniform or linear clause.
3416 if (UniformedArgs.count(CanonPVD) > 0) {
3417 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3418 << getOpenMPClauseName(OMPC_linear)
3419 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3420 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3421 diag::note_omp_explicit_dsa)
3422 << getOpenMPClauseName(OMPC_uniform);
3423 continue;
3424 }
3425 LinearArgs[CanonPVD] = E;
3426 if (E->isValueDependent() || E->isTypeDependent() ||
3427 E->isInstantiationDependent() ||
3428 E->containsUnexpandedParameterPack())
3429 continue;
3430 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3431 PVD->getOriginalType());
3432 continue;
3433 }
3434 }
3435 if (isa<CXXThisExpr>(E)) {
3436 if (UniformedLinearThis) {
3437 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3438 << getOpenMPClauseName(OMPC_linear)
3439 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3440 << E->getSourceRange();
3441 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3442 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3443 : OMPC_linear);
3444 continue;
3445 }
3446 UniformedLinearThis = E;
3447 if (E->isValueDependent() || E->isTypeDependent() ||
3448 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3449 continue;
3450 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3451 E->getType());
3452 continue;
3453 }
3454 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3455 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3456 }
3457 Expr *Step = nullptr;
3458 Expr *NewStep = nullptr;
3459 SmallVector<Expr *, 4> NewSteps;
3460 for (auto *E : Steps) {
3461 // Skip the same step expression, it was checked already.
3462 if (Step == E || !E) {
3463 NewSteps.push_back(E ? NewStep : nullptr);
3464 continue;
3465 }
3466 Step = E;
3467 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3468 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3469 auto *CanonPVD = PVD->getCanonicalDecl();
3470 if (UniformedArgs.count(CanonPVD) == 0) {
3471 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3472 << Step->getSourceRange();
3473 } else if (E->isValueDependent() || E->isTypeDependent() ||
3474 E->isInstantiationDependent() ||
3475 E->containsUnexpandedParameterPack() ||
3476 CanonPVD->getType()->hasIntegerRepresentation())
3477 NewSteps.push_back(Step);
3478 else {
3479 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3480 << Step->getSourceRange();
3481 }
3482 continue;
3483 }
3484 NewStep = Step;
3485 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3486 !Step->isInstantiationDependent() &&
3487 !Step->containsUnexpandedParameterPack()) {
3488 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3489 .get();
3490 if (NewStep)
3491 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3492 }
3493 NewSteps.push_back(NewStep);
3494 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003495 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3496 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003497 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003498 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3499 const_cast<Expr **>(Linears.data()), Linears.size(),
3500 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3501 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003502 ADecl->addAttr(NewAttr);
3503 return ConvertDeclToDeclGroup(ADecl);
3504}
3505
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003506StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3507 Stmt *AStmt,
3508 SourceLocation StartLoc,
3509 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003510 if (!AStmt)
3511 return StmtError();
3512
Alexey Bataev9959db52014-05-06 10:08:46 +00003513 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3514 // 1.2.2 OpenMP Language Terminology
3515 // Structured block - An executable statement with a single entry at the
3516 // top and a single exit at the bottom.
3517 // The point of exit cannot be a branch out of the structured block.
3518 // longjmp() and throw() must not violate the entry/exit criteria.
3519 CS->getCapturedDecl()->setNothrow();
3520
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003521 getCurFunction()->setHasBranchProtectedScope();
3522
Alexey Bataev25e5b442015-09-15 12:52:43 +00003523 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3524 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003525}
3526
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003527namespace {
3528/// \brief Helper class for checking canonical form of the OpenMP loops and
3529/// extracting iteration space of each loop in the loop nest, that will be used
3530/// for IR generation.
3531class OpenMPIterationSpaceChecker {
3532 /// \brief Reference to Sema.
3533 Sema &SemaRef;
3534 /// \brief A location for diagnostics (when there is no some better location).
3535 SourceLocation DefaultLoc;
3536 /// \brief A location for diagnostics (when increment is not compatible).
3537 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003538 /// \brief A source location for referring to loop init later.
3539 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003540 /// \brief A source location for referring to condition later.
3541 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003542 /// \brief A source location for referring to increment later.
3543 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003544 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003545 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003546 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003547 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003548 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003549 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003550 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003551 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003552 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003553 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003554 /// \brief This flag is true when condition is one of:
3555 /// Var < UB
3556 /// Var <= UB
3557 /// UB > Var
3558 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003559 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003560 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003561 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003562 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003563 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003564
3565public:
3566 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003567 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003568 /// \brief Check init-expr for canonical loop form and save loop counter
3569 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003570 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003571 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3572 /// for less/greater and for strict/non-strict comparison.
3573 bool CheckCond(Expr *S);
3574 /// \brief Check incr-expr for canonical loop form and return true if it
3575 /// does not conform, otherwise save loop step (#Step).
3576 bool CheckInc(Expr *S);
3577 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003578 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003579 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003580 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003581 /// \brief Source range of the loop init.
3582 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3583 /// \brief Source range of the loop condition.
3584 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3585 /// \brief Source range of the loop increment.
3586 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3587 /// \brief True if the step should be subtracted.
3588 bool ShouldSubtractStep() const { return SubtractStep; }
3589 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003590 Expr *
3591 BuildNumIterations(Scope *S, const bool LimitedType,
3592 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003593 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003594 Expr *BuildPreCond(Scope *S, Expr *Cond,
3595 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003596 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003597 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3598 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003599 /// \brief Build reference expression to the private counter be used for
3600 /// codegen.
3601 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003602 /// \brief Build initization of the counter be used for codegen.
3603 Expr *BuildCounterInit() const;
3604 /// \brief Build step of the counter be used for codegen.
3605 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003606 /// \brief Return true if any expression is dependent.
3607 bool Dependent() const;
3608
3609private:
3610 /// \brief Check the right-hand side of an assignment in the increment
3611 /// expression.
3612 bool CheckIncRHS(Expr *RHS);
3613 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003614 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003616 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003617 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003618 /// \brief Helper to set loop increment.
3619 bool SetStep(Expr *NewStep, bool Subtract);
3620};
3621
3622bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003623 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003624 assert(!LB && !UB && !Step);
3625 return false;
3626 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003627 return LCDecl->getType()->isDependentType() ||
3628 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3629 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003630}
3631
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003632static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003633 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3634 E = ExprTemp->getSubExpr();
3635
3636 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3637 E = MTE->GetTemporaryExpr();
3638
3639 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3640 E = Binder->getSubExpr();
3641
3642 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3643 E = ICE->getSubExprAsWritten();
3644 return E->IgnoreParens();
3645}
3646
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003647bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3648 Expr *NewLCRefExpr,
3649 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003650 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003651 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003652 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003653 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003654 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003655 LCDecl = getCanonicalDecl(NewLCDecl);
3656 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003657 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3658 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003659 if ((Ctor->isCopyOrMoveConstructor() ||
3660 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3661 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003662 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003663 LB = NewLB;
3664 return false;
3665}
3666
3667bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003668 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003669 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003670 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3671 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003672 if (!NewUB)
3673 return true;
3674 UB = NewUB;
3675 TestIsLessOp = LessOp;
3676 TestIsStrictOp = StrictOp;
3677 ConditionSrcRange = SR;
3678 ConditionLoc = SL;
3679 return false;
3680}
3681
3682bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3683 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003684 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003685 if (!NewStep)
3686 return true;
3687 if (!NewStep->isValueDependent()) {
3688 // Check that the step is integer expression.
3689 SourceLocation StepLoc = NewStep->getLocStart();
3690 ExprResult Val =
3691 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3692 if (Val.isInvalid())
3693 return true;
3694 NewStep = Val.get();
3695
3696 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3697 // If test-expr is of form var relational-op b and relational-op is < or
3698 // <= then incr-expr must cause var to increase on each iteration of the
3699 // loop. If test-expr is of form var relational-op b and relational-op is
3700 // > or >= then incr-expr must cause var to decrease on each iteration of
3701 // the loop.
3702 // If test-expr is of form b relational-op var and relational-op is < or
3703 // <= then incr-expr must cause var to decrease on each iteration of the
3704 // loop. If test-expr is of form b relational-op var and relational-op is
3705 // > or >= then incr-expr must cause var to increase on each iteration of
3706 // the loop.
3707 llvm::APSInt Result;
3708 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3709 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3710 bool IsConstNeg =
3711 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003712 bool IsConstPos =
3713 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003714 bool IsConstZero = IsConstant && !Result.getBoolValue();
3715 if (UB && (IsConstZero ||
3716 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003717 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003718 SemaRef.Diag(NewStep->getExprLoc(),
3719 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003720 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003721 SemaRef.Diag(ConditionLoc,
3722 diag::note_omp_loop_cond_requres_compatible_incr)
3723 << TestIsLessOp << ConditionSrcRange;
3724 return true;
3725 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003726 if (TestIsLessOp == Subtract) {
3727 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3728 NewStep).get();
3729 Subtract = !Subtract;
3730 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003731 }
3732
3733 Step = NewStep;
3734 SubtractStep = Subtract;
3735 return false;
3736}
3737
Alexey Bataev9c821032015-04-30 04:23:23 +00003738bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003739 // Check init-expr for canonical loop form and save loop counter
3740 // variable - #Var and its initialization value - #LB.
3741 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3742 // var = lb
3743 // integer-type var = lb
3744 // random-access-iterator-type var = lb
3745 // pointer-type var = lb
3746 //
3747 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003748 if (EmitDiags) {
3749 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3750 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003751 return true;
3752 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003753 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003754 if (Expr *E = dyn_cast<Expr>(S))
3755 S = E->IgnoreParens();
3756 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003757 if (BO->getOpcode() == BO_Assign) {
3758 auto *LHS = BO->getLHS()->IgnoreParens();
3759 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3760 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3761 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3762 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3763 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3764 }
3765 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3766 if (ME->isArrow() &&
3767 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3768 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3769 }
3770 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003771 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3772 if (DS->isSingleDecl()) {
3773 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003774 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003775 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003776 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003777 SemaRef.Diag(S->getLocStart(),
3778 diag::ext_omp_loop_not_canonical_init)
3779 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003780 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 }
3782 }
3783 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003784 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3785 if (CE->getOperator() == OO_Equal) {
3786 auto *LHS = CE->getArg(0);
3787 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3788 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3789 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3790 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3791 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3792 }
3793 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3794 if (ME->isArrow() &&
3795 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3796 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3797 }
3798 }
3799 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003801 if (Dependent() || SemaRef.CurContext->isDependentContext())
3802 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003803 if (EmitDiags) {
3804 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3805 << S->getSourceRange();
3806 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003807 return true;
3808}
3809
Alexey Bataev23b69422014-06-18 07:08:49 +00003810/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003811/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003812static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003813 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003814 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003815 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003816 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3817 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003818 if ((Ctor->isCopyOrMoveConstructor() ||
3819 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3820 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003821 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003822 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3823 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3824 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3825 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3826 return getCanonicalDecl(ME->getMemberDecl());
3827 return getCanonicalDecl(VD);
3828 }
3829 }
3830 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3831 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3832 return getCanonicalDecl(ME->getMemberDecl());
3833 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003834}
3835
3836bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3837 // Check test-expr for canonical form, save upper-bound UB, flags for
3838 // less/greater and for strict/non-strict comparison.
3839 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3840 // var relational-op b
3841 // b relational-op var
3842 //
3843 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003844 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003845 return true;
3846 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003847 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003848 SourceLocation CondLoc = S->getLocStart();
3849 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3850 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003851 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003852 return SetUB(BO->getRHS(),
3853 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3854 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3855 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003856 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003857 return SetUB(BO->getLHS(),
3858 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3859 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3860 BO->getSourceRange(), BO->getOperatorLoc());
3861 }
3862 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3863 if (CE->getNumArgs() == 2) {
3864 auto Op = CE->getOperator();
3865 switch (Op) {
3866 case OO_Greater:
3867 case OO_GreaterEqual:
3868 case OO_Less:
3869 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003870 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3872 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3873 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003874 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003875 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3876 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3877 CE->getOperatorLoc());
3878 break;
3879 default:
3880 break;
3881 }
3882 }
3883 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003884 if (Dependent() || SemaRef.CurContext->isDependentContext())
3885 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003886 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003887 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003888 return true;
3889}
3890
3891bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3892 // RHS of canonical loop form increment can be:
3893 // var + incr
3894 // incr + var
3895 // var - incr
3896 //
3897 RHS = RHS->IgnoreParenImpCasts();
3898 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3899 if (BO->isAdditiveOp()) {
3900 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003901 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003902 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003903 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003904 return SetStep(BO->getLHS(), false);
3905 }
3906 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3907 bool IsAdd = CE->getOperator() == OO_Plus;
3908 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003911 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 return SetStep(CE->getArg(0), false);
3913 }
3914 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003915 if (Dependent() || SemaRef.CurContext->isDependentContext())
3916 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003917 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003918 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919 return true;
3920}
3921
3922bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3923 // Check incr-expr for canonical loop form and return true if it
3924 // does not conform.
3925 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3926 // ++var
3927 // var++
3928 // --var
3929 // var--
3930 // var += incr
3931 // var -= incr
3932 // var = var + incr
3933 // var = incr + var
3934 // var = var - incr
3935 //
3936 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 return true;
3939 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003940 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003941 S = S->IgnoreParens();
3942 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003943 if (UO->isIncrementDecrementOp() &&
3944 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003945 return SetStep(
3946 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3947 (UO->isDecrementOp() ? -1 : 1)).get(),
3948 false);
3949 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3950 switch (BO->getOpcode()) {
3951 case BO_AddAssign:
3952 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003953 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003954 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3955 break;
3956 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003957 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003958 return CheckIncRHS(BO->getRHS());
3959 break;
3960 default:
3961 break;
3962 }
3963 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3964 switch (CE->getOperator()) {
3965 case OO_PlusPlus:
3966 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003967 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003968 return SetStep(
3969 SemaRef.ActOnIntegerConstant(
3970 CE->getLocStart(),
3971 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3972 false);
3973 break;
3974 case OO_PlusEqual:
3975 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003976 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3978 break;
3979 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003980 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003981 return CheckIncRHS(CE->getArg(1));
3982 break;
3983 default:
3984 break;
3985 }
3986 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003987 if (Dependent() || SemaRef.CurContext->isDependentContext())
3988 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003989 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003990 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003991 return true;
3992}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003993
Alexey Bataev5a3af132016-03-29 08:58:54 +00003994static ExprResult
3995tryBuildCapture(Sema &SemaRef, Expr *Capture,
3996 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3997 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3998 return SemaRef.PerformImplicitConversion(
3999 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4000 /*AllowExplicit=*/true);
4001 auto I = Captures.find(Capture);
4002 if (I != Captures.end())
4003 return buildCapture(SemaRef, Capture, I->second);
4004 DeclRefExpr *Ref = nullptr;
4005 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4006 Captures[Capture] = Ref;
4007 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004008}
4009
Alexander Musmana5f070a2014-10-01 06:03:56 +00004010/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004011Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4012 Scope *S, const bool LimitedType,
4013 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004014 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004015 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004016 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004017 SemaRef.getLangOpts().CPlusPlus) {
4018 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004019 auto *UBExpr = TestIsLessOp ? UB : LB;
4020 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004021 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4022 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004023 if (!Upper || !Lower)
4024 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004025
4026 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4027
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004028 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004029 // BuildBinOp already emitted error, this one is to point user to upper
4030 // and lower bound, and to tell what is passed to 'operator-'.
4031 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4032 << Upper->getSourceRange() << Lower->getSourceRange();
4033 return nullptr;
4034 }
4035 }
4036
4037 if (!Diff.isUsable())
4038 return nullptr;
4039
4040 // Upper - Lower [- 1]
4041 if (TestIsStrictOp)
4042 Diff = SemaRef.BuildBinOp(
4043 S, DefaultLoc, BO_Sub, Diff.get(),
4044 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4045 if (!Diff.isUsable())
4046 return nullptr;
4047
4048 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004049 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4050 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004051 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004052 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004053 if (!Diff.isUsable())
4054 return nullptr;
4055
4056 // Parentheses (for dumping/debugging purposes only).
4057 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4058 if (!Diff.isUsable())
4059 return nullptr;
4060
4061 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004062 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004063 if (!Diff.isUsable())
4064 return nullptr;
4065
Alexander Musman174b3ca2014-10-06 11:16:29 +00004066 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004067 QualType Type = Diff.get()->getType();
4068 auto &C = SemaRef.Context;
4069 bool UseVarType = VarType->hasIntegerRepresentation() &&
4070 C.getTypeSize(Type) > C.getTypeSize(VarType);
4071 if (!Type->isIntegerType() || UseVarType) {
4072 unsigned NewSize =
4073 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4074 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4075 : Type->hasSignedIntegerRepresentation();
4076 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004077 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4078 Diff = SemaRef.PerformImplicitConversion(
4079 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4080 if (!Diff.isUsable())
4081 return nullptr;
4082 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004083 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004084 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004085 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4086 if (NewSize != C.getTypeSize(Type)) {
4087 if (NewSize < C.getTypeSize(Type)) {
4088 assert(NewSize == 64 && "incorrect loop var size");
4089 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4090 << InitSrcRange << ConditionSrcRange;
4091 }
4092 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004093 NewSize, Type->hasSignedIntegerRepresentation() ||
4094 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004095 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4096 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4097 Sema::AA_Converting, true);
4098 if (!Diff.isUsable())
4099 return nullptr;
4100 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004101 }
4102 }
4103
Alexander Musmana5f070a2014-10-01 06:03:56 +00004104 return Diff.get();
4105}
4106
Alexey Bataev5a3af132016-03-29 08:58:54 +00004107Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4108 Scope *S, Expr *Cond,
4109 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004110 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4111 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4112 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004113
Alexey Bataev5a3af132016-03-29 08:58:54 +00004114 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4115 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4116 if (!NewLB.isUsable() || !NewUB.isUsable())
4117 return nullptr;
4118
Alexey Bataev62dbb972015-04-22 11:59:37 +00004119 auto CondExpr = SemaRef.BuildBinOp(
4120 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4121 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004122 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004123 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004124 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4125 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004126 CondExpr = SemaRef.PerformImplicitConversion(
4127 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4128 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004129 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004130 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4131 // Otherwise use original loop conditon and evaluate it in runtime.
4132 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4133}
4134
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004136DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004137 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004138 auto *VD = dyn_cast<VarDecl>(LCDecl);
4139 if (!VD) {
4140 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4141 auto *Ref = buildDeclRefExpr(
4142 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004143 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4144 // If the loop control decl is explicitly marked as private, do not mark it
4145 // as captured again.
4146 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4147 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004148 return Ref;
4149 }
4150 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004151 DefaultLoc);
4152}
4153
4154Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004155 if (LCDecl && !LCDecl->isInvalidDecl()) {
4156 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004157 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004158 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4159 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004160 if (PrivateVar->isInvalidDecl())
4161 return nullptr;
4162 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4163 }
4164 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004165}
4166
4167/// \brief Build initization of the counter be used for codegen.
4168Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4169
4170/// \brief Build step of the counter be used for codegen.
4171Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4172
4173/// \brief Iteration space of a single for loop.
4174struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004175 /// \brief Condition of the loop.
4176 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004177 /// \brief This expression calculates the number of iterations in the loop.
4178 /// It is always possible to calculate it before starting the loop.
4179 Expr *NumIterations;
4180 /// \brief The loop counter variable.
4181 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004182 /// \brief Private loop counter variable.
4183 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004184 /// \brief This is initializer for the initial value of #CounterVar.
4185 Expr *CounterInit;
4186 /// \brief This is step for the #CounterVar used to generate its update:
4187 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4188 Expr *CounterStep;
4189 /// \brief Should step be subtracted?
4190 bool Subtract;
4191 /// \brief Source range of the loop init.
4192 SourceRange InitSrcRange;
4193 /// \brief Source range of the loop condition.
4194 SourceRange CondSrcRange;
4195 /// \brief Source range of the loop increment.
4196 SourceRange IncSrcRange;
4197};
4198
Alexey Bataev23b69422014-06-18 07:08:49 +00004199} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200
Alexey Bataev9c821032015-04-30 04:23:23 +00004201void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4202 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4203 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004204 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4205 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004206 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4207 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004208 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4209 if (auto *D = ISC.GetLoopDecl()) {
4210 auto *VD = dyn_cast<VarDecl>(D);
4211 if (!VD) {
4212 if (auto *Private = IsOpenMPCapturedDecl(D))
4213 VD = Private;
4214 else {
4215 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4216 /*WithInit=*/false);
4217 VD = cast<VarDecl>(Ref->getDecl());
4218 }
4219 }
4220 DSAStack->addLoopControlVariable(D, VD);
4221 }
4222 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004223 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004224 }
4225}
4226
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004227/// \brief Called on a for stmt to check and extract its iteration space
4228/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004229static bool CheckOpenMPIterationSpace(
4230 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4231 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004232 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004233 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004234 LoopIterationSpace &ResultIterSpace,
4235 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004236 // OpenMP [2.6, Canonical Loop Form]
4237 // for (init-expr; test-expr; incr-expr) structured-block
4238 auto For = dyn_cast_or_null<ForStmt>(S);
4239 if (!For) {
4240 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004241 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4242 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4243 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4244 if (NestedLoopCount > 1) {
4245 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4246 SemaRef.Diag(DSA.getConstructLoc(),
4247 diag::note_omp_collapse_ordered_expr)
4248 << 2 << CollapseLoopCountExpr->getSourceRange()
4249 << OrderedLoopCountExpr->getSourceRange();
4250 else if (CollapseLoopCountExpr)
4251 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4252 diag::note_omp_collapse_ordered_expr)
4253 << 0 << CollapseLoopCountExpr->getSourceRange();
4254 else
4255 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4256 diag::note_omp_collapse_ordered_expr)
4257 << 1 << OrderedLoopCountExpr->getSourceRange();
4258 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004259 return true;
4260 }
4261 assert(For->getBody());
4262
4263 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4264
4265 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004266 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004267 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269
4270 bool HasErrors = false;
4271
4272 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004273 if (auto *LCDecl = ISC.GetLoopDecl()) {
4274 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004275
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004276 // OpenMP [2.6, Canonical Loop Form]
4277 // Var is one of the following:
4278 // A variable of signed or unsigned integer type.
4279 // For C++, a variable of a random access iterator type.
4280 // For C, a variable of a pointer type.
4281 auto VarType = LCDecl->getType().getNonReferenceType();
4282 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4283 !VarType->isPointerType() &&
4284 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4285 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4286 << SemaRef.getLangOpts().CPlusPlus;
4287 HasErrors = true;
4288 }
4289
4290 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4291 // a Construct
4292 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4293 // parallel for construct is (are) private.
4294 // The loop iteration variable in the associated for-loop of a simd
4295 // construct with just one associated for-loop is linear with a
4296 // constant-linear-step that is the increment of the associated for-loop.
4297 // Exclude loop var from the list of variables with implicitly defined data
4298 // sharing attributes.
4299 VarsWithImplicitDSA.erase(LCDecl);
4300
4301 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4302 // in a Construct, C/C++].
4303 // The loop iteration variable in the associated for-loop of a simd
4304 // construct with just one associated for-loop may be listed in a linear
4305 // clause with a constant-linear-step that is the increment of the
4306 // associated for-loop.
4307 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4308 // parallel for construct may be listed in a private or lastprivate clause.
4309 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4310 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4311 // declared in the loop and it is predetermined as a private.
4312 auto PredeterminedCKind =
4313 isOpenMPSimdDirective(DKind)
4314 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4315 : OMPC_private;
4316 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4317 DVar.CKind != PredeterminedCKind) ||
4318 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4319 isOpenMPDistributeDirective(DKind)) &&
4320 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4321 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4322 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4323 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4324 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4325 << getOpenMPClauseName(PredeterminedCKind);
4326 if (DVar.RefExpr == nullptr)
4327 DVar.CKind = PredeterminedCKind;
4328 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4329 HasErrors = true;
4330 } else if (LoopDeclRefExpr != nullptr) {
4331 // Make the loop iteration variable private (for worksharing constructs),
4332 // linear (for simd directives with the only one associated loop) or
4333 // lastprivate (for simd directives with several collapsed or ordered
4334 // loops).
4335 if (DVar.CKind == OMPC_unknown)
4336 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4337 /*FromParent=*/false);
4338 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4339 }
4340
4341 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4342
4343 // Check test-expr.
4344 HasErrors |= ISC.CheckCond(For->getCond());
4345
4346 // Check incr-expr.
4347 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004348 }
4349
Alexander Musmana5f070a2014-10-01 06:03:56 +00004350 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004351 return HasErrors;
4352
Alexander Musmana5f070a2014-10-01 06:03:56 +00004353 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004354 ResultIterSpace.PreCond =
4355 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004356 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004357 DSA.getCurScope(),
4358 (isOpenMPWorksharingDirective(DKind) ||
4359 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4360 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004361 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004362 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004363 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4364 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4365 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4366 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4367 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4368 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4369
Alexey Bataev62dbb972015-04-22 11:59:37 +00004370 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4371 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004372 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004373 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004374 ResultIterSpace.CounterInit == nullptr ||
4375 ResultIterSpace.CounterStep == nullptr);
4376
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004377 return HasErrors;
4378}
4379
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004380/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004381static ExprResult
4382BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4383 ExprResult Start,
4384 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004385 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004386 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4387 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004388 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004389 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004390 VarRef.get()->getType())) {
4391 NewStart = SemaRef.PerformImplicitConversion(
4392 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4393 /*AllowExplicit=*/true);
4394 if (!NewStart.isUsable())
4395 return ExprError();
4396 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004397
4398 auto Init =
4399 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4400 return Init;
4401}
4402
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004404static ExprResult
4405BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4406 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4407 ExprResult Step, bool Subtract,
4408 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004409 // Add parentheses (for debugging purposes only).
4410 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4411 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4412 !Step.isUsable())
4413 return ExprError();
4414
Alexey Bataev5a3af132016-03-29 08:58:54 +00004415 ExprResult NewStep = Step;
4416 if (Captures)
4417 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004418 if (NewStep.isInvalid())
4419 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004420 ExprResult Update =
4421 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004422 if (!Update.isUsable())
4423 return ExprError();
4424
Alexey Bataevc0214e02016-02-16 12:13:49 +00004425 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4426 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004427 ExprResult NewStart = Start;
4428 if (Captures)
4429 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004430 if (NewStart.isInvalid())
4431 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004432
Alexey Bataevc0214e02016-02-16 12:13:49 +00004433 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4434 ExprResult SavedUpdate = Update;
4435 ExprResult UpdateVal;
4436 if (VarRef.get()->getType()->isOverloadableType() ||
4437 NewStart.get()->getType()->isOverloadableType() ||
4438 Update.get()->getType()->isOverloadableType()) {
4439 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4440 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4441 Update =
4442 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4443 if (Update.isUsable()) {
4444 UpdateVal =
4445 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4446 VarRef.get(), SavedUpdate.get());
4447 if (UpdateVal.isUsable()) {
4448 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4449 UpdateVal.get());
4450 }
4451 }
4452 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4453 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004454
Alexey Bataevc0214e02016-02-16 12:13:49 +00004455 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4456 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4457 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4458 NewStart.get(), SavedUpdate.get());
4459 if (!Update.isUsable())
4460 return ExprError();
4461
Alexey Bataev11481f52016-02-17 10:29:05 +00004462 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4463 VarRef.get()->getType())) {
4464 Update = SemaRef.PerformImplicitConversion(
4465 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4466 if (!Update.isUsable())
4467 return ExprError();
4468 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004469
4470 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4471 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472 return Update;
4473}
4474
4475/// \brief Convert integer expression \a E to make it have at least \a Bits
4476/// bits.
4477static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4478 Sema &SemaRef) {
4479 if (E == nullptr)
4480 return ExprError();
4481 auto &C = SemaRef.Context;
4482 QualType OldType = E->getType();
4483 unsigned HasBits = C.getTypeSize(OldType);
4484 if (HasBits >= Bits)
4485 return ExprResult(E);
4486 // OK to convert to signed, because new type has more bits than old.
4487 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4488 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4489 true);
4490}
4491
4492/// \brief Check if the given expression \a E is a constant integer that fits
4493/// into \a Bits bits.
4494static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4495 if (E == nullptr)
4496 return false;
4497 llvm::APSInt Result;
4498 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4499 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4500 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004501}
4502
Alexey Bataev5a3af132016-03-29 08:58:54 +00004503/// Build preinits statement for the given declarations.
4504static Stmt *buildPreInits(ASTContext &Context,
4505 SmallVectorImpl<Decl *> &PreInits) {
4506 if (!PreInits.empty()) {
4507 return new (Context) DeclStmt(
4508 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4509 SourceLocation(), SourceLocation());
4510 }
4511 return nullptr;
4512}
4513
4514/// Build preinits statement for the given declarations.
4515static Stmt *buildPreInits(ASTContext &Context,
4516 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4517 if (!Captures.empty()) {
4518 SmallVector<Decl *, 16> PreInits;
4519 for (auto &Pair : Captures)
4520 PreInits.push_back(Pair.second->getDecl());
4521 return buildPreInits(Context, PreInits);
4522 }
4523 return nullptr;
4524}
4525
4526/// Build postupdate expression for the given list of postupdates expressions.
4527static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4528 Expr *PostUpdate = nullptr;
4529 if (!PostUpdates.empty()) {
4530 for (auto *E : PostUpdates) {
4531 Expr *ConvE = S.BuildCStyleCastExpr(
4532 E->getExprLoc(),
4533 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4534 E->getExprLoc(), E)
4535 .get();
4536 PostUpdate = PostUpdate
4537 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4538 PostUpdate, ConvE)
4539 .get()
4540 : ConvE;
4541 }
4542 }
4543 return PostUpdate;
4544}
4545
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004546/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004547/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4548/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004549static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004550CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4551 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4552 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004553 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004554 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004555 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004556 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004557 // Found 'collapse' clause - calculate collapse number.
4558 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004559 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004560 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004561 }
4562 if (OrderedLoopCountExpr) {
4563 // Found 'ordered' clause - calculate collapse number.
4564 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004565 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4566 if (Result.getLimitedValue() < NestedLoopCount) {
4567 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4568 diag::err_omp_wrong_ordered_loop_count)
4569 << OrderedLoopCountExpr->getSourceRange();
4570 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4571 diag::note_collapse_loop_count)
4572 << CollapseLoopCountExpr->getSourceRange();
4573 }
4574 NestedLoopCount = Result.getLimitedValue();
4575 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004576 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004577 // This is helper routine for loop directives (e.g., 'for', 'simd',
4578 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004579 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004580 SmallVector<LoopIterationSpace, 4> IterSpaces;
4581 IterSpaces.resize(NestedLoopCount);
4582 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004583 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004584 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004585 NestedLoopCount, CollapseLoopCountExpr,
4586 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004587 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004588 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004589 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004590 // OpenMP [2.8.1, simd construct, Restrictions]
4591 // All loops associated with the construct must be perfectly nested; that
4592 // is, there must be no intervening code nor any OpenMP directive between
4593 // any two loops.
4594 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004595 }
4596
Alexander Musmana5f070a2014-10-01 06:03:56 +00004597 Built.clear(/* size */ NestedLoopCount);
4598
4599 if (SemaRef.CurContext->isDependentContext())
4600 return NestedLoopCount;
4601
4602 // An example of what is generated for the following code:
4603 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004604 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004605 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004606 // for (k = 0; k < NK; ++k)
4607 // for (j = J0; j < NJ; j+=2) {
4608 // <loop body>
4609 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004610 //
4611 // We generate the code below.
4612 // Note: the loop body may be outlined in CodeGen.
4613 // Note: some counters may be C++ classes, operator- is used to find number of
4614 // iterations and operator+= to calculate counter value.
4615 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4616 // or i64 is currently supported).
4617 //
4618 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4619 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4620 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4621 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4622 // // similar updates for vars in clauses (e.g. 'linear')
4623 // <loop body (using local i and j)>
4624 // }
4625 // i = NI; // assign final values of counters
4626 // j = NJ;
4627 //
4628
4629 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4630 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004631 // Precondition tests if there is at least one iteration (all conditions are
4632 // true).
4633 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004634 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004635 ExprResult LastIteration32 = WidenIterationCount(
4636 32 /* Bits */, SemaRef.PerformImplicitConversion(
4637 N0->IgnoreImpCasts(), N0->getType(),
4638 Sema::AA_Converting, /*AllowExplicit=*/true)
4639 .get(),
4640 SemaRef);
4641 ExprResult LastIteration64 = WidenIterationCount(
4642 64 /* Bits */, SemaRef.PerformImplicitConversion(
4643 N0->IgnoreImpCasts(), N0->getType(),
4644 Sema::AA_Converting, /*AllowExplicit=*/true)
4645 .get(),
4646 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004647
4648 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4649 return NestedLoopCount;
4650
4651 auto &C = SemaRef.Context;
4652 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4653
4654 Scope *CurScope = DSA.getCurScope();
4655 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004656 if (PreCond.isUsable()) {
4657 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4658 PreCond.get(), IterSpaces[Cnt].PreCond);
4659 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660 auto N = IterSpaces[Cnt].NumIterations;
4661 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4662 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004663 LastIteration32 = SemaRef.BuildBinOp(
4664 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4665 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4666 Sema::AA_Converting,
4667 /*AllowExplicit=*/true)
4668 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004669 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004670 LastIteration64 = SemaRef.BuildBinOp(
4671 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4672 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4673 Sema::AA_Converting,
4674 /*AllowExplicit=*/true)
4675 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004676 }
4677
4678 // Choose either the 32-bit or 64-bit version.
4679 ExprResult LastIteration = LastIteration64;
4680 if (LastIteration32.isUsable() &&
4681 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4682 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4683 FitsInto(
4684 32 /* Bits */,
4685 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4686 LastIteration64.get(), SemaRef)))
4687 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004688 QualType VType = LastIteration.get()->getType();
4689 QualType RealVType = VType;
4690 QualType StrideVType = VType;
4691 if (isOpenMPTaskLoopDirective(DKind)) {
4692 VType =
4693 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4694 StrideVType =
4695 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4696 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004697
4698 if (!LastIteration.isUsable())
4699 return 0;
4700
4701 // Save the number of iterations.
4702 ExprResult NumIterations = LastIteration;
4703 {
4704 LastIteration = SemaRef.BuildBinOp(
4705 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4706 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4707 if (!LastIteration.isUsable())
4708 return 0;
4709 }
4710
4711 // Calculate the last iteration number beforehand instead of doing this on
4712 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4713 llvm::APSInt Result;
4714 bool IsConstant =
4715 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4716 ExprResult CalcLastIteration;
4717 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004718 ExprResult SaveRef =
4719 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004720 LastIteration = SaveRef;
4721
4722 // Prepare SaveRef + 1.
4723 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004724 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004725 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4726 if (!NumIterations.isUsable())
4727 return 0;
4728 }
4729
4730 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4731
Alexander Musmanc6388682014-12-15 07:07:06 +00004732 // Build variables passed into runtime, nesessary for worksharing directives.
4733 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004734 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4735 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004736 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004737 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4738 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004739 SemaRef.AddInitializerToDecl(
4740 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4741 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4742
4743 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004744 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4745 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004746 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4747 /*DirectInit*/ false,
4748 /*TypeMayContainAuto*/ false);
4749
4750 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4751 // This will be used to implement clause 'lastprivate'.
4752 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004753 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4754 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004755 SemaRef.AddInitializerToDecl(
4756 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4757 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4758
4759 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004760 VarDecl *STDecl =
4761 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4762 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004763 SemaRef.AddInitializerToDecl(
4764 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4765 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4766
4767 // Build expression: UB = min(UB, LastIteration)
4768 // It is nesessary for CodeGen of directives with static scheduling.
4769 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4770 UB.get(), LastIteration.get());
4771 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4772 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4773 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4774 CondOp.get());
4775 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4776 }
4777
4778 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004779 ExprResult IV;
4780 ExprResult Init;
4781 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004782 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4783 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004784 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004785 isOpenMPTaskLoopDirective(DKind) ||
4786 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004787 ? LB.get()
4788 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4789 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4790 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004791 }
4792
Alexander Musmanc6388682014-12-15 07:07:06 +00004793 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004794 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004795 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004796 (isOpenMPWorksharingDirective(DKind) ||
4797 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004798 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4799 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4800 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004801
4802 // Loop increment (IV = IV + 1)
4803 SourceLocation IncLoc;
4804 ExprResult Inc =
4805 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4806 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4807 if (!Inc.isUsable())
4808 return 0;
4809 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004810 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4811 if (!Inc.isUsable())
4812 return 0;
4813
4814 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4815 // Used for directives with static scheduling.
4816 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004817 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4818 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004819 // LB + ST
4820 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4821 if (!NextLB.isUsable())
4822 return 0;
4823 // LB = LB + ST
4824 NextLB =
4825 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4826 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4827 if (!NextLB.isUsable())
4828 return 0;
4829 // UB + ST
4830 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4831 if (!NextUB.isUsable())
4832 return 0;
4833 // UB = UB + ST
4834 NextUB =
4835 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4836 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4837 if (!NextUB.isUsable())
4838 return 0;
4839 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004840
4841 // Build updates and final values of the loop counters.
4842 bool HasErrors = false;
4843 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004844 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004845 Built.Updates.resize(NestedLoopCount);
4846 Built.Finals.resize(NestedLoopCount);
4847 {
4848 ExprResult Div;
4849 // Go from inner nested loop to outer.
4850 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4851 LoopIterationSpace &IS = IterSpaces[Cnt];
4852 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4853 // Build: Iter = (IV / Div) % IS.NumIters
4854 // where Div is product of previous iterations' IS.NumIters.
4855 ExprResult Iter;
4856 if (Div.isUsable()) {
4857 Iter =
4858 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4859 } else {
4860 Iter = IV;
4861 assert((Cnt == (int)NestedLoopCount - 1) &&
4862 "unusable div expected on first iteration only");
4863 }
4864
4865 if (Cnt != 0 && Iter.isUsable())
4866 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4867 IS.NumIterations);
4868 if (!Iter.isUsable()) {
4869 HasErrors = true;
4870 break;
4871 }
4872
Alexey Bataev39f915b82015-05-08 10:41:21 +00004873 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004874 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4875 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4876 IS.CounterVar->getExprLoc(),
4877 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004878 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004879 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004880 if (!Init.isUsable()) {
4881 HasErrors = true;
4882 break;
4883 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004884 ExprResult Update = BuildCounterUpdate(
4885 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4886 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004887 if (!Update.isUsable()) {
4888 HasErrors = true;
4889 break;
4890 }
4891
4892 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4893 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004894 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004895 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004896 if (!Final.isUsable()) {
4897 HasErrors = true;
4898 break;
4899 }
4900
4901 // Build Div for the next iteration: Div <- Div * IS.NumIters
4902 if (Cnt != 0) {
4903 if (Div.isUnset())
4904 Div = IS.NumIterations;
4905 else
4906 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4907 IS.NumIterations);
4908
4909 // Add parentheses (for debugging purposes only).
4910 if (Div.isUsable())
4911 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4912 if (!Div.isUsable()) {
4913 HasErrors = true;
4914 break;
4915 }
4916 }
4917 if (!Update.isUsable() || !Final.isUsable()) {
4918 HasErrors = true;
4919 break;
4920 }
4921 // Save results
4922 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004923 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004924 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004925 Built.Updates[Cnt] = Update.get();
4926 Built.Finals[Cnt] = Final.get();
4927 }
4928 }
4929
4930 if (HasErrors)
4931 return 0;
4932
4933 // Save results
4934 Built.IterationVarRef = IV.get();
4935 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004936 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004937 Built.CalcLastIteration =
4938 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004939 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004940 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004941 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004942 Built.Init = Init.get();
4943 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004944 Built.LB = LB.get();
4945 Built.UB = UB.get();
4946 Built.IL = IL.get();
4947 Built.ST = ST.get();
4948 Built.EUB = EUB.get();
4949 Built.NLB = NextLB.get();
4950 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004951
Alexey Bataevabfc0692014-06-25 06:52:00 +00004952 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004953}
4954
Alexey Bataev10e775f2015-07-30 11:36:16 +00004955static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004956 auto CollapseClauses =
4957 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4958 if (CollapseClauses.begin() != CollapseClauses.end())
4959 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004960 return nullptr;
4961}
4962
Alexey Bataev10e775f2015-07-30 11:36:16 +00004963static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004964 auto OrderedClauses =
4965 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4966 if (OrderedClauses.begin() != OrderedClauses.end())
4967 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004968 return nullptr;
4969}
4970
Alexey Bataev66b15b52015-08-21 11:14:16 +00004971static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4972 const Expr *Safelen) {
4973 llvm::APSInt SimdlenRes, SafelenRes;
4974 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4975 Simdlen->isInstantiationDependent() ||
4976 Simdlen->containsUnexpandedParameterPack())
4977 return false;
4978 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4979 Safelen->isInstantiationDependent() ||
4980 Safelen->containsUnexpandedParameterPack())
4981 return false;
4982 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4983 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4984 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4985 // If both simdlen and safelen clauses are specified, the value of the simdlen
4986 // parameter must be less than or equal to the value of the safelen parameter.
4987 if (SimdlenRes > SafelenRes) {
4988 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4989 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4990 return true;
4991 }
4992 return false;
4993}
4994
Alexey Bataev4acb8592014-07-07 13:01:15 +00004995StmtResult Sema::ActOnOpenMPSimdDirective(
4996 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4997 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004998 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004999 if (!AStmt)
5000 return StmtError();
5001
5002 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005003 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005004 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5005 // define the nested loops number.
5006 unsigned NestedLoopCount = CheckOpenMPLoop(
5007 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5008 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005009 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005010 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005011
Alexander Musmana5f070a2014-10-01 06:03:56 +00005012 assert((CurContext->isDependentContext() || B.builtAll()) &&
5013 "omp simd loop exprs were not built");
5014
Alexander Musman3276a272015-03-21 10:12:56 +00005015 if (!CurContext->isDependentContext()) {
5016 // Finalize the clauses that need pre-built expressions for CodeGen.
5017 for (auto C : Clauses) {
5018 if (auto LC = dyn_cast<OMPLinearClause>(C))
5019 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005020 B.NumIterations, *this, CurScope,
5021 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005022 return StmtError();
5023 }
5024 }
5025
Alexey Bataev66b15b52015-08-21 11:14:16 +00005026 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5027 // If both simdlen and safelen clauses are specified, the value of the simdlen
5028 // parameter must be less than or equal to the value of the safelen parameter.
5029 OMPSafelenClause *Safelen = nullptr;
5030 OMPSimdlenClause *Simdlen = nullptr;
5031 for (auto *Clause : Clauses) {
5032 if (Clause->getClauseKind() == OMPC_safelen)
5033 Safelen = cast<OMPSafelenClause>(Clause);
5034 else if (Clause->getClauseKind() == OMPC_simdlen)
5035 Simdlen = cast<OMPSimdlenClause>(Clause);
5036 if (Safelen && Simdlen)
5037 break;
5038 }
5039 if (Simdlen && Safelen &&
5040 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5041 Safelen->getSafelen()))
5042 return StmtError();
5043
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005044 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005045 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5046 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005047}
5048
Alexey Bataev4acb8592014-07-07 13:01:15 +00005049StmtResult Sema::ActOnOpenMPForDirective(
5050 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5051 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005052 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005053 if (!AStmt)
5054 return StmtError();
5055
5056 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005057 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005058 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5059 // define the nested loops number.
5060 unsigned NestedLoopCount = CheckOpenMPLoop(
5061 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5062 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005063 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005064 return StmtError();
5065
Alexander Musmana5f070a2014-10-01 06:03:56 +00005066 assert((CurContext->isDependentContext() || B.builtAll()) &&
5067 "omp for loop exprs were not built");
5068
Alexey Bataev54acd402015-08-04 11:18:19 +00005069 if (!CurContext->isDependentContext()) {
5070 // Finalize the clauses that need pre-built expressions for CodeGen.
5071 for (auto C : Clauses) {
5072 if (auto LC = dyn_cast<OMPLinearClause>(C))
5073 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005074 B.NumIterations, *this, CurScope,
5075 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005076 return StmtError();
5077 }
5078 }
5079
Alexey Bataevf29276e2014-06-18 04:14:57 +00005080 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005081 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005082 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005083}
5084
Alexander Musmanf82886e2014-09-18 05:12:34 +00005085StmtResult Sema::ActOnOpenMPForSimdDirective(
5086 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5087 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005088 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005089 if (!AStmt)
5090 return StmtError();
5091
5092 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005093 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005094 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5095 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005096 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005097 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5098 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5099 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005100 if (NestedLoopCount == 0)
5101 return StmtError();
5102
Alexander Musmanc6388682014-12-15 07:07:06 +00005103 assert((CurContext->isDependentContext() || B.builtAll()) &&
5104 "omp for simd loop exprs were not built");
5105
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005106 if (!CurContext->isDependentContext()) {
5107 // Finalize the clauses that need pre-built expressions for CodeGen.
5108 for (auto C : Clauses) {
5109 if (auto LC = dyn_cast<OMPLinearClause>(C))
5110 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005111 B.NumIterations, *this, CurScope,
5112 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005113 return StmtError();
5114 }
5115 }
5116
Alexey Bataev66b15b52015-08-21 11:14:16 +00005117 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5118 // If both simdlen and safelen clauses are specified, the value of the simdlen
5119 // parameter must be less than or equal to the value of the safelen parameter.
5120 OMPSafelenClause *Safelen = nullptr;
5121 OMPSimdlenClause *Simdlen = nullptr;
5122 for (auto *Clause : Clauses) {
5123 if (Clause->getClauseKind() == OMPC_safelen)
5124 Safelen = cast<OMPSafelenClause>(Clause);
5125 else if (Clause->getClauseKind() == OMPC_simdlen)
5126 Simdlen = cast<OMPSimdlenClause>(Clause);
5127 if (Safelen && Simdlen)
5128 break;
5129 }
5130 if (Simdlen && Safelen &&
5131 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5132 Safelen->getSafelen()))
5133 return StmtError();
5134
Alexander Musmanf82886e2014-09-18 05:12:34 +00005135 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005136 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5137 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005138}
5139
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005140StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5141 Stmt *AStmt,
5142 SourceLocation StartLoc,
5143 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005144 if (!AStmt)
5145 return StmtError();
5146
5147 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005148 auto BaseStmt = AStmt;
5149 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5150 BaseStmt = CS->getCapturedStmt();
5151 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5152 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005153 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005154 return StmtError();
5155 // All associated statements must be '#pragma omp section' except for
5156 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005157 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005158 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5159 if (SectionStmt)
5160 Diag(SectionStmt->getLocStart(),
5161 diag::err_omp_sections_substmt_not_section);
5162 return StmtError();
5163 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005164 cast<OMPSectionDirective>(SectionStmt)
5165 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005166 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005167 } else {
5168 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5169 return StmtError();
5170 }
5171
5172 getCurFunction()->setHasBranchProtectedScope();
5173
Alexey Bataev25e5b442015-09-15 12:52:43 +00005174 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5175 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005176}
5177
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005178StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5179 SourceLocation StartLoc,
5180 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005181 if (!AStmt)
5182 return StmtError();
5183
5184 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005185
5186 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005187 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005188
Alexey Bataev25e5b442015-09-15 12:52:43 +00005189 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5190 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005191}
5192
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005193StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5194 Stmt *AStmt,
5195 SourceLocation StartLoc,
5196 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005197 if (!AStmt)
5198 return StmtError();
5199
5200 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005201
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005202 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005203
Alexey Bataev3255bf32015-01-19 05:20:46 +00005204 // OpenMP [2.7.3, single Construct, Restrictions]
5205 // The copyprivate clause must not be used with the nowait clause.
5206 OMPClause *Nowait = nullptr;
5207 OMPClause *Copyprivate = nullptr;
5208 for (auto *Clause : Clauses) {
5209 if (Clause->getClauseKind() == OMPC_nowait)
5210 Nowait = Clause;
5211 else if (Clause->getClauseKind() == OMPC_copyprivate)
5212 Copyprivate = Clause;
5213 if (Copyprivate && Nowait) {
5214 Diag(Copyprivate->getLocStart(),
5215 diag::err_omp_single_copyprivate_with_nowait);
5216 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5217 return StmtError();
5218 }
5219 }
5220
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005221 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5222}
5223
Alexander Musman80c22892014-07-17 08:54:58 +00005224StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5225 SourceLocation StartLoc,
5226 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005227 if (!AStmt)
5228 return StmtError();
5229
5230 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005231
5232 getCurFunction()->setHasBranchProtectedScope();
5233
5234 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5235}
5236
Alexey Bataev28c75412015-12-15 08:19:24 +00005237StmtResult Sema::ActOnOpenMPCriticalDirective(
5238 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5239 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005240 if (!AStmt)
5241 return StmtError();
5242
5243 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005244
Alexey Bataev28c75412015-12-15 08:19:24 +00005245 bool ErrorFound = false;
5246 llvm::APSInt Hint;
5247 SourceLocation HintLoc;
5248 bool DependentHint = false;
5249 for (auto *C : Clauses) {
5250 if (C->getClauseKind() == OMPC_hint) {
5251 if (!DirName.getName()) {
5252 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5253 ErrorFound = true;
5254 }
5255 Expr *E = cast<OMPHintClause>(C)->getHint();
5256 if (E->isTypeDependent() || E->isValueDependent() ||
5257 E->isInstantiationDependent())
5258 DependentHint = true;
5259 else {
5260 Hint = E->EvaluateKnownConstInt(Context);
5261 HintLoc = C->getLocStart();
5262 }
5263 }
5264 }
5265 if (ErrorFound)
5266 return StmtError();
5267 auto Pair = DSAStack->getCriticalWithHint(DirName);
5268 if (Pair.first && DirName.getName() && !DependentHint) {
5269 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5270 Diag(StartLoc, diag::err_omp_critical_with_hint);
5271 if (HintLoc.isValid()) {
5272 Diag(HintLoc, diag::note_omp_critical_hint_here)
5273 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5274 } else
5275 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5276 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5277 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5278 << 1
5279 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5280 /*Radix=*/10, /*Signed=*/false);
5281 } else
5282 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5283 }
5284 }
5285
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005286 getCurFunction()->setHasBranchProtectedScope();
5287
Alexey Bataev28c75412015-12-15 08:19:24 +00005288 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5289 Clauses, AStmt);
5290 if (!Pair.first && DirName.getName() && !DependentHint)
5291 DSAStack->addCriticalWithHint(Dir, Hint);
5292 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005293}
5294
Alexey Bataev4acb8592014-07-07 13:01:15 +00005295StmtResult Sema::ActOnOpenMPParallelForDirective(
5296 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5297 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005298 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005299 if (!AStmt)
5300 return StmtError();
5301
Alexey Bataev4acb8592014-07-07 13:01:15 +00005302 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5303 // 1.2.2 OpenMP Language Terminology
5304 // Structured block - An executable statement with a single entry at the
5305 // top and a single exit at the bottom.
5306 // The point of exit cannot be a branch out of the structured block.
5307 // longjmp() and throw() must not violate the entry/exit criteria.
5308 CS->getCapturedDecl()->setNothrow();
5309
Alexander Musmanc6388682014-12-15 07:07:06 +00005310 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005311 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5312 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005313 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005314 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5315 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5316 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005317 if (NestedLoopCount == 0)
5318 return StmtError();
5319
Alexander Musmana5f070a2014-10-01 06:03:56 +00005320 assert((CurContext->isDependentContext() || B.builtAll()) &&
5321 "omp parallel for loop exprs were not built");
5322
Alexey Bataev54acd402015-08-04 11:18:19 +00005323 if (!CurContext->isDependentContext()) {
5324 // Finalize the clauses that need pre-built expressions for CodeGen.
5325 for (auto C : Clauses) {
5326 if (auto LC = dyn_cast<OMPLinearClause>(C))
5327 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005328 B.NumIterations, *this, CurScope,
5329 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005330 return StmtError();
5331 }
5332 }
5333
Alexey Bataev4acb8592014-07-07 13:01:15 +00005334 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005335 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005336 NestedLoopCount, Clauses, AStmt, B,
5337 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005338}
5339
Alexander Musmane4e893b2014-09-23 09:33:00 +00005340StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5341 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5342 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005343 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005344 if (!AStmt)
5345 return StmtError();
5346
Alexander Musmane4e893b2014-09-23 09:33:00 +00005347 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5348 // 1.2.2 OpenMP Language Terminology
5349 // Structured block - An executable statement with a single entry at the
5350 // top and a single exit at the bottom.
5351 // The point of exit cannot be a branch out of the structured block.
5352 // longjmp() and throw() must not violate the entry/exit criteria.
5353 CS->getCapturedDecl()->setNothrow();
5354
Alexander Musmanc6388682014-12-15 07:07:06 +00005355 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005356 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5357 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005358 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005359 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5360 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5361 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005362 if (NestedLoopCount == 0)
5363 return StmtError();
5364
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005365 if (!CurContext->isDependentContext()) {
5366 // Finalize the clauses that need pre-built expressions for CodeGen.
5367 for (auto C : Clauses) {
5368 if (auto LC = dyn_cast<OMPLinearClause>(C))
5369 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005370 B.NumIterations, *this, CurScope,
5371 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005372 return StmtError();
5373 }
5374 }
5375
Alexey Bataev66b15b52015-08-21 11:14:16 +00005376 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5377 // If both simdlen and safelen clauses are specified, the value of the simdlen
5378 // parameter must be less than or equal to the value of the safelen parameter.
5379 OMPSafelenClause *Safelen = nullptr;
5380 OMPSimdlenClause *Simdlen = nullptr;
5381 for (auto *Clause : Clauses) {
5382 if (Clause->getClauseKind() == OMPC_safelen)
5383 Safelen = cast<OMPSafelenClause>(Clause);
5384 else if (Clause->getClauseKind() == OMPC_simdlen)
5385 Simdlen = cast<OMPSimdlenClause>(Clause);
5386 if (Safelen && Simdlen)
5387 break;
5388 }
5389 if (Simdlen && Safelen &&
5390 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5391 Safelen->getSafelen()))
5392 return StmtError();
5393
Alexander Musmane4e893b2014-09-23 09:33:00 +00005394 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005395 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005396 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005397}
5398
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005399StmtResult
5400Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5401 Stmt *AStmt, SourceLocation StartLoc,
5402 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005403 if (!AStmt)
5404 return StmtError();
5405
5406 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005407 auto BaseStmt = AStmt;
5408 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5409 BaseStmt = CS->getCapturedStmt();
5410 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5411 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005412 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005413 return StmtError();
5414 // All associated statements must be '#pragma omp section' except for
5415 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005416 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005417 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5418 if (SectionStmt)
5419 Diag(SectionStmt->getLocStart(),
5420 diag::err_omp_parallel_sections_substmt_not_section);
5421 return StmtError();
5422 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005423 cast<OMPSectionDirective>(SectionStmt)
5424 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005425 }
5426 } else {
5427 Diag(AStmt->getLocStart(),
5428 diag::err_omp_parallel_sections_not_compound_stmt);
5429 return StmtError();
5430 }
5431
5432 getCurFunction()->setHasBranchProtectedScope();
5433
Alexey Bataev25e5b442015-09-15 12:52:43 +00005434 return OMPParallelSectionsDirective::Create(
5435 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005436}
5437
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005438StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5439 Stmt *AStmt, SourceLocation StartLoc,
5440 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005441 if (!AStmt)
5442 return StmtError();
5443
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005444 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5445 // 1.2.2 OpenMP Language Terminology
5446 // Structured block - An executable statement with a single entry at the
5447 // top and a single exit at the bottom.
5448 // The point of exit cannot be a branch out of the structured block.
5449 // longjmp() and throw() must not violate the entry/exit criteria.
5450 CS->getCapturedDecl()->setNothrow();
5451
5452 getCurFunction()->setHasBranchProtectedScope();
5453
Alexey Bataev25e5b442015-09-15 12:52:43 +00005454 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5455 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005456}
5457
Alexey Bataev68446b72014-07-18 07:47:19 +00005458StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5459 SourceLocation EndLoc) {
5460 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5461}
5462
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005463StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5464 SourceLocation EndLoc) {
5465 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5466}
5467
Alexey Bataev2df347a2014-07-18 10:17:07 +00005468StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5469 SourceLocation EndLoc) {
5470 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5471}
5472
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005473StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5474 SourceLocation StartLoc,
5475 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005476 if (!AStmt)
5477 return StmtError();
5478
5479 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005480
5481 getCurFunction()->setHasBranchProtectedScope();
5482
5483 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5484}
5485
Alexey Bataev6125da92014-07-21 11:26:11 +00005486StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5487 SourceLocation StartLoc,
5488 SourceLocation EndLoc) {
5489 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5490 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5491}
5492
Alexey Bataev346265e2015-09-25 10:37:12 +00005493StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5494 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005495 SourceLocation StartLoc,
5496 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005497 OMPClause *DependFound = nullptr;
5498 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005499 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005500 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005501 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005502 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005503 for (auto *C : Clauses) {
5504 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5505 DependFound = C;
5506 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5507 if (DependSourceClause) {
5508 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5509 << getOpenMPDirectiveName(OMPD_ordered)
5510 << getOpenMPClauseName(OMPC_depend) << 2;
5511 ErrorFound = true;
5512 } else
5513 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005514 if (DependSinkClause) {
5515 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5516 << 0;
5517 ErrorFound = true;
5518 }
5519 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5520 if (DependSourceClause) {
5521 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5522 << 1;
5523 ErrorFound = true;
5524 }
5525 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005526 }
5527 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005528 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005529 else if (C->getClauseKind() == OMPC_simd)
5530 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005531 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005532 if (!ErrorFound && !SC &&
5533 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005534 // OpenMP [2.8.1,simd Construct, Restrictions]
5535 // An ordered construct with the simd clause is the only OpenMP construct
5536 // that can appear in the simd region.
5537 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005538 ErrorFound = true;
5539 } else if (DependFound && (TC || SC)) {
5540 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5541 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5542 ErrorFound = true;
5543 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5544 Diag(DependFound->getLocStart(),
5545 diag::err_omp_ordered_directive_without_param);
5546 ErrorFound = true;
5547 } else if (TC || Clauses.empty()) {
5548 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5549 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5550 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5551 << (TC != nullptr);
5552 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5553 ErrorFound = true;
5554 }
5555 }
5556 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005557 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005558
5559 if (AStmt) {
5560 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5561
5562 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005563 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005564
5565 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005566}
5567
Alexey Bataev1d160b12015-03-13 12:27:31 +00005568namespace {
5569/// \brief Helper class for checking expression in 'omp atomic [update]'
5570/// construct.
5571class OpenMPAtomicUpdateChecker {
5572 /// \brief Error results for atomic update expressions.
5573 enum ExprAnalysisErrorCode {
5574 /// \brief A statement is not an expression statement.
5575 NotAnExpression,
5576 /// \brief Expression is not builtin binary or unary operation.
5577 NotABinaryOrUnaryExpression,
5578 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5579 NotAnUnaryIncDecExpression,
5580 /// \brief An expression is not of scalar type.
5581 NotAScalarType,
5582 /// \brief A binary operation is not an assignment operation.
5583 NotAnAssignmentOp,
5584 /// \brief RHS part of the binary operation is not a binary expression.
5585 NotABinaryExpression,
5586 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5587 /// expression.
5588 NotABinaryOperator,
5589 /// \brief RHS binary operation does not have reference to the updated LHS
5590 /// part.
5591 NotAnUpdateExpression,
5592 /// \brief No errors is found.
5593 NoError
5594 };
5595 /// \brief Reference to Sema.
5596 Sema &SemaRef;
5597 /// \brief A location for note diagnostics (when error is found).
5598 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005599 /// \brief 'x' lvalue part of the source atomic expression.
5600 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005601 /// \brief 'expr' rvalue part of the source atomic expression.
5602 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005603 /// \brief Helper expression of the form
5604 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5605 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5606 Expr *UpdateExpr;
5607 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5608 /// important for non-associative operations.
5609 bool IsXLHSInRHSPart;
5610 BinaryOperatorKind Op;
5611 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005612 /// \brief true if the source expression is a postfix unary operation, false
5613 /// if it is a prefix unary operation.
5614 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005615
5616public:
5617 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005618 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005619 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005620 /// \brief Check specified statement that it is suitable for 'atomic update'
5621 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005622 /// expression. If DiagId and NoteId == 0, then only check is performed
5623 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005624 /// \param DiagId Diagnostic which should be emitted if error is found.
5625 /// \param NoteId Diagnostic note for the main error message.
5626 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005627 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005628 /// \brief Return the 'x' lvalue part of the source atomic expression.
5629 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005630 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5631 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005632 /// \brief Return the update expression used in calculation of the updated
5633 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5634 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5635 Expr *getUpdateExpr() const { return UpdateExpr; }
5636 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5637 /// false otherwise.
5638 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5639
Alexey Bataevb78ca832015-04-01 03:33:17 +00005640 /// \brief true if the source expression is a postfix unary operation, false
5641 /// if it is a prefix unary operation.
5642 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5643
Alexey Bataev1d160b12015-03-13 12:27:31 +00005644private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005645 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5646 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005647};
5648} // namespace
5649
5650bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5651 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5652 ExprAnalysisErrorCode ErrorFound = NoError;
5653 SourceLocation ErrorLoc, NoteLoc;
5654 SourceRange ErrorRange, NoteRange;
5655 // Allowed constructs are:
5656 // x = x binop expr;
5657 // x = expr binop x;
5658 if (AtomicBinOp->getOpcode() == BO_Assign) {
5659 X = AtomicBinOp->getLHS();
5660 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5661 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5662 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5663 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5664 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005665 Op = AtomicInnerBinOp->getOpcode();
5666 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005667 auto *LHS = AtomicInnerBinOp->getLHS();
5668 auto *RHS = AtomicInnerBinOp->getRHS();
5669 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5670 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5671 /*Canonical=*/true);
5672 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5673 /*Canonical=*/true);
5674 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5675 /*Canonical=*/true);
5676 if (XId == LHSId) {
5677 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005678 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005679 } else if (XId == RHSId) {
5680 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005681 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005682 } else {
5683 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5684 ErrorRange = AtomicInnerBinOp->getSourceRange();
5685 NoteLoc = X->getExprLoc();
5686 NoteRange = X->getSourceRange();
5687 ErrorFound = NotAnUpdateExpression;
5688 }
5689 } else {
5690 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5691 ErrorRange = AtomicInnerBinOp->getSourceRange();
5692 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5693 NoteRange = SourceRange(NoteLoc, NoteLoc);
5694 ErrorFound = NotABinaryOperator;
5695 }
5696 } else {
5697 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5698 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5699 ErrorFound = NotABinaryExpression;
5700 }
5701 } else {
5702 ErrorLoc = AtomicBinOp->getExprLoc();
5703 ErrorRange = AtomicBinOp->getSourceRange();
5704 NoteLoc = AtomicBinOp->getOperatorLoc();
5705 NoteRange = SourceRange(NoteLoc, NoteLoc);
5706 ErrorFound = NotAnAssignmentOp;
5707 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005708 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005709 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5710 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5711 return true;
5712 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005713 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005714 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005715}
5716
5717bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5718 unsigned NoteId) {
5719 ExprAnalysisErrorCode ErrorFound = NoError;
5720 SourceLocation ErrorLoc, NoteLoc;
5721 SourceRange ErrorRange, NoteRange;
5722 // Allowed constructs are:
5723 // x++;
5724 // x--;
5725 // ++x;
5726 // --x;
5727 // x binop= expr;
5728 // x = x binop expr;
5729 // x = expr binop x;
5730 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5731 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5732 if (AtomicBody->getType()->isScalarType() ||
5733 AtomicBody->isInstantiationDependent()) {
5734 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5735 AtomicBody->IgnoreParenImpCasts())) {
5736 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005737 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005738 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005739 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005740 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005741 X = AtomicCompAssignOp->getLHS();
5742 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005743 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5744 AtomicBody->IgnoreParenImpCasts())) {
5745 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005746 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5747 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005748 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005749 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5750 // Check for Unary Operation
5751 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005752 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005753 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5754 OpLoc = AtomicUnaryOp->getOperatorLoc();
5755 X = AtomicUnaryOp->getSubExpr();
5756 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5757 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005758 } else {
5759 ErrorFound = NotAnUnaryIncDecExpression;
5760 ErrorLoc = AtomicUnaryOp->getExprLoc();
5761 ErrorRange = AtomicUnaryOp->getSourceRange();
5762 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5763 NoteRange = SourceRange(NoteLoc, NoteLoc);
5764 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005765 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005766 ErrorFound = NotABinaryOrUnaryExpression;
5767 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5768 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5769 }
5770 } else {
5771 ErrorFound = NotAScalarType;
5772 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5773 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5774 }
5775 } else {
5776 ErrorFound = NotAnExpression;
5777 NoteLoc = ErrorLoc = S->getLocStart();
5778 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5779 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005780 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005781 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5782 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5783 return true;
5784 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005785 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005786 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005787 // Build an update expression of form 'OpaqueValueExpr(x) binop
5788 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5789 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5790 auto *OVEX = new (SemaRef.getASTContext())
5791 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5792 auto *OVEExpr = new (SemaRef.getASTContext())
5793 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5794 auto Update =
5795 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5796 IsXLHSInRHSPart ? OVEExpr : OVEX);
5797 if (Update.isInvalid())
5798 return true;
5799 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5800 Sema::AA_Casting);
5801 if (Update.isInvalid())
5802 return true;
5803 UpdateExpr = Update.get();
5804 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005805 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005806}
5807
Alexey Bataev0162e452014-07-22 10:10:35 +00005808StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5809 Stmt *AStmt,
5810 SourceLocation StartLoc,
5811 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005812 if (!AStmt)
5813 return StmtError();
5814
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005815 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005816 // 1.2.2 OpenMP Language Terminology
5817 // Structured block - An executable statement with a single entry at the
5818 // top and a single exit at the bottom.
5819 // The point of exit cannot be a branch out of the structured block.
5820 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005821 OpenMPClauseKind AtomicKind = OMPC_unknown;
5822 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005823 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005824 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005825 C->getClauseKind() == OMPC_update ||
5826 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005827 if (AtomicKind != OMPC_unknown) {
5828 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5829 << SourceRange(C->getLocStart(), C->getLocEnd());
5830 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5831 << getOpenMPClauseName(AtomicKind);
5832 } else {
5833 AtomicKind = C->getClauseKind();
5834 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005835 }
5836 }
5837 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005838
Alexey Bataev459dec02014-07-24 06:46:57 +00005839 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005840 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5841 Body = EWC->getSubExpr();
5842
Alexey Bataev62cec442014-11-18 10:14:22 +00005843 Expr *X = nullptr;
5844 Expr *V = nullptr;
5845 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005846 Expr *UE = nullptr;
5847 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005848 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005849 // OpenMP [2.12.6, atomic Construct]
5850 // In the next expressions:
5851 // * x and v (as applicable) are both l-value expressions with scalar type.
5852 // * During the execution of an atomic region, multiple syntactic
5853 // occurrences of x must designate the same storage location.
5854 // * Neither of v and expr (as applicable) may access the storage location
5855 // designated by x.
5856 // * Neither of x and expr (as applicable) may access the storage location
5857 // designated by v.
5858 // * expr is an expression with scalar type.
5859 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5860 // * binop, binop=, ++, and -- are not overloaded operators.
5861 // * The expression x binop expr must be numerically equivalent to x binop
5862 // (expr). This requirement is satisfied if the operators in expr have
5863 // precedence greater than binop, or by using parentheses around expr or
5864 // subexpressions of expr.
5865 // * The expression expr binop x must be numerically equivalent to (expr)
5866 // binop x. This requirement is satisfied if the operators in expr have
5867 // precedence equal to or greater than binop, or by using parentheses around
5868 // expr or subexpressions of expr.
5869 // * For forms that allow multiple occurrences of x, the number of times
5870 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005871 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005872 enum {
5873 NotAnExpression,
5874 NotAnAssignmentOp,
5875 NotAScalarType,
5876 NotAnLValue,
5877 NoError
5878 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005879 SourceLocation ErrorLoc, NoteLoc;
5880 SourceRange ErrorRange, NoteRange;
5881 // If clause is read:
5882 // v = x;
5883 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5884 auto AtomicBinOp =
5885 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5886 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5887 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5888 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5889 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5890 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5891 if (!X->isLValue() || !V->isLValue()) {
5892 auto NotLValueExpr = X->isLValue() ? V : X;
5893 ErrorFound = NotAnLValue;
5894 ErrorLoc = AtomicBinOp->getExprLoc();
5895 ErrorRange = AtomicBinOp->getSourceRange();
5896 NoteLoc = NotLValueExpr->getExprLoc();
5897 NoteRange = NotLValueExpr->getSourceRange();
5898 }
5899 } else if (!X->isInstantiationDependent() ||
5900 !V->isInstantiationDependent()) {
5901 auto NotScalarExpr =
5902 (X->isInstantiationDependent() || X->getType()->isScalarType())
5903 ? V
5904 : X;
5905 ErrorFound = NotAScalarType;
5906 ErrorLoc = AtomicBinOp->getExprLoc();
5907 ErrorRange = AtomicBinOp->getSourceRange();
5908 NoteLoc = NotScalarExpr->getExprLoc();
5909 NoteRange = NotScalarExpr->getSourceRange();
5910 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005911 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005912 ErrorFound = NotAnAssignmentOp;
5913 ErrorLoc = AtomicBody->getExprLoc();
5914 ErrorRange = AtomicBody->getSourceRange();
5915 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5916 : AtomicBody->getExprLoc();
5917 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5918 : AtomicBody->getSourceRange();
5919 }
5920 } else {
5921 ErrorFound = NotAnExpression;
5922 NoteLoc = ErrorLoc = Body->getLocStart();
5923 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005924 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005925 if (ErrorFound != NoError) {
5926 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5927 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005928 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5929 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005930 return StmtError();
5931 } else if (CurContext->isDependentContext())
5932 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005933 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005934 enum {
5935 NotAnExpression,
5936 NotAnAssignmentOp,
5937 NotAScalarType,
5938 NotAnLValue,
5939 NoError
5940 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005941 SourceLocation ErrorLoc, NoteLoc;
5942 SourceRange ErrorRange, NoteRange;
5943 // If clause is write:
5944 // x = expr;
5945 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5946 auto AtomicBinOp =
5947 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5948 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005949 X = AtomicBinOp->getLHS();
5950 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005951 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5952 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5953 if (!X->isLValue()) {
5954 ErrorFound = NotAnLValue;
5955 ErrorLoc = AtomicBinOp->getExprLoc();
5956 ErrorRange = AtomicBinOp->getSourceRange();
5957 NoteLoc = X->getExprLoc();
5958 NoteRange = X->getSourceRange();
5959 }
5960 } else if (!X->isInstantiationDependent() ||
5961 !E->isInstantiationDependent()) {
5962 auto NotScalarExpr =
5963 (X->isInstantiationDependent() || X->getType()->isScalarType())
5964 ? E
5965 : X;
5966 ErrorFound = NotAScalarType;
5967 ErrorLoc = AtomicBinOp->getExprLoc();
5968 ErrorRange = AtomicBinOp->getSourceRange();
5969 NoteLoc = NotScalarExpr->getExprLoc();
5970 NoteRange = NotScalarExpr->getSourceRange();
5971 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005972 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005973 ErrorFound = NotAnAssignmentOp;
5974 ErrorLoc = AtomicBody->getExprLoc();
5975 ErrorRange = AtomicBody->getSourceRange();
5976 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5977 : AtomicBody->getExprLoc();
5978 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5979 : AtomicBody->getSourceRange();
5980 }
5981 } else {
5982 ErrorFound = NotAnExpression;
5983 NoteLoc = ErrorLoc = Body->getLocStart();
5984 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005985 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005986 if (ErrorFound != NoError) {
5987 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5988 << ErrorRange;
5989 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5990 << NoteRange;
5991 return StmtError();
5992 } else if (CurContext->isDependentContext())
5993 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005994 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005995 // If clause is update:
5996 // x++;
5997 // x--;
5998 // ++x;
5999 // --x;
6000 // x binop= expr;
6001 // x = x binop expr;
6002 // x = expr binop x;
6003 OpenMPAtomicUpdateChecker Checker(*this);
6004 if (Checker.checkStatement(
6005 Body, (AtomicKind == OMPC_update)
6006 ? diag::err_omp_atomic_update_not_expression_statement
6007 : diag::err_omp_atomic_not_expression_statement,
6008 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006009 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006010 if (!CurContext->isDependentContext()) {
6011 E = Checker.getExpr();
6012 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006013 UE = Checker.getUpdateExpr();
6014 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006015 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006016 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006017 enum {
6018 NotAnAssignmentOp,
6019 NotACompoundStatement,
6020 NotTwoSubstatements,
6021 NotASpecificExpression,
6022 NoError
6023 } ErrorFound = NoError;
6024 SourceLocation ErrorLoc, NoteLoc;
6025 SourceRange ErrorRange, NoteRange;
6026 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6027 // If clause is a capture:
6028 // v = x++;
6029 // v = x--;
6030 // v = ++x;
6031 // v = --x;
6032 // v = x binop= expr;
6033 // v = x = x binop expr;
6034 // v = x = expr binop x;
6035 auto *AtomicBinOp =
6036 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6037 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6038 V = AtomicBinOp->getLHS();
6039 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6040 OpenMPAtomicUpdateChecker Checker(*this);
6041 if (Checker.checkStatement(
6042 Body, diag::err_omp_atomic_capture_not_expression_statement,
6043 diag::note_omp_atomic_update))
6044 return StmtError();
6045 E = Checker.getExpr();
6046 X = Checker.getX();
6047 UE = Checker.getUpdateExpr();
6048 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6049 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006050 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006051 ErrorLoc = AtomicBody->getExprLoc();
6052 ErrorRange = AtomicBody->getSourceRange();
6053 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6054 : AtomicBody->getExprLoc();
6055 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6056 : AtomicBody->getSourceRange();
6057 ErrorFound = NotAnAssignmentOp;
6058 }
6059 if (ErrorFound != NoError) {
6060 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6061 << ErrorRange;
6062 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6063 return StmtError();
6064 } else if (CurContext->isDependentContext()) {
6065 UE = V = E = X = nullptr;
6066 }
6067 } else {
6068 // If clause is a capture:
6069 // { v = x; x = expr; }
6070 // { v = x; x++; }
6071 // { v = x; x--; }
6072 // { v = x; ++x; }
6073 // { v = x; --x; }
6074 // { v = x; x binop= expr; }
6075 // { v = x; x = x binop expr; }
6076 // { v = x; x = expr binop x; }
6077 // { x++; v = x; }
6078 // { x--; v = x; }
6079 // { ++x; v = x; }
6080 // { --x; v = x; }
6081 // { x binop= expr; v = x; }
6082 // { x = x binop expr; v = x; }
6083 // { x = expr binop x; v = x; }
6084 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6085 // Check that this is { expr1; expr2; }
6086 if (CS->size() == 2) {
6087 auto *First = CS->body_front();
6088 auto *Second = CS->body_back();
6089 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6090 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6091 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6092 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6093 // Need to find what subexpression is 'v' and what is 'x'.
6094 OpenMPAtomicUpdateChecker Checker(*this);
6095 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6096 BinaryOperator *BinOp = nullptr;
6097 if (IsUpdateExprFound) {
6098 BinOp = dyn_cast<BinaryOperator>(First);
6099 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6100 }
6101 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6102 // { v = x; x++; }
6103 // { v = x; x--; }
6104 // { v = x; ++x; }
6105 // { v = x; --x; }
6106 // { v = x; x binop= expr; }
6107 // { v = x; x = x binop expr; }
6108 // { v = x; x = expr binop x; }
6109 // Check that the first expression has form v = x.
6110 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6111 llvm::FoldingSetNodeID XId, PossibleXId;
6112 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6113 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6114 IsUpdateExprFound = XId == PossibleXId;
6115 if (IsUpdateExprFound) {
6116 V = BinOp->getLHS();
6117 X = Checker.getX();
6118 E = Checker.getExpr();
6119 UE = Checker.getUpdateExpr();
6120 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006121 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006122 }
6123 }
6124 if (!IsUpdateExprFound) {
6125 IsUpdateExprFound = !Checker.checkStatement(First);
6126 BinOp = nullptr;
6127 if (IsUpdateExprFound) {
6128 BinOp = dyn_cast<BinaryOperator>(Second);
6129 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6130 }
6131 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6132 // { x++; v = x; }
6133 // { x--; v = x; }
6134 // { ++x; v = x; }
6135 // { --x; v = x; }
6136 // { x binop= expr; v = x; }
6137 // { x = x binop expr; v = x; }
6138 // { x = expr binop x; v = x; }
6139 // Check that the second expression has form v = x.
6140 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6141 llvm::FoldingSetNodeID XId, PossibleXId;
6142 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6143 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6144 IsUpdateExprFound = XId == PossibleXId;
6145 if (IsUpdateExprFound) {
6146 V = BinOp->getLHS();
6147 X = Checker.getX();
6148 E = Checker.getExpr();
6149 UE = Checker.getUpdateExpr();
6150 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006151 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006152 }
6153 }
6154 }
6155 if (!IsUpdateExprFound) {
6156 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006157 auto *FirstExpr = dyn_cast<Expr>(First);
6158 auto *SecondExpr = dyn_cast<Expr>(Second);
6159 if (!FirstExpr || !SecondExpr ||
6160 !(FirstExpr->isInstantiationDependent() ||
6161 SecondExpr->isInstantiationDependent())) {
6162 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6163 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006164 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006165 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6166 : First->getLocStart();
6167 NoteRange = ErrorRange = FirstBinOp
6168 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006169 : SourceRange(ErrorLoc, ErrorLoc);
6170 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006171 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6172 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6173 ErrorFound = NotAnAssignmentOp;
6174 NoteLoc = ErrorLoc = SecondBinOp
6175 ? SecondBinOp->getOperatorLoc()
6176 : Second->getLocStart();
6177 NoteRange = ErrorRange =
6178 SecondBinOp ? SecondBinOp->getSourceRange()
6179 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006180 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006181 auto *PossibleXRHSInFirst =
6182 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6183 auto *PossibleXLHSInSecond =
6184 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6185 llvm::FoldingSetNodeID X1Id, X2Id;
6186 PossibleXRHSInFirst->Profile(X1Id, Context,
6187 /*Canonical=*/true);
6188 PossibleXLHSInSecond->Profile(X2Id, Context,
6189 /*Canonical=*/true);
6190 IsUpdateExprFound = X1Id == X2Id;
6191 if (IsUpdateExprFound) {
6192 V = FirstBinOp->getLHS();
6193 X = SecondBinOp->getLHS();
6194 E = SecondBinOp->getRHS();
6195 UE = nullptr;
6196 IsXLHSInRHSPart = false;
6197 IsPostfixUpdate = true;
6198 } else {
6199 ErrorFound = NotASpecificExpression;
6200 ErrorLoc = FirstBinOp->getExprLoc();
6201 ErrorRange = FirstBinOp->getSourceRange();
6202 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6203 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6204 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006205 }
6206 }
6207 }
6208 }
6209 } else {
6210 NoteLoc = ErrorLoc = Body->getLocStart();
6211 NoteRange = ErrorRange =
6212 SourceRange(Body->getLocStart(), Body->getLocStart());
6213 ErrorFound = NotTwoSubstatements;
6214 }
6215 } else {
6216 NoteLoc = ErrorLoc = Body->getLocStart();
6217 NoteRange = ErrorRange =
6218 SourceRange(Body->getLocStart(), Body->getLocStart());
6219 ErrorFound = NotACompoundStatement;
6220 }
6221 if (ErrorFound != NoError) {
6222 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6223 << ErrorRange;
6224 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6225 return StmtError();
6226 } else if (CurContext->isDependentContext()) {
6227 UE = V = E = X = nullptr;
6228 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006229 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006230 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006231
6232 getCurFunction()->setHasBranchProtectedScope();
6233
Alexey Bataev62cec442014-11-18 10:14:22 +00006234 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006235 X, V, E, UE, IsXLHSInRHSPart,
6236 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006237}
6238
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006239StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6240 Stmt *AStmt,
6241 SourceLocation StartLoc,
6242 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006243 if (!AStmt)
6244 return StmtError();
6245
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006246 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6247 // 1.2.2 OpenMP Language Terminology
6248 // Structured block - An executable statement with a single entry at the
6249 // top and a single exit at the bottom.
6250 // The point of exit cannot be a branch out of the structured block.
6251 // longjmp() and throw() must not violate the entry/exit criteria.
6252 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006253
Alexey Bataev13314bf2014-10-09 04:18:56 +00006254 // OpenMP [2.16, Nesting of Regions]
6255 // If specified, a teams construct must be contained within a target
6256 // construct. That target construct must contain no statements or directives
6257 // outside of the teams construct.
6258 if (DSAStack->hasInnerTeamsRegion()) {
6259 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6260 bool OMPTeamsFound = true;
6261 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6262 auto I = CS->body_begin();
6263 while (I != CS->body_end()) {
6264 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6265 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6266 OMPTeamsFound = false;
6267 break;
6268 }
6269 ++I;
6270 }
6271 assert(I != CS->body_end() && "Not found statement");
6272 S = *I;
6273 }
6274 if (!OMPTeamsFound) {
6275 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6276 Diag(DSAStack->getInnerTeamsRegionLoc(),
6277 diag::note_omp_nested_teams_construct_here);
6278 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6279 << isa<OMPExecutableDirective>(S);
6280 return StmtError();
6281 }
6282 }
6283
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006284 getCurFunction()->setHasBranchProtectedScope();
6285
6286 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6287}
6288
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006289StmtResult
6290Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6291 Stmt *AStmt, SourceLocation StartLoc,
6292 SourceLocation EndLoc) {
6293 if (!AStmt)
6294 return StmtError();
6295
6296 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6297 // 1.2.2 OpenMP Language Terminology
6298 // Structured block - An executable statement with a single entry at the
6299 // top and a single exit at the bottom.
6300 // The point of exit cannot be a branch out of the structured block.
6301 // longjmp() and throw() must not violate the entry/exit criteria.
6302 CS->getCapturedDecl()->setNothrow();
6303
6304 getCurFunction()->setHasBranchProtectedScope();
6305
6306 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6307 AStmt);
6308}
6309
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006310StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6311 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6312 SourceLocation EndLoc,
6313 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6314 if (!AStmt)
6315 return StmtError();
6316
6317 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6318 // 1.2.2 OpenMP Language Terminology
6319 // Structured block - An executable statement with a single entry at the
6320 // top and a single exit at the bottom.
6321 // The point of exit cannot be a branch out of the structured block.
6322 // longjmp() and throw() must not violate the entry/exit criteria.
6323 CS->getCapturedDecl()->setNothrow();
6324
6325 OMPLoopDirective::HelperExprs B;
6326 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6327 // define the nested loops number.
6328 unsigned NestedLoopCount =
6329 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6330 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6331 VarsWithImplicitDSA, B);
6332 if (NestedLoopCount == 0)
6333 return StmtError();
6334
6335 assert((CurContext->isDependentContext() || B.builtAll()) &&
6336 "omp target parallel for loop exprs were not built");
6337
6338 if (!CurContext->isDependentContext()) {
6339 // Finalize the clauses that need pre-built expressions for CodeGen.
6340 for (auto C : Clauses) {
6341 if (auto LC = dyn_cast<OMPLinearClause>(C))
6342 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006343 B.NumIterations, *this, CurScope,
6344 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006345 return StmtError();
6346 }
6347 }
6348
6349 getCurFunction()->setHasBranchProtectedScope();
6350 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6351 NestedLoopCount, Clauses, AStmt,
6352 B, DSAStack->isCancelRegion());
6353}
6354
Samuel Antaodf67fc42016-01-19 19:15:56 +00006355/// \brief Check for existence of a map clause in the list of clauses.
6356static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6357 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6358 I != E; ++I) {
6359 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6360 return true;
6361 }
6362 }
6363
6364 return false;
6365}
6366
Michael Wong65f367f2015-07-21 13:44:28 +00006367StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6368 Stmt *AStmt,
6369 SourceLocation StartLoc,
6370 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006371 if (!AStmt)
6372 return StmtError();
6373
6374 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6375
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006376 // OpenMP [2.10.1, Restrictions, p. 97]
6377 // At least one map clause must appear on the directive.
6378 if (!HasMapClause(Clauses)) {
6379 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6380 getOpenMPDirectiveName(OMPD_target_data);
6381 return StmtError();
6382 }
6383
Michael Wong65f367f2015-07-21 13:44:28 +00006384 getCurFunction()->setHasBranchProtectedScope();
6385
6386 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6387 AStmt);
6388}
6389
Samuel Antaodf67fc42016-01-19 19:15:56 +00006390StmtResult
6391Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6392 SourceLocation StartLoc,
6393 SourceLocation EndLoc) {
6394 // OpenMP [2.10.2, Restrictions, p. 99]
6395 // At least one map clause must appear on the directive.
6396 if (!HasMapClause(Clauses)) {
6397 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6398 << getOpenMPDirectiveName(OMPD_target_enter_data);
6399 return StmtError();
6400 }
6401
6402 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6403 Clauses);
6404}
6405
Samuel Antao72590762016-01-19 20:04:50 +00006406StmtResult
6407Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6408 SourceLocation StartLoc,
6409 SourceLocation EndLoc) {
6410 // OpenMP [2.10.3, Restrictions, p. 102]
6411 // At least one map clause must appear on the directive.
6412 if (!HasMapClause(Clauses)) {
6413 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6414 << getOpenMPDirectiveName(OMPD_target_exit_data);
6415 return StmtError();
6416 }
6417
6418 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6419}
6420
Alexey Bataev13314bf2014-10-09 04:18:56 +00006421StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6422 Stmt *AStmt, SourceLocation StartLoc,
6423 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006424 if (!AStmt)
6425 return StmtError();
6426
Alexey Bataev13314bf2014-10-09 04:18:56 +00006427 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6428 // 1.2.2 OpenMP Language Terminology
6429 // Structured block - An executable statement with a single entry at the
6430 // top and a single exit at the bottom.
6431 // The point of exit cannot be a branch out of the structured block.
6432 // longjmp() and throw() must not violate the entry/exit criteria.
6433 CS->getCapturedDecl()->setNothrow();
6434
6435 getCurFunction()->setHasBranchProtectedScope();
6436
6437 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6438}
6439
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006440StmtResult
6441Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6442 SourceLocation EndLoc,
6443 OpenMPDirectiveKind CancelRegion) {
6444 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6445 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6446 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6447 << getOpenMPDirectiveName(CancelRegion);
6448 return StmtError();
6449 }
6450 if (DSAStack->isParentNowaitRegion()) {
6451 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6452 return StmtError();
6453 }
6454 if (DSAStack->isParentOrderedRegion()) {
6455 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6456 return StmtError();
6457 }
6458 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6459 CancelRegion);
6460}
6461
Alexey Bataev87933c72015-09-18 08:07:34 +00006462StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6463 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006464 SourceLocation EndLoc,
6465 OpenMPDirectiveKind CancelRegion) {
6466 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6467 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6468 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6469 << getOpenMPDirectiveName(CancelRegion);
6470 return StmtError();
6471 }
6472 if (DSAStack->isParentNowaitRegion()) {
6473 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6474 return StmtError();
6475 }
6476 if (DSAStack->isParentOrderedRegion()) {
6477 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6478 return StmtError();
6479 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006480 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006481 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6482 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006483}
6484
Alexey Bataev382967a2015-12-08 12:06:20 +00006485static bool checkGrainsizeNumTasksClauses(Sema &S,
6486 ArrayRef<OMPClause *> Clauses) {
6487 OMPClause *PrevClause = nullptr;
6488 bool ErrorFound = false;
6489 for (auto *C : Clauses) {
6490 if (C->getClauseKind() == OMPC_grainsize ||
6491 C->getClauseKind() == OMPC_num_tasks) {
6492 if (!PrevClause)
6493 PrevClause = C;
6494 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6495 S.Diag(C->getLocStart(),
6496 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6497 << getOpenMPClauseName(C->getClauseKind())
6498 << getOpenMPClauseName(PrevClause->getClauseKind());
6499 S.Diag(PrevClause->getLocStart(),
6500 diag::note_omp_previous_grainsize_num_tasks)
6501 << getOpenMPClauseName(PrevClause->getClauseKind());
6502 ErrorFound = true;
6503 }
6504 }
6505 }
6506 return ErrorFound;
6507}
6508
Alexey Bataev49f6e782015-12-01 04:18:41 +00006509StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6510 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6511 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006512 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006513 if (!AStmt)
6514 return StmtError();
6515
6516 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6517 OMPLoopDirective::HelperExprs B;
6518 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6519 // define the nested loops number.
6520 unsigned NestedLoopCount =
6521 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006522 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006523 VarsWithImplicitDSA, B);
6524 if (NestedLoopCount == 0)
6525 return StmtError();
6526
6527 assert((CurContext->isDependentContext() || B.builtAll()) &&
6528 "omp for loop exprs were not built");
6529
Alexey Bataev382967a2015-12-08 12:06:20 +00006530 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6531 // The grainsize clause and num_tasks clause are mutually exclusive and may
6532 // not appear on the same taskloop directive.
6533 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6534 return StmtError();
6535
Alexey Bataev49f6e782015-12-01 04:18:41 +00006536 getCurFunction()->setHasBranchProtectedScope();
6537 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6538 NestedLoopCount, Clauses, AStmt, B);
6539}
6540
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006541StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6542 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6543 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006544 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006545 if (!AStmt)
6546 return StmtError();
6547
6548 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6549 OMPLoopDirective::HelperExprs B;
6550 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6551 // define the nested loops number.
6552 unsigned NestedLoopCount =
6553 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6554 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6555 VarsWithImplicitDSA, B);
6556 if (NestedLoopCount == 0)
6557 return StmtError();
6558
6559 assert((CurContext->isDependentContext() || B.builtAll()) &&
6560 "omp for loop exprs were not built");
6561
Alexey Bataev5a3af132016-03-29 08:58:54 +00006562 if (!CurContext->isDependentContext()) {
6563 // Finalize the clauses that need pre-built expressions for CodeGen.
6564 for (auto C : Clauses) {
6565 if (auto LC = dyn_cast<OMPLinearClause>(C))
6566 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006567 B.NumIterations, *this, CurScope,
6568 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006569 return StmtError();
6570 }
6571 }
6572
Alexey Bataev382967a2015-12-08 12:06:20 +00006573 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6574 // The grainsize clause and num_tasks clause are mutually exclusive and may
6575 // not appear on the same taskloop directive.
6576 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6577 return StmtError();
6578
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006579 getCurFunction()->setHasBranchProtectedScope();
6580 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6581 NestedLoopCount, Clauses, AStmt, B);
6582}
6583
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006584StmtResult Sema::ActOnOpenMPDistributeDirective(
6585 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6586 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006587 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006588 if (!AStmt)
6589 return StmtError();
6590
6591 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6592 OMPLoopDirective::HelperExprs B;
6593 // In presence of clause 'collapse' with number of loops, it will
6594 // define the nested loops number.
6595 unsigned NestedLoopCount =
6596 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6597 nullptr /*ordered not a clause on distribute*/, AStmt,
6598 *this, *DSAStack, VarsWithImplicitDSA, B);
6599 if (NestedLoopCount == 0)
6600 return StmtError();
6601
6602 assert((CurContext->isDependentContext() || B.builtAll()) &&
6603 "omp for loop exprs were not built");
6604
6605 getCurFunction()->setHasBranchProtectedScope();
6606 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6607 NestedLoopCount, Clauses, AStmt, B);
6608}
6609
Alexey Bataeved09d242014-05-28 05:53:51 +00006610OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006611 SourceLocation StartLoc,
6612 SourceLocation LParenLoc,
6613 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006614 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006615 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006616 case OMPC_final:
6617 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6618 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006619 case OMPC_num_threads:
6620 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6621 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006622 case OMPC_safelen:
6623 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6624 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006625 case OMPC_simdlen:
6626 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6627 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006628 case OMPC_collapse:
6629 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6630 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006631 case OMPC_ordered:
6632 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6633 break;
Michael Wonge710d542015-08-07 16:16:36 +00006634 case OMPC_device:
6635 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6636 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006637 case OMPC_num_teams:
6638 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6639 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006640 case OMPC_thread_limit:
6641 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6642 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006643 case OMPC_priority:
6644 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6645 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006646 case OMPC_grainsize:
6647 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6648 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006649 case OMPC_num_tasks:
6650 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6651 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006652 case OMPC_hint:
6653 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6654 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006655 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006656 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006657 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006658 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006659 case OMPC_private:
6660 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006661 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006662 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006663 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006664 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006665 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006666 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006667 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006668 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006669 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006670 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006671 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006672 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006673 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006674 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006675 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006676 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006677 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006678 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006679 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006680 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006681 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006682 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006683 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006684 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006685 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006686 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006687 llvm_unreachable("Clause is not allowed.");
6688 }
6689 return Res;
6690}
6691
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006692OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6693 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006694 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006695 SourceLocation NameModifierLoc,
6696 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006697 SourceLocation EndLoc) {
6698 Expr *ValExpr = Condition;
6699 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6700 !Condition->isInstantiationDependent() &&
6701 !Condition->containsUnexpandedParameterPack()) {
6702 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006703 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006704 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006705 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006706
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006707 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006708 }
6709
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006710 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6711 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006712}
6713
Alexey Bataev3778b602014-07-17 07:32:53 +00006714OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6715 SourceLocation StartLoc,
6716 SourceLocation LParenLoc,
6717 SourceLocation EndLoc) {
6718 Expr *ValExpr = Condition;
6719 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6720 !Condition->isInstantiationDependent() &&
6721 !Condition->containsUnexpandedParameterPack()) {
6722 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6723 Condition->getExprLoc(), Condition);
6724 if (Val.isInvalid())
6725 return nullptr;
6726
6727 ValExpr = Val.get();
6728 }
6729
6730 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6731}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006732ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6733 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006734 if (!Op)
6735 return ExprError();
6736
6737 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6738 public:
6739 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006740 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006741 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6742 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006743 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6744 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006745 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6746 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006747 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6748 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006749 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6750 QualType T,
6751 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006752 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6753 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006754 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6755 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006756 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006757 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006758 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006759 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6760 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006761 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6762 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006763 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6764 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006765 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006766 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006767 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006768 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6769 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006770 llvm_unreachable("conversion functions are permitted");
6771 }
6772 } ConvertDiagnoser;
6773 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6774}
6775
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006776static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006777 OpenMPClauseKind CKind,
6778 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006779 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6780 !ValExpr->isInstantiationDependent()) {
6781 SourceLocation Loc = ValExpr->getExprLoc();
6782 ExprResult Value =
6783 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6784 if (Value.isInvalid())
6785 return false;
6786
6787 ValExpr = Value.get();
6788 // The expression must evaluate to a non-negative integer value.
6789 llvm::APSInt Result;
6790 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006791 Result.isSigned() &&
6792 !((!StrictlyPositive && Result.isNonNegative()) ||
6793 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006794 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006795 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6796 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006797 return false;
6798 }
6799 }
6800 return true;
6801}
6802
Alexey Bataev568a8332014-03-06 06:15:19 +00006803OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6804 SourceLocation StartLoc,
6805 SourceLocation LParenLoc,
6806 SourceLocation EndLoc) {
6807 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006808
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006809 // OpenMP [2.5, Restrictions]
6810 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006811 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6812 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006813 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006814
Alexey Bataeved09d242014-05-28 05:53:51 +00006815 return new (Context)
6816 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006817}
6818
Alexey Bataev62c87d22014-03-21 04:51:18 +00006819ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006820 OpenMPClauseKind CKind,
6821 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006822 if (!E)
6823 return ExprError();
6824 if (E->isValueDependent() || E->isTypeDependent() ||
6825 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006826 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006827 llvm::APSInt Result;
6828 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6829 if (ICE.isInvalid())
6830 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006831 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6832 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006833 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006834 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6835 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006836 return ExprError();
6837 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006838 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6839 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6840 << E->getSourceRange();
6841 return ExprError();
6842 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006843 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6844 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006845 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006846 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006847 return ICE;
6848}
6849
6850OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6851 SourceLocation LParenLoc,
6852 SourceLocation EndLoc) {
6853 // OpenMP [2.8.1, simd construct, Description]
6854 // The parameter of the safelen clause must be a constant
6855 // positive integer expression.
6856 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6857 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006858 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006859 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006860 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006861}
6862
Alexey Bataev66b15b52015-08-21 11:14:16 +00006863OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6864 SourceLocation LParenLoc,
6865 SourceLocation EndLoc) {
6866 // OpenMP [2.8.1, simd construct, Description]
6867 // The parameter of the simdlen clause must be a constant
6868 // positive integer expression.
6869 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6870 if (Simdlen.isInvalid())
6871 return nullptr;
6872 return new (Context)
6873 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6874}
6875
Alexander Musman64d33f12014-06-04 07:53:32 +00006876OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6877 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006878 SourceLocation LParenLoc,
6879 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006880 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006881 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006882 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006883 // The parameter of the collapse clause must be a constant
6884 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006885 ExprResult NumForLoopsResult =
6886 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6887 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006888 return nullptr;
6889 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006890 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006891}
6892
Alexey Bataev10e775f2015-07-30 11:36:16 +00006893OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6894 SourceLocation EndLoc,
6895 SourceLocation LParenLoc,
6896 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006897 // OpenMP [2.7.1, loop construct, Description]
6898 // OpenMP [2.8.1, simd construct, Description]
6899 // OpenMP [2.9.6, distribute construct, Description]
6900 // The parameter of the ordered clause must be a constant
6901 // positive integer expression if any.
6902 if (NumForLoops && LParenLoc.isValid()) {
6903 ExprResult NumForLoopsResult =
6904 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6905 if (NumForLoopsResult.isInvalid())
6906 return nullptr;
6907 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006908 } else
6909 NumForLoops = nullptr;
6910 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006911 return new (Context)
6912 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6913}
6914
Alexey Bataeved09d242014-05-28 05:53:51 +00006915OMPClause *Sema::ActOnOpenMPSimpleClause(
6916 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6917 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006918 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006919 switch (Kind) {
6920 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006921 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006922 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6923 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006924 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006925 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006926 Res = ActOnOpenMPProcBindClause(
6927 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6928 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006929 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006930 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006931 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006932 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006933 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006934 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006935 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006936 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006937 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006938 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006939 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006940 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006941 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006942 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006943 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006944 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006945 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006946 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006947 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006948 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006949 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006950 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006951 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006952 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006953 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006954 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006955 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006956 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006957 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006958 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006959 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006960 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006961 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006962 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006963 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006964 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006965 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006966 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006967 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006968 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006969 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006970 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006971 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006972 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006973 llvm_unreachable("Clause is not allowed.");
6974 }
6975 return Res;
6976}
6977
Alexey Bataev6402bca2015-12-28 07:25:51 +00006978static std::string
6979getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6980 ArrayRef<unsigned> Exclude = llvm::None) {
6981 std::string Values;
6982 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6983 unsigned Skipped = Exclude.size();
6984 auto S = Exclude.begin(), E = Exclude.end();
6985 for (unsigned i = First; i < Last; ++i) {
6986 if (std::find(S, E, i) != E) {
6987 --Skipped;
6988 continue;
6989 }
6990 Values += "'";
6991 Values += getOpenMPSimpleClauseTypeName(K, i);
6992 Values += "'";
6993 if (i == Bound - Skipped)
6994 Values += " or ";
6995 else if (i != Bound + 1 - Skipped)
6996 Values += ", ";
6997 }
6998 return Values;
6999}
7000
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007001OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7002 SourceLocation KindKwLoc,
7003 SourceLocation StartLoc,
7004 SourceLocation LParenLoc,
7005 SourceLocation EndLoc) {
7006 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00007007 static_assert(OMPC_DEFAULT_unknown > 0,
7008 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007009 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007010 << getListOfPossibleValues(OMPC_default, /*First=*/0,
7011 /*Last=*/OMPC_DEFAULT_unknown)
7012 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007013 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007014 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00007015 switch (Kind) {
7016 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007017 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007018 break;
7019 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007020 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007021 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007022 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007023 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00007024 break;
7025 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007026 return new (Context)
7027 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007028}
7029
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007030OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7031 SourceLocation KindKwLoc,
7032 SourceLocation StartLoc,
7033 SourceLocation LParenLoc,
7034 SourceLocation EndLoc) {
7035 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007036 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00007037 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7038 /*Last=*/OMPC_PROC_BIND_unknown)
7039 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007040 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007041 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007042 return new (Context)
7043 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007044}
7045
Alexey Bataev56dafe82014-06-20 07:16:17 +00007046OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007047 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007048 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007049 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007050 SourceLocation EndLoc) {
7051 OMPClause *Res = nullptr;
7052 switch (Kind) {
7053 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007054 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7055 assert(Argument.size() == NumberOfElements &&
7056 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007057 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007058 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7059 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7060 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7061 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7062 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007063 break;
7064 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007065 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7066 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7067 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7068 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007069 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007070 case OMPC_dist_schedule:
7071 Res = ActOnOpenMPDistScheduleClause(
7072 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7073 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7074 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007075 case OMPC_defaultmap:
7076 enum { Modifier, DefaultmapKind };
7077 Res = ActOnOpenMPDefaultmapClause(
7078 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7079 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7080 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7081 ArgumentLoc[DefaultmapKind], EndLoc);
7082 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007083 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007084 case OMPC_num_threads:
7085 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007086 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007087 case OMPC_collapse:
7088 case OMPC_default:
7089 case OMPC_proc_bind:
7090 case OMPC_private:
7091 case OMPC_firstprivate:
7092 case OMPC_lastprivate:
7093 case OMPC_shared:
7094 case OMPC_reduction:
7095 case OMPC_linear:
7096 case OMPC_aligned:
7097 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007098 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007099 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007100 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007101 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007102 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007103 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007104 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007105 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007106 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007107 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007108 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007109 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007110 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007111 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007112 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007113 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007114 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007115 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007116 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007117 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007118 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007119 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007120 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007121 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007122 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007123 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007124 llvm_unreachable("Clause is not allowed.");
7125 }
7126 return Res;
7127}
7128
Alexey Bataev6402bca2015-12-28 07:25:51 +00007129static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7130 OpenMPScheduleClauseModifier M2,
7131 SourceLocation M1Loc, SourceLocation M2Loc) {
7132 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7133 SmallVector<unsigned, 2> Excluded;
7134 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7135 Excluded.push_back(M2);
7136 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7137 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7138 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7139 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7140 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7141 << getListOfPossibleValues(OMPC_schedule,
7142 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7143 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7144 Excluded)
7145 << getOpenMPClauseName(OMPC_schedule);
7146 return true;
7147 }
7148 return false;
7149}
7150
Alexey Bataev56dafe82014-06-20 07:16:17 +00007151OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007152 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007153 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007154 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7155 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7156 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7157 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7158 return nullptr;
7159 // OpenMP, 2.7.1, Loop Construct, Restrictions
7160 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7161 // but not both.
7162 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7163 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7164 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7165 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7166 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7167 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7168 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7169 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7170 return nullptr;
7171 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007172 if (Kind == OMPC_SCHEDULE_unknown) {
7173 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007174 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7175 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7176 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7177 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7178 Exclude);
7179 } else {
7180 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7181 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007182 }
7183 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7184 << Values << getOpenMPClauseName(OMPC_schedule);
7185 return nullptr;
7186 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007187 // OpenMP, 2.7.1, Loop Construct, Restrictions
7188 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7189 // schedule(guided).
7190 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7191 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7192 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7193 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7194 diag::err_omp_schedule_nonmonotonic_static);
7195 return nullptr;
7196 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007197 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007198 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007199 if (ChunkSize) {
7200 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7201 !ChunkSize->isInstantiationDependent() &&
7202 !ChunkSize->containsUnexpandedParameterPack()) {
7203 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7204 ExprResult Val =
7205 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7206 if (Val.isInvalid())
7207 return nullptr;
7208
7209 ValExpr = Val.get();
7210
7211 // OpenMP [2.7.1, Restrictions]
7212 // chunk_size must be a loop invariant integer expression with a positive
7213 // value.
7214 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007215 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7216 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7217 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007218 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007219 return nullptr;
7220 }
7221 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007222 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7223 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7224 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007225 }
7226 }
7227 }
7228
Alexey Bataev6402bca2015-12-28 07:25:51 +00007229 return new (Context)
7230 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007231 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007232}
7233
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007234OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7235 SourceLocation StartLoc,
7236 SourceLocation EndLoc) {
7237 OMPClause *Res = nullptr;
7238 switch (Kind) {
7239 case OMPC_ordered:
7240 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7241 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007242 case OMPC_nowait:
7243 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7244 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007245 case OMPC_untied:
7246 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7247 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007248 case OMPC_mergeable:
7249 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7250 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007251 case OMPC_read:
7252 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7253 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007254 case OMPC_write:
7255 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7256 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007257 case OMPC_update:
7258 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7259 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007260 case OMPC_capture:
7261 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7262 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007263 case OMPC_seq_cst:
7264 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7265 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007266 case OMPC_threads:
7267 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7268 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007269 case OMPC_simd:
7270 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7271 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007272 case OMPC_nogroup:
7273 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7274 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007275 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007276 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007277 case OMPC_num_threads:
7278 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007279 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007280 case OMPC_collapse:
7281 case OMPC_schedule:
7282 case OMPC_private:
7283 case OMPC_firstprivate:
7284 case OMPC_lastprivate:
7285 case OMPC_shared:
7286 case OMPC_reduction:
7287 case OMPC_linear:
7288 case OMPC_aligned:
7289 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007290 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007291 case OMPC_default:
7292 case OMPC_proc_bind:
7293 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007294 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007295 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007296 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007297 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007298 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007299 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007300 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007301 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007302 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007303 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007304 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007305 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007306 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007307 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007308 llvm_unreachable("Clause is not allowed.");
7309 }
7310 return Res;
7311}
7312
Alexey Bataev236070f2014-06-20 11:19:47 +00007313OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7314 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007315 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007316 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7317}
7318
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007319OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7320 SourceLocation EndLoc) {
7321 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7322}
7323
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007324OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7325 SourceLocation EndLoc) {
7326 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7327}
7328
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007329OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7330 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007331 return new (Context) OMPReadClause(StartLoc, EndLoc);
7332}
7333
Alexey Bataevdea47612014-07-23 07:46:59 +00007334OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7335 SourceLocation EndLoc) {
7336 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7337}
7338
Alexey Bataev67a4f222014-07-23 10:25:33 +00007339OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7340 SourceLocation EndLoc) {
7341 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7342}
7343
Alexey Bataev459dec02014-07-24 06:46:57 +00007344OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7345 SourceLocation EndLoc) {
7346 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7347}
7348
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007349OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7350 SourceLocation EndLoc) {
7351 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7352}
7353
Alexey Bataev346265e2015-09-25 10:37:12 +00007354OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7355 SourceLocation EndLoc) {
7356 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7357}
7358
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007359OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7360 SourceLocation EndLoc) {
7361 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7362}
7363
Alexey Bataevb825de12015-12-07 10:51:44 +00007364OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7365 SourceLocation EndLoc) {
7366 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7367}
7368
Alexey Bataevc5e02582014-06-16 07:08:35 +00007369OMPClause *Sema::ActOnOpenMPVarListClause(
7370 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7371 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7372 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007373 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007374 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7375 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7376 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007377 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007378 switch (Kind) {
7379 case OMPC_private:
7380 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7381 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007382 case OMPC_firstprivate:
7383 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7384 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007385 case OMPC_lastprivate:
7386 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7387 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007388 case OMPC_shared:
7389 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7390 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007391 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007392 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7393 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007394 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007395 case OMPC_linear:
7396 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007397 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007398 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007399 case OMPC_aligned:
7400 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7401 ColonLoc, EndLoc);
7402 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007403 case OMPC_copyin:
7404 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7405 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007406 case OMPC_copyprivate:
7407 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7408 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007409 case OMPC_flush:
7410 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7411 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007412 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007413 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7414 StartLoc, LParenLoc, EndLoc);
7415 break;
7416 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007417 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7418 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7419 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007420 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007421 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007422 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007423 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007424 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007425 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007426 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007427 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007428 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007429 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007430 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007431 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007432 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007433 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007434 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007435 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007436 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007437 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007438 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007439 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007440 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007441 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007442 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007443 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007444 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007445 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007446 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007447 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007448 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007449 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007450 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007451 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007452 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007453 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007454 llvm_unreachable("Clause is not allowed.");
7455 }
7456 return Res;
7457}
7458
Alexey Bataev90c228f2016-02-08 09:29:13 +00007459ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007460 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007461 ExprResult Res = BuildDeclRefExpr(
7462 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7463 if (!Res.isUsable())
7464 return ExprError();
7465 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7466 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7467 if (!Res.isUsable())
7468 return ExprError();
7469 }
7470 if (VK != VK_LValue && Res.get()->isGLValue()) {
7471 Res = DefaultLvalueConversion(Res.get());
7472 if (!Res.isUsable())
7473 return ExprError();
7474 }
7475 return Res;
7476}
7477
Alexey Bataev60da77e2016-02-29 05:54:20 +00007478static std::pair<ValueDecl *, bool>
7479getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7480 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007481 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7482 RefExpr->containsUnexpandedParameterPack())
7483 return std::make_pair(nullptr, true);
7484
Alexey Bataevd985eda2016-02-10 11:29:16 +00007485 // OpenMP [3.1, C/C++]
7486 // A list item is a variable name.
7487 // OpenMP [2.9.3.3, Restrictions, p.1]
7488 // A variable that is part of another variable (as an array or
7489 // structure element) cannot appear in a private clause.
7490 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007491 enum {
7492 NoArrayExpr = -1,
7493 ArraySubscript = 0,
7494 OMPArraySection = 1
7495 } IsArrayExpr = NoArrayExpr;
7496 if (AllowArraySection) {
7497 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7498 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7499 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7500 Base = TempASE->getBase()->IgnoreParenImpCasts();
7501 RefExpr = Base;
7502 IsArrayExpr = ArraySubscript;
7503 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7504 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7505 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7506 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7507 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7508 Base = TempASE->getBase()->IgnoreParenImpCasts();
7509 RefExpr = Base;
7510 IsArrayExpr = OMPArraySection;
7511 }
7512 }
7513 ELoc = RefExpr->getExprLoc();
7514 ERange = RefExpr->getSourceRange();
7515 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007516 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7517 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7518 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7519 (S.getCurrentThisType().isNull() || !ME ||
7520 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7521 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007522 if (IsArrayExpr != NoArrayExpr)
7523 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7524 << ERange;
7525 else {
7526 S.Diag(ELoc,
7527 AllowArraySection
7528 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7529 : diag::err_omp_expected_var_name_member_expr)
7530 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7531 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007532 return std::make_pair(nullptr, false);
7533 }
7534 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7535}
7536
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007537OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7538 SourceLocation StartLoc,
7539 SourceLocation LParenLoc,
7540 SourceLocation EndLoc) {
7541 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007542 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007543 for (auto &RefExpr : VarList) {
7544 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007545 SourceLocation ELoc;
7546 SourceRange ERange;
7547 Expr *SimpleRefExpr = RefExpr;
7548 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007549 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007550 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007551 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007552 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007553 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007554 ValueDecl *D = Res.first;
7555 if (!D)
7556 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007557
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007558 QualType Type = D->getType();
7559 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007560
7561 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7562 // A variable that appears in a private clause must not have an incomplete
7563 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007564 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007565 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007566 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007567
Alexey Bataev758e55e2013-09-06 18:03:48 +00007568 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7569 // in a Construct]
7570 // Variables with the predetermined data-sharing attributes may not be
7571 // listed in data-sharing attributes clauses, except for the cases
7572 // listed below. For these exceptions only, listing a predetermined
7573 // variable in a data-sharing attribute clause is allowed and overrides
7574 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007575 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007576 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007577 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7578 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007579 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007580 continue;
7581 }
7582
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007583 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007584 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007585 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007586 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7587 << getOpenMPClauseName(OMPC_private) << Type
7588 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7589 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007590 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007591 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007592 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007593 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007594 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007595 continue;
7596 }
7597
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007598 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7599 // A list item cannot appear in both a map clause and a data-sharing
7600 // attribute clause on the same construct
7601 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007602 if (DSAStack->checkMappableExprComponentListsForDecl(
7603 VD, /* CurrentRegionOnly = */ true,
7604 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7605 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007606 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7607 << getOpenMPClauseName(OMPC_private)
7608 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7609 ReportOriginalDSA(*this, DSAStack, D, DVar);
7610 continue;
7611 }
7612 }
7613
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007614 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7615 // A variable of class type (or array thereof) that appears in a private
7616 // clause requires an accessible, unambiguous default constructor for the
7617 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007618 // Generate helper private variable and initialize it with the default
7619 // value. The address of the original variable is replaced by the address of
7620 // the new private variable in CodeGen. This new variable is not added to
7621 // IdResolver, so the code in the OpenMP region uses original variable for
7622 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007623 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007624 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7625 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007626 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007627 if (VDPrivate->isInvalidDecl())
7628 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007629 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007630 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007631
Alexey Bataev90c228f2016-02-08 09:29:13 +00007632 DeclRefExpr *Ref = nullptr;
7633 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007634 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007635 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7636 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007637 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007638 }
7639
Alexey Bataeved09d242014-05-28 05:53:51 +00007640 if (Vars.empty())
7641 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007642
Alexey Bataev03b340a2014-10-21 03:16:40 +00007643 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7644 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007645}
7646
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007647namespace {
7648class DiagsUninitializedSeveretyRAII {
7649private:
7650 DiagnosticsEngine &Diags;
7651 SourceLocation SavedLoc;
7652 bool IsIgnored;
7653
7654public:
7655 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7656 bool IsIgnored)
7657 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7658 if (!IsIgnored) {
7659 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7660 /*Map*/ diag::Severity::Ignored, Loc);
7661 }
7662 }
7663 ~DiagsUninitializedSeveretyRAII() {
7664 if (!IsIgnored)
7665 Diags.popMappings(SavedLoc);
7666 }
7667};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007668}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007669
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007670OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7671 SourceLocation StartLoc,
7672 SourceLocation LParenLoc,
7673 SourceLocation EndLoc) {
7674 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007675 SmallVector<Expr *, 8> PrivateCopies;
7676 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007677 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007678 bool IsImplicitClause =
7679 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7680 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7681
Alexey Bataeved09d242014-05-28 05:53:51 +00007682 for (auto &RefExpr : VarList) {
7683 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007684 SourceLocation ELoc;
7685 SourceRange ERange;
7686 Expr *SimpleRefExpr = RefExpr;
7687 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007688 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007689 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007690 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007691 PrivateCopies.push_back(nullptr);
7692 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007693 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007694 ValueDecl *D = Res.first;
7695 if (!D)
7696 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007697
Alexey Bataev60da77e2016-02-29 05:54:20 +00007698 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007699 QualType Type = D->getType();
7700 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007701
7702 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7703 // A variable that appears in a private clause must not have an incomplete
7704 // type or a reference type.
7705 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007706 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007707 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007708 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007709
7710 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7711 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007712 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007713 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007714 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007715
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007716 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007717 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007718 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007719 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007720 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007721 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007722 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7723 // A list item that specifies a given variable may not appear in more
7724 // than one clause on the same directive, except that a variable may be
7725 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007726 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007727 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007728 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007729 << getOpenMPClauseName(DVar.CKind)
7730 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007731 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007732 continue;
7733 }
7734
7735 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7736 // in a Construct]
7737 // Variables with the predetermined data-sharing attributes may not be
7738 // listed in data-sharing attributes clauses, except for the cases
7739 // listed below. For these exceptions only, listing a predetermined
7740 // variable in a data-sharing attribute clause is allowed and overrides
7741 // the variable's predetermined data-sharing attributes.
7742 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7743 // in a Construct, C/C++, p.2]
7744 // Variables with const-qualified type having no mutable member may be
7745 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007746 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007747 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7748 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007749 << getOpenMPClauseName(DVar.CKind)
7750 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007751 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007752 continue;
7753 }
7754
Alexey Bataevf29276e2014-06-18 04:14:57 +00007755 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007756 // OpenMP [2.9.3.4, Restrictions, p.2]
7757 // A list item that is private within a parallel region must not appear
7758 // in a firstprivate clause on a worksharing construct if any of the
7759 // worksharing regions arising from the worksharing construct ever bind
7760 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007761 if (isOpenMPWorksharingDirective(CurrDir) &&
7762 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007763 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007764 if (DVar.CKind != OMPC_shared &&
7765 (isOpenMPParallelDirective(DVar.DKind) ||
7766 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007767 Diag(ELoc, diag::err_omp_required_access)
7768 << getOpenMPClauseName(OMPC_firstprivate)
7769 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007770 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007771 continue;
7772 }
7773 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007774 // OpenMP [2.9.3.4, Restrictions, p.3]
7775 // A list item that appears in a reduction clause of a parallel construct
7776 // must not appear in a firstprivate clause on a worksharing or task
7777 // construct if any of the worksharing or task regions arising from the
7778 // worksharing or task construct ever bind to any of the parallel regions
7779 // arising from the parallel construct.
7780 // OpenMP [2.9.3.4, Restrictions, p.4]
7781 // A list item that appears in a reduction clause in worksharing
7782 // construct must not appear in a firstprivate clause in a task construct
7783 // encountered during execution of any of the worksharing regions arising
7784 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007785 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007786 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007787 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007788 [](OpenMPDirectiveKind K) -> bool {
7789 return isOpenMPParallelDirective(K) ||
7790 isOpenMPWorksharingDirective(K);
7791 },
7792 false);
7793 if (DVar.CKind == OMPC_reduction &&
7794 (isOpenMPParallelDirective(DVar.DKind) ||
7795 isOpenMPWorksharingDirective(DVar.DKind))) {
7796 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7797 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007798 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007799 continue;
7800 }
7801 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007802
7803 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7804 // A list item that is private within a teams region must not appear in a
7805 // firstprivate clause on a distribute construct if any of the distribute
7806 // regions arising from the distribute construct ever bind to any of the
7807 // teams regions arising from the teams construct.
7808 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7809 // A list item that appears in a reduction clause of a teams construct
7810 // must not appear in a firstprivate clause on a distribute construct if
7811 // any of the distribute regions arising from the distribute construct
7812 // ever bind to any of the teams regions arising from the teams construct.
7813 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7814 // A list item may appear in a firstprivate or lastprivate clause but not
7815 // both.
7816 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007817 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007818 [](OpenMPDirectiveKind K) -> bool {
7819 return isOpenMPTeamsDirective(K);
7820 },
7821 false);
7822 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7823 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007824 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007825 continue;
7826 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007827 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007828 [](OpenMPDirectiveKind K) -> bool {
7829 return isOpenMPTeamsDirective(K);
7830 },
7831 false);
7832 if (DVar.CKind == OMPC_reduction &&
7833 isOpenMPTeamsDirective(DVar.DKind)) {
7834 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007835 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007836 continue;
7837 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007838 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007839 if (DVar.CKind == OMPC_lastprivate) {
7840 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007841 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007842 continue;
7843 }
7844 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007845 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7846 // A list item cannot appear in both a map clause and a data-sharing
7847 // attribute clause on the same construct
7848 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007849 if (DSAStack->checkMappableExprComponentListsForDecl(
7850 VD, /* CurrentRegionOnly = */ true,
7851 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7852 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007853 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7854 << getOpenMPClauseName(OMPC_firstprivate)
7855 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7856 ReportOriginalDSA(*this, DSAStack, D, DVar);
7857 continue;
7858 }
7859 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007860 }
7861
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007862 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007863 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007864 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007865 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7866 << getOpenMPClauseName(OMPC_firstprivate) << Type
7867 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7868 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007869 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007870 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007871 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007872 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007873 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007874 continue;
7875 }
7876
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007877 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007878 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7879 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007880 // Generate helper private variable and initialize it with the value of the
7881 // original variable. The address of the original variable is replaced by
7882 // the address of the new private variable in the CodeGen. This new variable
7883 // is not added to IdResolver, so the code in the OpenMP region uses
7884 // original variable for proper diagnostics and variable capturing.
7885 Expr *VDInitRefExpr = nullptr;
7886 // For arrays generate initializer for single element and replace it by the
7887 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007888 if (Type->isArrayType()) {
7889 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007890 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007891 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007892 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007893 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007894 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007895 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007896 InitializedEntity Entity =
7897 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007898 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7899
7900 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7901 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7902 if (Result.isInvalid())
7903 VDPrivate->setInvalidDecl();
7904 else
7905 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007906 // Remove temp variable declaration.
7907 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007908 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007909 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7910 ".firstprivate.temp");
7911 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7912 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007913 AddInitializerToDecl(VDPrivate,
7914 DefaultLvalueConversion(VDInitRefExpr).get(),
7915 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007916 }
7917 if (VDPrivate->isInvalidDecl()) {
7918 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007919 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007920 diag::note_omp_task_predetermined_firstprivate_here);
7921 }
7922 continue;
7923 }
7924 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007925 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007926 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7927 RefExpr->getExprLoc());
7928 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007929 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007930 if (TopDVar.CKind == OMPC_lastprivate)
7931 Ref = TopDVar.PrivateCopy;
7932 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007933 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007934 if (!IsOpenMPCapturedDecl(D))
7935 ExprCaptures.push_back(Ref->getDecl());
7936 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007937 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007938 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7939 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007940 PrivateCopies.push_back(VDPrivateRefExpr);
7941 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007942 }
7943
Alexey Bataeved09d242014-05-28 05:53:51 +00007944 if (Vars.empty())
7945 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007946
7947 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007948 Vars, PrivateCopies, Inits,
7949 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007950}
7951
Alexander Musman1bb328c2014-06-04 13:06:39 +00007952OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7953 SourceLocation StartLoc,
7954 SourceLocation LParenLoc,
7955 SourceLocation EndLoc) {
7956 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007957 SmallVector<Expr *, 8> SrcExprs;
7958 SmallVector<Expr *, 8> DstExprs;
7959 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007960 SmallVector<Decl *, 4> ExprCaptures;
7961 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007962 for (auto &RefExpr : VarList) {
7963 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007964 SourceLocation ELoc;
7965 SourceRange ERange;
7966 Expr *SimpleRefExpr = RefExpr;
7967 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007968 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007969 // It will be analyzed later.
7970 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007971 SrcExprs.push_back(nullptr);
7972 DstExprs.push_back(nullptr);
7973 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007974 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007975 ValueDecl *D = Res.first;
7976 if (!D)
7977 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007978
Alexey Bataev74caaf22016-02-20 04:09:36 +00007979 QualType Type = D->getType();
7980 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007981
7982 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7983 // A variable that appears in a lastprivate clause must not have an
7984 // incomplete type or a reference type.
7985 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007986 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007987 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007988 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007989
7990 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7991 // in a Construct]
7992 // Variables with the predetermined data-sharing attributes may not be
7993 // listed in data-sharing attributes clauses, except for the cases
7994 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007995 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007996 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7997 DVar.CKind != OMPC_firstprivate &&
7998 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7999 Diag(ELoc, diag::err_omp_wrong_dsa)
8000 << getOpenMPClauseName(DVar.CKind)
8001 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008002 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00008003 continue;
8004 }
8005
Alexey Bataevf29276e2014-06-18 04:14:57 +00008006 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8007 // OpenMP [2.14.3.5, Restrictions, p.2]
8008 // A list item that is private within a parallel region, or that appears in
8009 // the reduction clause of a parallel construct, must not appear in a
8010 // lastprivate clause on a worksharing construct if any of the corresponding
8011 // worksharing regions ever binds to any of the corresponding parallel
8012 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00008013 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00008014 if (isOpenMPWorksharingDirective(CurrDir) &&
8015 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00008016 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008017 if (DVar.CKind != OMPC_shared) {
8018 Diag(ELoc, diag::err_omp_required_access)
8019 << getOpenMPClauseName(OMPC_lastprivate)
8020 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00008021 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008022 continue;
8023 }
8024 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00008025
8026 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8027 // A list item may appear in a firstprivate or lastprivate clause but not
8028 // both.
8029 if (CurrDir == OMPD_distribute) {
8030 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8031 if (DVar.CKind == OMPC_firstprivate) {
8032 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8033 ReportOriginalDSA(*this, DSAStack, D, DVar);
8034 continue;
8035 }
8036 }
8037
Alexander Musman1bb328c2014-06-04 13:06:39 +00008038 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008039 // A variable of class type (or array thereof) that appears in a
8040 // lastprivate clause requires an accessible, unambiguous default
8041 // constructor for the class type, unless the list item is also specified
8042 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008043 // A variable of class type (or array thereof) that appears in a
8044 // lastprivate clause requires an accessible, unambiguous copy assignment
8045 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008046 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008047 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008048 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008049 D->hasAttrs() ? &D->getAttrs() : nullptr);
8050 auto *PseudoSrcExpr =
8051 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008052 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008053 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008054 D->hasAttrs() ? &D->getAttrs() : nullptr);
8055 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008056 // For arrays generate assignment operation for single element and replace
8057 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008058 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008059 PseudoDstExpr, PseudoSrcExpr);
8060 if (AssignmentOp.isInvalid())
8061 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008062 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008063 /*DiscardedValue=*/true);
8064 if (AssignmentOp.isInvalid())
8065 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008066
Alexey Bataev74caaf22016-02-20 04:09:36 +00008067 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008068 if (!VD) {
8069 if (TopDVar.CKind == OMPC_firstprivate)
8070 Ref = TopDVar.PrivateCopy;
8071 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008072 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008073 if (!IsOpenMPCapturedDecl(D))
8074 ExprCaptures.push_back(Ref->getDecl());
8075 }
8076 if (TopDVar.CKind == OMPC_firstprivate ||
8077 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008078 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008079 ExprResult RefRes = DefaultLvalueConversion(Ref);
8080 if (!RefRes.isUsable())
8081 continue;
8082 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008083 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8084 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008085 if (!PostUpdateRes.isUsable())
8086 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008087 ExprPostUpdates.push_back(
8088 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008089 }
8090 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00008091 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00008092 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8093 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008094 SrcExprs.push_back(PseudoSrcExpr);
8095 DstExprs.push_back(PseudoDstExpr);
8096 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008097 }
8098
8099 if (Vars.empty())
8100 return nullptr;
8101
8102 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008103 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008104 buildPreInits(Context, ExprCaptures),
8105 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008106}
8107
Alexey Bataev758e55e2013-09-06 18:03:48 +00008108OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8109 SourceLocation StartLoc,
8110 SourceLocation LParenLoc,
8111 SourceLocation EndLoc) {
8112 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008113 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008114 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008115 SourceLocation ELoc;
8116 SourceRange ERange;
8117 Expr *SimpleRefExpr = RefExpr;
8118 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008119 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008120 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008121 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008122 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008123 ValueDecl *D = Res.first;
8124 if (!D)
8125 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008126
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008127 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008128 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8129 // in a Construct]
8130 // Variables with the predetermined data-sharing attributes may not be
8131 // listed in data-sharing attributes clauses, except for the cases
8132 // listed below. For these exceptions only, listing a predetermined
8133 // variable in a data-sharing attribute clause is allowed and overrides
8134 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008135 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008136 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8137 DVar.RefExpr) {
8138 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8139 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008140 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008141 continue;
8142 }
8143
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008144 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008145 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008146 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008147 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008148 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008149 }
8150
Alexey Bataeved09d242014-05-28 05:53:51 +00008151 if (Vars.empty())
8152 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008153
8154 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8155}
8156
Alexey Bataevc5e02582014-06-16 07:08:35 +00008157namespace {
8158class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8159 DSAStackTy *Stack;
8160
8161public:
8162 bool VisitDeclRefExpr(DeclRefExpr *E) {
8163 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008164 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008165 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8166 return false;
8167 if (DVar.CKind != OMPC_unknown)
8168 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008169 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008170 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008171 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008172 return true;
8173 return false;
8174 }
8175 return false;
8176 }
8177 bool VisitStmt(Stmt *S) {
8178 for (auto Child : S->children()) {
8179 if (Child && Visit(Child))
8180 return true;
8181 }
8182 return false;
8183 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008184 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008185};
Alexey Bataev23b69422014-06-18 07:08:49 +00008186} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008187
Alexey Bataev60da77e2016-02-29 05:54:20 +00008188namespace {
8189// Transform MemberExpression for specified FieldDecl of current class to
8190// DeclRefExpr to specified OMPCapturedExprDecl.
8191class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8192 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8193 ValueDecl *Field;
8194 DeclRefExpr *CapturedExpr;
8195
8196public:
8197 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8198 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8199
8200 ExprResult TransformMemberExpr(MemberExpr *E) {
8201 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8202 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008203 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008204 return CapturedExpr;
8205 }
8206 return BaseTransform::TransformMemberExpr(E);
8207 }
8208 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8209};
8210} // namespace
8211
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008212template <typename T>
8213static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8214 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8215 for (auto &Set : Lookups) {
8216 for (auto *D : Set) {
8217 if (auto Res = Gen(cast<ValueDecl>(D)))
8218 return Res;
8219 }
8220 }
8221 return T();
8222}
8223
8224static ExprResult
8225buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8226 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8227 const DeclarationNameInfo &ReductionId, QualType Ty,
8228 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8229 if (ReductionIdScopeSpec.isInvalid())
8230 return ExprError();
8231 SmallVector<UnresolvedSet<8>, 4> Lookups;
8232 if (S) {
8233 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8234 Lookup.suppressDiagnostics();
8235 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8236 auto *D = Lookup.getRepresentativeDecl();
8237 do {
8238 S = S->getParent();
8239 } while (S && !S->isDeclScope(D));
8240 if (S)
8241 S = S->getParent();
8242 Lookups.push_back(UnresolvedSet<8>());
8243 Lookups.back().append(Lookup.begin(), Lookup.end());
8244 Lookup.clear();
8245 }
8246 } else if (auto *ULE =
8247 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8248 Lookups.push_back(UnresolvedSet<8>());
8249 Decl *PrevD = nullptr;
8250 for(auto *D : ULE->decls()) {
8251 if (D == PrevD)
8252 Lookups.push_back(UnresolvedSet<8>());
8253 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8254 Lookups.back().addDecl(DRD);
8255 PrevD = D;
8256 }
8257 }
8258 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8259 Ty->containsUnexpandedParameterPack() ||
8260 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8261 return !D->isInvalidDecl() &&
8262 (D->getType()->isDependentType() ||
8263 D->getType()->isInstantiationDependentType() ||
8264 D->getType()->containsUnexpandedParameterPack());
8265 })) {
8266 UnresolvedSet<8> ResSet;
8267 for (auto &Set : Lookups) {
8268 ResSet.append(Set.begin(), Set.end());
8269 // The last item marks the end of all declarations at the specified scope.
8270 ResSet.addDecl(Set[Set.size() - 1]);
8271 }
8272 return UnresolvedLookupExpr::Create(
8273 SemaRef.Context, /*NamingClass=*/nullptr,
8274 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8275 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8276 }
8277 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8278 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8279 if (!D->isInvalidDecl() &&
8280 SemaRef.Context.hasSameType(D->getType(), Ty))
8281 return D;
8282 return nullptr;
8283 }))
8284 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8285 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8286 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8287 if (!D->isInvalidDecl() &&
8288 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8289 !Ty.isMoreQualifiedThan(D->getType()))
8290 return D;
8291 return nullptr;
8292 })) {
8293 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8294 /*DetectVirtual=*/false);
8295 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8296 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8297 VD->getType().getUnqualifiedType()))) {
8298 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8299 /*DiagID=*/0) !=
8300 Sema::AR_inaccessible) {
8301 SemaRef.BuildBasePathArray(Paths, BasePath);
8302 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8303 }
8304 }
8305 }
8306 }
8307 if (ReductionIdScopeSpec.isSet()) {
8308 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8309 return ExprError();
8310 }
8311 return ExprEmpty();
8312}
8313
Alexey Bataevc5e02582014-06-16 07:08:35 +00008314OMPClause *Sema::ActOnOpenMPReductionClause(
8315 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8316 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008317 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8318 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008319 auto DN = ReductionId.getName();
8320 auto OOK = DN.getCXXOverloadedOperator();
8321 BinaryOperatorKind BOK = BO_Comma;
8322
8323 // OpenMP [2.14.3.6, reduction clause]
8324 // C
8325 // reduction-identifier is either an identifier or one of the following
8326 // operators: +, -, *, &, |, ^, && and ||
8327 // C++
8328 // reduction-identifier is either an id-expression or one of the following
8329 // operators: +, -, *, &, |, ^, && and ||
8330 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8331 switch (OOK) {
8332 case OO_Plus:
8333 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008334 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008335 break;
8336 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008337 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008338 break;
8339 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008340 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008341 break;
8342 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008343 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008344 break;
8345 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008346 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008347 break;
8348 case OO_AmpAmp:
8349 BOK = BO_LAnd;
8350 break;
8351 case OO_PipePipe:
8352 BOK = BO_LOr;
8353 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008354 case OO_New:
8355 case OO_Delete:
8356 case OO_Array_New:
8357 case OO_Array_Delete:
8358 case OO_Slash:
8359 case OO_Percent:
8360 case OO_Tilde:
8361 case OO_Exclaim:
8362 case OO_Equal:
8363 case OO_Less:
8364 case OO_Greater:
8365 case OO_LessEqual:
8366 case OO_GreaterEqual:
8367 case OO_PlusEqual:
8368 case OO_MinusEqual:
8369 case OO_StarEqual:
8370 case OO_SlashEqual:
8371 case OO_PercentEqual:
8372 case OO_CaretEqual:
8373 case OO_AmpEqual:
8374 case OO_PipeEqual:
8375 case OO_LessLess:
8376 case OO_GreaterGreater:
8377 case OO_LessLessEqual:
8378 case OO_GreaterGreaterEqual:
8379 case OO_EqualEqual:
8380 case OO_ExclaimEqual:
8381 case OO_PlusPlus:
8382 case OO_MinusMinus:
8383 case OO_Comma:
8384 case OO_ArrowStar:
8385 case OO_Arrow:
8386 case OO_Call:
8387 case OO_Subscript:
8388 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008389 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008390 case NUM_OVERLOADED_OPERATORS:
8391 llvm_unreachable("Unexpected reduction identifier");
8392 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008393 if (auto II = DN.getAsIdentifierInfo()) {
8394 if (II->isStr("max"))
8395 BOK = BO_GT;
8396 else if (II->isStr("min"))
8397 BOK = BO_LT;
8398 }
8399 break;
8400 }
8401 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008402 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008403 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008404 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008405
8406 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008407 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008408 SmallVector<Expr *, 8> LHSs;
8409 SmallVector<Expr *, 8> RHSs;
8410 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008411 SmallVector<Decl *, 4> ExprCaptures;
8412 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008413 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8414 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008415 for (auto RefExpr : VarList) {
8416 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008417 // OpenMP [2.1, C/C++]
8418 // A list item is a variable or array section, subject to the restrictions
8419 // specified in Section 2.4 on page 42 and in each of the sections
8420 // describing clauses and directives for which a list appears.
8421 // OpenMP [2.14.3.3, Restrictions, p.1]
8422 // A variable that is part of another variable (as an array or
8423 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008424 if (!FirstIter && IR != ER)
8425 ++IR;
8426 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008427 SourceLocation ELoc;
8428 SourceRange ERange;
8429 Expr *SimpleRefExpr = RefExpr;
8430 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8431 /*AllowArraySection=*/true);
8432 if (Res.second) {
8433 // It will be analyzed later.
8434 Vars.push_back(RefExpr);
8435 Privates.push_back(nullptr);
8436 LHSs.push_back(nullptr);
8437 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008438 // Try to find 'declare reduction' corresponding construct before using
8439 // builtin/overloaded operators.
8440 QualType Type = Context.DependentTy;
8441 CXXCastPath BasePath;
8442 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8443 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8444 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8445 if (CurContext->isDependentContext() &&
8446 (DeclareReductionRef.isUnset() ||
8447 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8448 ReductionOps.push_back(DeclareReductionRef.get());
8449 else
8450 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008451 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008452 ValueDecl *D = Res.first;
8453 if (!D)
8454 continue;
8455
Alexey Bataeva1764212015-09-30 09:22:36 +00008456 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008457 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8458 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8459 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008460 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008461 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008462 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8463 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8464 Type = ATy->getElementType();
8465 else
8466 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008467 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008468 } else
8469 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8470 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008471
Alexey Bataevc5e02582014-06-16 07:08:35 +00008472 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8473 // A variable that appears in a private clause must not have an incomplete
8474 // type or a reference type.
8475 if (RequireCompleteType(ELoc, Type,
8476 diag::err_omp_reduction_incomplete_type))
8477 continue;
8478 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008479 // A list item that appears in a reduction clause must not be
8480 // const-qualified.
8481 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008482 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008483 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008484 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008485 bool IsDecl = !VD ||
8486 VD->isThisDeclarationADefinition(Context) ==
8487 VarDecl::DeclarationOnly;
8488 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008489 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008490 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008491 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008492 continue;
8493 }
8494 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8495 // If a list-item is a reference type then it must bind to the same object
8496 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008497 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008498 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008499 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008500 DSARefChecker Check(DSAStack);
8501 if (Check.Visit(VDDef->getInit())) {
8502 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8503 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8504 continue;
8505 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008506 }
8507 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008508
Alexey Bataevc5e02582014-06-16 07:08:35 +00008509 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8510 // in a Construct]
8511 // Variables with the predetermined data-sharing attributes may not be
8512 // listed in data-sharing attributes clauses, except for the cases
8513 // listed below. For these exceptions only, listing a predetermined
8514 // variable in a data-sharing attribute clause is allowed and overrides
8515 // the variable's predetermined data-sharing attributes.
8516 // OpenMP [2.14.3.6, Restrictions, p.3]
8517 // Any number of reduction clauses can be specified on the directive,
8518 // but a list item can appear only once in the reduction clauses for that
8519 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008520 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008521 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008522 if (DVar.CKind == OMPC_reduction) {
8523 Diag(ELoc, diag::err_omp_once_referenced)
8524 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008525 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008526 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008527 } else if (DVar.CKind != OMPC_unknown) {
8528 Diag(ELoc, diag::err_omp_wrong_dsa)
8529 << getOpenMPClauseName(DVar.CKind)
8530 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008531 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008532 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008533 }
8534
8535 // OpenMP [2.14.3.6, Restrictions, p.1]
8536 // A list item that appears in a reduction clause of a worksharing
8537 // construct must be shared in the parallel regions to which any of the
8538 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008539 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8540 if (isOpenMPWorksharingDirective(CurrDir) &&
8541 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008542 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008543 if (DVar.CKind != OMPC_shared) {
8544 Diag(ELoc, diag::err_omp_required_access)
8545 << getOpenMPClauseName(OMPC_reduction)
8546 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008547 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008548 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008549 }
8550 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008551
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008552 // Try to find 'declare reduction' corresponding construct before using
8553 // builtin/overloaded operators.
8554 CXXCastPath BasePath;
8555 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8556 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8557 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8558 if (DeclareReductionRef.isInvalid())
8559 continue;
8560 if (CurContext->isDependentContext() &&
8561 (DeclareReductionRef.isUnset() ||
8562 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8563 Vars.push_back(RefExpr);
8564 Privates.push_back(nullptr);
8565 LHSs.push_back(nullptr);
8566 RHSs.push_back(nullptr);
8567 ReductionOps.push_back(DeclareReductionRef.get());
8568 continue;
8569 }
8570 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8571 // Not allowed reduction identifier is found.
8572 Diag(ReductionId.getLocStart(),
8573 diag::err_omp_unknown_reduction_identifier)
8574 << Type << ReductionIdRange;
8575 continue;
8576 }
8577
8578 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8579 // The type of a list item that appears in a reduction clause must be valid
8580 // for the reduction-identifier. For a max or min reduction in C, the type
8581 // of the list item must be an allowed arithmetic data type: char, int,
8582 // float, double, or _Bool, possibly modified with long, short, signed, or
8583 // unsigned. For a max or min reduction in C++, the type of the list item
8584 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8585 // double, or bool, possibly modified with long, short, signed, or unsigned.
8586 if (DeclareReductionRef.isUnset()) {
8587 if ((BOK == BO_GT || BOK == BO_LT) &&
8588 !(Type->isScalarType() ||
8589 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8590 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8591 << getLangOpts().CPlusPlus;
8592 if (!ASE && !OASE) {
8593 bool IsDecl = !VD ||
8594 VD->isThisDeclarationADefinition(Context) ==
8595 VarDecl::DeclarationOnly;
8596 Diag(D->getLocation(),
8597 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8598 << D;
8599 }
8600 continue;
8601 }
8602 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8603 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8604 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8605 if (!ASE && !OASE) {
8606 bool IsDecl = !VD ||
8607 VD->isThisDeclarationADefinition(Context) ==
8608 VarDecl::DeclarationOnly;
8609 Diag(D->getLocation(),
8610 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8611 << D;
8612 }
8613 continue;
8614 }
8615 }
8616
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008617 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008618 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008619 D->hasAttrs() ? &D->getAttrs() : nullptr);
8620 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8621 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008622 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008623 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008624 (!ASE &&
8625 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008626 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008627 // Create pseudo array type for private copy. The size for this array will
8628 // be generated during codegen.
8629 // For array subscripts or single variables Private Ty is the same as Type
8630 // (type of the variable or single array element).
8631 PrivateTy = Context.getVariableArrayType(
8632 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8633 Context.getSizeType(), VK_RValue),
8634 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008635 } else if (!ASE && !OASE &&
8636 Context.getAsArrayType(D->getType().getNonReferenceType()))
8637 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008638 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008639 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8640 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008641 // Add initializer for private variable.
8642 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008643 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8644 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8645 if (DeclareReductionRef.isUsable()) {
8646 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8647 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8648 if (DRD->getInitializer()) {
8649 Init = DRDRef;
8650 RHSVD->setInit(DRDRef);
8651 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008652 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008653 } else {
8654 switch (BOK) {
8655 case BO_Add:
8656 case BO_Xor:
8657 case BO_Or:
8658 case BO_LOr:
8659 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8660 if (Type->isScalarType() || Type->isAnyComplexType())
8661 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8662 break;
8663 case BO_Mul:
8664 case BO_LAnd:
8665 if (Type->isScalarType() || Type->isAnyComplexType()) {
8666 // '*' and '&&' reduction ops - initializer is '1'.
8667 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008668 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008669 break;
8670 case BO_And: {
8671 // '&' reduction op - initializer is '~0'.
8672 QualType OrigType = Type;
8673 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8674 Type = ComplexTy->getElementType();
8675 if (Type->isRealFloatingType()) {
8676 llvm::APFloat InitValue =
8677 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8678 /*isIEEE=*/true);
8679 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8680 Type, ELoc);
8681 } else if (Type->isScalarType()) {
8682 auto Size = Context.getTypeSize(Type);
8683 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8684 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8685 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8686 }
8687 if (Init && OrigType->isAnyComplexType()) {
8688 // Init = 0xFFFF + 0xFFFFi;
8689 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8690 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8691 }
8692 Type = OrigType;
8693 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008694 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008695 case BO_LT:
8696 case BO_GT: {
8697 // 'min' reduction op - initializer is 'Largest representable number in
8698 // the reduction list item type'.
8699 // 'max' reduction op - initializer is 'Least representable number in
8700 // the reduction list item type'.
8701 if (Type->isIntegerType() || Type->isPointerType()) {
8702 bool IsSigned = Type->hasSignedIntegerRepresentation();
8703 auto Size = Context.getTypeSize(Type);
8704 QualType IntTy =
8705 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8706 llvm::APInt InitValue =
8707 (BOK != BO_LT)
8708 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8709 : llvm::APInt::getMinValue(Size)
8710 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8711 : llvm::APInt::getMaxValue(Size);
8712 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8713 if (Type->isPointerType()) {
8714 // Cast to pointer type.
8715 auto CastExpr = BuildCStyleCastExpr(
8716 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8717 SourceLocation(), Init);
8718 if (CastExpr.isInvalid())
8719 continue;
8720 Init = CastExpr.get();
8721 }
8722 } else if (Type->isRealFloatingType()) {
8723 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8724 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8725 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8726 Type, ELoc);
8727 }
8728 break;
8729 }
8730 case BO_PtrMemD:
8731 case BO_PtrMemI:
8732 case BO_MulAssign:
8733 case BO_Div:
8734 case BO_Rem:
8735 case BO_Sub:
8736 case BO_Shl:
8737 case BO_Shr:
8738 case BO_LE:
8739 case BO_GE:
8740 case BO_EQ:
8741 case BO_NE:
8742 case BO_AndAssign:
8743 case BO_XorAssign:
8744 case BO_OrAssign:
8745 case BO_Assign:
8746 case BO_AddAssign:
8747 case BO_SubAssign:
8748 case BO_DivAssign:
8749 case BO_RemAssign:
8750 case BO_ShlAssign:
8751 case BO_ShrAssign:
8752 case BO_Comma:
8753 llvm_unreachable("Unexpected reduction operation");
8754 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008755 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008756 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008757 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8758 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008759 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008760 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008761 if (RHSVD->isInvalidDecl())
8762 continue;
8763 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008764 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8765 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008766 bool IsDecl =
8767 !VD ||
8768 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8769 Diag(D->getLocation(),
8770 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8771 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008772 continue;
8773 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008774 // Store initializer for single element in private copy. Will be used during
8775 // codegen.
8776 PrivateVD->setInit(RHSVD->getInit());
8777 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008778 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008779 ExprResult ReductionOp;
8780 if (DeclareReductionRef.isUsable()) {
8781 QualType RedTy = DeclareReductionRef.get()->getType();
8782 QualType PtrRedTy = Context.getPointerType(RedTy);
8783 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8784 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8785 if (!BasePath.empty()) {
8786 LHS = DefaultLvalueConversion(LHS.get());
8787 RHS = DefaultLvalueConversion(RHS.get());
8788 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8789 CK_UncheckedDerivedToBase, LHS.get(),
8790 &BasePath, LHS.get()->getValueKind());
8791 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8792 CK_UncheckedDerivedToBase, RHS.get(),
8793 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008794 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008795 FunctionProtoType::ExtProtoInfo EPI;
8796 QualType Params[] = {PtrRedTy, PtrRedTy};
8797 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8798 auto *OVE = new (Context) OpaqueValueExpr(
8799 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8800 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8801 Expr *Args[] = {LHS.get(), RHS.get()};
8802 ReductionOp = new (Context)
8803 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8804 } else {
8805 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8806 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8807 if (ReductionOp.isUsable()) {
8808 if (BOK != BO_LT && BOK != BO_GT) {
8809 ReductionOp =
8810 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8811 BO_Assign, LHSDRE, ReductionOp.get());
8812 } else {
8813 auto *ConditionalOp = new (Context) ConditionalOperator(
8814 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8815 RHSDRE, Type, VK_LValue, OK_Ordinary);
8816 ReductionOp =
8817 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8818 BO_Assign, LHSDRE, ConditionalOp);
8819 }
8820 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8821 }
8822 if (ReductionOp.isInvalid())
8823 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008824 }
8825
Alexey Bataev60da77e2016-02-29 05:54:20 +00008826 DeclRefExpr *Ref = nullptr;
8827 Expr *VarsExpr = RefExpr->IgnoreParens();
8828 if (!VD) {
8829 if (ASE || OASE) {
8830 TransformExprToCaptures RebuildToCapture(*this, D);
8831 VarsExpr =
8832 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8833 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008834 } else {
8835 VarsExpr = Ref =
8836 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008837 }
8838 if (!IsOpenMPCapturedDecl(D)) {
8839 ExprCaptures.push_back(Ref->getDecl());
8840 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8841 ExprResult RefRes = DefaultLvalueConversion(Ref);
8842 if (!RefRes.isUsable())
8843 continue;
8844 ExprResult PostUpdateRes =
8845 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8846 SimpleRefExpr, RefRes.get());
8847 if (!PostUpdateRes.isUsable())
8848 continue;
8849 ExprPostUpdates.push_back(
8850 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008851 }
8852 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008853 }
8854 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8855 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008856 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008857 LHSs.push_back(LHSDRE);
8858 RHSs.push_back(RHSDRE);
8859 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008860 }
8861
8862 if (Vars.empty())
8863 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008864
Alexey Bataevc5e02582014-06-16 07:08:35 +00008865 return OMPReductionClause::Create(
8866 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008867 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008868 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8869 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008870}
8871
Alexey Bataevecba70f2016-04-12 11:02:11 +00008872bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8873 SourceLocation LinLoc) {
8874 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8875 LinKind == OMPC_LINEAR_unknown) {
8876 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8877 return true;
8878 }
8879 return false;
8880}
8881
8882bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8883 OpenMPLinearClauseKind LinKind,
8884 QualType Type) {
8885 auto *VD = dyn_cast_or_null<VarDecl>(D);
8886 // A variable must not have an incomplete type or a reference type.
8887 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8888 return true;
8889 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8890 !Type->isReferenceType()) {
8891 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8892 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8893 return true;
8894 }
8895 Type = Type.getNonReferenceType();
8896
8897 // A list item must not be const-qualified.
8898 if (Type.isConstant(Context)) {
8899 Diag(ELoc, diag::err_omp_const_variable)
8900 << getOpenMPClauseName(OMPC_linear);
8901 if (D) {
8902 bool IsDecl =
8903 !VD ||
8904 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8905 Diag(D->getLocation(),
8906 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8907 << D;
8908 }
8909 return true;
8910 }
8911
8912 // A list item must be of integral or pointer type.
8913 Type = Type.getUnqualifiedType().getCanonicalType();
8914 const auto *Ty = Type.getTypePtrOrNull();
8915 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8916 !Ty->isPointerType())) {
8917 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8918 if (D) {
8919 bool IsDecl =
8920 !VD ||
8921 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8922 Diag(D->getLocation(),
8923 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8924 << D;
8925 }
8926 return true;
8927 }
8928 return false;
8929}
8930
Alexey Bataev182227b2015-08-20 10:54:39 +00008931OMPClause *Sema::ActOnOpenMPLinearClause(
8932 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8933 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8934 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008935 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008936 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008937 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008938 SmallVector<Decl *, 4> ExprCaptures;
8939 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008940 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008941 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008942 for (auto &RefExpr : VarList) {
8943 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008944 SourceLocation ELoc;
8945 SourceRange ERange;
8946 Expr *SimpleRefExpr = RefExpr;
8947 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8948 /*AllowArraySection=*/false);
8949 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008950 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008951 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008952 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008953 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008954 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008955 ValueDecl *D = Res.first;
8956 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008957 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008958
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008959 QualType Type = D->getType();
8960 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008961
8962 // OpenMP [2.14.3.7, linear clause]
8963 // A list-item cannot appear in more than one linear clause.
8964 // A list-item that appears in a linear clause cannot appear in any
8965 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008966 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008967 if (DVar.RefExpr) {
8968 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8969 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008970 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008971 continue;
8972 }
8973
Alexey Bataevecba70f2016-04-12 11:02:11 +00008974 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008975 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008976 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008977
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008978 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008979 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8980 D->hasAttrs() ? &D->getAttrs() : nullptr);
8981 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008982 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008983 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008984 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008985 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008986 if (!VD) {
8987 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8988 if (!IsOpenMPCapturedDecl(D)) {
8989 ExprCaptures.push_back(Ref->getDecl());
8990 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8991 ExprResult RefRes = DefaultLvalueConversion(Ref);
8992 if (!RefRes.isUsable())
8993 continue;
8994 ExprResult PostUpdateRes =
8995 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8996 SimpleRefExpr, RefRes.get());
8997 if (!PostUpdateRes.isUsable())
8998 continue;
8999 ExprPostUpdates.push_back(
9000 IgnoredValueConversions(PostUpdateRes.get()).get());
9001 }
9002 }
9003 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009004 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009005 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009006 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009007 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009008 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00009009 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9010 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9011
9012 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9013 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009014 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00009015 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00009016 }
9017
9018 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009019 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009020
9021 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00009022 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00009023 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9024 !Step->isInstantiationDependent() &&
9025 !Step->containsUnexpandedParameterPack()) {
9026 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009027 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00009028 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009029 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009030 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00009031
Alexander Musman3276a272015-03-21 10:12:56 +00009032 // Build var to save the step value.
9033 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009034 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00009035 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00009036 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00009037 ExprResult CalcStep =
9038 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009039 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009040
Alexander Musman8dba6642014-04-22 13:09:42 +00009041 // Warn about zero linear step (it would be probably better specified as
9042 // making corresponding variables 'const').
9043 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009044 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9045 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009046 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9047 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009048 if (!IsConstant && CalcStep.isUsable()) {
9049 // Calculate the step beforehand instead of doing this on each iteration.
9050 // (This is not used if the number of iterations may be kfold-ed).
9051 CalcStepExpr = CalcStep.get();
9052 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009053 }
9054
Alexey Bataev182227b2015-08-20 10:54:39 +00009055 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9056 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009057 StepExpr, CalcStepExpr,
9058 buildPreInits(Context, ExprCaptures),
9059 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009060}
9061
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009062static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9063 Expr *NumIterations, Sema &SemaRef,
9064 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009065 // Walk the vars and build update/final expressions for the CodeGen.
9066 SmallVector<Expr *, 8> Updates;
9067 SmallVector<Expr *, 8> Finals;
9068 Expr *Step = Clause.getStep();
9069 Expr *CalcStep = Clause.getCalcStep();
9070 // OpenMP [2.14.3.7, linear clause]
9071 // If linear-step is not specified it is assumed to be 1.
9072 if (Step == nullptr)
9073 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009074 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009075 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009076 }
Alexander Musman3276a272015-03-21 10:12:56 +00009077 bool HasErrors = false;
9078 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009079 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009080 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009081 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009082 SourceLocation ELoc;
9083 SourceRange ERange;
9084 Expr *SimpleRefExpr = RefExpr;
9085 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9086 /*AllowArraySection=*/false);
9087 ValueDecl *D = Res.first;
9088 if (Res.second || !D) {
9089 Updates.push_back(nullptr);
9090 Finals.push_back(nullptr);
9091 HasErrors = true;
9092 continue;
9093 }
9094 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9095 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9096 ->getMemberDecl();
9097 }
9098 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009099 Expr *InitExpr = *CurInit;
9100
9101 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009102 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009103 Expr *CapturedRef;
9104 if (LinKind == OMPC_LINEAR_uval)
9105 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9106 else
9107 CapturedRef =
9108 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9109 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9110 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009111
9112 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009113 ExprResult Update;
9114 if (!Info.first) {
9115 Update =
9116 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9117 InitExpr, IV, Step, /* Subtract */ false);
9118 } else
9119 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009120 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9121 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009122
9123 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009124 ExprResult Final;
9125 if (!Info.first) {
9126 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9127 InitExpr, NumIterations, Step,
9128 /* Subtract */ false);
9129 } else
9130 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009131 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9132 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009133
Alexander Musman3276a272015-03-21 10:12:56 +00009134 if (!Update.isUsable() || !Final.isUsable()) {
9135 Updates.push_back(nullptr);
9136 Finals.push_back(nullptr);
9137 HasErrors = true;
9138 } else {
9139 Updates.push_back(Update.get());
9140 Finals.push_back(Final.get());
9141 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009142 ++CurInit;
9143 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009144 }
9145 Clause.setUpdates(Updates);
9146 Clause.setFinals(Finals);
9147 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009148}
9149
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009150OMPClause *Sema::ActOnOpenMPAlignedClause(
9151 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9152 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9153
9154 SmallVector<Expr *, 8> Vars;
9155 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009156 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9157 SourceLocation ELoc;
9158 SourceRange ERange;
9159 Expr *SimpleRefExpr = RefExpr;
9160 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9161 /*AllowArraySection=*/false);
9162 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009163 // It will be analyzed later.
9164 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009165 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009166 ValueDecl *D = Res.first;
9167 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009168 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009169
Alexey Bataev1efd1662016-03-29 10:59:56 +00009170 QualType QType = D->getType();
9171 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009172
9173 // OpenMP [2.8.1, simd construct, Restrictions]
9174 // The type of list items appearing in the aligned clause must be
9175 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009176 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009177 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009178 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009179 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009180 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009181 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009182 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009183 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009184 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009185 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009186 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009187 continue;
9188 }
9189
9190 // OpenMP [2.8.1, simd construct, Restrictions]
9191 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009192 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009193 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009194 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9195 << getOpenMPClauseName(OMPC_aligned);
9196 continue;
9197 }
9198
Alexey Bataev1efd1662016-03-29 10:59:56 +00009199 DeclRefExpr *Ref = nullptr;
9200 if (!VD && IsOpenMPCapturedDecl(D))
9201 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9202 Vars.push_back(DefaultFunctionArrayConversion(
9203 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9204 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009205 }
9206
9207 // OpenMP [2.8.1, simd construct, Description]
9208 // The parameter of the aligned clause, alignment, must be a constant
9209 // positive integer expression.
9210 // If no optional parameter is specified, implementation-defined default
9211 // alignments for SIMD instructions on the target platforms are assumed.
9212 if (Alignment != nullptr) {
9213 ExprResult AlignResult =
9214 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9215 if (AlignResult.isInvalid())
9216 return nullptr;
9217 Alignment = AlignResult.get();
9218 }
9219 if (Vars.empty())
9220 return nullptr;
9221
9222 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9223 EndLoc, Vars, Alignment);
9224}
9225
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009226OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9227 SourceLocation StartLoc,
9228 SourceLocation LParenLoc,
9229 SourceLocation EndLoc) {
9230 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009231 SmallVector<Expr *, 8> SrcExprs;
9232 SmallVector<Expr *, 8> DstExprs;
9233 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009234 for (auto &RefExpr : VarList) {
9235 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9236 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009237 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009238 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009239 SrcExprs.push_back(nullptr);
9240 DstExprs.push_back(nullptr);
9241 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009242 continue;
9243 }
9244
Alexey Bataeved09d242014-05-28 05:53:51 +00009245 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009246 // OpenMP [2.1, C/C++]
9247 // A list item is a variable name.
9248 // OpenMP [2.14.4.1, Restrictions, p.1]
9249 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009250 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009251 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009252 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9253 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009254 continue;
9255 }
9256
9257 Decl *D = DE->getDecl();
9258 VarDecl *VD = cast<VarDecl>(D);
9259
9260 QualType Type = VD->getType();
9261 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9262 // It will be analyzed later.
9263 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009264 SrcExprs.push_back(nullptr);
9265 DstExprs.push_back(nullptr);
9266 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009267 continue;
9268 }
9269
9270 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9271 // A list item that appears in a copyin clause must be threadprivate.
9272 if (!DSAStack->isThreadPrivate(VD)) {
9273 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009274 << getOpenMPClauseName(OMPC_copyin)
9275 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009276 continue;
9277 }
9278
9279 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9280 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009281 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009282 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009283 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009284 auto *SrcVD =
9285 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9286 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009287 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009288 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9289 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009290 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9291 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009292 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009293 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009294 // For arrays generate assignment operation for single element and replace
9295 // it by the original array element in CodeGen.
9296 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9297 PseudoDstExpr, PseudoSrcExpr);
9298 if (AssignmentOp.isInvalid())
9299 continue;
9300 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9301 /*DiscardedValue=*/true);
9302 if (AssignmentOp.isInvalid())
9303 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009304
9305 DSAStack->addDSA(VD, DE, OMPC_copyin);
9306 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009307 SrcExprs.push_back(PseudoSrcExpr);
9308 DstExprs.push_back(PseudoDstExpr);
9309 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009310 }
9311
Alexey Bataeved09d242014-05-28 05:53:51 +00009312 if (Vars.empty())
9313 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009314
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009315 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9316 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009317}
9318
Alexey Bataevbae9a792014-06-27 10:37:06 +00009319OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9320 SourceLocation StartLoc,
9321 SourceLocation LParenLoc,
9322 SourceLocation EndLoc) {
9323 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009324 SmallVector<Expr *, 8> SrcExprs;
9325 SmallVector<Expr *, 8> DstExprs;
9326 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009327 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009328 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9329 SourceLocation ELoc;
9330 SourceRange ERange;
9331 Expr *SimpleRefExpr = RefExpr;
9332 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9333 /*AllowArraySection=*/false);
9334 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009335 // It will be analyzed later.
9336 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009337 SrcExprs.push_back(nullptr);
9338 DstExprs.push_back(nullptr);
9339 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009340 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009341 ValueDecl *D = Res.first;
9342 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009343 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009344
Alexey Bataeve122da12016-03-17 10:50:17 +00009345 QualType Type = D->getType();
9346 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009347
9348 // OpenMP [2.14.4.2, Restrictions, p.2]
9349 // A list item that appears in a copyprivate clause may not appear in a
9350 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009351 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9352 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009353 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9354 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009355 Diag(ELoc, diag::err_omp_wrong_dsa)
9356 << getOpenMPClauseName(DVar.CKind)
9357 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009358 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009359 continue;
9360 }
9361
9362 // OpenMP [2.11.4.2, Restrictions, p.1]
9363 // All list items that appear in a copyprivate clause must be either
9364 // threadprivate or private in the enclosing context.
9365 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009366 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009367 if (DVar.CKind == OMPC_shared) {
9368 Diag(ELoc, diag::err_omp_required_access)
9369 << getOpenMPClauseName(OMPC_copyprivate)
9370 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009371 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009372 continue;
9373 }
9374 }
9375 }
9376
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009377 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009378 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009379 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009380 << getOpenMPClauseName(OMPC_copyprivate) << Type
9381 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009382 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009383 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009384 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009385 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009386 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009387 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009388 continue;
9389 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009390
Alexey Bataevbae9a792014-06-27 10:37:06 +00009391 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9392 // A variable of class type (or array thereof) that appears in a
9393 // copyin clause requires an accessible, unambiguous copy assignment
9394 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009395 Type = Context.getBaseElementType(Type.getNonReferenceType())
9396 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009397 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009398 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9399 D->hasAttrs() ? &D->getAttrs() : nullptr);
9400 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009401 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009402 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9403 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009404 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009405 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9406 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009407 PseudoDstExpr, PseudoSrcExpr);
9408 if (AssignmentOp.isInvalid())
9409 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009410 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009411 /*DiscardedValue=*/true);
9412 if (AssignmentOp.isInvalid())
9413 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009414
9415 // No need to mark vars as copyprivate, they are already threadprivate or
9416 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009417 assert(VD || IsOpenMPCapturedDecl(D));
9418 Vars.push_back(
9419 VD ? RefExpr->IgnoreParens()
9420 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009421 SrcExprs.push_back(PseudoSrcExpr);
9422 DstExprs.push_back(PseudoDstExpr);
9423 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009424 }
9425
9426 if (Vars.empty())
9427 return nullptr;
9428
Alexey Bataeva63048e2015-03-23 06:18:07 +00009429 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9430 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009431}
9432
Alexey Bataev6125da92014-07-21 11:26:11 +00009433OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9434 SourceLocation StartLoc,
9435 SourceLocation LParenLoc,
9436 SourceLocation EndLoc) {
9437 if (VarList.empty())
9438 return nullptr;
9439
9440 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9441}
Alexey Bataevdea47612014-07-23 07:46:59 +00009442
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009443OMPClause *
9444Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9445 SourceLocation DepLoc, SourceLocation ColonLoc,
9446 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9447 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009448 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009449 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009450 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009451 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009452 return nullptr;
9453 }
9454 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009455 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9456 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009457 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009458 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009459 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9460 /*Last=*/OMPC_DEPEND_unknown, Except)
9461 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009462 return nullptr;
9463 }
9464 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009465 llvm::APSInt DepCounter(/*BitWidth=*/32);
9466 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9467 if (DepKind == OMPC_DEPEND_sink) {
9468 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9469 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9470 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009471 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009472 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009473 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9474 DSAStack->getParentOrderedRegionParam()) {
9475 for (auto &RefExpr : VarList) {
9476 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9477 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9478 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9479 // It will be analyzed later.
9480 Vars.push_back(RefExpr);
9481 continue;
9482 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009483
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009484 SourceLocation ELoc = RefExpr->getExprLoc();
9485 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9486 if (DepKind == OMPC_DEPEND_sink) {
9487 if (DepCounter >= TotalDepCount) {
9488 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9489 continue;
9490 }
9491 ++DepCounter;
9492 // OpenMP [2.13.9, Summary]
9493 // depend(dependence-type : vec), where dependence-type is:
9494 // 'sink' and where vec is the iteration vector, which has the form:
9495 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9496 // where n is the value specified by the ordered clause in the loop
9497 // directive, xi denotes the loop iteration variable of the i-th nested
9498 // loop associated with the loop directive, and di is a constant
9499 // non-negative integer.
9500 SimpleExpr = SimpleExpr->IgnoreImplicit();
9501 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9502 if (!DE) {
9503 OverloadedOperatorKind OOK = OO_None;
9504 SourceLocation OOLoc;
9505 Expr *LHS, *RHS;
9506 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9507 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9508 OOLoc = BO->getOperatorLoc();
9509 LHS = BO->getLHS()->IgnoreParenImpCasts();
9510 RHS = BO->getRHS()->IgnoreParenImpCasts();
9511 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9512 OOK = OCE->getOperator();
9513 OOLoc = OCE->getOperatorLoc();
9514 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9515 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9516 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9517 OOK = MCE->getMethodDecl()
9518 ->getNameInfo()
9519 .getName()
9520 .getCXXOverloadedOperator();
9521 OOLoc = MCE->getCallee()->getExprLoc();
9522 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9523 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9524 } else {
9525 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9526 continue;
9527 }
9528 DE = dyn_cast<DeclRefExpr>(LHS);
9529 if (!DE) {
9530 Diag(LHS->getExprLoc(),
9531 diag::err_omp_depend_sink_expected_loop_iteration)
9532 << DSAStack->getParentLoopControlVariable(
9533 DepCounter.getZExtValue());
9534 continue;
9535 }
9536 if (OOK != OO_Plus && OOK != OO_Minus) {
9537 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9538 continue;
9539 }
9540 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9541 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9542 if (Res.isInvalid())
9543 continue;
9544 }
9545 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9546 if (!CurContext->isDependentContext() &&
9547 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009548 (!VD ||
9549 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009550 Diag(DE->getExprLoc(),
9551 diag::err_omp_depend_sink_expected_loop_iteration)
9552 << DSAStack->getParentLoopControlVariable(
9553 DepCounter.getZExtValue());
9554 continue;
9555 }
9556 } else {
9557 // OpenMP [2.11.1.1, Restrictions, p.3]
9558 // A variable that is part of another variable (such as a field of a
9559 // structure) but is not an array element or an array section cannot
9560 // appear in a depend clause.
9561 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9562 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9563 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9564 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9565 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009566 (ASE &&
9567 !ASE->getBase()
9568 ->getType()
9569 .getNonReferenceType()
9570 ->isPointerType() &&
9571 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009572 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9573 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009574 continue;
9575 }
9576 }
9577
9578 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9579 }
9580
9581 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9582 TotalDepCount > VarList.size() &&
9583 DSAStack->getParentOrderedRegionParam()) {
9584 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9585 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9586 }
9587 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9588 Vars.empty())
9589 return nullptr;
9590 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009591
9592 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9593 DepLoc, ColonLoc, Vars);
9594}
Michael Wonge710d542015-08-07 16:16:36 +00009595
9596OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9597 SourceLocation LParenLoc,
9598 SourceLocation EndLoc) {
9599 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009600
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009601 // OpenMP [2.9.1, Restrictions]
9602 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009603 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9604 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009605 return nullptr;
9606
Michael Wonge710d542015-08-07 16:16:36 +00009607 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9608}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009609
9610static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9611 DSAStackTy *Stack, CXXRecordDecl *RD) {
9612 if (!RD || RD->isInvalidDecl())
9613 return true;
9614
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009615 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9616 if (auto *CTD = CTSD->getSpecializedTemplate())
9617 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009618 auto QTy = SemaRef.Context.getRecordType(RD);
9619 if (RD->isDynamicClass()) {
9620 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9621 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9622 return false;
9623 }
9624 auto *DC = RD;
9625 bool IsCorrect = true;
9626 for (auto *I : DC->decls()) {
9627 if (I) {
9628 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9629 if (MD->isStatic()) {
9630 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9631 SemaRef.Diag(MD->getLocation(),
9632 diag::note_omp_static_member_in_target);
9633 IsCorrect = false;
9634 }
9635 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9636 if (VD->isStaticDataMember()) {
9637 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9638 SemaRef.Diag(VD->getLocation(),
9639 diag::note_omp_static_member_in_target);
9640 IsCorrect = false;
9641 }
9642 }
9643 }
9644 }
9645
9646 for (auto &I : RD->bases()) {
9647 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9648 I.getType()->getAsCXXRecordDecl()))
9649 IsCorrect = false;
9650 }
9651 return IsCorrect;
9652}
9653
9654static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9655 DSAStackTy *Stack, QualType QTy) {
9656 NamedDecl *ND;
9657 if (QTy->isIncompleteType(&ND)) {
9658 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9659 return false;
9660 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9661 if (!RD->isInvalidDecl() &&
9662 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9663 return false;
9664 }
9665 return true;
9666}
9667
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009668/// \brief Return true if it can be proven that the provided array expression
9669/// (array section or array subscript) does NOT specify the whole size of the
9670/// array whose base type is \a BaseQTy.
9671static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9672 const Expr *E,
9673 QualType BaseQTy) {
9674 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9675
9676 // If this is an array subscript, it refers to the whole size if the size of
9677 // the dimension is constant and equals 1. Also, an array section assumes the
9678 // format of an array subscript if no colon is used.
9679 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9680 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9681 return ATy->getSize().getSExtValue() != 1;
9682 // Size can't be evaluated statically.
9683 return false;
9684 }
9685
9686 assert(OASE && "Expecting array section if not an array subscript.");
9687 auto *LowerBound = OASE->getLowerBound();
9688 auto *Length = OASE->getLength();
9689
9690 // If there is a lower bound that does not evaluates to zero, we are not
9691 // convering the whole dimension.
9692 if (LowerBound) {
9693 llvm::APSInt ConstLowerBound;
9694 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9695 return false; // Can't get the integer value as a constant.
9696 if (ConstLowerBound.getSExtValue())
9697 return true;
9698 }
9699
9700 // If we don't have a length we covering the whole dimension.
9701 if (!Length)
9702 return false;
9703
9704 // If the base is a pointer, we don't have a way to get the size of the
9705 // pointee.
9706 if (BaseQTy->isPointerType())
9707 return false;
9708
9709 // We can only check if the length is the same as the size of the dimension
9710 // if we have a constant array.
9711 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9712 if (!CATy)
9713 return false;
9714
9715 llvm::APSInt ConstLength;
9716 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9717 return false; // Can't get the integer value as a constant.
9718
9719 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9720}
9721
9722// Return true if it can be proven that the provided array expression (array
9723// section or array subscript) does NOT specify a single element of the array
9724// whose base type is \a BaseQTy.
9725static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9726 const Expr *E,
9727 QualType BaseQTy) {
9728 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9729
9730 // An array subscript always refer to a single element. Also, an array section
9731 // assumes the format of an array subscript if no colon is used.
9732 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9733 return false;
9734
9735 assert(OASE && "Expecting array section if not an array subscript.");
9736 auto *Length = OASE->getLength();
9737
9738 // If we don't have a length we have to check if the array has unitary size
9739 // for this dimension. Also, we should always expect a length if the base type
9740 // is pointer.
9741 if (!Length) {
9742 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9743 return ATy->getSize().getSExtValue() != 1;
9744 // We cannot assume anything.
9745 return false;
9746 }
9747
9748 // Check if the length evaluates to 1.
9749 llvm::APSInt ConstLength;
9750 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9751 return false; // Can't get the integer value as a constant.
9752
9753 return ConstLength.getSExtValue() != 1;
9754}
9755
Samuel Antao5de996e2016-01-22 20:21:36 +00009756// Return the expression of the base of the map clause or null if it cannot
9757// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009758// valid as a standalone map clause expression. In the process, record all the
9759// components of the expression.
9760static Expr *CheckMapClauseExpressionBase(
9761 Sema &SemaRef, Expr *E,
9762 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009763 SourceLocation ELoc = E->getExprLoc();
9764 SourceRange ERange = E->getSourceRange();
9765
9766 // The base of elements of list in a map clause have to be either:
9767 // - a reference to variable or field.
9768 // - a member expression.
9769 // - an array expression.
9770 //
9771 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9772 // reference to 'r'.
9773 //
9774 // If we have:
9775 //
9776 // struct SS {
9777 // Bla S;
9778 // foo() {
9779 // #pragma omp target map (S.Arr[:12]);
9780 // }
9781 // }
9782 //
9783 // We want to retrieve the member expression 'this->S';
9784
9785 Expr *RelevantExpr = nullptr;
9786
Samuel Antao5de996e2016-01-22 20:21:36 +00009787 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9788 // If a list item is an array section, it must specify contiguous storage.
9789 //
9790 // For this restriction it is sufficient that we make sure only references
9791 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009792 // exist except in the rightmost expression (unless they cover the whole
9793 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009794 //
9795 // r.ArrS[3:5].Arr[6:7]
9796 //
9797 // r.ArrS[3:5].x
9798 //
9799 // but these would be valid:
9800 // r.ArrS[3].Arr[6:7]
9801 //
9802 // r.ArrS[3].x
9803
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009804 bool AllowUnitySizeArraySection = true;
9805 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009806
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009807 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009808 E = E->IgnoreParenImpCasts();
9809
9810 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9811 if (!isa<VarDecl>(CurE->getDecl()))
9812 break;
9813
9814 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009815
9816 // If we got a reference to a declaration, we should not expect any array
9817 // section before that.
9818 AllowUnitySizeArraySection = false;
9819 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009820
9821 // Record the component.
9822 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9823 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009824 continue;
9825 }
9826
9827 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9828 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9829
9830 if (isa<CXXThisExpr>(BaseE))
9831 // We found a base expression: this->Val.
9832 RelevantExpr = CurE;
9833 else
9834 E = BaseE;
9835
9836 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9837 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9838 << CurE->getSourceRange();
9839 break;
9840 }
9841
9842 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9843
9844 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9845 // A bit-field cannot appear in a map clause.
9846 //
9847 if (FD->isBitField()) {
9848 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9849 << CurE->getSourceRange();
9850 break;
9851 }
9852
9853 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9854 // If the type of a list item is a reference to a type T then the type
9855 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009856 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009857
9858 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9859 // A list item cannot be a variable that is a member of a structure with
9860 // a union type.
9861 //
9862 if (auto *RT = CurType->getAs<RecordType>())
9863 if (RT->isUnionType()) {
9864 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9865 << CurE->getSourceRange();
9866 break;
9867 }
9868
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009869 // If we got a member expression, we should not expect any array section
9870 // before that:
9871 //
9872 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9873 // If a list item is an element of a structure, only the rightmost symbol
9874 // of the variable reference can be an array section.
9875 //
9876 AllowUnitySizeArraySection = false;
9877 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009878
9879 // Record the component.
9880 CurComponents.push_back(
9881 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009882 continue;
9883 }
9884
9885 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9886 E = CurE->getBase()->IgnoreParenImpCasts();
9887
9888 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9889 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9890 << 0 << CurE->getSourceRange();
9891 break;
9892 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009893
9894 // If we got an array subscript that express the whole dimension we
9895 // can have any array expressions before. If it only expressing part of
9896 // the dimension, we can only have unitary-size array expressions.
9897 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9898 E->getType()))
9899 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009900
9901 // Record the component - we don't have any declaration associated.
9902 CurComponents.push_back(
9903 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009904 continue;
9905 }
9906
9907 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009908 E = CurE->getBase()->IgnoreParenImpCasts();
9909
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009910 auto CurType =
9911 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9912
Samuel Antao5de996e2016-01-22 20:21:36 +00009913 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9914 // If the type of a list item is a reference to a type T then the type
9915 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009916 if (CurType->isReferenceType())
9917 CurType = CurType->getPointeeType();
9918
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009919 bool IsPointer = CurType->isAnyPointerType();
9920
9921 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009922 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9923 << 0 << CurE->getSourceRange();
9924 break;
9925 }
9926
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009927 bool NotWhole =
9928 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9929 bool NotUnity =
9930 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9931
9932 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9933 // Any array section is currently allowed.
9934 //
9935 // If this array section refers to the whole dimension we can still
9936 // accept other array sections before this one, except if the base is a
9937 // pointer. Otherwise, only unitary sections are accepted.
9938 if (NotWhole || IsPointer)
9939 AllowWholeSizeArraySection = false;
9940 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9941 (AllowWholeSizeArraySection && NotWhole)) {
9942 // A unity or whole array section is not allowed and that is not
9943 // compatible with the properties of the current array section.
9944 SemaRef.Diag(
9945 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9946 << CurE->getSourceRange();
9947 break;
9948 }
Samuel Antao90927002016-04-26 14:54:23 +00009949
9950 // Record the component - we don't have any declaration associated.
9951 CurComponents.push_back(
9952 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009953 continue;
9954 }
9955
9956 // If nothing else worked, this is not a valid map clause expression.
9957 SemaRef.Diag(ELoc,
9958 diag::err_omp_expected_named_var_member_or_array_expression)
9959 << ERange;
9960 break;
9961 }
9962
9963 return RelevantExpr;
9964}
9965
9966// Return true if expression E associated with value VD has conflicts with other
9967// map information.
Samuel Antao90927002016-04-26 14:54:23 +00009968static bool CheckMapConflicts(
9969 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9970 bool CurrentRegionOnly,
9971 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009972 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +00009973 SourceLocation ELoc = E->getExprLoc();
9974 SourceRange ERange = E->getSourceRange();
9975
9976 // In order to easily check the conflicts we need to match each component of
9977 // the expression under test with the components of the expressions that are
9978 // already in the stack.
9979
Samuel Antao5de996e2016-01-22 20:21:36 +00009980 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009981 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009982 "Map clause expression with unexpected base!");
9983
9984 // Variables to help detecting enclosing problems in data environment nests.
9985 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +00009986 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +00009987
Samuel Antao90927002016-04-26 14:54:23 +00009988 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9989 VD, CurrentRegionOnly,
9990 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
9991 StackComponents) -> bool {
9992
Samuel Antao5de996e2016-01-22 20:21:36 +00009993 assert(!StackComponents.empty() &&
9994 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009995 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009996 "Map clause expression with unexpected base!");
9997
Samuel Antao90927002016-04-26 14:54:23 +00009998 // The whole expression in the stack.
9999 auto *RE = StackComponents.front().getAssociatedExpression();
10000
Samuel Antao5de996e2016-01-22 20:21:36 +000010001 // Expressions must start from the same base. Here we detect at which
10002 // point both expressions diverge from each other and see if we can
10003 // detect if the memory referred to both expressions is contiguous and
10004 // do not overlap.
10005 auto CI = CurComponents.rbegin();
10006 auto CE = CurComponents.rend();
10007 auto SI = StackComponents.rbegin();
10008 auto SE = StackComponents.rend();
10009 for (; CI != CE && SI != SE; ++CI, ++SI) {
10010
10011 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10012 // At most one list item can be an array item derived from a given
10013 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000010014 if (CurrentRegionOnly &&
10015 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10016 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10017 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10018 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10019 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000010020 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000010021 << CI->getAssociatedExpression()->getSourceRange();
10022 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10023 diag::note_used_here)
10024 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000010025 return true;
10026 }
10027
10028 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000010029 if (CI->getAssociatedExpression()->getStmtClass() !=
10030 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000010031 break;
10032
10033 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000010034 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000010035 break;
10036 }
10037
10038 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10039 // List items of map clauses in the same construct must not share
10040 // original storage.
10041 //
10042 // If the expressions are exactly the same or one is a subset of the
10043 // other, it means they are sharing storage.
10044 if (CI == CE && SI == SE) {
10045 if (CurrentRegionOnly) {
10046 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10047 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10048 << RE->getSourceRange();
10049 return true;
10050 } else {
10051 // If we find the same expression in the enclosing data environment,
10052 // that is legal.
10053 IsEnclosedByDataEnvironmentExpr = true;
10054 return false;
10055 }
10056 }
10057
Samuel Antao90927002016-04-26 14:54:23 +000010058 QualType DerivedType =
10059 std::prev(CI)->getAssociatedDeclaration()->getType();
10060 SourceLocation DerivedLoc =
10061 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010062
10063 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10064 // If the type of a list item is a reference to a type T then the type
10065 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010066 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010067
10068 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10069 // A variable for which the type is pointer and an array section
10070 // derived from that variable must not appear as list items of map
10071 // clauses of the same construct.
10072 //
10073 // Also, cover one of the cases in:
10074 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10075 // If any part of the original storage of a list item has corresponding
10076 // storage in the device data environment, all of the original storage
10077 // must have corresponding storage in the device data environment.
10078 //
10079 if (DerivedType->isAnyPointerType()) {
10080 if (CI == CE || SI == SE) {
10081 SemaRef.Diag(
10082 DerivedLoc,
10083 diag::err_omp_pointer_mapped_along_with_derived_section)
10084 << DerivedLoc;
10085 } else {
10086 assert(CI != CE && SI != SE);
10087 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10088 << DerivedLoc;
10089 }
10090 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10091 << RE->getSourceRange();
10092 return true;
10093 }
10094
10095 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10096 // List items of map clauses in the same construct must not share
10097 // original storage.
10098 //
10099 // An expression is a subset of the other.
10100 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10101 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10102 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10103 << RE->getSourceRange();
10104 return true;
10105 }
10106
10107 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010108 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010109 if (!CurrentRegionOnly && SI != SE)
10110 EnclosingExpr = RE;
10111
10112 // The current expression is a subset of the expression in the data
10113 // environment.
10114 IsEnclosedByDataEnvironmentExpr |=
10115 (!CurrentRegionOnly && CI != CE && SI == SE);
10116
10117 return false;
10118 });
10119
10120 if (CurrentRegionOnly)
10121 return FoundError;
10122
10123 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10124 // If any part of the original storage of a list item has corresponding
10125 // storage in the device data environment, all of the original storage must
10126 // have corresponding storage in the device data environment.
10127 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10128 // If a list item is an element of a structure, and a different element of
10129 // the structure has a corresponding list item in the device data environment
10130 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010131 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010132 // data environment prior to the task encountering the construct.
10133 //
10134 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10135 SemaRef.Diag(ELoc,
10136 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10137 << ERange;
10138 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10139 << EnclosingExpr->getSourceRange();
10140 return true;
10141 }
10142
10143 return FoundError;
10144}
10145
Samuel Antao23abd722016-01-19 20:40:49 +000010146OMPClause *
10147Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10148 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10149 SourceLocation MapLoc, SourceLocation ColonLoc,
10150 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10151 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010152 SmallVector<Expr *, 4> Vars;
10153
Samuel Antao90927002016-04-26 14:54:23 +000010154 // Keep track of the mappable components and base declarations in this clause.
10155 // Each entry in the list is going to have a list of components associated. We
10156 // record each set of the components so that we can build the clause later on.
10157 // In the end we should have the same amount of declarations and component
10158 // lists.
10159 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10160 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10161
10162 ClauseComponents.reserve(VarList.size());
10163 ClauseBaseDeclarations.reserve(VarList.size());
10164
Kelvin Li0bff7af2015-11-23 05:32:03 +000010165 for (auto &RE : VarList) {
10166 assert(RE && "Null expr in omp map");
10167 if (isa<DependentScopeDeclRefExpr>(RE)) {
10168 // It will be analyzed later.
10169 Vars.push_back(RE);
10170 continue;
10171 }
10172 SourceLocation ELoc = RE->getExprLoc();
10173
Kelvin Li0bff7af2015-11-23 05:32:03 +000010174 auto *VE = RE->IgnoreParenLValueCasts();
10175
10176 if (VE->isValueDependent() || VE->isTypeDependent() ||
10177 VE->isInstantiationDependent() ||
10178 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010179 // We can only analyze this information once the missing information is
10180 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010181 Vars.push_back(RE);
10182 continue;
10183 }
10184
10185 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010186
Samuel Antao5de996e2016-01-22 20:21:36 +000010187 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10188 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10189 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010190 continue;
10191 }
10192
Samuel Antao90927002016-04-26 14:54:23 +000010193 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10194 ValueDecl *CurDeclaration = nullptr;
10195
10196 // Obtain the array or member expression bases if required. Also, fill the
10197 // components array with all the components identified in the process.
10198 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010199 if (!BE)
10200 continue;
10201
Samuel Antao90927002016-04-26 14:54:23 +000010202 assert(!CurComponents.empty() &&
10203 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010204
Samuel Antao90927002016-04-26 14:54:23 +000010205 // For the following checks, we rely on the base declaration which is
10206 // expected to be associated with the last component. The declaration is
10207 // expected to be a variable or a field (if 'this' is being mapped).
10208 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10209 assert(CurDeclaration && "Null decl on map clause.");
10210 assert(
10211 CurDeclaration->isCanonicalDecl() &&
10212 "Expecting components to have associated only canonical declarations.");
10213
10214 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10215 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010216
10217 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010218 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010219
10220 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10221 // threadprivate variables cannot appear in a map clause.
10222 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010223 auto DVar = DSAStack->getTopDSA(VD, false);
10224 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10225 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10226 continue;
10227 }
10228
Samuel Antao5de996e2016-01-22 20:21:36 +000010229 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10230 // A list item cannot appear in both a map clause and a data-sharing
10231 // attribute clause on the same construct.
10232 //
10233 // TODO: Implement this check - it cannot currently be tested because of
10234 // missing implementation of the other data sharing clauses in target
10235 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010236
Samuel Antao5de996e2016-01-22 20:21:36 +000010237 // Check conflicts with other map clause expressions. We check the conflicts
10238 // with the current construct separately from the enclosing data
10239 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010240 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10241 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010242 break;
Samuel Antao90927002016-04-26 14:54:23 +000010243 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10244 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010245 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010246
Samuel Antao5de996e2016-01-22 20:21:36 +000010247 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10248 // If the type of a list item is a reference to a type T then the type will
10249 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010250 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010251
10252 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010253 // A list item must have a mappable type.
10254 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10255 DSAStack, Type))
10256 continue;
10257
Samuel Antaodf67fc42016-01-19 19:15:56 +000010258 // target enter data
10259 // OpenMP [2.10.2, Restrictions, p. 99]
10260 // A map-type must be specified in all map clauses and must be either
10261 // to or alloc.
10262 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10263 if (DKind == OMPD_target_enter_data &&
10264 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10265 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010266 << (IsMapTypeImplicit ? 1 : 0)
10267 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010268 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010269 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010270 }
10271
Samuel Antao72590762016-01-19 20:04:50 +000010272 // target exit_data
10273 // OpenMP [2.10.3, Restrictions, p. 102]
10274 // A map-type must be specified in all map clauses and must be either
10275 // from, release, or delete.
10276 DKind = DSAStack->getCurrentDirective();
10277 if (DKind == OMPD_target_exit_data &&
10278 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10279 MapType == OMPC_MAP_delete)) {
10280 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010281 << (IsMapTypeImplicit ? 1 : 0)
10282 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010283 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010284 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010285 }
10286
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010287 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10288 // A list item cannot appear in both a map clause and a data-sharing
10289 // attribute clause on the same construct
10290 if (DKind == OMPD_target && VD) {
10291 auto DVar = DSAStack->getTopDSA(VD, false);
10292 if (isOpenMPPrivate(DVar.CKind)) {
10293 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10294 << getOpenMPClauseName(DVar.CKind)
10295 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010296 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010297 continue;
10298 }
10299 }
10300
Samuel Antao90927002016-04-26 14:54:23 +000010301 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010302 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010303
10304 // Store the components in the stack so that they can be used to check
10305 // against other clauses later on.
10306 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10307
10308 // Save the components and declaration to create the clause. For purposes of
10309 // the clause creation, any component list that has has base 'this' uses
10310 // null has
10311 ClauseComponents.resize(ClauseComponents.size() + 1);
10312 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10313 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10314 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010315 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010316
Samuel Antao5de996e2016-01-22 20:21:36 +000010317 // We need to produce a map clause even if we don't have variables so that
10318 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010319 return OMPMapClause::Create(
10320 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10321 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010322}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010323
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010324QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10325 TypeResult ParsedType) {
10326 assert(ParsedType.isUsable());
10327
10328 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10329 if (ReductionType.isNull())
10330 return QualType();
10331
10332 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10333 // A type name in a declare reduction directive cannot be a function type, an
10334 // array type, a reference type, or a type qualified with const, volatile or
10335 // restrict.
10336 if (ReductionType.hasQualifiers()) {
10337 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10338 return QualType();
10339 }
10340
10341 if (ReductionType->isFunctionType()) {
10342 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10343 return QualType();
10344 }
10345 if (ReductionType->isReferenceType()) {
10346 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10347 return QualType();
10348 }
10349 if (ReductionType->isArrayType()) {
10350 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10351 return QualType();
10352 }
10353 return ReductionType;
10354}
10355
10356Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10357 Scope *S, DeclContext *DC, DeclarationName Name,
10358 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10359 AccessSpecifier AS, Decl *PrevDeclInScope) {
10360 SmallVector<Decl *, 8> Decls;
10361 Decls.reserve(ReductionTypes.size());
10362
10363 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10364 ForRedeclaration);
10365 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10366 // A reduction-identifier may not be re-declared in the current scope for the
10367 // same type or for a type that is compatible according to the base language
10368 // rules.
10369 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10370 OMPDeclareReductionDecl *PrevDRD = nullptr;
10371 bool InCompoundScope = true;
10372 if (S != nullptr) {
10373 // Find previous declaration with the same name not referenced in other
10374 // declarations.
10375 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10376 InCompoundScope =
10377 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10378 LookupName(Lookup, S);
10379 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10380 /*AllowInlineNamespace=*/false);
10381 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10382 auto Filter = Lookup.makeFilter();
10383 while (Filter.hasNext()) {
10384 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10385 if (InCompoundScope) {
10386 auto I = UsedAsPrevious.find(PrevDecl);
10387 if (I == UsedAsPrevious.end())
10388 UsedAsPrevious[PrevDecl] = false;
10389 if (auto *D = PrevDecl->getPrevDeclInScope())
10390 UsedAsPrevious[D] = true;
10391 }
10392 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10393 PrevDecl->getLocation();
10394 }
10395 Filter.done();
10396 if (InCompoundScope) {
10397 for (auto &PrevData : UsedAsPrevious) {
10398 if (!PrevData.second) {
10399 PrevDRD = PrevData.first;
10400 break;
10401 }
10402 }
10403 }
10404 } else if (PrevDeclInScope != nullptr) {
10405 auto *PrevDRDInScope = PrevDRD =
10406 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10407 do {
10408 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10409 PrevDRDInScope->getLocation();
10410 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10411 } while (PrevDRDInScope != nullptr);
10412 }
10413 for (auto &TyData : ReductionTypes) {
10414 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10415 bool Invalid = false;
10416 if (I != PreviousRedeclTypes.end()) {
10417 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10418 << TyData.first;
10419 Diag(I->second, diag::note_previous_definition);
10420 Invalid = true;
10421 }
10422 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10423 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10424 Name, TyData.first, PrevDRD);
10425 DC->addDecl(DRD);
10426 DRD->setAccess(AS);
10427 Decls.push_back(DRD);
10428 if (Invalid)
10429 DRD->setInvalidDecl();
10430 else
10431 PrevDRD = DRD;
10432 }
10433
10434 return DeclGroupPtrTy::make(
10435 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10436}
10437
10438void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10439 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10440
10441 // Enter new function scope.
10442 PushFunctionScope();
10443 getCurFunction()->setHasBranchProtectedScope();
10444 getCurFunction()->setHasOMPDeclareReductionCombiner();
10445
10446 if (S != nullptr)
10447 PushDeclContext(S, DRD);
10448 else
10449 CurContext = DRD;
10450
10451 PushExpressionEvaluationContext(PotentiallyEvaluated);
10452
10453 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010454 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10455 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10456 // uses semantics of argument handles by value, but it should be passed by
10457 // reference. C lang does not support references, so pass all parameters as
10458 // pointers.
10459 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010460 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010461 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010462 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10463 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10464 // uses semantics of argument handles by value, but it should be passed by
10465 // reference. C lang does not support references, so pass all parameters as
10466 // pointers.
10467 // Create 'T omp_out;' variable.
10468 auto *OmpOutParm =
10469 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10470 if (S != nullptr) {
10471 PushOnScopeChains(OmpInParm, S);
10472 PushOnScopeChains(OmpOutParm, S);
10473 } else {
10474 DRD->addDecl(OmpInParm);
10475 DRD->addDecl(OmpOutParm);
10476 }
10477}
10478
10479void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10480 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10481 DiscardCleanupsInEvaluationContext();
10482 PopExpressionEvaluationContext();
10483
10484 PopDeclContext();
10485 PopFunctionScopeInfo();
10486
10487 if (Combiner != nullptr)
10488 DRD->setCombiner(Combiner);
10489 else
10490 DRD->setInvalidDecl();
10491}
10492
10493void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10494 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10495
10496 // Enter new function scope.
10497 PushFunctionScope();
10498 getCurFunction()->setHasBranchProtectedScope();
10499
10500 if (S != nullptr)
10501 PushDeclContext(S, DRD);
10502 else
10503 CurContext = DRD;
10504
10505 PushExpressionEvaluationContext(PotentiallyEvaluated);
10506
10507 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010508 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10509 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10510 // uses semantics of argument handles by value, but it should be passed by
10511 // reference. C lang does not support references, so pass all parameters as
10512 // pointers.
10513 // Create 'T omp_priv;' variable.
10514 auto *OmpPrivParm =
10515 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010516 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10517 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10518 // uses semantics of argument handles by value, but it should be passed by
10519 // reference. C lang does not support references, so pass all parameters as
10520 // pointers.
10521 // Create 'T omp_orig;' variable.
10522 auto *OmpOrigParm =
10523 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010524 if (S != nullptr) {
10525 PushOnScopeChains(OmpPrivParm, S);
10526 PushOnScopeChains(OmpOrigParm, S);
10527 } else {
10528 DRD->addDecl(OmpPrivParm);
10529 DRD->addDecl(OmpOrigParm);
10530 }
10531}
10532
10533void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10534 Expr *Initializer) {
10535 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10536 DiscardCleanupsInEvaluationContext();
10537 PopExpressionEvaluationContext();
10538
10539 PopDeclContext();
10540 PopFunctionScopeInfo();
10541
10542 if (Initializer != nullptr)
10543 DRD->setInitializer(Initializer);
10544 else
10545 DRD->setInvalidDecl();
10546}
10547
10548Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10549 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10550 for (auto *D : DeclReductions.get()) {
10551 if (IsValid) {
10552 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10553 if (S != nullptr)
10554 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10555 } else
10556 D->setInvalidDecl();
10557 }
10558 return DeclReductions;
10559}
10560
Kelvin Li099bb8c2015-11-24 20:50:12 +000010561OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10562 SourceLocation StartLoc,
10563 SourceLocation LParenLoc,
10564 SourceLocation EndLoc) {
10565 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010566
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010567 // OpenMP [teams Constrcut, Restrictions]
10568 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010569 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10570 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010571 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010572
10573 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10574}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010575
10576OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10577 SourceLocation StartLoc,
10578 SourceLocation LParenLoc,
10579 SourceLocation EndLoc) {
10580 Expr *ValExpr = ThreadLimit;
10581
10582 // OpenMP [teams Constrcut, Restrictions]
10583 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010584 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10585 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010586 return nullptr;
10587
10588 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10589 EndLoc);
10590}
Alexey Bataeva0569352015-12-01 10:17:31 +000010591
10592OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10593 SourceLocation StartLoc,
10594 SourceLocation LParenLoc,
10595 SourceLocation EndLoc) {
10596 Expr *ValExpr = Priority;
10597
10598 // OpenMP [2.9.1, task Constrcut]
10599 // The priority-value is a non-negative numerical scalar expression.
10600 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10601 /*StrictlyPositive=*/false))
10602 return nullptr;
10603
10604 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10605}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010606
10607OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10608 SourceLocation StartLoc,
10609 SourceLocation LParenLoc,
10610 SourceLocation EndLoc) {
10611 Expr *ValExpr = Grainsize;
10612
10613 // OpenMP [2.9.2, taskloop Constrcut]
10614 // The parameter of the grainsize clause must be a positive integer
10615 // expression.
10616 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10617 /*StrictlyPositive=*/true))
10618 return nullptr;
10619
10620 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10621}
Alexey Bataev382967a2015-12-08 12:06:20 +000010622
10623OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10624 SourceLocation StartLoc,
10625 SourceLocation LParenLoc,
10626 SourceLocation EndLoc) {
10627 Expr *ValExpr = NumTasks;
10628
10629 // OpenMP [2.9.2, taskloop Constrcut]
10630 // The parameter of the num_tasks clause must be a positive integer
10631 // expression.
10632 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10633 /*StrictlyPositive=*/true))
10634 return nullptr;
10635
10636 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10637}
10638
Alexey Bataev28c75412015-12-15 08:19:24 +000010639OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10640 SourceLocation LParenLoc,
10641 SourceLocation EndLoc) {
10642 // OpenMP [2.13.2, critical construct, Description]
10643 // ... where hint-expression is an integer constant expression that evaluates
10644 // to a valid lock hint.
10645 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10646 if (HintExpr.isInvalid())
10647 return nullptr;
10648 return new (Context)
10649 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10650}
10651
Carlo Bertollib4adf552016-01-15 18:50:31 +000010652OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10653 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10654 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10655 SourceLocation EndLoc) {
10656 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10657 std::string Values;
10658 Values += "'";
10659 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10660 Values += "'";
10661 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10662 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10663 return nullptr;
10664 }
10665 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010666 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010667 if (ChunkSize) {
10668 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10669 !ChunkSize->isInstantiationDependent() &&
10670 !ChunkSize->containsUnexpandedParameterPack()) {
10671 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10672 ExprResult Val =
10673 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10674 if (Val.isInvalid())
10675 return nullptr;
10676
10677 ValExpr = Val.get();
10678
10679 // OpenMP [2.7.1, Restrictions]
10680 // chunk_size must be a loop invariant integer expression with a positive
10681 // value.
10682 llvm::APSInt Result;
10683 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10684 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10685 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10686 << "dist_schedule" << ChunkSize->getSourceRange();
10687 return nullptr;
10688 }
10689 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010690 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10691 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10692 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010693 }
10694 }
10695 }
10696
10697 return new (Context)
10698 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010699 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010700}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010701
10702OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10703 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10704 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10705 SourceLocation KindLoc, SourceLocation EndLoc) {
10706 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10707 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10708 Kind != OMPC_DEFAULTMAP_scalar) {
10709 std::string Value;
10710 SourceLocation Loc;
10711 Value += "'";
10712 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10713 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10714 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10715 Loc = MLoc;
10716 } else {
10717 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10718 OMPC_DEFAULTMAP_scalar);
10719 Loc = KindLoc;
10720 }
10721 Value += "'";
10722 Diag(Loc, diag::err_omp_unexpected_clause_value)
10723 << Value << getOpenMPClauseName(OMPC_defaultmap);
10724 return nullptr;
10725 }
10726
10727 return new (Context)
10728 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10729}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010730
10731bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10732 DeclContext *CurLexicalContext = getCurLexicalContext();
10733 if (!CurLexicalContext->isFileContext() &&
10734 !CurLexicalContext->isExternCContext() &&
10735 !CurLexicalContext->isExternCXXContext()) {
10736 Diag(Loc, diag::err_omp_region_not_file_context);
10737 return false;
10738 }
10739 if (IsInOpenMPDeclareTargetContext) {
10740 Diag(Loc, diag::err_omp_enclosed_declare_target);
10741 return false;
10742 }
10743
10744 IsInOpenMPDeclareTargetContext = true;
10745 return true;
10746}
10747
10748void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10749 assert(IsInOpenMPDeclareTargetContext &&
10750 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10751
10752 IsInOpenMPDeclareTargetContext = false;
10753}
10754
10755static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10756 Sema &SemaRef, Decl *D) {
10757 if (!D)
10758 return;
10759 Decl *LD = nullptr;
10760 if (isa<TagDecl>(D)) {
10761 LD = cast<TagDecl>(D)->getDefinition();
10762 } else if (isa<VarDecl>(D)) {
10763 LD = cast<VarDecl>(D)->getDefinition();
10764
10765 // If this is an implicit variable that is legal and we do not need to do
10766 // anything.
10767 if (cast<VarDecl>(D)->isImplicit()) {
10768 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10769 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10770 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10771 return;
10772 }
10773
10774 } else if (isa<FunctionDecl>(D)) {
10775 const FunctionDecl *FD = nullptr;
10776 if (cast<FunctionDecl>(D)->hasBody(FD))
10777 LD = const_cast<FunctionDecl *>(FD);
10778
10779 // If the definition is associated with the current declaration in the
10780 // target region (it can be e.g. a lambda) that is legal and we do not need
10781 // to do anything else.
10782 if (LD == D) {
10783 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10784 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10785 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10786 return;
10787 }
10788 }
10789 if (!LD)
10790 LD = D;
10791 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10792 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10793 // Outlined declaration is not declared target.
10794 if (LD->isOutOfLine()) {
10795 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10796 SemaRef.Diag(SL, diag::note_used_here) << SR;
10797 } else {
10798 DeclContext *DC = LD->getDeclContext();
10799 while (DC) {
10800 if (isa<FunctionDecl>(DC) &&
10801 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10802 break;
10803 DC = DC->getParent();
10804 }
10805 if (DC)
10806 return;
10807
10808 // Is not declared in target context.
10809 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10810 SemaRef.Diag(SL, diag::note_used_here) << SR;
10811 }
10812 // Mark decl as declared target to prevent further diagnostic.
10813 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10814 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10815 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10816 }
10817}
10818
10819static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10820 Sema &SemaRef, DSAStackTy *Stack,
10821 ValueDecl *VD) {
10822 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10823 return true;
10824 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10825 return false;
10826 return true;
10827}
10828
10829void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10830 if (!D || D->isInvalidDecl())
10831 return;
10832 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10833 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10834 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10835 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10836 if (DSAStack->isThreadPrivate(VD)) {
10837 Diag(SL, diag::err_omp_threadprivate_in_target);
10838 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10839 return;
10840 }
10841 }
10842 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10843 // Problem if any with var declared with incomplete type will be reported
10844 // as normal, so no need to check it here.
10845 if ((E || !VD->getType()->isIncompleteType()) &&
10846 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10847 // Mark decl as declared target to prevent further diagnostic.
10848 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10849 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10850 if (ASTMutationListener *ML = Context.getASTMutationListener())
10851 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10852 }
10853 return;
10854 }
10855 }
10856 if (!E) {
10857 // Checking declaration inside declare target region.
10858 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10859 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10860 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10861 if (ASTMutationListener *ML = Context.getASTMutationListener())
10862 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10863 }
10864 return;
10865 }
10866 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10867}