blob: 074170207581e4441138f93af8a097535142764c [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
890 // FIXME: Right now, only implicit maps are implemented. Properly mapping
891 // values requires having the map, private, and firstprivate clauses SEMA
892 // and parsing in place, which we don't yet.
893
894 if (Ty->isReferenceType())
895 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
896 IsByRef = !Ty->isScalarType();
897 }
898
899 // When passing data by value, we need to make sure it fits the uintptr size
900 // and alignment, because the runtime library only deals with uintptr types.
901 // If it does not fit the uintptr size, we need to pass the data by reference
902 // instead.
903 if (!IsByRef &&
904 (Ctx.getTypeSizeInChars(Ty) >
905 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000906 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000907 IsByRef = true;
908
909 return IsByRef;
910}
911
Alexey Bataev90c228f2016-02-08 09:29:13 +0000912VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000913 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000914 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000915
916 // If we are attempting to capture a global variable in a directive with
917 // 'target' we return true so that this global is also mapped to the device.
918 //
919 // FIXME: If the declaration is enclosed in a 'declare target' directive,
920 // then it should not be captured. Therefore, an extra check has to be
921 // inserted here once support for 'declare target' is added.
922 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000923 auto *VD = dyn_cast<VarDecl>(D);
924 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000925 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000926 !DSAStack->isClauseParsingMode())
927 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000928 if (DSAStack->getCurScope() &&
929 DSAStack->hasDirective(
930 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
931 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000932 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000933 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000934 false))
935 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000936 }
937
Alexey Bataev48977c32015-08-04 08:10:48 +0000938 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
939 (!DSAStack->isClauseParsingMode() ||
940 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000941 auto &&Info = DSAStack->isLoopControlVariable(D);
942 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000943 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000944 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000945 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000946 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000947 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000948 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000949 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000950 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000951 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000952 if (DVarPrivate.CKind != OMPC_unknown)
953 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000954 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000955 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000956}
957
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000958bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000959 assert(LangOpts.OpenMP && "OpenMP is not allowed");
960 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000961 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000962}
963
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000964bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000965 assert(LangOpts.OpenMP && "OpenMP is not allowed");
966 // Return true if the current level is no longer enclosed in a target region.
967
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000968 auto *VD = dyn_cast<VarDecl>(D);
969 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000970 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
971 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000972}
973
Alexey Bataeved09d242014-05-28 05:53:51 +0000974void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000975
976void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
977 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000978 Scope *CurScope, SourceLocation Loc) {
979 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000980 PushExpressionEvaluationContext(PotentiallyEvaluated);
981}
982
Alexey Bataevaac108a2015-06-23 04:51:00 +0000983void Sema::StartOpenMPClause(OpenMPClauseKind K) {
984 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000985}
986
Alexey Bataevaac108a2015-06-23 04:51:00 +0000987void Sema::EndOpenMPClause() {
988 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000989}
990
Alexey Bataev758e55e2013-09-06 18:03:48 +0000991void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000992 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
993 // A variable of class type (or array thereof) that appears in a lastprivate
994 // clause requires an accessible, unambiguous default constructor for the
995 // class type, unless the list item is also specified in a firstprivate
996 // clause.
997 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000998 for (auto *C : D->clauses()) {
999 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1000 SmallVector<Expr *, 8> PrivateCopies;
1001 for (auto *DE : Clause->varlists()) {
1002 if (DE->isValueDependent() || DE->isTypeDependent()) {
1003 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001004 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001005 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001006 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001007 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1008 QualType Type = VD->getType().getNonReferenceType();
1009 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001010 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001011 // Generate helper private variable and initialize it with the
1012 // default value. The address of the original variable is replaced
1013 // by the address of the new private variable in CodeGen. This new
1014 // variable is not added to IdResolver, so the code in the OpenMP
1015 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001016 auto *VDPrivate = buildVarDecl(
1017 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001018 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001019 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1020 if (VDPrivate->isInvalidDecl())
1021 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001022 PrivateCopies.push_back(buildDeclRefExpr(
1023 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001024 } else {
1025 // The variable is also a firstprivate, so initialization sequence
1026 // for private copy is generated already.
1027 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001028 }
1029 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001030 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001031 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001032 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001033 }
1034 }
1035 }
1036
Alexey Bataev758e55e2013-09-06 18:03:48 +00001037 DSAStack->pop();
1038 DiscardCleanupsInEvaluationContext();
1039 PopExpressionEvaluationContext();
1040}
1041
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001042static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1043 Expr *NumIterations, Sema &SemaRef,
1044 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001045
Alexey Bataeva769e072013-03-22 06:34:35 +00001046namespace {
1047
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001048class VarDeclFilterCCC : public CorrectionCandidateCallback {
1049private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001050 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001051
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001052public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001053 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001054 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001055 NamedDecl *ND = Candidate.getCorrectionDecl();
1056 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1057 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001058 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1059 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001060 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001061 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001062 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001063};
Alexey Bataeved09d242014-05-28 05:53:51 +00001064} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065
1066ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1067 CXXScopeSpec &ScopeSpec,
1068 const DeclarationNameInfo &Id) {
1069 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1070 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1071
1072 if (Lookup.isAmbiguous())
1073 return ExprError();
1074
1075 VarDecl *VD;
1076 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001077 if (TypoCorrection Corrected = CorrectTypo(
1078 Id, LookupOrdinaryName, CurScope, nullptr,
1079 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001080 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001081 PDiag(Lookup.empty()
1082 ? diag::err_undeclared_var_use_suggest
1083 : diag::err_omp_expected_var_arg_suggest)
1084 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001085 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001087 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1088 : diag::err_omp_expected_var_arg)
1089 << Id.getName();
1090 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001092 } else {
1093 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001094 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001095 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1096 return ExprError();
1097 }
1098 }
1099 Lookup.suppressDiagnostics();
1100
1101 // OpenMP [2.9.2, Syntax, C/C++]
1102 // Variables must be file-scope, namespace-scope, or static block-scope.
1103 if (!VD->hasGlobalStorage()) {
1104 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001105 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1106 bool IsDecl =
1107 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001108 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001109 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1110 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001111 return ExprError();
1112 }
1113
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001114 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1115 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001116 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1117 // A threadprivate directive for file-scope variables must appear outside
1118 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001119 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1120 !getCurLexicalContext()->isTranslationUnit()) {
1121 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001122 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1123 bool IsDecl =
1124 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1125 Diag(VD->getLocation(),
1126 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1127 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001128 return ExprError();
1129 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001130 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1131 // A threadprivate directive for static class member variables must appear
1132 // in the class definition, in the same scope in which the member
1133 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001134 if (CanonicalVD->isStaticDataMember() &&
1135 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1136 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001137 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1138 bool IsDecl =
1139 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1140 Diag(VD->getLocation(),
1141 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1142 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001143 return ExprError();
1144 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001145 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1146 // A threadprivate directive for namespace-scope variables must appear
1147 // outside any definition or declaration other than the namespace
1148 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001149 if (CanonicalVD->getDeclContext()->isNamespace() &&
1150 (!getCurLexicalContext()->isFileContext() ||
1151 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1152 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001153 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1154 bool IsDecl =
1155 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1156 Diag(VD->getLocation(),
1157 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1158 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001159 return ExprError();
1160 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001161 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1162 // A threadprivate directive for static block-scope variables must appear
1163 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001164 if (CanonicalVD->isStaticLocal() && CurScope &&
1165 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001166 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001167 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1168 bool IsDecl =
1169 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1170 Diag(VD->getLocation(),
1171 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1172 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001173 return ExprError();
1174 }
1175
1176 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1177 // A threadprivate directive must lexically precede all references to any
1178 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001179 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001180 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001181 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001182 return ExprError();
1183 }
1184
1185 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001186 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1187 SourceLocation(), VD,
1188 /*RefersToEnclosingVariableOrCapture=*/false,
1189 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001190}
1191
Alexey Bataeved09d242014-05-28 05:53:51 +00001192Sema::DeclGroupPtrTy
1193Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1194 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001195 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001196 CurContext->addDecl(D);
1197 return DeclGroupPtrTy::make(DeclGroupRef(D));
1198 }
David Blaikie0403cb12016-01-15 23:43:25 +00001199 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001200}
1201
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001202namespace {
1203class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1204 Sema &SemaRef;
1205
1206public:
1207 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1208 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1209 if (VD->hasLocalStorage()) {
1210 SemaRef.Diag(E->getLocStart(),
1211 diag::err_omp_local_var_in_threadprivate_init)
1212 << E->getSourceRange();
1213 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1214 << VD << VD->getSourceRange();
1215 return true;
1216 }
1217 }
1218 return false;
1219 }
1220 bool VisitStmt(const Stmt *S) {
1221 for (auto Child : S->children()) {
1222 if (Child && Visit(Child))
1223 return true;
1224 }
1225 return false;
1226 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001227 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001228};
1229} // namespace
1230
Alexey Bataeved09d242014-05-28 05:53:51 +00001231OMPThreadPrivateDecl *
1232Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001233 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001234 for (auto &RefExpr : VarList) {
1235 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001236 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1237 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001238
Alexey Bataev376b4a42016-02-09 09:41:09 +00001239 // Mark variable as used.
1240 VD->setReferenced();
1241 VD->markUsed(Context);
1242
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001243 QualType QType = VD->getType();
1244 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1245 // It will be analyzed later.
1246 Vars.push_back(DE);
1247 continue;
1248 }
1249
Alexey Bataeva769e072013-03-22 06:34:35 +00001250 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1251 // A threadprivate variable must not have an incomplete type.
1252 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001253 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001254 continue;
1255 }
1256
1257 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1258 // A threadprivate variable must not have a reference type.
1259 if (VD->getType()->isReferenceType()) {
1260 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001261 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1262 bool IsDecl =
1263 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1264 Diag(VD->getLocation(),
1265 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1266 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001267 continue;
1268 }
1269
Samuel Antaof8b50122015-07-13 22:54:53 +00001270 // Check if this is a TLS variable. If TLS is not being supported, produce
1271 // the corresponding diagnostic.
1272 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1273 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1274 getLangOpts().OpenMPUseTLS &&
1275 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001276 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1277 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001278 Diag(ILoc, diag::err_omp_var_thread_local)
1279 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001280 bool IsDecl =
1281 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1282 Diag(VD->getLocation(),
1283 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1284 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001285 continue;
1286 }
1287
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001288 // Check if initial value of threadprivate variable reference variable with
1289 // local storage (it is not supported by runtime).
1290 if (auto Init = VD->getAnyInitializer()) {
1291 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001292 if (Checker.Visit(Init))
1293 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001294 }
1295
Alexey Bataeved09d242014-05-28 05:53:51 +00001296 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001297 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001298 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1299 Context, SourceRange(Loc, Loc)));
1300 if (auto *ML = Context.getASTMutationListener())
1301 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001302 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001303 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001304 if (!Vars.empty()) {
1305 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1306 Vars);
1307 D->setAccess(AS_public);
1308 }
1309 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001310}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001311
Alexey Bataev7ff55242014-06-19 09:13:45 +00001312static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001313 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001314 bool IsLoopIterVar = false) {
1315 if (DVar.RefExpr) {
1316 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1317 << getOpenMPClauseName(DVar.CKind);
1318 return;
1319 }
1320 enum {
1321 PDSA_StaticMemberShared,
1322 PDSA_StaticLocalVarShared,
1323 PDSA_LoopIterVarPrivate,
1324 PDSA_LoopIterVarLinear,
1325 PDSA_LoopIterVarLastprivate,
1326 PDSA_ConstVarShared,
1327 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001328 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001329 PDSA_LocalVarPrivate,
1330 PDSA_Implicit
1331 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001332 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001333 auto ReportLoc = D->getLocation();
1334 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001335 if (IsLoopIterVar) {
1336 if (DVar.CKind == OMPC_private)
1337 Reason = PDSA_LoopIterVarPrivate;
1338 else if (DVar.CKind == OMPC_lastprivate)
1339 Reason = PDSA_LoopIterVarLastprivate;
1340 else
1341 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001342 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1343 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001344 Reason = PDSA_TaskVarFirstprivate;
1345 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001349 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001350 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001351 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001352 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001353 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001354 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001355 ReportHint = true;
1356 Reason = PDSA_LocalVarPrivate;
1357 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001358 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001360 << Reason << ReportHint
1361 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1362 } else if (DVar.ImplicitDSALoc.isValid()) {
1363 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1364 << getOpenMPClauseName(DVar.CKind);
1365 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001366}
1367
Alexey Bataev758e55e2013-09-06 18:03:48 +00001368namespace {
1369class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1370 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001371 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001372 bool ErrorFound;
1373 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001374 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001375 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001376
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377public:
1378 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001379 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001380 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001381 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1382 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001383
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001384 auto DVar = Stack->getTopDSA(VD, false);
1385 // Check if the variable has explicit DSA set and stop analysis if it so.
1386 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001387
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001388 auto ELoc = E->getExprLoc();
1389 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001390 // The default(none) clause requires that each variable that is referenced
1391 // in the construct, and does not have a predetermined data-sharing
1392 // attribute, must have its data-sharing attribute explicitly determined
1393 // by being listed in a data-sharing attribute clause.
1394 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001395 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001396 VarsWithInheritedDSA.count(VD) == 0) {
1397 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001398 return;
1399 }
1400
1401 // OpenMP [2.9.3.6, Restrictions, p.2]
1402 // A list item that appears in a reduction clause of the innermost
1403 // enclosing worksharing or parallel construct may not be accessed in an
1404 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001405 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001406 [](OpenMPDirectiveKind K) -> bool {
1407 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001408 isOpenMPWorksharingDirective(K) ||
1409 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 },
1411 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001412 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001413 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001414 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1415 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001416 return;
1417 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001418
1419 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001420 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001421 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1422 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001423 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001424 }
1425 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001426 void VisitMemberExpr(MemberExpr *E) {
1427 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1428 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1429 auto DVar = Stack->getTopDSA(FD, false);
1430 // Check if the variable has explicit DSA set and stop analysis if it
1431 // so.
1432 if (DVar.RefExpr)
1433 return;
1434
1435 auto ELoc = E->getExprLoc();
1436 auto DKind = Stack->getCurrentDirective();
1437 // OpenMP [2.9.3.6, Restrictions, p.2]
1438 // A list item that appears in a reduction clause of the innermost
1439 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001440 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001441 DVar =
1442 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1443 [](OpenMPDirectiveKind K) -> bool {
1444 return isOpenMPParallelDirective(K) ||
1445 isOpenMPWorksharingDirective(K) ||
1446 isOpenMPTeamsDirective(K);
1447 },
1448 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001449 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001450 ErrorFound = true;
1451 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1452 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1453 return;
1454 }
1455
1456 // Define implicit data-sharing attributes for task.
1457 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001458 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1459 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 ImplicitFirstprivate.push_back(E);
1461 }
1462 }
1463 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001464 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001465 for (auto *C : S->clauses()) {
1466 // Skip analysis of arguments of implicitly defined firstprivate clause
1467 // for task directives.
1468 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1469 for (auto *CC : C->children()) {
1470 if (CC)
1471 Visit(CC);
1472 }
1473 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001474 }
1475 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 for (auto *C : S->children()) {
1477 if (C && !isa<OMPExecutableDirective>(C))
1478 Visit(C);
1479 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
1482 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001483 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001484 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001485 return VarsWithInheritedDSA;
1486 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001487
Alexey Bataev7ff55242014-06-19 09:13:45 +00001488 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1489 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001490};
Alexey Bataeved09d242014-05-28 05:53:51 +00001491} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001492
Alexey Bataevbae9a792014-06-27 10:37:06 +00001493void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001494 switch (DKind) {
1495 case OMPD_parallel: {
1496 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001497 QualType KmpInt32PtrTy =
1498 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001499 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001500 std::make_pair(".global_tid.", KmpInt32PtrTy),
1501 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1502 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001503 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001504 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1505 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001506 break;
1507 }
1508 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001509 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001510 std::make_pair(StringRef(), QualType()) // __context with shared vars
1511 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001512 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1513 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001514 break;
1515 }
1516 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001517 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001518 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001519 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001520 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1521 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001522 break;
1523 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001524 case OMPD_for_simd: {
1525 Sema::CapturedParamNameType Params[] = {
1526 std::make_pair(StringRef(), QualType()) // __context with shared vars
1527 };
1528 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1529 Params);
1530 break;
1531 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001532 case OMPD_sections: {
1533 Sema::CapturedParamNameType Params[] = {
1534 std::make_pair(StringRef(), QualType()) // __context with shared vars
1535 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001536 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1537 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001538 break;
1539 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001540 case OMPD_section: {
1541 Sema::CapturedParamNameType Params[] = {
1542 std::make_pair(StringRef(), QualType()) // __context with shared vars
1543 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001544 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1545 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001546 break;
1547 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 case OMPD_single: {
1549 Sema::CapturedParamNameType Params[] = {
1550 std::make_pair(StringRef(), QualType()) // __context with shared vars
1551 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001552 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1553 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001554 break;
1555 }
Alexander Musman80c22892014-07-17 08:54:58 +00001556 case OMPD_master: {
1557 Sema::CapturedParamNameType Params[] = {
1558 std::make_pair(StringRef(), QualType()) // __context with shared vars
1559 };
1560 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1561 Params);
1562 break;
1563 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001564 case OMPD_critical: {
1565 Sema::CapturedParamNameType Params[] = {
1566 std::make_pair(StringRef(), QualType()) // __context with shared vars
1567 };
1568 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1569 Params);
1570 break;
1571 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001572 case OMPD_parallel_for: {
1573 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001574 QualType KmpInt32PtrTy =
1575 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001576 Sema::CapturedParamNameType Params[] = {
1577 std::make_pair(".global_tid.", KmpInt32PtrTy),
1578 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1579 std::make_pair(StringRef(), QualType()) // __context with shared vars
1580 };
1581 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1582 Params);
1583 break;
1584 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001585 case OMPD_parallel_for_simd: {
1586 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001587 QualType KmpInt32PtrTy =
1588 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001589 Sema::CapturedParamNameType Params[] = {
1590 std::make_pair(".global_tid.", KmpInt32PtrTy),
1591 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1592 std::make_pair(StringRef(), QualType()) // __context with shared vars
1593 };
1594 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1595 Params);
1596 break;
1597 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001598 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001599 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001600 QualType KmpInt32PtrTy =
1601 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001602 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001603 std::make_pair(".global_tid.", KmpInt32PtrTy),
1604 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001605 std::make_pair(StringRef(), QualType()) // __context with shared vars
1606 };
1607 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1608 Params);
1609 break;
1610 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001613 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1614 FunctionProtoType::ExtProtoInfo EPI;
1615 EPI.Variadic = true;
1616 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001617 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001618 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001619 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1620 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1621 std::make_pair(".copy_fn.",
1622 Context.getPointerType(CopyFnType).withConst()),
1623 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001624 std::make_pair(StringRef(), QualType()) // __context with shared vars
1625 };
1626 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1627 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001628 // Mark this captured region as inlined, because we don't use outlined
1629 // function directly.
1630 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1631 AlwaysInlineAttr::CreateImplicit(
1632 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001633 break;
1634 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001635 case OMPD_ordered: {
1636 Sema::CapturedParamNameType Params[] = {
1637 std::make_pair(StringRef(), QualType()) // __context with shared vars
1638 };
1639 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1640 Params);
1641 break;
1642 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001643 case OMPD_atomic: {
1644 Sema::CapturedParamNameType Params[] = {
1645 std::make_pair(StringRef(), QualType()) // __context with shared vars
1646 };
1647 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1648 Params);
1649 break;
1650 }
Michael Wong65f367f2015-07-21 13:44:28 +00001651 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001652 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001653 case OMPD_target_parallel:
1654 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001655 Sema::CapturedParamNameType Params[] = {
1656 std::make_pair(StringRef(), QualType()) // __context with shared vars
1657 };
1658 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1659 Params);
1660 break;
1661 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001662 case OMPD_teams: {
1663 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001664 QualType KmpInt32PtrTy =
1665 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001666 Sema::CapturedParamNameType Params[] = {
1667 std::make_pair(".global_tid.", KmpInt32PtrTy),
1668 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1669 std::make_pair(StringRef(), QualType()) // __context with shared vars
1670 };
1671 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1672 Params);
1673 break;
1674 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001675 case OMPD_taskgroup: {
1676 Sema::CapturedParamNameType Params[] = {
1677 std::make_pair(StringRef(), QualType()) // __context with shared vars
1678 };
1679 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1680 Params);
1681 break;
1682 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001683 case OMPD_taskloop: {
Alexey Bataev7292c292016-04-25 12:22:29 +00001684 QualType KmpInt32Ty =
1685 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1686 QualType KmpUInt64Ty =
1687 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1688 QualType KmpInt64Ty =
1689 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1690 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1691 FunctionProtoType::ExtProtoInfo EPI;
1692 EPI.Variadic = true;
1693 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00001694 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00001695 std::make_pair(".global_tid.", KmpInt32Ty),
1696 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1697 std::make_pair(".privates.",
1698 Context.VoidPtrTy.withConst().withRestrict()),
1699 std::make_pair(
1700 ".copy_fn.",
1701 Context.getPointerType(CopyFnType).withConst().withRestrict()),
1702 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1703 std::make_pair(".lb.", KmpUInt64Ty),
1704 std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1705 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataev49f6e782015-12-01 04:18:41 +00001706 std::make_pair(StringRef(), QualType()) // __context with shared vars
1707 };
1708 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1709 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00001710 // Mark this captured region as inlined, because we don't use outlined
1711 // function directly.
1712 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1713 AlwaysInlineAttr::CreateImplicit(
1714 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev49f6e782015-12-01 04:18:41 +00001715 break;
1716 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001717 case OMPD_taskloop_simd: {
1718 Sema::CapturedParamNameType Params[] = {
1719 std::make_pair(StringRef(), QualType()) // __context with shared vars
1720 };
1721 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1722 Params);
1723 break;
1724 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001725 case OMPD_distribute: {
1726 Sema::CapturedParamNameType Params[] = {
1727 std::make_pair(StringRef(), QualType()) // __context with shared vars
1728 };
1729 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1730 Params);
1731 break;
1732 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001733 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001734 case OMPD_taskyield:
1735 case OMPD_barrier:
1736 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001737 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001738 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001739 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001740 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001741 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001742 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001743 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001744 case OMPD_declare_target:
1745 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001746 llvm_unreachable("OpenMP Directive is not allowed");
1747 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001748 llvm_unreachable("Unknown OpenMP directive");
1749 }
1750}
1751
Alexey Bataev3392d762016-02-16 11:18:12 +00001752static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001753 Expr *CaptureExpr, bool WithInit,
1754 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001755 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001756 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001757 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001758 QualType Ty = Init->getType();
1759 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1760 if (S.getLangOpts().CPlusPlus)
1761 Ty = C.getLValueReferenceType(Ty);
1762 else {
1763 Ty = C.getPointerType(Ty);
1764 ExprResult Res =
1765 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1766 if (!Res.isUsable())
1767 return nullptr;
1768 Init = Res.get();
1769 }
Alexey Bataev61205072016-03-02 04:57:40 +00001770 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001771 }
1772 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001773 if (!WithInit)
1774 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001775 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001776 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1777 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001778 return CED;
1779}
1780
Alexey Bataev61205072016-03-02 04:57:40 +00001781static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1782 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001783 OMPCapturedExprDecl *CD;
1784 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1785 CD = cast<OMPCapturedExprDecl>(VD);
1786 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001787 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1788 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001789 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001790 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001791}
1792
Alexey Bataev5a3af132016-03-29 08:58:54 +00001793static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1794 if (!Ref) {
1795 auto *CD =
1796 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1797 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1798 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1799 CaptureExpr->getExprLoc());
1800 }
1801 ExprResult Res = Ref;
1802 if (!S.getLangOpts().CPlusPlus &&
1803 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1804 Ref->getType()->isPointerType())
1805 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1806 if (!Res.isUsable())
1807 return ExprError();
1808 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001809}
1810
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001811StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1812 ArrayRef<OMPClause *> Clauses) {
1813 if (!S.isUsable()) {
1814 ActOnCapturedRegionError();
1815 return StmtError();
1816 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001817
1818 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001819 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001820 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001821 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001822 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001823 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001824 Clause->getClauseKind() == OMPC_copyprivate ||
1825 (getLangOpts().OpenMPUseTLS &&
1826 getASTContext().getTargetInfo().isTLSSupported() &&
1827 Clause->getClauseKind() == OMPC_copyin)) {
1828 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001829 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001830 for (auto *VarRef : Clause->children()) {
1831 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001832 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001833 }
1834 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001835 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001836 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001837 // Mark all variables in private list clauses as used in inner region.
1838 // Required for proper codegen of combined directives.
1839 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001840 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001841 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1842 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001843 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1844 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001845 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001846 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1847 if (auto *E = C->getPostUpdateExpr())
1848 MarkDeclarationsReferencedInExpr(E);
1849 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001850 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001851 if (Clause->getClauseKind() == OMPC_schedule)
1852 SC = cast<OMPScheduleClause>(Clause);
1853 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001854 OC = cast<OMPOrderedClause>(Clause);
1855 else if (Clause->getClauseKind() == OMPC_linear)
1856 LCs.push_back(cast<OMPLinearClause>(Clause));
1857 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001858 bool ErrorFound = false;
1859 // OpenMP, 2.7.1 Loop Construct, Restrictions
1860 // The nonmonotonic modifier cannot be specified if an ordered clause is
1861 // specified.
1862 if (SC &&
1863 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1864 SC->getSecondScheduleModifier() ==
1865 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1866 OC) {
1867 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1868 ? SC->getFirstScheduleModifierLoc()
1869 : SC->getSecondScheduleModifierLoc(),
1870 diag::err_omp_schedule_nonmonotonic_ordered)
1871 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1872 ErrorFound = true;
1873 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001874 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1875 for (auto *C : LCs) {
1876 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1877 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1878 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001879 ErrorFound = true;
1880 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001881 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1882 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1883 OC->getNumForLoops()) {
1884 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1885 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1886 ErrorFound = true;
1887 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001888 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001889 ActOnCapturedRegionError();
1890 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001891 }
1892 return ActOnCapturedRegionEnd(S.get());
1893}
1894
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001895static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1896 OpenMPDirectiveKind CurrentRegion,
1897 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001898 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001899 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001900 // Allowed nesting of constructs
1901 // +------------------+-----------------+------------------------------------+
1902 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1903 // +------------------+-----------------+------------------------------------+
1904 // | parallel | parallel | * |
1905 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001906 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001907 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001908 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001909 // | parallel | simd | * |
1910 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001911 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001912 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001913 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001914 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001915 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001916 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001917 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001918 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001919 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001920 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001921 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001922 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001923 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001924 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001925 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001926 // | parallel | target parallel | * |
1927 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001928 // | parallel | target enter | * |
1929 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001930 // | parallel | target exit | * |
1931 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001932 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001933 // | parallel | cancellation | |
1934 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001935 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001936 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001937 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001938 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001939 // +------------------+-----------------+------------------------------------+
1940 // | for | parallel | * |
1941 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001942 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001943 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001944 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001945 // | for | simd | * |
1946 // | for | sections | + |
1947 // | for | section | + |
1948 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001949 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001950 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001951 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001952 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001953 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001954 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001955 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001956 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001957 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001958 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001959 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001960 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001961 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001962 // | for | target parallel | * |
1963 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001964 // | for | target enter | * |
1965 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001966 // | for | target exit | * |
1967 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001968 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001969 // | for | cancellation | |
1970 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001971 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001972 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001973 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001974 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001975 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001976 // | master | parallel | * |
1977 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001978 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001979 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001981 // | master | simd | * |
1982 // | master | sections | + |
1983 // | master | section | + |
1984 // | master | single | + |
1985 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001986 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001987 // | master |parallel sections| * |
1988 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001989 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001990 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001991 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001992 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001993 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001994 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001995 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001996 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001997 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001998 // | master | target parallel | * |
1999 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002000 // | master | target enter | * |
2001 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002002 // | master | target exit | * |
2003 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002004 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002005 // | master | cancellation | |
2006 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002007 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002008 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002009 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002010 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00002011 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002012 // | critical | parallel | * |
2013 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002014 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002015 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002016 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002017 // | critical | simd | * |
2018 // | critical | sections | + |
2019 // | critical | section | + |
2020 // | critical | single | + |
2021 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002022 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002023 // | critical |parallel sections| * |
2024 // | critical | task | * |
2025 // | critical | taskyield | * |
2026 // | critical | barrier | + |
2027 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002028 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002029 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002030 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002031 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002032 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002033 // | critical | target parallel | * |
2034 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002035 // | critical | target enter | * |
2036 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002037 // | critical | target exit | * |
2038 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002039 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002040 // | critical | cancellation | |
2041 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002042 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002043 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002044 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002045 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002046 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002047 // | simd | parallel | |
2048 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002049 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002050 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002051 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002052 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002053 // | simd | sections | |
2054 // | simd | section | |
2055 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002056 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002059 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002060 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002061 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002062 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002064 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002065 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002066 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002067 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002068 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002069 // | simd | target parallel | |
2070 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002071 // | simd | target enter | |
2072 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002073 // | simd | target exit | |
2074 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002075 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002076 // | simd | cancellation | |
2077 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002078 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002079 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002080 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002081 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002082 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002083 // | for simd | parallel | |
2084 // | for simd | for | |
2085 // | for simd | for simd | |
2086 // | for simd | master | |
2087 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002088 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002089 // | for simd | sections | |
2090 // | for simd | section | |
2091 // | for simd | single | |
2092 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002093 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002094 // | for simd |parallel sections| |
2095 // | for simd | task | |
2096 // | for simd | taskyield | |
2097 // | for simd | barrier | |
2098 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002099 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002100 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002101 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002102 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002103 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002104 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002105 // | for simd | target parallel | |
2106 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002107 // | for simd | target enter | |
2108 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002109 // | for simd | target exit | |
2110 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002111 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002112 // | for simd | cancellation | |
2113 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002114 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002115 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002116 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002117 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002118 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002119 // | parallel for simd| parallel | |
2120 // | parallel for simd| for | |
2121 // | parallel for simd| for simd | |
2122 // | parallel for simd| master | |
2123 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002124 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002125 // | parallel for simd| sections | |
2126 // | parallel for simd| section | |
2127 // | parallel for simd| single | |
2128 // | parallel for simd| parallel for | |
2129 // | parallel for simd|parallel for simd| |
2130 // | parallel for simd|parallel sections| |
2131 // | parallel for simd| task | |
2132 // | parallel for simd| taskyield | |
2133 // | parallel for simd| barrier | |
2134 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002135 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002136 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002137 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002138 // | parallel for simd| atomic | |
2139 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002140 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002141 // | parallel for simd| target parallel | |
2142 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002143 // | parallel for simd| target enter | |
2144 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002145 // | parallel for simd| target exit | |
2146 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002147 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002148 // | parallel for simd| cancellation | |
2149 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002150 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002151 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002152 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002153 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002154 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002155 // | sections | parallel | * |
2156 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002157 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002158 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002159 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002160 // | sections | simd | * |
2161 // | sections | sections | + |
2162 // | sections | section | * |
2163 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002164 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002165 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002166 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002167 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002168 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002169 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002170 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002171 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002172 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002173 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002174 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002175 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002176 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002177 // | sections | target parallel | * |
2178 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002179 // | sections | target enter | * |
2180 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002181 // | sections | target exit | * |
2182 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002183 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002184 // | sections | cancellation | |
2185 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002186 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002187 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002188 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002189 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002190 // +------------------+-----------------+------------------------------------+
2191 // | section | parallel | * |
2192 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002193 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002194 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002195 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002196 // | section | simd | * |
2197 // | section | sections | + |
2198 // | section | section | + |
2199 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002200 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002201 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002202 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002203 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002204 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002205 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002206 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002207 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002208 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002209 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002210 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002211 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002212 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002213 // | section | target parallel | * |
2214 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002215 // | section | target enter | * |
2216 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002217 // | section | target exit | * |
2218 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002219 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002220 // | section | cancellation | |
2221 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002222 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002223 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002224 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002225 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002226 // +------------------+-----------------+------------------------------------+
2227 // | single | parallel | * |
2228 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002229 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002230 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002231 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002232 // | single | simd | * |
2233 // | single | sections | + |
2234 // | single | section | + |
2235 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002236 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002237 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002238 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002239 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002240 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002241 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002242 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002243 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002244 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002245 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002246 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002247 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002248 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002249 // | single | target parallel | * |
2250 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002251 // | single | target enter | * |
2252 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002253 // | single | target exit | * |
2254 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002255 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002256 // | single | cancellation | |
2257 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002258 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002259 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002260 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002261 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002262 // +------------------+-----------------+------------------------------------+
2263 // | parallel for | parallel | * |
2264 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002265 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002266 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002267 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002268 // | parallel for | simd | * |
2269 // | parallel for | sections | + |
2270 // | parallel for | section | + |
2271 // | parallel for | single | + |
2272 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002273 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002274 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002275 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002276 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002277 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002278 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002279 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002280 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002281 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002282 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002283 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002284 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002285 // | parallel for | target parallel | * |
2286 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002287 // | parallel for | target enter | * |
2288 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002289 // | parallel for | target exit | * |
2290 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002291 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002292 // | parallel for | cancellation | |
2293 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002294 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002295 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002296 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002297 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002298 // +------------------+-----------------+------------------------------------+
2299 // | parallel sections| parallel | * |
2300 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002301 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002302 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002303 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002304 // | parallel sections| simd | * |
2305 // | parallel sections| sections | + |
2306 // | parallel sections| section | * |
2307 // | parallel sections| single | + |
2308 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002309 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002310 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002311 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002312 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002313 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002314 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002315 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002316 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002317 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002318 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002319 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002320 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002321 // | parallel sections| target parallel | * |
2322 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002323 // | parallel sections| target enter | * |
2324 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002325 // | parallel sections| target exit | * |
2326 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002327 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002328 // | parallel sections| cancellation | |
2329 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002330 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002331 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002332 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002333 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002334 // +------------------+-----------------+------------------------------------+
2335 // | task | parallel | * |
2336 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002337 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002338 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002339 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002340 // | task | simd | * |
2341 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002342 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002343 // | task | single | + |
2344 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002345 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002346 // | task |parallel sections| * |
2347 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002348 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002349 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002350 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002351 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002352 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002353 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002354 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002355 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002356 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002357 // | task | target parallel | * |
2358 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002359 // | task | target enter | * |
2360 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002361 // | task | target exit | * |
2362 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002363 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002364 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002365 // | | point | ! |
2366 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002367 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002368 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002369 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002370 // +------------------+-----------------+------------------------------------+
2371 // | ordered | parallel | * |
2372 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002373 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002374 // | ordered | master | * |
2375 // | ordered | critical | * |
2376 // | ordered | simd | * |
2377 // | ordered | sections | + |
2378 // | ordered | section | + |
2379 // | ordered | single | + |
2380 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002381 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002382 // | ordered |parallel sections| * |
2383 // | ordered | task | * |
2384 // | ordered | taskyield | * |
2385 // | ordered | barrier | + |
2386 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002387 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002388 // | ordered | flush | * |
2389 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002390 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002391 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002392 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002393 // | ordered | target parallel | * |
2394 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002395 // | ordered | target enter | * |
2396 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002397 // | ordered | target exit | * |
2398 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002399 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002400 // | ordered | cancellation | |
2401 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002402 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002403 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002404 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002405 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002406 // +------------------+-----------------+------------------------------------+
2407 // | atomic | parallel | |
2408 // | atomic | for | |
2409 // | atomic | for simd | |
2410 // | atomic | master | |
2411 // | atomic | critical | |
2412 // | atomic | simd | |
2413 // | atomic | sections | |
2414 // | atomic | section | |
2415 // | atomic | single | |
2416 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002417 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002418 // | atomic |parallel sections| |
2419 // | atomic | task | |
2420 // | atomic | taskyield | |
2421 // | atomic | barrier | |
2422 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002423 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002424 // | atomic | flush | |
2425 // | atomic | ordered | |
2426 // | atomic | atomic | |
2427 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002428 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002429 // | atomic | target parallel | |
2430 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002431 // | atomic | target enter | |
2432 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002433 // | atomic | target exit | |
2434 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002435 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002436 // | atomic | cancellation | |
2437 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002438 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002439 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002440 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002441 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002442 // +------------------+-----------------+------------------------------------+
2443 // | target | parallel | * |
2444 // | target | for | * |
2445 // | target | for simd | * |
2446 // | target | master | * |
2447 // | target | critical | * |
2448 // | target | simd | * |
2449 // | target | sections | * |
2450 // | target | section | * |
2451 // | target | single | * |
2452 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002453 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002454 // | target |parallel sections| * |
2455 // | target | task | * |
2456 // | target | taskyield | * |
2457 // | target | barrier | * |
2458 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002459 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002460 // | target | flush | * |
2461 // | target | ordered | * |
2462 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002463 // | target | target | |
2464 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002465 // | target | target parallel | |
2466 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002467 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002468 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002469 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002470 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002471 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002472 // | target | cancellation | |
2473 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002474 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002475 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002476 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002477 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002478 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002479 // | target parallel | parallel | * |
2480 // | target parallel | for | * |
2481 // | target parallel | for simd | * |
2482 // | target parallel | master | * |
2483 // | target parallel | critical | * |
2484 // | target parallel | simd | * |
2485 // | target parallel | sections | * |
2486 // | target parallel | section | * |
2487 // | target parallel | single | * |
2488 // | target parallel | parallel for | * |
2489 // | target parallel |parallel for simd| * |
2490 // | target parallel |parallel sections| * |
2491 // | target parallel | task | * |
2492 // | target parallel | taskyield | * |
2493 // | target parallel | barrier | * |
2494 // | target parallel | taskwait | * |
2495 // | target parallel | taskgroup | * |
2496 // | target parallel | flush | * |
2497 // | target parallel | ordered | * |
2498 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002499 // | target parallel | target | |
2500 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002501 // | target parallel | target parallel | |
2502 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002503 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002504 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002505 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002506 // | | data | |
2507 // | target parallel | teams | |
2508 // | target parallel | cancellation | |
2509 // | | point | ! |
2510 // | target parallel | cancel | ! |
2511 // | target parallel | taskloop | * |
2512 // | target parallel | taskloop simd | * |
2513 // | target parallel | distribute | |
2514 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002515 // | target parallel | parallel | * |
2516 // | for | | |
2517 // | target parallel | for | * |
2518 // | for | | |
2519 // | target parallel | for simd | * |
2520 // | for | | |
2521 // | target parallel | master | * |
2522 // | for | | |
2523 // | target parallel | critical | * |
2524 // | for | | |
2525 // | target parallel | simd | * |
2526 // | for | | |
2527 // | target parallel | sections | * |
2528 // | for | | |
2529 // | target parallel | section | * |
2530 // | for | | |
2531 // | target parallel | single | * |
2532 // | for | | |
2533 // | target parallel | parallel for | * |
2534 // | for | | |
2535 // | target parallel |parallel for simd| * |
2536 // | for | | |
2537 // | target parallel |parallel sections| * |
2538 // | for | | |
2539 // | target parallel | task | * |
2540 // | for | | |
2541 // | target parallel | taskyield | * |
2542 // | for | | |
2543 // | target parallel | barrier | * |
2544 // | for | | |
2545 // | target parallel | taskwait | * |
2546 // | for | | |
2547 // | target parallel | taskgroup | * |
2548 // | for | | |
2549 // | target parallel | flush | * |
2550 // | for | | |
2551 // | target parallel | ordered | * |
2552 // | for | | |
2553 // | target parallel | atomic | * |
2554 // | for | | |
2555 // | target parallel | target | |
2556 // | for | | |
2557 // | target parallel | target parallel | |
2558 // | for | | |
2559 // | target parallel | target parallel | |
2560 // | for | for | |
2561 // | target parallel | target enter | |
2562 // | for | data | |
2563 // | target parallel | target exit | |
2564 // | for | data | |
2565 // | target parallel | teams | |
2566 // | for | | |
2567 // | target parallel | cancellation | |
2568 // | for | point | ! |
2569 // | target parallel | cancel | ! |
2570 // | for | | |
2571 // | target parallel | taskloop | * |
2572 // | for | | |
2573 // | target parallel | taskloop simd | * |
2574 // | for | | |
2575 // | target parallel | distribute | |
2576 // | for | | |
2577 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002578 // | teams | parallel | * |
2579 // | teams | for | + |
2580 // | teams | for simd | + |
2581 // | teams | master | + |
2582 // | teams | critical | + |
2583 // | teams | simd | + |
2584 // | teams | sections | + |
2585 // | teams | section | + |
2586 // | teams | single | + |
2587 // | teams | parallel for | * |
2588 // | teams |parallel for simd| * |
2589 // | teams |parallel sections| * |
2590 // | teams | task | + |
2591 // | teams | taskyield | + |
2592 // | teams | barrier | + |
2593 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002594 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002595 // | teams | flush | + |
2596 // | teams | ordered | + |
2597 // | teams | atomic | + |
2598 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002599 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002600 // | teams | target parallel | + |
2601 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002602 // | teams | target enter | + |
2603 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002604 // | teams | target exit | + |
2605 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002606 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002607 // | teams | cancellation | |
2608 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002609 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002610 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002611 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002612 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002613 // +------------------+-----------------+------------------------------------+
2614 // | taskloop | parallel | * |
2615 // | taskloop | for | + |
2616 // | taskloop | for simd | + |
2617 // | taskloop | master | + |
2618 // | taskloop | critical | * |
2619 // | taskloop | simd | * |
2620 // | taskloop | sections | + |
2621 // | taskloop | section | + |
2622 // | taskloop | single | + |
2623 // | taskloop | parallel for | * |
2624 // | taskloop |parallel for simd| * |
2625 // | taskloop |parallel sections| * |
2626 // | taskloop | task | * |
2627 // | taskloop | taskyield | * |
2628 // | taskloop | barrier | + |
2629 // | taskloop | taskwait | * |
2630 // | taskloop | taskgroup | * |
2631 // | taskloop | flush | * |
2632 // | taskloop | ordered | + |
2633 // | taskloop | atomic | * |
2634 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002635 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002636 // | taskloop | target parallel | * |
2637 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002638 // | taskloop | target enter | * |
2639 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002640 // | taskloop | target exit | * |
2641 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002642 // | taskloop | teams | + |
2643 // | taskloop | cancellation | |
2644 // | | point | |
2645 // | taskloop | cancel | |
2646 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002647 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002648 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002649 // | taskloop simd | parallel | |
2650 // | taskloop simd | for | |
2651 // | taskloop simd | for simd | |
2652 // | taskloop simd | master | |
2653 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002654 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002655 // | taskloop simd | sections | |
2656 // | taskloop simd | section | |
2657 // | taskloop simd | single | |
2658 // | taskloop simd | parallel for | |
2659 // | taskloop simd |parallel for simd| |
2660 // | taskloop simd |parallel sections| |
2661 // | taskloop simd | task | |
2662 // | taskloop simd | taskyield | |
2663 // | taskloop simd | barrier | |
2664 // | taskloop simd | taskwait | |
2665 // | taskloop simd | taskgroup | |
2666 // | taskloop simd | flush | |
2667 // | taskloop simd | ordered | + (with simd clause) |
2668 // | taskloop simd | atomic | |
2669 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002670 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002671 // | taskloop simd | target parallel | |
2672 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002673 // | taskloop simd | target enter | |
2674 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002675 // | taskloop simd | target exit | |
2676 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002677 // | taskloop simd | teams | |
2678 // | taskloop simd | cancellation | |
2679 // | | point | |
2680 // | taskloop simd | cancel | |
2681 // | taskloop simd | taskloop | |
2682 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002683 // | taskloop simd | distribute | |
2684 // +------------------+-----------------+------------------------------------+
2685 // | distribute | parallel | * |
2686 // | distribute | for | * |
2687 // | distribute | for simd | * |
2688 // | distribute | master | * |
2689 // | distribute | critical | * |
2690 // | distribute | simd | * |
2691 // | distribute | sections | * |
2692 // | distribute | section | * |
2693 // | distribute | single | * |
2694 // | distribute | parallel for | * |
2695 // | distribute |parallel for simd| * |
2696 // | distribute |parallel sections| * |
2697 // | distribute | task | * |
2698 // | distribute | taskyield | * |
2699 // | distribute | barrier | * |
2700 // | distribute | taskwait | * |
2701 // | distribute | taskgroup | * |
2702 // | distribute | flush | * |
2703 // | distribute | ordered | + |
2704 // | distribute | atomic | * |
2705 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002706 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002707 // | distribute | target parallel | |
2708 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002709 // | distribute | target enter | |
2710 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002711 // | distribute | target exit | |
2712 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002713 // | distribute | teams | |
2714 // | distribute | cancellation | + |
2715 // | | point | |
2716 // | distribute | cancel | + |
2717 // | distribute | taskloop | * |
2718 // | distribute | taskloop simd | * |
2719 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002720 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002721 if (Stack->getCurScope()) {
2722 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002723 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002724 bool NestingProhibited = false;
2725 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002726 enum {
2727 NoRecommend,
2728 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002729 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002730 ShouldBeInTargetRegion,
2731 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002732 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002733 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2734 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002735 // OpenMP [2.16, Nesting of Regions]
2736 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002737 // OpenMP [2.8.1,simd Construct, Restrictions]
2738 // An ordered construct with the simd clause is the only OpenMP construct
2739 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002740 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2741 return true;
2742 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002743 if (ParentRegion == OMPD_atomic) {
2744 // OpenMP [2.16, Nesting of Regions]
2745 // OpenMP constructs may not be nested inside an atomic region.
2746 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2747 return true;
2748 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002749 if (CurrentRegion == OMPD_section) {
2750 // OpenMP [2.7.2, sections Construct, Restrictions]
2751 // Orphaned section directives are prohibited. That is, the section
2752 // directives must appear within the sections construct and must not be
2753 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002754 if (ParentRegion != OMPD_sections &&
2755 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002756 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2757 << (ParentRegion != OMPD_unknown)
2758 << getOpenMPDirectiveName(ParentRegion);
2759 return true;
2760 }
2761 return false;
2762 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002763 // Allow some constructs to be orphaned (they could be used in functions,
2764 // called from OpenMP regions with the required preconditions).
2765 if (ParentRegion == OMPD_unknown)
2766 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002767 if (CurrentRegion == OMPD_cancellation_point ||
2768 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002769 // OpenMP [2.16, Nesting of Regions]
2770 // A cancellation point construct for which construct-type-clause is
2771 // taskgroup must be nested inside a task construct. A cancellation
2772 // point construct for which construct-type-clause is not taskgroup must
2773 // be closely nested inside an OpenMP construct that matches the type
2774 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002775 // A cancel construct for which construct-type-clause is taskgroup must be
2776 // nested inside a task construct. A cancel construct for which
2777 // construct-type-clause is not taskgroup must be closely nested inside an
2778 // OpenMP construct that matches the type specified in
2779 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002780 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002781 !((CancelRegion == OMPD_parallel &&
2782 (ParentRegion == OMPD_parallel ||
2783 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002784 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002785 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2786 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002787 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2788 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002789 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2790 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002791 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002792 // OpenMP [2.16, Nesting of Regions]
2793 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002794 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002795 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002796 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002797 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2798 // OpenMP [2.16, Nesting of Regions]
2799 // A critical region may not be nested (closely or otherwise) inside a
2800 // critical region with the same name. Note that this restriction is not
2801 // sufficient to prevent deadlock.
2802 SourceLocation PreviousCriticalLoc;
2803 bool DeadLock =
2804 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2805 OpenMPDirectiveKind K,
2806 const DeclarationNameInfo &DNI,
2807 SourceLocation Loc)
2808 ->bool {
2809 if (K == OMPD_critical &&
2810 DNI.getName() == CurrentName.getName()) {
2811 PreviousCriticalLoc = Loc;
2812 return true;
2813 } else
2814 return false;
2815 },
2816 false /* skip top directive */);
2817 if (DeadLock) {
2818 SemaRef.Diag(StartLoc,
2819 diag::err_omp_prohibited_region_critical_same_name)
2820 << CurrentName.getName();
2821 if (PreviousCriticalLoc.isValid())
2822 SemaRef.Diag(PreviousCriticalLoc,
2823 diag::note_omp_previous_critical_region);
2824 return true;
2825 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002826 } else if (CurrentRegion == OMPD_barrier) {
2827 // OpenMP [2.16, Nesting of Regions]
2828 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002829 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002830 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2831 isOpenMPTaskingDirective(ParentRegion) ||
2832 ParentRegion == OMPD_master ||
2833 ParentRegion == OMPD_critical ||
2834 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002835 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002836 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002837 // OpenMP [2.16, Nesting of Regions]
2838 // A worksharing region may not be closely nested inside a worksharing,
2839 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002840 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2841 isOpenMPTaskingDirective(ParentRegion) ||
2842 ParentRegion == OMPD_master ||
2843 ParentRegion == OMPD_critical ||
2844 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002845 Recommend = ShouldBeInParallelRegion;
2846 } else if (CurrentRegion == OMPD_ordered) {
2847 // OpenMP [2.16, Nesting of Regions]
2848 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002849 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002850 // An ordered region must be closely nested inside a loop region (or
2851 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002852 // OpenMP [2.8.1,simd Construct, Restrictions]
2853 // An ordered construct with the simd clause is the only OpenMP construct
2854 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002855 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002856 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002857 !(isOpenMPSimdDirective(ParentRegion) ||
2858 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002859 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002860 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2861 // OpenMP [2.16, Nesting of Regions]
2862 // If specified, a teams construct must be contained within a target
2863 // construct.
2864 NestingProhibited = ParentRegion != OMPD_target;
2865 Recommend = ShouldBeInTargetRegion;
2866 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2867 }
2868 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2869 // OpenMP [2.16, Nesting of Regions]
2870 // distribute, parallel, parallel sections, parallel workshare, and the
2871 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2872 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002873 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2874 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002875 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002876 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002877 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2878 // OpenMP 4.5 [2.17 Nesting of Regions]
2879 // The region associated with the distribute construct must be strictly
2880 // nested inside a teams region
2881 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2882 Recommend = ShouldBeInTeamsRegion;
2883 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002884 if (!NestingProhibited &&
2885 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2886 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2887 // OpenMP 4.5 [2.17 Nesting of Regions]
2888 // If a target, target update, target data, target enter data, or
2889 // target exit data construct is encountered during execution of a
2890 // target region, the behavior is unspecified.
2891 NestingProhibited = Stack->hasDirective(
2892 [&OffendingRegion](OpenMPDirectiveKind K,
2893 const DeclarationNameInfo &DNI,
2894 SourceLocation Loc) -> bool {
2895 if (isOpenMPTargetExecutionDirective(K)) {
2896 OffendingRegion = K;
2897 return true;
2898 } else
2899 return false;
2900 },
2901 false /* don't skip top directive */);
2902 CloseNesting = false;
2903 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002904 if (NestingProhibited) {
2905 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002906 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2907 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002908 return true;
2909 }
2910 }
2911 return false;
2912}
2913
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002914static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2915 ArrayRef<OMPClause *> Clauses,
2916 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2917 bool ErrorFound = false;
2918 unsigned NamedModifiersNumber = 0;
2919 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2920 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002921 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002922 for (const auto *C : Clauses) {
2923 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2924 // At most one if clause without a directive-name-modifier can appear on
2925 // the directive.
2926 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2927 if (FoundNameModifiers[CurNM]) {
2928 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2929 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2930 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2931 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002932 } else if (CurNM != OMPD_unknown) {
2933 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002934 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002935 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002936 FoundNameModifiers[CurNM] = IC;
2937 if (CurNM == OMPD_unknown)
2938 continue;
2939 // Check if the specified name modifier is allowed for the current
2940 // directive.
2941 // At most one if clause with the particular directive-name-modifier can
2942 // appear on the directive.
2943 bool MatchFound = false;
2944 for (auto NM : AllowedNameModifiers) {
2945 if (CurNM == NM) {
2946 MatchFound = true;
2947 break;
2948 }
2949 }
2950 if (!MatchFound) {
2951 S.Diag(IC->getNameModifierLoc(),
2952 diag::err_omp_wrong_if_directive_name_modifier)
2953 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2954 ErrorFound = true;
2955 }
2956 }
2957 }
2958 // If any if clause on the directive includes a directive-name-modifier then
2959 // all if clauses on the directive must include a directive-name-modifier.
2960 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2961 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2962 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2963 diag::err_omp_no_more_if_clause);
2964 } else {
2965 std::string Values;
2966 std::string Sep(", ");
2967 unsigned AllowedCnt = 0;
2968 unsigned TotalAllowedNum =
2969 AllowedNameModifiers.size() - NamedModifiersNumber;
2970 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2971 ++Cnt) {
2972 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2973 if (!FoundNameModifiers[NM]) {
2974 Values += "'";
2975 Values += getOpenMPDirectiveName(NM);
2976 Values += "'";
2977 if (AllowedCnt + 2 == TotalAllowedNum)
2978 Values += " or ";
2979 else if (AllowedCnt + 1 != TotalAllowedNum)
2980 Values += Sep;
2981 ++AllowedCnt;
2982 }
2983 }
2984 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2985 diag::err_omp_unnamed_if_clause)
2986 << (TotalAllowedNum > 1) << Values;
2987 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002988 for (auto Loc : NameModifierLoc) {
2989 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2990 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002991 ErrorFound = true;
2992 }
2993 return ErrorFound;
2994}
2995
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002996StmtResult Sema::ActOnOpenMPExecutableDirective(
2997 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2998 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2999 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003000 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003001 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3002 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003003 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003004
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003005 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00003006 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003007 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003008 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00003009 if (AStmt) {
3010 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3011
3012 // Check default data sharing attributes for referenced variables.
3013 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3014 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3015 if (DSAChecker.isErrorFound())
3016 return StmtError();
3017 // Generate list of implicitly defined firstprivate variables.
3018 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003019
3020 if (!DSAChecker.getImplicitFirstprivate().empty()) {
3021 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3022 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3023 SourceLocation(), SourceLocation())) {
3024 ClausesWithImplicit.push_back(Implicit);
3025 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3026 DSAChecker.getImplicitFirstprivate().size();
3027 } else
3028 ErrorFound = true;
3029 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003030 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003031
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003032 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003033 switch (Kind) {
3034 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003035 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3036 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003037 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003038 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003039 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003040 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3041 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003042 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003043 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003044 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3045 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003046 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003047 case OMPD_for_simd:
3048 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3049 EndLoc, VarsWithInheritedDSA);
3050 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003051 case OMPD_sections:
3052 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3053 EndLoc);
3054 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003055 case OMPD_section:
3056 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003057 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003058 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3059 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003060 case OMPD_single:
3061 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3062 EndLoc);
3063 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003064 case OMPD_master:
3065 assert(ClausesWithImplicit.empty() &&
3066 "No clauses are allowed for 'omp master' directive");
3067 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3068 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003069 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003070 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3071 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003072 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003073 case OMPD_parallel_for:
3074 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3075 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003076 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003077 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003078 case OMPD_parallel_for_simd:
3079 Res = ActOnOpenMPParallelForSimdDirective(
3080 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003081 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003082 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003083 case OMPD_parallel_sections:
3084 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3085 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003086 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003087 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003088 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003089 Res =
3090 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003091 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003092 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003093 case OMPD_taskyield:
3094 assert(ClausesWithImplicit.empty() &&
3095 "No clauses are allowed for 'omp taskyield' directive");
3096 assert(AStmt == nullptr &&
3097 "No associated statement allowed for 'omp taskyield' directive");
3098 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3099 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003100 case OMPD_barrier:
3101 assert(ClausesWithImplicit.empty() &&
3102 "No clauses are allowed for 'omp barrier' directive");
3103 assert(AStmt == nullptr &&
3104 "No associated statement allowed for 'omp barrier' directive");
3105 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3106 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003107 case OMPD_taskwait:
3108 assert(ClausesWithImplicit.empty() &&
3109 "No clauses are allowed for 'omp taskwait' directive");
3110 assert(AStmt == nullptr &&
3111 "No associated statement allowed for 'omp taskwait' directive");
3112 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3113 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003114 case OMPD_taskgroup:
3115 assert(ClausesWithImplicit.empty() &&
3116 "No clauses are allowed for 'omp taskgroup' directive");
3117 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3118 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003119 case OMPD_flush:
3120 assert(AStmt == nullptr &&
3121 "No associated statement allowed for 'omp flush' directive");
3122 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3123 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003124 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003125 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3126 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003127 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003128 case OMPD_atomic:
3129 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3130 EndLoc);
3131 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003132 case OMPD_teams:
3133 Res =
3134 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3135 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003136 case OMPD_target:
3137 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3138 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003139 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003140 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003141 case OMPD_target_parallel:
3142 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3143 StartLoc, EndLoc);
3144 AllowedNameModifiers.push_back(OMPD_target);
3145 AllowedNameModifiers.push_back(OMPD_parallel);
3146 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003147 case OMPD_target_parallel_for:
3148 Res = ActOnOpenMPTargetParallelForDirective(
3149 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3150 AllowedNameModifiers.push_back(OMPD_target);
3151 AllowedNameModifiers.push_back(OMPD_parallel);
3152 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003153 case OMPD_cancellation_point:
3154 assert(ClausesWithImplicit.empty() &&
3155 "No clauses are allowed for 'omp cancellation point' directive");
3156 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3157 "cancellation point' directive");
3158 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3159 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003160 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003161 assert(AStmt == nullptr &&
3162 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003163 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3164 CancelRegion);
3165 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003166 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003167 case OMPD_target_data:
3168 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3169 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003170 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003171 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003172 case OMPD_target_enter_data:
3173 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3174 EndLoc);
3175 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3176 break;
Samuel Antao72590762016-01-19 20:04:50 +00003177 case OMPD_target_exit_data:
3178 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3179 EndLoc);
3180 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3181 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003182 case OMPD_taskloop:
3183 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3184 EndLoc, VarsWithInheritedDSA);
3185 AllowedNameModifiers.push_back(OMPD_taskloop);
3186 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003187 case OMPD_taskloop_simd:
3188 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3189 EndLoc, VarsWithInheritedDSA);
3190 AllowedNameModifiers.push_back(OMPD_taskloop);
3191 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003192 case OMPD_distribute:
3193 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3194 EndLoc, VarsWithInheritedDSA);
3195 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003196 case OMPD_declare_target:
3197 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003198 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003199 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003200 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003201 llvm_unreachable("OpenMP Directive is not allowed");
3202 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003203 llvm_unreachable("Unknown OpenMP directive");
3204 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003205
Alexey Bataev4acb8592014-07-07 13:01:15 +00003206 for (auto P : VarsWithInheritedDSA) {
3207 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3208 << P.first << P.second->getSourceRange();
3209 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003210 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3211
3212 if (!AllowedNameModifiers.empty())
3213 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3214 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003215
Alexey Bataeved09d242014-05-28 05:53:51 +00003216 if (ErrorFound)
3217 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003218 return Res;
3219}
3220
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003221Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3222 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003223 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003224 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3225 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003226 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003227 assert(Linears.size() == LinModifiers.size());
3228 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003229 if (!DG || DG.get().isNull())
3230 return DeclGroupPtrTy();
3231
3232 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003233 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003234 return DG;
3235 }
3236 auto *ADecl = DG.get().getSingleDecl();
3237 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3238 ADecl = FTD->getTemplatedDecl();
3239
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003240 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3241 if (!FD) {
3242 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003243 return DeclGroupPtrTy();
3244 }
3245
Alexey Bataev2af33e32016-04-07 12:45:37 +00003246 // OpenMP [2.8.2, declare simd construct, Description]
3247 // The parameter of the simdlen clause must be a constant positive integer
3248 // expression.
3249 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003250 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003251 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003252 // OpenMP [2.8.2, declare simd construct, Description]
3253 // The special this pointer can be used as if was one of the arguments to the
3254 // function in any of the linear, aligned, or uniform clauses.
3255 // The uniform clause declares one or more arguments to have an invariant
3256 // value for all concurrent invocations of the function in the execution of a
3257 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003258 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3259 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003260 for (auto *E : Uniforms) {
3261 E = E->IgnoreParenImpCasts();
3262 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3263 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3264 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3265 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003266 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3267 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003268 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003269 }
3270 if (isa<CXXThisExpr>(E)) {
3271 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003272 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003273 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003274 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3275 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003276 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003277 // OpenMP [2.8.2, declare simd construct, Description]
3278 // The aligned clause declares that the object to which each list item points
3279 // is aligned to the number of bytes expressed in the optional parameter of
3280 // the aligned clause.
3281 // The special this pointer can be used as if was one of the arguments to the
3282 // function in any of the linear, aligned, or uniform clauses.
3283 // The type of list items appearing in the aligned clause must be array,
3284 // pointer, reference to array, or reference to pointer.
3285 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3286 Expr *AlignedThis = nullptr;
3287 for (auto *E : Aligneds) {
3288 E = E->IgnoreParenImpCasts();
3289 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3290 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3291 auto *CanonPVD = PVD->getCanonicalDecl();
3292 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3293 FD->getParamDecl(PVD->getFunctionScopeIndex())
3294 ->getCanonicalDecl() == CanonPVD) {
3295 // OpenMP [2.8.1, simd construct, Restrictions]
3296 // A list-item cannot appear in more than one aligned clause.
3297 if (AlignedArgs.count(CanonPVD) > 0) {
3298 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3299 << 1 << E->getSourceRange();
3300 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3301 diag::note_omp_explicit_dsa)
3302 << getOpenMPClauseName(OMPC_aligned);
3303 continue;
3304 }
3305 AlignedArgs[CanonPVD] = E;
3306 QualType QTy = PVD->getType()
3307 .getNonReferenceType()
3308 .getUnqualifiedType()
3309 .getCanonicalType();
3310 const Type *Ty = QTy.getTypePtrOrNull();
3311 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3312 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3313 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3314 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3315 }
3316 continue;
3317 }
3318 }
3319 if (isa<CXXThisExpr>(E)) {
3320 if (AlignedThis) {
3321 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3322 << 2 << E->getSourceRange();
3323 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3324 << getOpenMPClauseName(OMPC_aligned);
3325 }
3326 AlignedThis = E;
3327 continue;
3328 }
3329 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3330 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3331 }
3332 // The optional parameter of the aligned clause, alignment, must be a constant
3333 // positive integer expression. If no optional parameter is specified,
3334 // implementation-defined default alignments for SIMD instructions on the
3335 // target platforms are assumed.
3336 SmallVector<Expr *, 4> NewAligns;
3337 for (auto *E : Alignments) {
3338 ExprResult Align;
3339 if (E)
3340 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3341 NewAligns.push_back(Align.get());
3342 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003343 // OpenMP [2.8.2, declare simd construct, Description]
3344 // The linear clause declares one or more list items to be private to a SIMD
3345 // lane and to have a linear relationship with respect to the iteration space
3346 // of a loop.
3347 // The special this pointer can be used as if was one of the arguments to the
3348 // function in any of the linear, aligned, or uniform clauses.
3349 // When a linear-step expression is specified in a linear clause it must be
3350 // either a constant integer expression or an integer-typed parameter that is
3351 // specified in a uniform clause on the directive.
3352 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3353 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3354 auto MI = LinModifiers.begin();
3355 for (auto *E : Linears) {
3356 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3357 ++MI;
3358 E = E->IgnoreParenImpCasts();
3359 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3360 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3361 auto *CanonPVD = PVD->getCanonicalDecl();
3362 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3363 FD->getParamDecl(PVD->getFunctionScopeIndex())
3364 ->getCanonicalDecl() == CanonPVD) {
3365 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3366 // A list-item cannot appear in more than one linear clause.
3367 if (LinearArgs.count(CanonPVD) > 0) {
3368 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3369 << getOpenMPClauseName(OMPC_linear)
3370 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3371 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3372 diag::note_omp_explicit_dsa)
3373 << getOpenMPClauseName(OMPC_linear);
3374 continue;
3375 }
3376 // Each argument can appear in at most one uniform or linear clause.
3377 if (UniformedArgs.count(CanonPVD) > 0) {
3378 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3379 << getOpenMPClauseName(OMPC_linear)
3380 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3381 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3382 diag::note_omp_explicit_dsa)
3383 << getOpenMPClauseName(OMPC_uniform);
3384 continue;
3385 }
3386 LinearArgs[CanonPVD] = E;
3387 if (E->isValueDependent() || E->isTypeDependent() ||
3388 E->isInstantiationDependent() ||
3389 E->containsUnexpandedParameterPack())
3390 continue;
3391 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3392 PVD->getOriginalType());
3393 continue;
3394 }
3395 }
3396 if (isa<CXXThisExpr>(E)) {
3397 if (UniformedLinearThis) {
3398 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3399 << getOpenMPClauseName(OMPC_linear)
3400 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3401 << E->getSourceRange();
3402 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3403 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3404 : OMPC_linear);
3405 continue;
3406 }
3407 UniformedLinearThis = E;
3408 if (E->isValueDependent() || E->isTypeDependent() ||
3409 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3410 continue;
3411 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3412 E->getType());
3413 continue;
3414 }
3415 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3416 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3417 }
3418 Expr *Step = nullptr;
3419 Expr *NewStep = nullptr;
3420 SmallVector<Expr *, 4> NewSteps;
3421 for (auto *E : Steps) {
3422 // Skip the same step expression, it was checked already.
3423 if (Step == E || !E) {
3424 NewSteps.push_back(E ? NewStep : nullptr);
3425 continue;
3426 }
3427 Step = E;
3428 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3429 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3430 auto *CanonPVD = PVD->getCanonicalDecl();
3431 if (UniformedArgs.count(CanonPVD) == 0) {
3432 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3433 << Step->getSourceRange();
3434 } else if (E->isValueDependent() || E->isTypeDependent() ||
3435 E->isInstantiationDependent() ||
3436 E->containsUnexpandedParameterPack() ||
3437 CanonPVD->getType()->hasIntegerRepresentation())
3438 NewSteps.push_back(Step);
3439 else {
3440 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3441 << Step->getSourceRange();
3442 }
3443 continue;
3444 }
3445 NewStep = Step;
3446 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3447 !Step->isInstantiationDependent() &&
3448 !Step->containsUnexpandedParameterPack()) {
3449 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3450 .get();
3451 if (NewStep)
3452 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3453 }
3454 NewSteps.push_back(NewStep);
3455 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003456 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3457 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003458 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003459 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3460 const_cast<Expr **>(Linears.data()), Linears.size(),
3461 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3462 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003463 ADecl->addAttr(NewAttr);
3464 return ConvertDeclToDeclGroup(ADecl);
3465}
3466
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003467StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3468 Stmt *AStmt,
3469 SourceLocation StartLoc,
3470 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003471 if (!AStmt)
3472 return StmtError();
3473
Alexey Bataev9959db52014-05-06 10:08:46 +00003474 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3475 // 1.2.2 OpenMP Language Terminology
3476 // Structured block - An executable statement with a single entry at the
3477 // top and a single exit at the bottom.
3478 // The point of exit cannot be a branch out of the structured block.
3479 // longjmp() and throw() must not violate the entry/exit criteria.
3480 CS->getCapturedDecl()->setNothrow();
3481
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003482 getCurFunction()->setHasBranchProtectedScope();
3483
Alexey Bataev25e5b442015-09-15 12:52:43 +00003484 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3485 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003486}
3487
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003488namespace {
3489/// \brief Helper class for checking canonical form of the OpenMP loops and
3490/// extracting iteration space of each loop in the loop nest, that will be used
3491/// for IR generation.
3492class OpenMPIterationSpaceChecker {
3493 /// \brief Reference to Sema.
3494 Sema &SemaRef;
3495 /// \brief A location for diagnostics (when there is no some better location).
3496 SourceLocation DefaultLoc;
3497 /// \brief A location for diagnostics (when increment is not compatible).
3498 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003499 /// \brief A source location for referring to loop init later.
3500 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003501 /// \brief A source location for referring to condition later.
3502 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003503 /// \brief A source location for referring to increment later.
3504 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003505 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003506 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003507 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003508 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003509 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003510 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003511 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003512 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003513 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003514 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003515 /// \brief This flag is true when condition is one of:
3516 /// Var < UB
3517 /// Var <= UB
3518 /// UB > Var
3519 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003520 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003521 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003522 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003523 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003524 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003525
3526public:
3527 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003528 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003529 /// \brief Check init-expr for canonical loop form and save loop counter
3530 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003531 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003532 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3533 /// for less/greater and for strict/non-strict comparison.
3534 bool CheckCond(Expr *S);
3535 /// \brief Check incr-expr for canonical loop form and return true if it
3536 /// does not conform, otherwise save loop step (#Step).
3537 bool CheckInc(Expr *S);
3538 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003539 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003540 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003541 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003542 /// \brief Source range of the loop init.
3543 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3544 /// \brief Source range of the loop condition.
3545 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3546 /// \brief Source range of the loop increment.
3547 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3548 /// \brief True if the step should be subtracted.
3549 bool ShouldSubtractStep() const { return SubtractStep; }
3550 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003551 Expr *
3552 BuildNumIterations(Scope *S, const bool LimitedType,
3553 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003554 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003555 Expr *BuildPreCond(Scope *S, Expr *Cond,
3556 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003557 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003558 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3559 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003560 /// \brief Build reference expression to the private counter be used for
3561 /// codegen.
3562 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003563 /// \brief Build initization of the counter be used for codegen.
3564 Expr *BuildCounterInit() const;
3565 /// \brief Build step of the counter be used for codegen.
3566 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003567 /// \brief Return true if any expression is dependent.
3568 bool Dependent() const;
3569
3570private:
3571 /// \brief Check the right-hand side of an assignment in the increment
3572 /// expression.
3573 bool CheckIncRHS(Expr *RHS);
3574 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003575 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003576 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003577 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003578 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 /// \brief Helper to set loop increment.
3580 bool SetStep(Expr *NewStep, bool Subtract);
3581};
3582
3583bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003584 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003585 assert(!LB && !UB && !Step);
3586 return false;
3587 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003588 return LCDecl->getType()->isDependentType() ||
3589 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3590 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003591}
3592
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003593static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003594 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3595 E = ExprTemp->getSubExpr();
3596
3597 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3598 E = MTE->GetTemporaryExpr();
3599
3600 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3601 E = Binder->getSubExpr();
3602
3603 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3604 E = ICE->getSubExprAsWritten();
3605 return E->IgnoreParens();
3606}
3607
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003608bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3609 Expr *NewLCRefExpr,
3610 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003611 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003612 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003613 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003614 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003616 LCDecl = getCanonicalDecl(NewLCDecl);
3617 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003618 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3619 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003620 if ((Ctor->isCopyOrMoveConstructor() ||
3621 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3622 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003623 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003624 LB = NewLB;
3625 return false;
3626}
3627
3628bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003629 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003630 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003631 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3632 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003633 if (!NewUB)
3634 return true;
3635 UB = NewUB;
3636 TestIsLessOp = LessOp;
3637 TestIsStrictOp = StrictOp;
3638 ConditionSrcRange = SR;
3639 ConditionLoc = SL;
3640 return false;
3641}
3642
3643bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3644 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003645 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003646 if (!NewStep)
3647 return true;
3648 if (!NewStep->isValueDependent()) {
3649 // Check that the step is integer expression.
3650 SourceLocation StepLoc = NewStep->getLocStart();
3651 ExprResult Val =
3652 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3653 if (Val.isInvalid())
3654 return true;
3655 NewStep = Val.get();
3656
3657 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3658 // If test-expr is of form var relational-op b and relational-op is < or
3659 // <= then incr-expr must cause var to increase on each iteration of the
3660 // loop. If test-expr is of form var relational-op b and relational-op is
3661 // > or >= then incr-expr must cause var to decrease on each iteration of
3662 // the loop.
3663 // If test-expr is of form b relational-op var and relational-op is < or
3664 // <= then incr-expr must cause var to decrease on each iteration of the
3665 // loop. If test-expr is of form b relational-op var and relational-op is
3666 // > or >= then incr-expr must cause var to increase on each iteration of
3667 // the loop.
3668 llvm::APSInt Result;
3669 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3670 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3671 bool IsConstNeg =
3672 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003673 bool IsConstPos =
3674 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003675 bool IsConstZero = IsConstant && !Result.getBoolValue();
3676 if (UB && (IsConstZero ||
3677 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003678 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003679 SemaRef.Diag(NewStep->getExprLoc(),
3680 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003681 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003682 SemaRef.Diag(ConditionLoc,
3683 diag::note_omp_loop_cond_requres_compatible_incr)
3684 << TestIsLessOp << ConditionSrcRange;
3685 return true;
3686 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003687 if (TestIsLessOp == Subtract) {
3688 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3689 NewStep).get();
3690 Subtract = !Subtract;
3691 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003692 }
3693
3694 Step = NewStep;
3695 SubtractStep = Subtract;
3696 return false;
3697}
3698
Alexey Bataev9c821032015-04-30 04:23:23 +00003699bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003700 // Check init-expr for canonical loop form and save loop counter
3701 // variable - #Var and its initialization value - #LB.
3702 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3703 // var = lb
3704 // integer-type var = lb
3705 // random-access-iterator-type var = lb
3706 // pointer-type var = lb
3707 //
3708 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003709 if (EmitDiags) {
3710 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3711 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003712 return true;
3713 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003714 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003715 if (Expr *E = dyn_cast<Expr>(S))
3716 S = E->IgnoreParens();
3717 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003718 if (BO->getOpcode() == BO_Assign) {
3719 auto *LHS = BO->getLHS()->IgnoreParens();
3720 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3721 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3722 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3723 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3724 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3725 }
3726 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3727 if (ME->isArrow() &&
3728 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3729 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3730 }
3731 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003732 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3733 if (DS->isSingleDecl()) {
3734 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003735 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003736 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003737 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003738 SemaRef.Diag(S->getLocStart(),
3739 diag::ext_omp_loop_not_canonical_init)
3740 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003741 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003742 }
3743 }
3744 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003745 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3746 if (CE->getOperator() == OO_Equal) {
3747 auto *LHS = CE->getArg(0);
3748 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3749 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3750 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3751 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3752 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3753 }
3754 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3755 if (ME->isArrow() &&
3756 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3757 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3758 }
3759 }
3760 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003761
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003762 if (Dependent() || SemaRef.CurContext->isDependentContext())
3763 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003764 if (EmitDiags) {
3765 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3766 << S->getSourceRange();
3767 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768 return true;
3769}
3770
Alexey Bataev23b69422014-06-18 07:08:49 +00003771/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003772/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003773static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003774 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003775 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003776 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003777 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3778 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003779 if ((Ctor->isCopyOrMoveConstructor() ||
3780 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3781 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003782 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003783 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3784 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3785 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3786 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3787 return getCanonicalDecl(ME->getMemberDecl());
3788 return getCanonicalDecl(VD);
3789 }
3790 }
3791 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3792 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3793 return getCanonicalDecl(ME->getMemberDecl());
3794 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003795}
3796
3797bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3798 // Check test-expr for canonical form, save upper-bound UB, flags for
3799 // less/greater and for strict/non-strict comparison.
3800 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3801 // var relational-op b
3802 // b relational-op var
3803 //
3804 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003805 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003806 return true;
3807 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003808 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003809 SourceLocation CondLoc = S->getLocStart();
3810 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3811 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003812 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003813 return SetUB(BO->getRHS(),
3814 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3815 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3816 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003817 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003818 return SetUB(BO->getLHS(),
3819 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3820 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3821 BO->getSourceRange(), BO->getOperatorLoc());
3822 }
3823 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3824 if (CE->getNumArgs() == 2) {
3825 auto Op = CE->getOperator();
3826 switch (Op) {
3827 case OO_Greater:
3828 case OO_GreaterEqual:
3829 case OO_Less:
3830 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003831 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003832 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3833 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3834 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003835 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003836 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3837 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3838 CE->getOperatorLoc());
3839 break;
3840 default:
3841 break;
3842 }
3843 }
3844 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003845 if (Dependent() || SemaRef.CurContext->isDependentContext())
3846 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003847 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003848 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003849 return true;
3850}
3851
3852bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3853 // RHS of canonical loop form increment can be:
3854 // var + incr
3855 // incr + var
3856 // var - incr
3857 //
3858 RHS = RHS->IgnoreParenImpCasts();
3859 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3860 if (BO->isAdditiveOp()) {
3861 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003862 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003863 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003864 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003865 return SetStep(BO->getLHS(), false);
3866 }
3867 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3868 bool IsAdd = CE->getOperator() == OO_Plus;
3869 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003870 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003872 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003873 return SetStep(CE->getArg(0), false);
3874 }
3875 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003876 if (Dependent() || SemaRef.CurContext->isDependentContext())
3877 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003878 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003879 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003880 return true;
3881}
3882
3883bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3884 // Check incr-expr for canonical loop form and return true if it
3885 // does not conform.
3886 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3887 // ++var
3888 // var++
3889 // --var
3890 // var--
3891 // var += incr
3892 // var -= incr
3893 // var = var + incr
3894 // var = incr + var
3895 // var = var - incr
3896 //
3897 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003898 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003899 return true;
3900 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003901 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003902 S = S->IgnoreParens();
3903 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003904 if (UO->isIncrementDecrementOp() &&
3905 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906 return SetStep(
3907 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3908 (UO->isDecrementOp() ? -1 : 1)).get(),
3909 false);
3910 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3911 switch (BO->getOpcode()) {
3912 case BO_AddAssign:
3913 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003914 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003915 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3916 break;
3917 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003918 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919 return CheckIncRHS(BO->getRHS());
3920 break;
3921 default:
3922 break;
3923 }
3924 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3925 switch (CE->getOperator()) {
3926 case OO_PlusPlus:
3927 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003928 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003929 return SetStep(
3930 SemaRef.ActOnIntegerConstant(
3931 CE->getLocStart(),
3932 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3933 false);
3934 break;
3935 case OO_PlusEqual:
3936 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3939 break;
3940 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003941 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942 return CheckIncRHS(CE->getArg(1));
3943 break;
3944 default:
3945 break;
3946 }
3947 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003948 if (Dependent() || SemaRef.CurContext->isDependentContext())
3949 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003950 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003951 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003952 return true;
3953}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954
Alexey Bataev5a3af132016-03-29 08:58:54 +00003955static ExprResult
3956tryBuildCapture(Sema &SemaRef, Expr *Capture,
3957 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3958 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3959 return SemaRef.PerformImplicitConversion(
3960 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3961 /*AllowExplicit=*/true);
3962 auto I = Captures.find(Capture);
3963 if (I != Captures.end())
3964 return buildCapture(SemaRef, Capture, I->second);
3965 DeclRefExpr *Ref = nullptr;
3966 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3967 Captures[Capture] = Ref;
3968 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003969}
3970
Alexander Musmana5f070a2014-10-01 06:03:56 +00003971/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003972Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3973 Scope *S, const bool LimitedType,
3974 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003975 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003976 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003977 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003978 SemaRef.getLangOpts().CPlusPlus) {
3979 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003980 auto *UBExpr = TestIsLessOp ? UB : LB;
3981 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003982 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3983 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003984 if (!Upper || !Lower)
3985 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003986
3987 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3988
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003989 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003990 // BuildBinOp already emitted error, this one is to point user to upper
3991 // and lower bound, and to tell what is passed to 'operator-'.
3992 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3993 << Upper->getSourceRange() << Lower->getSourceRange();
3994 return nullptr;
3995 }
3996 }
3997
3998 if (!Diff.isUsable())
3999 return nullptr;
4000
4001 // Upper - Lower [- 1]
4002 if (TestIsStrictOp)
4003 Diff = SemaRef.BuildBinOp(
4004 S, DefaultLoc, BO_Sub, Diff.get(),
4005 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4006 if (!Diff.isUsable())
4007 return nullptr;
4008
4009 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00004010 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4011 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004012 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004013 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004014 if (!Diff.isUsable())
4015 return nullptr;
4016
4017 // Parentheses (for dumping/debugging purposes only).
4018 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4019 if (!Diff.isUsable())
4020 return nullptr;
4021
4022 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004023 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004024 if (!Diff.isUsable())
4025 return nullptr;
4026
Alexander Musman174b3ca2014-10-06 11:16:29 +00004027 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004028 QualType Type = Diff.get()->getType();
4029 auto &C = SemaRef.Context;
4030 bool UseVarType = VarType->hasIntegerRepresentation() &&
4031 C.getTypeSize(Type) > C.getTypeSize(VarType);
4032 if (!Type->isIntegerType() || UseVarType) {
4033 unsigned NewSize =
4034 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4035 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4036 : Type->hasSignedIntegerRepresentation();
4037 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004038 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4039 Diff = SemaRef.PerformImplicitConversion(
4040 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4041 if (!Diff.isUsable())
4042 return nullptr;
4043 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004044 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004045 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004046 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4047 if (NewSize != C.getTypeSize(Type)) {
4048 if (NewSize < C.getTypeSize(Type)) {
4049 assert(NewSize == 64 && "incorrect loop var size");
4050 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4051 << InitSrcRange << ConditionSrcRange;
4052 }
4053 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004054 NewSize, Type->hasSignedIntegerRepresentation() ||
4055 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004056 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4057 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4058 Sema::AA_Converting, true);
4059 if (!Diff.isUsable())
4060 return nullptr;
4061 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004062 }
4063 }
4064
Alexander Musmana5f070a2014-10-01 06:03:56 +00004065 return Diff.get();
4066}
4067
Alexey Bataev5a3af132016-03-29 08:58:54 +00004068Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4069 Scope *S, Expr *Cond,
4070 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004071 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4072 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4073 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004074
Alexey Bataev5a3af132016-03-29 08:58:54 +00004075 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4076 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4077 if (!NewLB.isUsable() || !NewUB.isUsable())
4078 return nullptr;
4079
Alexey Bataev62dbb972015-04-22 11:59:37 +00004080 auto CondExpr = SemaRef.BuildBinOp(
4081 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4082 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004083 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004084 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004085 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4086 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004087 CondExpr = SemaRef.PerformImplicitConversion(
4088 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4089 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004090 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004091 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4092 // Otherwise use original loop conditon and evaluate it in runtime.
4093 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4094}
4095
Alexander Musmana5f070a2014-10-01 06:03:56 +00004096/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004097DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004098 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004099 auto *VD = dyn_cast<VarDecl>(LCDecl);
4100 if (!VD) {
4101 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4102 auto *Ref = buildDeclRefExpr(
4103 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004104 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4105 // If the loop control decl is explicitly marked as private, do not mark it
4106 // as captured again.
4107 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4108 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004109 return Ref;
4110 }
4111 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004112 DefaultLoc);
4113}
4114
4115Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004116 if (LCDecl && !LCDecl->isInvalidDecl()) {
4117 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004118 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004119 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4120 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004121 if (PrivateVar->isInvalidDecl())
4122 return nullptr;
4123 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4124 }
4125 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004126}
4127
4128/// \brief Build initization of the counter be used for codegen.
4129Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4130
4131/// \brief Build step of the counter be used for codegen.
4132Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4133
4134/// \brief Iteration space of a single for loop.
4135struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004136 /// \brief Condition of the loop.
4137 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004138 /// \brief This expression calculates the number of iterations in the loop.
4139 /// It is always possible to calculate it before starting the loop.
4140 Expr *NumIterations;
4141 /// \brief The loop counter variable.
4142 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004143 /// \brief Private loop counter variable.
4144 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004145 /// \brief This is initializer for the initial value of #CounterVar.
4146 Expr *CounterInit;
4147 /// \brief This is step for the #CounterVar used to generate its update:
4148 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4149 Expr *CounterStep;
4150 /// \brief Should step be subtracted?
4151 bool Subtract;
4152 /// \brief Source range of the loop init.
4153 SourceRange InitSrcRange;
4154 /// \brief Source range of the loop condition.
4155 SourceRange CondSrcRange;
4156 /// \brief Source range of the loop increment.
4157 SourceRange IncSrcRange;
4158};
4159
Alexey Bataev23b69422014-06-18 07:08:49 +00004160} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004161
Alexey Bataev9c821032015-04-30 04:23:23 +00004162void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4163 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4164 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004165 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4166 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004167 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4168 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004169 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4170 if (auto *D = ISC.GetLoopDecl()) {
4171 auto *VD = dyn_cast<VarDecl>(D);
4172 if (!VD) {
4173 if (auto *Private = IsOpenMPCapturedDecl(D))
4174 VD = Private;
4175 else {
4176 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4177 /*WithInit=*/false);
4178 VD = cast<VarDecl>(Ref->getDecl());
4179 }
4180 }
4181 DSAStack->addLoopControlVariable(D, VD);
4182 }
4183 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004184 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004185 }
4186}
4187
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188/// \brief Called on a for stmt to check and extract its iteration space
4189/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004190static bool CheckOpenMPIterationSpace(
4191 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4192 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004193 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004194 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004195 LoopIterationSpace &ResultIterSpace,
4196 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 // OpenMP [2.6, Canonical Loop Form]
4198 // for (init-expr; test-expr; incr-expr) structured-block
4199 auto For = dyn_cast_or_null<ForStmt>(S);
4200 if (!For) {
4201 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004202 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4203 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4204 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4205 if (NestedLoopCount > 1) {
4206 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4207 SemaRef.Diag(DSA.getConstructLoc(),
4208 diag::note_omp_collapse_ordered_expr)
4209 << 2 << CollapseLoopCountExpr->getSourceRange()
4210 << OrderedLoopCountExpr->getSourceRange();
4211 else if (CollapseLoopCountExpr)
4212 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4213 diag::note_omp_collapse_ordered_expr)
4214 << 0 << CollapseLoopCountExpr->getSourceRange();
4215 else
4216 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4217 diag::note_omp_collapse_ordered_expr)
4218 << 1 << OrderedLoopCountExpr->getSourceRange();
4219 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004220 return true;
4221 }
4222 assert(For->getBody());
4223
4224 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4225
4226 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004227 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004228 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004229 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230
4231 bool HasErrors = false;
4232
4233 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004234 if (auto *LCDecl = ISC.GetLoopDecl()) {
4235 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004236
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004237 // OpenMP [2.6, Canonical Loop Form]
4238 // Var is one of the following:
4239 // A variable of signed or unsigned integer type.
4240 // For C++, a variable of a random access iterator type.
4241 // For C, a variable of a pointer type.
4242 auto VarType = LCDecl->getType().getNonReferenceType();
4243 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4244 !VarType->isPointerType() &&
4245 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4246 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4247 << SemaRef.getLangOpts().CPlusPlus;
4248 HasErrors = true;
4249 }
4250
4251 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4252 // a Construct
4253 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4254 // parallel for construct is (are) private.
4255 // The loop iteration variable in the associated for-loop of a simd
4256 // construct with just one associated for-loop is linear with a
4257 // constant-linear-step that is the increment of the associated for-loop.
4258 // Exclude loop var from the list of variables with implicitly defined data
4259 // sharing attributes.
4260 VarsWithImplicitDSA.erase(LCDecl);
4261
4262 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4263 // in a Construct, C/C++].
4264 // The loop iteration variable in the associated for-loop of a simd
4265 // construct with just one associated for-loop may be listed in a linear
4266 // clause with a constant-linear-step that is the increment of the
4267 // associated for-loop.
4268 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4269 // parallel for construct may be listed in a private or lastprivate clause.
4270 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4271 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4272 // declared in the loop and it is predetermined as a private.
4273 auto PredeterminedCKind =
4274 isOpenMPSimdDirective(DKind)
4275 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4276 : OMPC_private;
4277 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4278 DVar.CKind != PredeterminedCKind) ||
4279 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4280 isOpenMPDistributeDirective(DKind)) &&
4281 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4282 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4283 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4284 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4285 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4286 << getOpenMPClauseName(PredeterminedCKind);
4287 if (DVar.RefExpr == nullptr)
4288 DVar.CKind = PredeterminedCKind;
4289 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4290 HasErrors = true;
4291 } else if (LoopDeclRefExpr != nullptr) {
4292 // Make the loop iteration variable private (for worksharing constructs),
4293 // linear (for simd directives with the only one associated loop) or
4294 // lastprivate (for simd directives with several collapsed or ordered
4295 // loops).
4296 if (DVar.CKind == OMPC_unknown)
4297 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4298 /*FromParent=*/false);
4299 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4300 }
4301
4302 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4303
4304 // Check test-expr.
4305 HasErrors |= ISC.CheckCond(For->getCond());
4306
4307 // Check incr-expr.
4308 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004309 }
4310
Alexander Musmana5f070a2014-10-01 06:03:56 +00004311 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004312 return HasErrors;
4313
Alexander Musmana5f070a2014-10-01 06:03:56 +00004314 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004315 ResultIterSpace.PreCond =
4316 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004317 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004318 DSA.getCurScope(),
4319 (isOpenMPWorksharingDirective(DKind) ||
4320 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4321 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004322 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004323 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004324 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4325 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4326 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4327 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4328 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4329 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4330
Alexey Bataev62dbb972015-04-22 11:59:37 +00004331 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4332 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004333 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004334 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004335 ResultIterSpace.CounterInit == nullptr ||
4336 ResultIterSpace.CounterStep == nullptr);
4337
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004338 return HasErrors;
4339}
4340
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004341/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004342static ExprResult
4343BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4344 ExprResult Start,
4345 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004346 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004347 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4348 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004349 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004350 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004351 VarRef.get()->getType())) {
4352 NewStart = SemaRef.PerformImplicitConversion(
4353 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4354 /*AllowExplicit=*/true);
4355 if (!NewStart.isUsable())
4356 return ExprError();
4357 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004358
4359 auto Init =
4360 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4361 return Init;
4362}
4363
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004365static ExprResult
4366BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4367 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4368 ExprResult Step, bool Subtract,
4369 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004370 // Add parentheses (for debugging purposes only).
4371 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4372 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4373 !Step.isUsable())
4374 return ExprError();
4375
Alexey Bataev5a3af132016-03-29 08:58:54 +00004376 ExprResult NewStep = Step;
4377 if (Captures)
4378 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004379 if (NewStep.isInvalid())
4380 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004381 ExprResult Update =
4382 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383 if (!Update.isUsable())
4384 return ExprError();
4385
Alexey Bataevc0214e02016-02-16 12:13:49 +00004386 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4387 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004388 ExprResult NewStart = Start;
4389 if (Captures)
4390 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004391 if (NewStart.isInvalid())
4392 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004393
Alexey Bataevc0214e02016-02-16 12:13:49 +00004394 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4395 ExprResult SavedUpdate = Update;
4396 ExprResult UpdateVal;
4397 if (VarRef.get()->getType()->isOverloadableType() ||
4398 NewStart.get()->getType()->isOverloadableType() ||
4399 Update.get()->getType()->isOverloadableType()) {
4400 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4401 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4402 Update =
4403 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4404 if (Update.isUsable()) {
4405 UpdateVal =
4406 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4407 VarRef.get(), SavedUpdate.get());
4408 if (UpdateVal.isUsable()) {
4409 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4410 UpdateVal.get());
4411 }
4412 }
4413 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4414 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004415
Alexey Bataevc0214e02016-02-16 12:13:49 +00004416 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4417 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4418 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4419 NewStart.get(), SavedUpdate.get());
4420 if (!Update.isUsable())
4421 return ExprError();
4422
Alexey Bataev11481f52016-02-17 10:29:05 +00004423 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4424 VarRef.get()->getType())) {
4425 Update = SemaRef.PerformImplicitConversion(
4426 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4427 if (!Update.isUsable())
4428 return ExprError();
4429 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004430
4431 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4432 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433 return Update;
4434}
4435
4436/// \brief Convert integer expression \a E to make it have at least \a Bits
4437/// bits.
4438static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4439 Sema &SemaRef) {
4440 if (E == nullptr)
4441 return ExprError();
4442 auto &C = SemaRef.Context;
4443 QualType OldType = E->getType();
4444 unsigned HasBits = C.getTypeSize(OldType);
4445 if (HasBits >= Bits)
4446 return ExprResult(E);
4447 // OK to convert to signed, because new type has more bits than old.
4448 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4449 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4450 true);
4451}
4452
4453/// \brief Check if the given expression \a E is a constant integer that fits
4454/// into \a Bits bits.
4455static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4456 if (E == nullptr)
4457 return false;
4458 llvm::APSInt Result;
4459 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4460 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4461 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004462}
4463
Alexey Bataev5a3af132016-03-29 08:58:54 +00004464/// Build preinits statement for the given declarations.
4465static Stmt *buildPreInits(ASTContext &Context,
4466 SmallVectorImpl<Decl *> &PreInits) {
4467 if (!PreInits.empty()) {
4468 return new (Context) DeclStmt(
4469 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4470 SourceLocation(), SourceLocation());
4471 }
4472 return nullptr;
4473}
4474
4475/// Build preinits statement for the given declarations.
4476static Stmt *buildPreInits(ASTContext &Context,
4477 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4478 if (!Captures.empty()) {
4479 SmallVector<Decl *, 16> PreInits;
4480 for (auto &Pair : Captures)
4481 PreInits.push_back(Pair.second->getDecl());
4482 return buildPreInits(Context, PreInits);
4483 }
4484 return nullptr;
4485}
4486
4487/// Build postupdate expression for the given list of postupdates expressions.
4488static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4489 Expr *PostUpdate = nullptr;
4490 if (!PostUpdates.empty()) {
4491 for (auto *E : PostUpdates) {
4492 Expr *ConvE = S.BuildCStyleCastExpr(
4493 E->getExprLoc(),
4494 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4495 E->getExprLoc(), E)
4496 .get();
4497 PostUpdate = PostUpdate
4498 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4499 PostUpdate, ConvE)
4500 .get()
4501 : ConvE;
4502 }
4503 }
4504 return PostUpdate;
4505}
4506
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004507/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004508/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4509/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004510static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004511CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4512 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4513 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004514 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004515 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004516 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004517 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004518 // Found 'collapse' clause - calculate collapse number.
4519 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004520 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004521 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004522 }
4523 if (OrderedLoopCountExpr) {
4524 // Found 'ordered' clause - calculate collapse number.
4525 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004526 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4527 if (Result.getLimitedValue() < NestedLoopCount) {
4528 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4529 diag::err_omp_wrong_ordered_loop_count)
4530 << OrderedLoopCountExpr->getSourceRange();
4531 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4532 diag::note_collapse_loop_count)
4533 << CollapseLoopCountExpr->getSourceRange();
4534 }
4535 NestedLoopCount = Result.getLimitedValue();
4536 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004537 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004538 // This is helper routine for loop directives (e.g., 'for', 'simd',
4539 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004540 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004541 SmallVector<LoopIterationSpace, 4> IterSpaces;
4542 IterSpaces.resize(NestedLoopCount);
4543 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004545 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004546 NestedLoopCount, CollapseLoopCountExpr,
4547 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004548 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004549 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004550 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004551 // OpenMP [2.8.1, simd construct, Restrictions]
4552 // All loops associated with the construct must be perfectly nested; that
4553 // is, there must be no intervening code nor any OpenMP directive between
4554 // any two loops.
4555 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004556 }
4557
Alexander Musmana5f070a2014-10-01 06:03:56 +00004558 Built.clear(/* size */ NestedLoopCount);
4559
4560 if (SemaRef.CurContext->isDependentContext())
4561 return NestedLoopCount;
4562
4563 // An example of what is generated for the following code:
4564 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004565 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004566 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004567 // for (k = 0; k < NK; ++k)
4568 // for (j = J0; j < NJ; j+=2) {
4569 // <loop body>
4570 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004571 //
4572 // We generate the code below.
4573 // Note: the loop body may be outlined in CodeGen.
4574 // Note: some counters may be C++ classes, operator- is used to find number of
4575 // iterations and operator+= to calculate counter value.
4576 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4577 // or i64 is currently supported).
4578 //
4579 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4580 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4581 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4582 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4583 // // similar updates for vars in clauses (e.g. 'linear')
4584 // <loop body (using local i and j)>
4585 // }
4586 // i = NI; // assign final values of counters
4587 // j = NJ;
4588 //
4589
4590 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4591 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004592 // Precondition tests if there is at least one iteration (all conditions are
4593 // true).
4594 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004595 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004596 ExprResult LastIteration32 = WidenIterationCount(
4597 32 /* Bits */, SemaRef.PerformImplicitConversion(
4598 N0->IgnoreImpCasts(), N0->getType(),
4599 Sema::AA_Converting, /*AllowExplicit=*/true)
4600 .get(),
4601 SemaRef);
4602 ExprResult LastIteration64 = WidenIterationCount(
4603 64 /* Bits */, SemaRef.PerformImplicitConversion(
4604 N0->IgnoreImpCasts(), N0->getType(),
4605 Sema::AA_Converting, /*AllowExplicit=*/true)
4606 .get(),
4607 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004608
4609 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4610 return NestedLoopCount;
4611
4612 auto &C = SemaRef.Context;
4613 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4614
4615 Scope *CurScope = DSA.getCurScope();
4616 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004617 if (PreCond.isUsable()) {
4618 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4619 PreCond.get(), IterSpaces[Cnt].PreCond);
4620 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004621 auto N = IterSpaces[Cnt].NumIterations;
4622 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4623 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004624 LastIteration32 = SemaRef.BuildBinOp(
4625 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4626 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4627 Sema::AA_Converting,
4628 /*AllowExplicit=*/true)
4629 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004631 LastIteration64 = SemaRef.BuildBinOp(
4632 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4633 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4634 Sema::AA_Converting,
4635 /*AllowExplicit=*/true)
4636 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004637 }
4638
4639 // Choose either the 32-bit or 64-bit version.
4640 ExprResult LastIteration = LastIteration64;
4641 if (LastIteration32.isUsable() &&
4642 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4643 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4644 FitsInto(
4645 32 /* Bits */,
4646 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4647 LastIteration64.get(), SemaRef)))
4648 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00004649 QualType VType = LastIteration.get()->getType();
4650 QualType RealVType = VType;
4651 QualType StrideVType = VType;
4652 if (isOpenMPTaskLoopDirective(DKind)) {
4653 VType =
4654 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4655 StrideVType =
4656 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4657 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004658
4659 if (!LastIteration.isUsable())
4660 return 0;
4661
4662 // Save the number of iterations.
4663 ExprResult NumIterations = LastIteration;
4664 {
4665 LastIteration = SemaRef.BuildBinOp(
4666 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4667 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4668 if (!LastIteration.isUsable())
4669 return 0;
4670 }
4671
4672 // Calculate the last iteration number beforehand instead of doing this on
4673 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4674 llvm::APSInt Result;
4675 bool IsConstant =
4676 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4677 ExprResult CalcLastIteration;
4678 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004679 ExprResult SaveRef =
4680 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004681 LastIteration = SaveRef;
4682
4683 // Prepare SaveRef + 1.
4684 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004685 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004686 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4687 if (!NumIterations.isUsable())
4688 return 0;
4689 }
4690
4691 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4692
Alexander Musmanc6388682014-12-15 07:07:06 +00004693 // Build variables passed into runtime, nesessary for worksharing directives.
4694 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004695 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4696 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004697 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004698 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4699 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004700 SemaRef.AddInitializerToDecl(
4701 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4702 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4703
4704 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004705 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4706 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004707 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4708 /*DirectInit*/ false,
4709 /*TypeMayContainAuto*/ false);
4710
4711 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4712 // This will be used to implement clause 'lastprivate'.
4713 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004714 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4715 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004716 SemaRef.AddInitializerToDecl(
4717 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4718 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4719
4720 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00004721 VarDecl *STDecl =
4722 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4723 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004724 SemaRef.AddInitializerToDecl(
4725 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4726 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4727
4728 // Build expression: UB = min(UB, LastIteration)
4729 // It is nesessary for CodeGen of directives with static scheduling.
4730 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4731 UB.get(), LastIteration.get());
4732 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4733 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4734 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4735 CondOp.get());
4736 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4737 }
4738
4739 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004740 ExprResult IV;
4741 ExprResult Init;
4742 {
Alexey Bataev7292c292016-04-25 12:22:29 +00004743 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4744 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004745 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004746 isOpenMPTaskLoopDirective(DKind) ||
4747 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004748 ? LB.get()
4749 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4750 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4751 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004752 }
4753
Alexander Musmanc6388682014-12-15 07:07:06 +00004754 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004755 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004756 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004757 (isOpenMPWorksharingDirective(DKind) ||
4758 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004759 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4760 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4761 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004762
4763 // Loop increment (IV = IV + 1)
4764 SourceLocation IncLoc;
4765 ExprResult Inc =
4766 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4767 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4768 if (!Inc.isUsable())
4769 return 0;
4770 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004771 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4772 if (!Inc.isUsable())
4773 return 0;
4774
4775 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4776 // Used for directives with static scheduling.
4777 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004778 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4779 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004780 // LB + ST
4781 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4782 if (!NextLB.isUsable())
4783 return 0;
4784 // LB = LB + ST
4785 NextLB =
4786 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4787 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4788 if (!NextLB.isUsable())
4789 return 0;
4790 // UB + ST
4791 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4792 if (!NextUB.isUsable())
4793 return 0;
4794 // UB = UB + ST
4795 NextUB =
4796 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4797 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4798 if (!NextUB.isUsable())
4799 return 0;
4800 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004801
4802 // Build updates and final values of the loop counters.
4803 bool HasErrors = false;
4804 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004805 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004806 Built.Updates.resize(NestedLoopCount);
4807 Built.Finals.resize(NestedLoopCount);
4808 {
4809 ExprResult Div;
4810 // Go from inner nested loop to outer.
4811 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4812 LoopIterationSpace &IS = IterSpaces[Cnt];
4813 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4814 // Build: Iter = (IV / Div) % IS.NumIters
4815 // where Div is product of previous iterations' IS.NumIters.
4816 ExprResult Iter;
4817 if (Div.isUsable()) {
4818 Iter =
4819 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4820 } else {
4821 Iter = IV;
4822 assert((Cnt == (int)NestedLoopCount - 1) &&
4823 "unusable div expected on first iteration only");
4824 }
4825
4826 if (Cnt != 0 && Iter.isUsable())
4827 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4828 IS.NumIterations);
4829 if (!Iter.isUsable()) {
4830 HasErrors = true;
4831 break;
4832 }
4833
Alexey Bataev39f915b82015-05-08 10:41:21 +00004834 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004835 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4836 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4837 IS.CounterVar->getExprLoc(),
4838 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004839 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004840 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004841 if (!Init.isUsable()) {
4842 HasErrors = true;
4843 break;
4844 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004845 ExprResult Update = BuildCounterUpdate(
4846 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4847 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004848 if (!Update.isUsable()) {
4849 HasErrors = true;
4850 break;
4851 }
4852
4853 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4854 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004855 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004856 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004857 if (!Final.isUsable()) {
4858 HasErrors = true;
4859 break;
4860 }
4861
4862 // Build Div for the next iteration: Div <- Div * IS.NumIters
4863 if (Cnt != 0) {
4864 if (Div.isUnset())
4865 Div = IS.NumIterations;
4866 else
4867 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4868 IS.NumIterations);
4869
4870 // Add parentheses (for debugging purposes only).
4871 if (Div.isUsable())
4872 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4873 if (!Div.isUsable()) {
4874 HasErrors = true;
4875 break;
4876 }
4877 }
4878 if (!Update.isUsable() || !Final.isUsable()) {
4879 HasErrors = true;
4880 break;
4881 }
4882 // Save results
4883 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004884 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004885 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004886 Built.Updates[Cnt] = Update.get();
4887 Built.Finals[Cnt] = Final.get();
4888 }
4889 }
4890
4891 if (HasErrors)
4892 return 0;
4893
4894 // Save results
4895 Built.IterationVarRef = IV.get();
4896 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004897 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004898 Built.CalcLastIteration =
4899 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004900 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004901 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004902 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004903 Built.Init = Init.get();
4904 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004905 Built.LB = LB.get();
4906 Built.UB = UB.get();
4907 Built.IL = IL.get();
4908 Built.ST = ST.get();
4909 Built.EUB = EUB.get();
4910 Built.NLB = NextLB.get();
4911 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004912
Alexey Bataevabfc0692014-06-25 06:52:00 +00004913 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004914}
4915
Alexey Bataev10e775f2015-07-30 11:36:16 +00004916static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004917 auto CollapseClauses =
4918 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4919 if (CollapseClauses.begin() != CollapseClauses.end())
4920 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004921 return nullptr;
4922}
4923
Alexey Bataev10e775f2015-07-30 11:36:16 +00004924static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004925 auto OrderedClauses =
4926 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4927 if (OrderedClauses.begin() != OrderedClauses.end())
4928 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004929 return nullptr;
4930}
4931
Alexey Bataev66b15b52015-08-21 11:14:16 +00004932static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4933 const Expr *Safelen) {
4934 llvm::APSInt SimdlenRes, SafelenRes;
4935 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4936 Simdlen->isInstantiationDependent() ||
4937 Simdlen->containsUnexpandedParameterPack())
4938 return false;
4939 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4940 Safelen->isInstantiationDependent() ||
4941 Safelen->containsUnexpandedParameterPack())
4942 return false;
4943 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4944 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4945 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4946 // If both simdlen and safelen clauses are specified, the value of the simdlen
4947 // parameter must be less than or equal to the value of the safelen parameter.
4948 if (SimdlenRes > SafelenRes) {
4949 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4950 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4951 return true;
4952 }
4953 return false;
4954}
4955
Alexey Bataev4acb8592014-07-07 13:01:15 +00004956StmtResult Sema::ActOnOpenMPSimdDirective(
4957 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4958 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004959 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004960 if (!AStmt)
4961 return StmtError();
4962
4963 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004964 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004965 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4966 // define the nested loops number.
4967 unsigned NestedLoopCount = CheckOpenMPLoop(
4968 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4969 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004970 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004971 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004972
Alexander Musmana5f070a2014-10-01 06:03:56 +00004973 assert((CurContext->isDependentContext() || B.builtAll()) &&
4974 "omp simd loop exprs were not built");
4975
Alexander Musman3276a272015-03-21 10:12:56 +00004976 if (!CurContext->isDependentContext()) {
4977 // Finalize the clauses that need pre-built expressions for CodeGen.
4978 for (auto C : Clauses) {
4979 if (auto LC = dyn_cast<OMPLinearClause>(C))
4980 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004981 B.NumIterations, *this, CurScope,
4982 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00004983 return StmtError();
4984 }
4985 }
4986
Alexey Bataev66b15b52015-08-21 11:14:16 +00004987 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4988 // If both simdlen and safelen clauses are specified, the value of the simdlen
4989 // parameter must be less than or equal to the value of the safelen parameter.
4990 OMPSafelenClause *Safelen = nullptr;
4991 OMPSimdlenClause *Simdlen = nullptr;
4992 for (auto *Clause : Clauses) {
4993 if (Clause->getClauseKind() == OMPC_safelen)
4994 Safelen = cast<OMPSafelenClause>(Clause);
4995 else if (Clause->getClauseKind() == OMPC_simdlen)
4996 Simdlen = cast<OMPSimdlenClause>(Clause);
4997 if (Safelen && Simdlen)
4998 break;
4999 }
5000 if (Simdlen && Safelen &&
5001 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5002 Safelen->getSafelen()))
5003 return StmtError();
5004
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005005 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005006 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5007 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005008}
5009
Alexey Bataev4acb8592014-07-07 13:01:15 +00005010StmtResult Sema::ActOnOpenMPForDirective(
5011 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5012 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005013 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005014 if (!AStmt)
5015 return StmtError();
5016
5017 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005018 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005019 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5020 // define the nested loops number.
5021 unsigned NestedLoopCount = CheckOpenMPLoop(
5022 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5023 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005024 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005025 return StmtError();
5026
Alexander Musmana5f070a2014-10-01 06:03:56 +00005027 assert((CurContext->isDependentContext() || B.builtAll()) &&
5028 "omp for loop exprs were not built");
5029
Alexey Bataev54acd402015-08-04 11:18:19 +00005030 if (!CurContext->isDependentContext()) {
5031 // Finalize the clauses that need pre-built expressions for CodeGen.
5032 for (auto C : Clauses) {
5033 if (auto LC = dyn_cast<OMPLinearClause>(C))
5034 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005035 B.NumIterations, *this, CurScope,
5036 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005037 return StmtError();
5038 }
5039 }
5040
Alexey Bataevf29276e2014-06-18 04:14:57 +00005041 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005042 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005043 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005044}
5045
Alexander Musmanf82886e2014-09-18 05:12:34 +00005046StmtResult Sema::ActOnOpenMPForSimdDirective(
5047 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5048 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005049 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005050 if (!AStmt)
5051 return StmtError();
5052
5053 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005054 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005055 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5056 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005057 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005058 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5059 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5060 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005061 if (NestedLoopCount == 0)
5062 return StmtError();
5063
Alexander Musmanc6388682014-12-15 07:07:06 +00005064 assert((CurContext->isDependentContext() || B.builtAll()) &&
5065 "omp for simd loop exprs were not built");
5066
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005067 if (!CurContext->isDependentContext()) {
5068 // Finalize the clauses that need pre-built expressions for CodeGen.
5069 for (auto C : Clauses) {
5070 if (auto LC = dyn_cast<OMPLinearClause>(C))
5071 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005072 B.NumIterations, *this, CurScope,
5073 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005074 return StmtError();
5075 }
5076 }
5077
Alexey Bataev66b15b52015-08-21 11:14:16 +00005078 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5079 // If both simdlen and safelen clauses are specified, the value of the simdlen
5080 // parameter must be less than or equal to the value of the safelen parameter.
5081 OMPSafelenClause *Safelen = nullptr;
5082 OMPSimdlenClause *Simdlen = nullptr;
5083 for (auto *Clause : Clauses) {
5084 if (Clause->getClauseKind() == OMPC_safelen)
5085 Safelen = cast<OMPSafelenClause>(Clause);
5086 else if (Clause->getClauseKind() == OMPC_simdlen)
5087 Simdlen = cast<OMPSimdlenClause>(Clause);
5088 if (Safelen && Simdlen)
5089 break;
5090 }
5091 if (Simdlen && Safelen &&
5092 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5093 Safelen->getSafelen()))
5094 return StmtError();
5095
Alexander Musmanf82886e2014-09-18 05:12:34 +00005096 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005097 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5098 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005099}
5100
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005101StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5102 Stmt *AStmt,
5103 SourceLocation StartLoc,
5104 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005105 if (!AStmt)
5106 return StmtError();
5107
5108 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005109 auto BaseStmt = AStmt;
5110 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5111 BaseStmt = CS->getCapturedStmt();
5112 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5113 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005114 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005115 return StmtError();
5116 // All associated statements must be '#pragma omp section' except for
5117 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005118 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005119 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5120 if (SectionStmt)
5121 Diag(SectionStmt->getLocStart(),
5122 diag::err_omp_sections_substmt_not_section);
5123 return StmtError();
5124 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005125 cast<OMPSectionDirective>(SectionStmt)
5126 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005127 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005128 } else {
5129 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5130 return StmtError();
5131 }
5132
5133 getCurFunction()->setHasBranchProtectedScope();
5134
Alexey Bataev25e5b442015-09-15 12:52:43 +00005135 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5136 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005137}
5138
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005139StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5140 SourceLocation StartLoc,
5141 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005142 if (!AStmt)
5143 return StmtError();
5144
5145 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005146
5147 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005148 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005149
Alexey Bataev25e5b442015-09-15 12:52:43 +00005150 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5151 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005152}
5153
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005154StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5155 Stmt *AStmt,
5156 SourceLocation StartLoc,
5157 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005158 if (!AStmt)
5159 return StmtError();
5160
5161 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005162
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005163 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005164
Alexey Bataev3255bf32015-01-19 05:20:46 +00005165 // OpenMP [2.7.3, single Construct, Restrictions]
5166 // The copyprivate clause must not be used with the nowait clause.
5167 OMPClause *Nowait = nullptr;
5168 OMPClause *Copyprivate = nullptr;
5169 for (auto *Clause : Clauses) {
5170 if (Clause->getClauseKind() == OMPC_nowait)
5171 Nowait = Clause;
5172 else if (Clause->getClauseKind() == OMPC_copyprivate)
5173 Copyprivate = Clause;
5174 if (Copyprivate && Nowait) {
5175 Diag(Copyprivate->getLocStart(),
5176 diag::err_omp_single_copyprivate_with_nowait);
5177 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5178 return StmtError();
5179 }
5180 }
5181
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005182 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5183}
5184
Alexander Musman80c22892014-07-17 08:54:58 +00005185StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5186 SourceLocation StartLoc,
5187 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005188 if (!AStmt)
5189 return StmtError();
5190
5191 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005192
5193 getCurFunction()->setHasBranchProtectedScope();
5194
5195 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5196}
5197
Alexey Bataev28c75412015-12-15 08:19:24 +00005198StmtResult Sema::ActOnOpenMPCriticalDirective(
5199 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5200 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005201 if (!AStmt)
5202 return StmtError();
5203
5204 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005205
Alexey Bataev28c75412015-12-15 08:19:24 +00005206 bool ErrorFound = false;
5207 llvm::APSInt Hint;
5208 SourceLocation HintLoc;
5209 bool DependentHint = false;
5210 for (auto *C : Clauses) {
5211 if (C->getClauseKind() == OMPC_hint) {
5212 if (!DirName.getName()) {
5213 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5214 ErrorFound = true;
5215 }
5216 Expr *E = cast<OMPHintClause>(C)->getHint();
5217 if (E->isTypeDependent() || E->isValueDependent() ||
5218 E->isInstantiationDependent())
5219 DependentHint = true;
5220 else {
5221 Hint = E->EvaluateKnownConstInt(Context);
5222 HintLoc = C->getLocStart();
5223 }
5224 }
5225 }
5226 if (ErrorFound)
5227 return StmtError();
5228 auto Pair = DSAStack->getCriticalWithHint(DirName);
5229 if (Pair.first && DirName.getName() && !DependentHint) {
5230 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5231 Diag(StartLoc, diag::err_omp_critical_with_hint);
5232 if (HintLoc.isValid()) {
5233 Diag(HintLoc, diag::note_omp_critical_hint_here)
5234 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5235 } else
5236 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5237 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5238 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5239 << 1
5240 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5241 /*Radix=*/10, /*Signed=*/false);
5242 } else
5243 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5244 }
5245 }
5246
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005247 getCurFunction()->setHasBranchProtectedScope();
5248
Alexey Bataev28c75412015-12-15 08:19:24 +00005249 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5250 Clauses, AStmt);
5251 if (!Pair.first && DirName.getName() && !DependentHint)
5252 DSAStack->addCriticalWithHint(Dir, Hint);
5253 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005254}
5255
Alexey Bataev4acb8592014-07-07 13:01:15 +00005256StmtResult Sema::ActOnOpenMPParallelForDirective(
5257 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5258 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005259 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005260 if (!AStmt)
5261 return StmtError();
5262
Alexey Bataev4acb8592014-07-07 13:01:15 +00005263 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5264 // 1.2.2 OpenMP Language Terminology
5265 // Structured block - An executable statement with a single entry at the
5266 // top and a single exit at the bottom.
5267 // The point of exit cannot be a branch out of the structured block.
5268 // longjmp() and throw() must not violate the entry/exit criteria.
5269 CS->getCapturedDecl()->setNothrow();
5270
Alexander Musmanc6388682014-12-15 07:07:06 +00005271 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005272 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5273 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005274 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005275 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5276 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5277 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005278 if (NestedLoopCount == 0)
5279 return StmtError();
5280
Alexander Musmana5f070a2014-10-01 06:03:56 +00005281 assert((CurContext->isDependentContext() || B.builtAll()) &&
5282 "omp parallel for loop exprs were not built");
5283
Alexey Bataev54acd402015-08-04 11:18:19 +00005284 if (!CurContext->isDependentContext()) {
5285 // Finalize the clauses that need pre-built expressions for CodeGen.
5286 for (auto C : Clauses) {
5287 if (auto LC = dyn_cast<OMPLinearClause>(C))
5288 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005289 B.NumIterations, *this, CurScope,
5290 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005291 return StmtError();
5292 }
5293 }
5294
Alexey Bataev4acb8592014-07-07 13:01:15 +00005295 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005296 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005297 NestedLoopCount, Clauses, AStmt, B,
5298 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005299}
5300
Alexander Musmane4e893b2014-09-23 09:33:00 +00005301StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5302 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5303 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005304 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005305 if (!AStmt)
5306 return StmtError();
5307
Alexander Musmane4e893b2014-09-23 09:33:00 +00005308 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5309 // 1.2.2 OpenMP Language Terminology
5310 // Structured block - An executable statement with a single entry at the
5311 // top and a single exit at the bottom.
5312 // The point of exit cannot be a branch out of the structured block.
5313 // longjmp() and throw() must not violate the entry/exit criteria.
5314 CS->getCapturedDecl()->setNothrow();
5315
Alexander Musmanc6388682014-12-15 07:07:06 +00005316 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005317 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5318 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005319 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005320 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5321 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5322 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005323 if (NestedLoopCount == 0)
5324 return StmtError();
5325
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005326 if (!CurContext->isDependentContext()) {
5327 // Finalize the clauses that need pre-built expressions for CodeGen.
5328 for (auto C : Clauses) {
5329 if (auto LC = dyn_cast<OMPLinearClause>(C))
5330 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005331 B.NumIterations, *this, CurScope,
5332 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005333 return StmtError();
5334 }
5335 }
5336
Alexey Bataev66b15b52015-08-21 11:14:16 +00005337 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5338 // If both simdlen and safelen clauses are specified, the value of the simdlen
5339 // parameter must be less than or equal to the value of the safelen parameter.
5340 OMPSafelenClause *Safelen = nullptr;
5341 OMPSimdlenClause *Simdlen = nullptr;
5342 for (auto *Clause : Clauses) {
5343 if (Clause->getClauseKind() == OMPC_safelen)
5344 Safelen = cast<OMPSafelenClause>(Clause);
5345 else if (Clause->getClauseKind() == OMPC_simdlen)
5346 Simdlen = cast<OMPSimdlenClause>(Clause);
5347 if (Safelen && Simdlen)
5348 break;
5349 }
5350 if (Simdlen && Safelen &&
5351 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5352 Safelen->getSafelen()))
5353 return StmtError();
5354
Alexander Musmane4e893b2014-09-23 09:33:00 +00005355 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005356 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005357 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005358}
5359
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005360StmtResult
5361Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5362 Stmt *AStmt, SourceLocation StartLoc,
5363 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005364 if (!AStmt)
5365 return StmtError();
5366
5367 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005368 auto BaseStmt = AStmt;
5369 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5370 BaseStmt = CS->getCapturedStmt();
5371 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5372 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005373 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005374 return StmtError();
5375 // All associated statements must be '#pragma omp section' except for
5376 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005377 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005378 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5379 if (SectionStmt)
5380 Diag(SectionStmt->getLocStart(),
5381 diag::err_omp_parallel_sections_substmt_not_section);
5382 return StmtError();
5383 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005384 cast<OMPSectionDirective>(SectionStmt)
5385 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005386 }
5387 } else {
5388 Diag(AStmt->getLocStart(),
5389 diag::err_omp_parallel_sections_not_compound_stmt);
5390 return StmtError();
5391 }
5392
5393 getCurFunction()->setHasBranchProtectedScope();
5394
Alexey Bataev25e5b442015-09-15 12:52:43 +00005395 return OMPParallelSectionsDirective::Create(
5396 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005397}
5398
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005399StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5400 Stmt *AStmt, SourceLocation StartLoc,
5401 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005402 if (!AStmt)
5403 return StmtError();
5404
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005405 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5406 // 1.2.2 OpenMP Language Terminology
5407 // Structured block - An executable statement with a single entry at the
5408 // top and a single exit at the bottom.
5409 // The point of exit cannot be a branch out of the structured block.
5410 // longjmp() and throw() must not violate the entry/exit criteria.
5411 CS->getCapturedDecl()->setNothrow();
5412
5413 getCurFunction()->setHasBranchProtectedScope();
5414
Alexey Bataev25e5b442015-09-15 12:52:43 +00005415 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5416 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005417}
5418
Alexey Bataev68446b72014-07-18 07:47:19 +00005419StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5420 SourceLocation EndLoc) {
5421 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5422}
5423
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005424StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5425 SourceLocation EndLoc) {
5426 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5427}
5428
Alexey Bataev2df347a2014-07-18 10:17:07 +00005429StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5430 SourceLocation EndLoc) {
5431 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5432}
5433
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005434StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5435 SourceLocation StartLoc,
5436 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005437 if (!AStmt)
5438 return StmtError();
5439
5440 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005441
5442 getCurFunction()->setHasBranchProtectedScope();
5443
5444 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5445}
5446
Alexey Bataev6125da92014-07-21 11:26:11 +00005447StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5448 SourceLocation StartLoc,
5449 SourceLocation EndLoc) {
5450 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5451 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5452}
5453
Alexey Bataev346265e2015-09-25 10:37:12 +00005454StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5455 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005456 SourceLocation StartLoc,
5457 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005458 OMPClause *DependFound = nullptr;
5459 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005460 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005461 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005462 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005463 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005464 for (auto *C : Clauses) {
5465 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5466 DependFound = C;
5467 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5468 if (DependSourceClause) {
5469 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5470 << getOpenMPDirectiveName(OMPD_ordered)
5471 << getOpenMPClauseName(OMPC_depend) << 2;
5472 ErrorFound = true;
5473 } else
5474 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005475 if (DependSinkClause) {
5476 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5477 << 0;
5478 ErrorFound = true;
5479 }
5480 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5481 if (DependSourceClause) {
5482 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5483 << 1;
5484 ErrorFound = true;
5485 }
5486 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005487 }
5488 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005489 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005490 else if (C->getClauseKind() == OMPC_simd)
5491 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005492 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005493 if (!ErrorFound && !SC &&
5494 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005495 // OpenMP [2.8.1,simd Construct, Restrictions]
5496 // An ordered construct with the simd clause is the only OpenMP construct
5497 // that can appear in the simd region.
5498 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005499 ErrorFound = true;
5500 } else if (DependFound && (TC || SC)) {
5501 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5502 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5503 ErrorFound = true;
5504 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5505 Diag(DependFound->getLocStart(),
5506 diag::err_omp_ordered_directive_without_param);
5507 ErrorFound = true;
5508 } else if (TC || Clauses.empty()) {
5509 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5510 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5511 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5512 << (TC != nullptr);
5513 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5514 ErrorFound = true;
5515 }
5516 }
5517 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005518 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005519
5520 if (AStmt) {
5521 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5522
5523 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005524 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005525
5526 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005527}
5528
Alexey Bataev1d160b12015-03-13 12:27:31 +00005529namespace {
5530/// \brief Helper class for checking expression in 'omp atomic [update]'
5531/// construct.
5532class OpenMPAtomicUpdateChecker {
5533 /// \brief Error results for atomic update expressions.
5534 enum ExprAnalysisErrorCode {
5535 /// \brief A statement is not an expression statement.
5536 NotAnExpression,
5537 /// \brief Expression is not builtin binary or unary operation.
5538 NotABinaryOrUnaryExpression,
5539 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5540 NotAnUnaryIncDecExpression,
5541 /// \brief An expression is not of scalar type.
5542 NotAScalarType,
5543 /// \brief A binary operation is not an assignment operation.
5544 NotAnAssignmentOp,
5545 /// \brief RHS part of the binary operation is not a binary expression.
5546 NotABinaryExpression,
5547 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5548 /// expression.
5549 NotABinaryOperator,
5550 /// \brief RHS binary operation does not have reference to the updated LHS
5551 /// part.
5552 NotAnUpdateExpression,
5553 /// \brief No errors is found.
5554 NoError
5555 };
5556 /// \brief Reference to Sema.
5557 Sema &SemaRef;
5558 /// \brief A location for note diagnostics (when error is found).
5559 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005560 /// \brief 'x' lvalue part of the source atomic expression.
5561 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005562 /// \brief 'expr' rvalue part of the source atomic expression.
5563 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005564 /// \brief Helper expression of the form
5565 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5566 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5567 Expr *UpdateExpr;
5568 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5569 /// important for non-associative operations.
5570 bool IsXLHSInRHSPart;
5571 BinaryOperatorKind Op;
5572 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005573 /// \brief true if the source expression is a postfix unary operation, false
5574 /// if it is a prefix unary operation.
5575 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005576
5577public:
5578 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005579 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005580 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005581 /// \brief Check specified statement that it is suitable for 'atomic update'
5582 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005583 /// expression. If DiagId and NoteId == 0, then only check is performed
5584 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005585 /// \param DiagId Diagnostic which should be emitted if error is found.
5586 /// \param NoteId Diagnostic note for the main error message.
5587 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005588 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005589 /// \brief Return the 'x' lvalue part of the source atomic expression.
5590 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005591 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5592 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005593 /// \brief Return the update expression used in calculation of the updated
5594 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5595 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5596 Expr *getUpdateExpr() const { return UpdateExpr; }
5597 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5598 /// false otherwise.
5599 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5600
Alexey Bataevb78ca832015-04-01 03:33:17 +00005601 /// \brief true if the source expression is a postfix unary operation, false
5602 /// if it is a prefix unary operation.
5603 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5604
Alexey Bataev1d160b12015-03-13 12:27:31 +00005605private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005606 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5607 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005608};
5609} // namespace
5610
5611bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5612 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5613 ExprAnalysisErrorCode ErrorFound = NoError;
5614 SourceLocation ErrorLoc, NoteLoc;
5615 SourceRange ErrorRange, NoteRange;
5616 // Allowed constructs are:
5617 // x = x binop expr;
5618 // x = expr binop x;
5619 if (AtomicBinOp->getOpcode() == BO_Assign) {
5620 X = AtomicBinOp->getLHS();
5621 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5622 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5623 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5624 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5625 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005626 Op = AtomicInnerBinOp->getOpcode();
5627 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005628 auto *LHS = AtomicInnerBinOp->getLHS();
5629 auto *RHS = AtomicInnerBinOp->getRHS();
5630 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5631 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5632 /*Canonical=*/true);
5633 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5634 /*Canonical=*/true);
5635 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5636 /*Canonical=*/true);
5637 if (XId == LHSId) {
5638 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005639 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005640 } else if (XId == RHSId) {
5641 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005642 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005643 } else {
5644 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5645 ErrorRange = AtomicInnerBinOp->getSourceRange();
5646 NoteLoc = X->getExprLoc();
5647 NoteRange = X->getSourceRange();
5648 ErrorFound = NotAnUpdateExpression;
5649 }
5650 } else {
5651 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5652 ErrorRange = AtomicInnerBinOp->getSourceRange();
5653 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5654 NoteRange = SourceRange(NoteLoc, NoteLoc);
5655 ErrorFound = NotABinaryOperator;
5656 }
5657 } else {
5658 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5659 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5660 ErrorFound = NotABinaryExpression;
5661 }
5662 } else {
5663 ErrorLoc = AtomicBinOp->getExprLoc();
5664 ErrorRange = AtomicBinOp->getSourceRange();
5665 NoteLoc = AtomicBinOp->getOperatorLoc();
5666 NoteRange = SourceRange(NoteLoc, NoteLoc);
5667 ErrorFound = NotAnAssignmentOp;
5668 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005669 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005670 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5671 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5672 return true;
5673 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005674 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005675 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005676}
5677
5678bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5679 unsigned NoteId) {
5680 ExprAnalysisErrorCode ErrorFound = NoError;
5681 SourceLocation ErrorLoc, NoteLoc;
5682 SourceRange ErrorRange, NoteRange;
5683 // Allowed constructs are:
5684 // x++;
5685 // x--;
5686 // ++x;
5687 // --x;
5688 // x binop= expr;
5689 // x = x binop expr;
5690 // x = expr binop x;
5691 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5692 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5693 if (AtomicBody->getType()->isScalarType() ||
5694 AtomicBody->isInstantiationDependent()) {
5695 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5696 AtomicBody->IgnoreParenImpCasts())) {
5697 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005698 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005699 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005700 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005701 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005702 X = AtomicCompAssignOp->getLHS();
5703 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005704 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5705 AtomicBody->IgnoreParenImpCasts())) {
5706 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005707 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5708 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005709 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005710 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5711 // Check for Unary Operation
5712 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005713 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005714 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5715 OpLoc = AtomicUnaryOp->getOperatorLoc();
5716 X = AtomicUnaryOp->getSubExpr();
5717 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5718 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005719 } else {
5720 ErrorFound = NotAnUnaryIncDecExpression;
5721 ErrorLoc = AtomicUnaryOp->getExprLoc();
5722 ErrorRange = AtomicUnaryOp->getSourceRange();
5723 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5724 NoteRange = SourceRange(NoteLoc, NoteLoc);
5725 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005726 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005727 ErrorFound = NotABinaryOrUnaryExpression;
5728 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5729 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5730 }
5731 } else {
5732 ErrorFound = NotAScalarType;
5733 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5734 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5735 }
5736 } else {
5737 ErrorFound = NotAnExpression;
5738 NoteLoc = ErrorLoc = S->getLocStart();
5739 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5740 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005741 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005742 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5743 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5744 return true;
5745 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005746 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005747 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005748 // Build an update expression of form 'OpaqueValueExpr(x) binop
5749 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5750 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5751 auto *OVEX = new (SemaRef.getASTContext())
5752 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5753 auto *OVEExpr = new (SemaRef.getASTContext())
5754 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5755 auto Update =
5756 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5757 IsXLHSInRHSPart ? OVEExpr : OVEX);
5758 if (Update.isInvalid())
5759 return true;
5760 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5761 Sema::AA_Casting);
5762 if (Update.isInvalid())
5763 return true;
5764 UpdateExpr = Update.get();
5765 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005766 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005767}
5768
Alexey Bataev0162e452014-07-22 10:10:35 +00005769StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5770 Stmt *AStmt,
5771 SourceLocation StartLoc,
5772 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005773 if (!AStmt)
5774 return StmtError();
5775
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005776 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005777 // 1.2.2 OpenMP Language Terminology
5778 // Structured block - An executable statement with a single entry at the
5779 // top and a single exit at the bottom.
5780 // The point of exit cannot be a branch out of the structured block.
5781 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005782 OpenMPClauseKind AtomicKind = OMPC_unknown;
5783 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005784 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005785 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005786 C->getClauseKind() == OMPC_update ||
5787 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005788 if (AtomicKind != OMPC_unknown) {
5789 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5790 << SourceRange(C->getLocStart(), C->getLocEnd());
5791 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5792 << getOpenMPClauseName(AtomicKind);
5793 } else {
5794 AtomicKind = C->getClauseKind();
5795 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005796 }
5797 }
5798 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005799
Alexey Bataev459dec02014-07-24 06:46:57 +00005800 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005801 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5802 Body = EWC->getSubExpr();
5803
Alexey Bataev62cec442014-11-18 10:14:22 +00005804 Expr *X = nullptr;
5805 Expr *V = nullptr;
5806 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005807 Expr *UE = nullptr;
5808 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005809 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005810 // OpenMP [2.12.6, atomic Construct]
5811 // In the next expressions:
5812 // * x and v (as applicable) are both l-value expressions with scalar type.
5813 // * During the execution of an atomic region, multiple syntactic
5814 // occurrences of x must designate the same storage location.
5815 // * Neither of v and expr (as applicable) may access the storage location
5816 // designated by x.
5817 // * Neither of x and expr (as applicable) may access the storage location
5818 // designated by v.
5819 // * expr is an expression with scalar type.
5820 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5821 // * binop, binop=, ++, and -- are not overloaded operators.
5822 // * The expression x binop expr must be numerically equivalent to x binop
5823 // (expr). This requirement is satisfied if the operators in expr have
5824 // precedence greater than binop, or by using parentheses around expr or
5825 // subexpressions of expr.
5826 // * The expression expr binop x must be numerically equivalent to (expr)
5827 // binop x. This requirement is satisfied if the operators in expr have
5828 // precedence equal to or greater than binop, or by using parentheses around
5829 // expr or subexpressions of expr.
5830 // * For forms that allow multiple occurrences of x, the number of times
5831 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005832 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005833 enum {
5834 NotAnExpression,
5835 NotAnAssignmentOp,
5836 NotAScalarType,
5837 NotAnLValue,
5838 NoError
5839 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005840 SourceLocation ErrorLoc, NoteLoc;
5841 SourceRange ErrorRange, NoteRange;
5842 // If clause is read:
5843 // v = x;
5844 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5845 auto AtomicBinOp =
5846 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5847 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5848 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5849 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5850 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5851 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5852 if (!X->isLValue() || !V->isLValue()) {
5853 auto NotLValueExpr = X->isLValue() ? V : X;
5854 ErrorFound = NotAnLValue;
5855 ErrorLoc = AtomicBinOp->getExprLoc();
5856 ErrorRange = AtomicBinOp->getSourceRange();
5857 NoteLoc = NotLValueExpr->getExprLoc();
5858 NoteRange = NotLValueExpr->getSourceRange();
5859 }
5860 } else if (!X->isInstantiationDependent() ||
5861 !V->isInstantiationDependent()) {
5862 auto NotScalarExpr =
5863 (X->isInstantiationDependent() || X->getType()->isScalarType())
5864 ? V
5865 : X;
5866 ErrorFound = NotAScalarType;
5867 ErrorLoc = AtomicBinOp->getExprLoc();
5868 ErrorRange = AtomicBinOp->getSourceRange();
5869 NoteLoc = NotScalarExpr->getExprLoc();
5870 NoteRange = NotScalarExpr->getSourceRange();
5871 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005872 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005873 ErrorFound = NotAnAssignmentOp;
5874 ErrorLoc = AtomicBody->getExprLoc();
5875 ErrorRange = AtomicBody->getSourceRange();
5876 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5877 : AtomicBody->getExprLoc();
5878 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5879 : AtomicBody->getSourceRange();
5880 }
5881 } else {
5882 ErrorFound = NotAnExpression;
5883 NoteLoc = ErrorLoc = Body->getLocStart();
5884 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005885 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005886 if (ErrorFound != NoError) {
5887 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5888 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005889 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5890 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005891 return StmtError();
5892 } else if (CurContext->isDependentContext())
5893 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005894 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005895 enum {
5896 NotAnExpression,
5897 NotAnAssignmentOp,
5898 NotAScalarType,
5899 NotAnLValue,
5900 NoError
5901 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005902 SourceLocation ErrorLoc, NoteLoc;
5903 SourceRange ErrorRange, NoteRange;
5904 // If clause is write:
5905 // x = expr;
5906 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5907 auto AtomicBinOp =
5908 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5909 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005910 X = AtomicBinOp->getLHS();
5911 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005912 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5913 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5914 if (!X->isLValue()) {
5915 ErrorFound = NotAnLValue;
5916 ErrorLoc = AtomicBinOp->getExprLoc();
5917 ErrorRange = AtomicBinOp->getSourceRange();
5918 NoteLoc = X->getExprLoc();
5919 NoteRange = X->getSourceRange();
5920 }
5921 } else if (!X->isInstantiationDependent() ||
5922 !E->isInstantiationDependent()) {
5923 auto NotScalarExpr =
5924 (X->isInstantiationDependent() || X->getType()->isScalarType())
5925 ? E
5926 : X;
5927 ErrorFound = NotAScalarType;
5928 ErrorLoc = AtomicBinOp->getExprLoc();
5929 ErrorRange = AtomicBinOp->getSourceRange();
5930 NoteLoc = NotScalarExpr->getExprLoc();
5931 NoteRange = NotScalarExpr->getSourceRange();
5932 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005933 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005934 ErrorFound = NotAnAssignmentOp;
5935 ErrorLoc = AtomicBody->getExprLoc();
5936 ErrorRange = AtomicBody->getSourceRange();
5937 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5938 : AtomicBody->getExprLoc();
5939 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5940 : AtomicBody->getSourceRange();
5941 }
5942 } else {
5943 ErrorFound = NotAnExpression;
5944 NoteLoc = ErrorLoc = Body->getLocStart();
5945 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005946 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005947 if (ErrorFound != NoError) {
5948 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5949 << ErrorRange;
5950 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5951 << NoteRange;
5952 return StmtError();
5953 } else if (CurContext->isDependentContext())
5954 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005955 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005956 // If clause is update:
5957 // x++;
5958 // x--;
5959 // ++x;
5960 // --x;
5961 // x binop= expr;
5962 // x = x binop expr;
5963 // x = expr binop x;
5964 OpenMPAtomicUpdateChecker Checker(*this);
5965 if (Checker.checkStatement(
5966 Body, (AtomicKind == OMPC_update)
5967 ? diag::err_omp_atomic_update_not_expression_statement
5968 : diag::err_omp_atomic_not_expression_statement,
5969 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005970 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005971 if (!CurContext->isDependentContext()) {
5972 E = Checker.getExpr();
5973 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005974 UE = Checker.getUpdateExpr();
5975 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005976 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005977 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005978 enum {
5979 NotAnAssignmentOp,
5980 NotACompoundStatement,
5981 NotTwoSubstatements,
5982 NotASpecificExpression,
5983 NoError
5984 } ErrorFound = NoError;
5985 SourceLocation ErrorLoc, NoteLoc;
5986 SourceRange ErrorRange, NoteRange;
5987 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5988 // If clause is a capture:
5989 // v = x++;
5990 // v = x--;
5991 // v = ++x;
5992 // v = --x;
5993 // v = x binop= expr;
5994 // v = x = x binop expr;
5995 // v = x = expr binop x;
5996 auto *AtomicBinOp =
5997 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5998 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5999 V = AtomicBinOp->getLHS();
6000 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6001 OpenMPAtomicUpdateChecker Checker(*this);
6002 if (Checker.checkStatement(
6003 Body, diag::err_omp_atomic_capture_not_expression_statement,
6004 diag::note_omp_atomic_update))
6005 return StmtError();
6006 E = Checker.getExpr();
6007 X = Checker.getX();
6008 UE = Checker.getUpdateExpr();
6009 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6010 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006011 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006012 ErrorLoc = AtomicBody->getExprLoc();
6013 ErrorRange = AtomicBody->getSourceRange();
6014 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6015 : AtomicBody->getExprLoc();
6016 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6017 : AtomicBody->getSourceRange();
6018 ErrorFound = NotAnAssignmentOp;
6019 }
6020 if (ErrorFound != NoError) {
6021 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6022 << ErrorRange;
6023 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6024 return StmtError();
6025 } else if (CurContext->isDependentContext()) {
6026 UE = V = E = X = nullptr;
6027 }
6028 } else {
6029 // If clause is a capture:
6030 // { v = x; x = expr; }
6031 // { v = x; x++; }
6032 // { v = x; x--; }
6033 // { v = x; ++x; }
6034 // { v = x; --x; }
6035 // { v = x; x binop= expr; }
6036 // { v = x; x = x binop expr; }
6037 // { v = x; x = expr binop x; }
6038 // { x++; v = x; }
6039 // { x--; v = x; }
6040 // { ++x; v = x; }
6041 // { --x; v = x; }
6042 // { x binop= expr; v = x; }
6043 // { x = x binop expr; v = x; }
6044 // { x = expr binop x; v = x; }
6045 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6046 // Check that this is { expr1; expr2; }
6047 if (CS->size() == 2) {
6048 auto *First = CS->body_front();
6049 auto *Second = CS->body_back();
6050 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6051 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6052 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6053 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6054 // Need to find what subexpression is 'v' and what is 'x'.
6055 OpenMPAtomicUpdateChecker Checker(*this);
6056 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6057 BinaryOperator *BinOp = nullptr;
6058 if (IsUpdateExprFound) {
6059 BinOp = dyn_cast<BinaryOperator>(First);
6060 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6061 }
6062 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6063 // { v = x; x++; }
6064 // { v = x; x--; }
6065 // { v = x; ++x; }
6066 // { v = x; --x; }
6067 // { v = x; x binop= expr; }
6068 // { v = x; x = x binop expr; }
6069 // { v = x; x = expr binop x; }
6070 // Check that the first expression has form v = x.
6071 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6072 llvm::FoldingSetNodeID XId, PossibleXId;
6073 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6074 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6075 IsUpdateExprFound = XId == PossibleXId;
6076 if (IsUpdateExprFound) {
6077 V = BinOp->getLHS();
6078 X = Checker.getX();
6079 E = Checker.getExpr();
6080 UE = Checker.getUpdateExpr();
6081 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006082 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006083 }
6084 }
6085 if (!IsUpdateExprFound) {
6086 IsUpdateExprFound = !Checker.checkStatement(First);
6087 BinOp = nullptr;
6088 if (IsUpdateExprFound) {
6089 BinOp = dyn_cast<BinaryOperator>(Second);
6090 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6091 }
6092 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6093 // { x++; v = x; }
6094 // { x--; v = x; }
6095 // { ++x; v = x; }
6096 // { --x; v = x; }
6097 // { x binop= expr; v = x; }
6098 // { x = x binop expr; v = x; }
6099 // { x = expr binop x; v = x; }
6100 // Check that the second expression has form v = x.
6101 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6102 llvm::FoldingSetNodeID XId, PossibleXId;
6103 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6104 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6105 IsUpdateExprFound = XId == PossibleXId;
6106 if (IsUpdateExprFound) {
6107 V = BinOp->getLHS();
6108 X = Checker.getX();
6109 E = Checker.getExpr();
6110 UE = Checker.getUpdateExpr();
6111 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006112 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006113 }
6114 }
6115 }
6116 if (!IsUpdateExprFound) {
6117 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006118 auto *FirstExpr = dyn_cast<Expr>(First);
6119 auto *SecondExpr = dyn_cast<Expr>(Second);
6120 if (!FirstExpr || !SecondExpr ||
6121 !(FirstExpr->isInstantiationDependent() ||
6122 SecondExpr->isInstantiationDependent())) {
6123 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6124 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006125 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006126 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6127 : First->getLocStart();
6128 NoteRange = ErrorRange = FirstBinOp
6129 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006130 : SourceRange(ErrorLoc, ErrorLoc);
6131 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006132 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6133 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6134 ErrorFound = NotAnAssignmentOp;
6135 NoteLoc = ErrorLoc = SecondBinOp
6136 ? SecondBinOp->getOperatorLoc()
6137 : Second->getLocStart();
6138 NoteRange = ErrorRange =
6139 SecondBinOp ? SecondBinOp->getSourceRange()
6140 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006141 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006142 auto *PossibleXRHSInFirst =
6143 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6144 auto *PossibleXLHSInSecond =
6145 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6146 llvm::FoldingSetNodeID X1Id, X2Id;
6147 PossibleXRHSInFirst->Profile(X1Id, Context,
6148 /*Canonical=*/true);
6149 PossibleXLHSInSecond->Profile(X2Id, Context,
6150 /*Canonical=*/true);
6151 IsUpdateExprFound = X1Id == X2Id;
6152 if (IsUpdateExprFound) {
6153 V = FirstBinOp->getLHS();
6154 X = SecondBinOp->getLHS();
6155 E = SecondBinOp->getRHS();
6156 UE = nullptr;
6157 IsXLHSInRHSPart = false;
6158 IsPostfixUpdate = true;
6159 } else {
6160 ErrorFound = NotASpecificExpression;
6161 ErrorLoc = FirstBinOp->getExprLoc();
6162 ErrorRange = FirstBinOp->getSourceRange();
6163 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6164 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6165 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006166 }
6167 }
6168 }
6169 }
6170 } else {
6171 NoteLoc = ErrorLoc = Body->getLocStart();
6172 NoteRange = ErrorRange =
6173 SourceRange(Body->getLocStart(), Body->getLocStart());
6174 ErrorFound = NotTwoSubstatements;
6175 }
6176 } else {
6177 NoteLoc = ErrorLoc = Body->getLocStart();
6178 NoteRange = ErrorRange =
6179 SourceRange(Body->getLocStart(), Body->getLocStart());
6180 ErrorFound = NotACompoundStatement;
6181 }
6182 if (ErrorFound != NoError) {
6183 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6184 << ErrorRange;
6185 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6186 return StmtError();
6187 } else if (CurContext->isDependentContext()) {
6188 UE = V = E = X = nullptr;
6189 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006190 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006191 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006192
6193 getCurFunction()->setHasBranchProtectedScope();
6194
Alexey Bataev62cec442014-11-18 10:14:22 +00006195 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006196 X, V, E, UE, IsXLHSInRHSPart,
6197 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006198}
6199
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006200StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6201 Stmt *AStmt,
6202 SourceLocation StartLoc,
6203 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006204 if (!AStmt)
6205 return StmtError();
6206
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006207 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6208 // 1.2.2 OpenMP Language Terminology
6209 // Structured block - An executable statement with a single entry at the
6210 // top and a single exit at the bottom.
6211 // The point of exit cannot be a branch out of the structured block.
6212 // longjmp() and throw() must not violate the entry/exit criteria.
6213 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006214
Alexey Bataev13314bf2014-10-09 04:18:56 +00006215 // OpenMP [2.16, Nesting of Regions]
6216 // If specified, a teams construct must be contained within a target
6217 // construct. That target construct must contain no statements or directives
6218 // outside of the teams construct.
6219 if (DSAStack->hasInnerTeamsRegion()) {
6220 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6221 bool OMPTeamsFound = true;
6222 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6223 auto I = CS->body_begin();
6224 while (I != CS->body_end()) {
6225 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6226 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6227 OMPTeamsFound = false;
6228 break;
6229 }
6230 ++I;
6231 }
6232 assert(I != CS->body_end() && "Not found statement");
6233 S = *I;
6234 }
6235 if (!OMPTeamsFound) {
6236 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6237 Diag(DSAStack->getInnerTeamsRegionLoc(),
6238 diag::note_omp_nested_teams_construct_here);
6239 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6240 << isa<OMPExecutableDirective>(S);
6241 return StmtError();
6242 }
6243 }
6244
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006245 getCurFunction()->setHasBranchProtectedScope();
6246
6247 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6248}
6249
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006250StmtResult
6251Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6252 Stmt *AStmt, SourceLocation StartLoc,
6253 SourceLocation EndLoc) {
6254 if (!AStmt)
6255 return StmtError();
6256
6257 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6258 // 1.2.2 OpenMP Language Terminology
6259 // Structured block - An executable statement with a single entry at the
6260 // top and a single exit at the bottom.
6261 // The point of exit cannot be a branch out of the structured block.
6262 // longjmp() and throw() must not violate the entry/exit criteria.
6263 CS->getCapturedDecl()->setNothrow();
6264
6265 getCurFunction()->setHasBranchProtectedScope();
6266
6267 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6268 AStmt);
6269}
6270
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006271StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6272 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6273 SourceLocation EndLoc,
6274 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6275 if (!AStmt)
6276 return StmtError();
6277
6278 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6279 // 1.2.2 OpenMP Language Terminology
6280 // Structured block - An executable statement with a single entry at the
6281 // top and a single exit at the bottom.
6282 // The point of exit cannot be a branch out of the structured block.
6283 // longjmp() and throw() must not violate the entry/exit criteria.
6284 CS->getCapturedDecl()->setNothrow();
6285
6286 OMPLoopDirective::HelperExprs B;
6287 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6288 // define the nested loops number.
6289 unsigned NestedLoopCount =
6290 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6291 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6292 VarsWithImplicitDSA, B);
6293 if (NestedLoopCount == 0)
6294 return StmtError();
6295
6296 assert((CurContext->isDependentContext() || B.builtAll()) &&
6297 "omp target parallel for loop exprs were not built");
6298
6299 if (!CurContext->isDependentContext()) {
6300 // Finalize the clauses that need pre-built expressions for CodeGen.
6301 for (auto C : Clauses) {
6302 if (auto LC = dyn_cast<OMPLinearClause>(C))
6303 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006304 B.NumIterations, *this, CurScope,
6305 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006306 return StmtError();
6307 }
6308 }
6309
6310 getCurFunction()->setHasBranchProtectedScope();
6311 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6312 NestedLoopCount, Clauses, AStmt,
6313 B, DSAStack->isCancelRegion());
6314}
6315
Samuel Antaodf67fc42016-01-19 19:15:56 +00006316/// \brief Check for existence of a map clause in the list of clauses.
6317static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6318 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6319 I != E; ++I) {
6320 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6321 return true;
6322 }
6323 }
6324
6325 return false;
6326}
6327
Michael Wong65f367f2015-07-21 13:44:28 +00006328StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6329 Stmt *AStmt,
6330 SourceLocation StartLoc,
6331 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006332 if (!AStmt)
6333 return StmtError();
6334
6335 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6336
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006337 // OpenMP [2.10.1, Restrictions, p. 97]
6338 // At least one map clause must appear on the directive.
6339 if (!HasMapClause(Clauses)) {
6340 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6341 getOpenMPDirectiveName(OMPD_target_data);
6342 return StmtError();
6343 }
6344
Michael Wong65f367f2015-07-21 13:44:28 +00006345 getCurFunction()->setHasBranchProtectedScope();
6346
6347 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6348 AStmt);
6349}
6350
Samuel Antaodf67fc42016-01-19 19:15:56 +00006351StmtResult
6352Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6353 SourceLocation StartLoc,
6354 SourceLocation EndLoc) {
6355 // OpenMP [2.10.2, Restrictions, p. 99]
6356 // At least one map clause must appear on the directive.
6357 if (!HasMapClause(Clauses)) {
6358 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6359 << getOpenMPDirectiveName(OMPD_target_enter_data);
6360 return StmtError();
6361 }
6362
6363 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6364 Clauses);
6365}
6366
Samuel Antao72590762016-01-19 20:04:50 +00006367StmtResult
6368Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6369 SourceLocation StartLoc,
6370 SourceLocation EndLoc) {
6371 // OpenMP [2.10.3, Restrictions, p. 102]
6372 // At least one map clause must appear on the directive.
6373 if (!HasMapClause(Clauses)) {
6374 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6375 << getOpenMPDirectiveName(OMPD_target_exit_data);
6376 return StmtError();
6377 }
6378
6379 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6380}
6381
Alexey Bataev13314bf2014-10-09 04:18:56 +00006382StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6383 Stmt *AStmt, SourceLocation StartLoc,
6384 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006385 if (!AStmt)
6386 return StmtError();
6387
Alexey Bataev13314bf2014-10-09 04:18:56 +00006388 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6389 // 1.2.2 OpenMP Language Terminology
6390 // Structured block - An executable statement with a single entry at the
6391 // top and a single exit at the bottom.
6392 // The point of exit cannot be a branch out of the structured block.
6393 // longjmp() and throw() must not violate the entry/exit criteria.
6394 CS->getCapturedDecl()->setNothrow();
6395
6396 getCurFunction()->setHasBranchProtectedScope();
6397
6398 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6399}
6400
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006401StmtResult
6402Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6403 SourceLocation EndLoc,
6404 OpenMPDirectiveKind CancelRegion) {
6405 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6406 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6407 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6408 << getOpenMPDirectiveName(CancelRegion);
6409 return StmtError();
6410 }
6411 if (DSAStack->isParentNowaitRegion()) {
6412 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6413 return StmtError();
6414 }
6415 if (DSAStack->isParentOrderedRegion()) {
6416 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6417 return StmtError();
6418 }
6419 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6420 CancelRegion);
6421}
6422
Alexey Bataev87933c72015-09-18 08:07:34 +00006423StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6424 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006425 SourceLocation EndLoc,
6426 OpenMPDirectiveKind CancelRegion) {
6427 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6428 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6429 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6430 << getOpenMPDirectiveName(CancelRegion);
6431 return StmtError();
6432 }
6433 if (DSAStack->isParentNowaitRegion()) {
6434 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6435 return StmtError();
6436 }
6437 if (DSAStack->isParentOrderedRegion()) {
6438 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6439 return StmtError();
6440 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006441 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006442 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6443 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006444}
6445
Alexey Bataev382967a2015-12-08 12:06:20 +00006446static bool checkGrainsizeNumTasksClauses(Sema &S,
6447 ArrayRef<OMPClause *> Clauses) {
6448 OMPClause *PrevClause = nullptr;
6449 bool ErrorFound = false;
6450 for (auto *C : Clauses) {
6451 if (C->getClauseKind() == OMPC_grainsize ||
6452 C->getClauseKind() == OMPC_num_tasks) {
6453 if (!PrevClause)
6454 PrevClause = C;
6455 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6456 S.Diag(C->getLocStart(),
6457 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6458 << getOpenMPClauseName(C->getClauseKind())
6459 << getOpenMPClauseName(PrevClause->getClauseKind());
6460 S.Diag(PrevClause->getLocStart(),
6461 diag::note_omp_previous_grainsize_num_tasks)
6462 << getOpenMPClauseName(PrevClause->getClauseKind());
6463 ErrorFound = true;
6464 }
6465 }
6466 }
6467 return ErrorFound;
6468}
6469
Alexey Bataev49f6e782015-12-01 04:18:41 +00006470StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6471 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6472 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006473 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006474 if (!AStmt)
6475 return StmtError();
6476
6477 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6478 OMPLoopDirective::HelperExprs B;
6479 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6480 // define the nested loops number.
6481 unsigned NestedLoopCount =
6482 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006483 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006484 VarsWithImplicitDSA, B);
6485 if (NestedLoopCount == 0)
6486 return StmtError();
6487
6488 assert((CurContext->isDependentContext() || B.builtAll()) &&
6489 "omp for loop exprs were not built");
6490
Alexey Bataev382967a2015-12-08 12:06:20 +00006491 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6492 // The grainsize clause and num_tasks clause are mutually exclusive and may
6493 // not appear on the same taskloop directive.
6494 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6495 return StmtError();
6496
Alexey Bataev49f6e782015-12-01 04:18:41 +00006497 getCurFunction()->setHasBranchProtectedScope();
6498 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6499 NestedLoopCount, Clauses, AStmt, B);
6500}
6501
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006502StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6503 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6504 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006505 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006506 if (!AStmt)
6507 return StmtError();
6508
6509 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6510 OMPLoopDirective::HelperExprs B;
6511 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6512 // define the nested loops number.
6513 unsigned NestedLoopCount =
6514 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6515 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6516 VarsWithImplicitDSA, B);
6517 if (NestedLoopCount == 0)
6518 return StmtError();
6519
6520 assert((CurContext->isDependentContext() || B.builtAll()) &&
6521 "omp for loop exprs were not built");
6522
Alexey Bataev5a3af132016-03-29 08:58:54 +00006523 if (!CurContext->isDependentContext()) {
6524 // Finalize the clauses that need pre-built expressions for CodeGen.
6525 for (auto C : Clauses) {
6526 if (auto LC = dyn_cast<OMPLinearClause>(C))
6527 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006528 B.NumIterations, *this, CurScope,
6529 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006530 return StmtError();
6531 }
6532 }
6533
Alexey Bataev382967a2015-12-08 12:06:20 +00006534 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6535 // The grainsize clause and num_tasks clause are mutually exclusive and may
6536 // not appear on the same taskloop directive.
6537 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6538 return StmtError();
6539
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006540 getCurFunction()->setHasBranchProtectedScope();
6541 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6542 NestedLoopCount, Clauses, AStmt, B);
6543}
6544
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006545StmtResult Sema::ActOnOpenMPDistributeDirective(
6546 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6547 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006548 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006549 if (!AStmt)
6550 return StmtError();
6551
6552 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6553 OMPLoopDirective::HelperExprs B;
6554 // In presence of clause 'collapse' with number of loops, it will
6555 // define the nested loops number.
6556 unsigned NestedLoopCount =
6557 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6558 nullptr /*ordered not a clause on distribute*/, AStmt,
6559 *this, *DSAStack, VarsWithImplicitDSA, B);
6560 if (NestedLoopCount == 0)
6561 return StmtError();
6562
6563 assert((CurContext->isDependentContext() || B.builtAll()) &&
6564 "omp for loop exprs were not built");
6565
6566 getCurFunction()->setHasBranchProtectedScope();
6567 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6568 NestedLoopCount, Clauses, AStmt, B);
6569}
6570
Alexey Bataeved09d242014-05-28 05:53:51 +00006571OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006572 SourceLocation StartLoc,
6573 SourceLocation LParenLoc,
6574 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006575 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006576 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006577 case OMPC_final:
6578 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6579 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006580 case OMPC_num_threads:
6581 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6582 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006583 case OMPC_safelen:
6584 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6585 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006586 case OMPC_simdlen:
6587 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6588 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006589 case OMPC_collapse:
6590 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6591 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006592 case OMPC_ordered:
6593 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6594 break;
Michael Wonge710d542015-08-07 16:16:36 +00006595 case OMPC_device:
6596 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6597 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006598 case OMPC_num_teams:
6599 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6600 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006601 case OMPC_thread_limit:
6602 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6603 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006604 case OMPC_priority:
6605 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6606 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006607 case OMPC_grainsize:
6608 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6609 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006610 case OMPC_num_tasks:
6611 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6612 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006613 case OMPC_hint:
6614 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6615 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006616 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006617 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006618 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006619 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006620 case OMPC_private:
6621 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006622 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006623 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006624 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006625 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006626 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006627 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006628 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006629 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006630 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006631 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006632 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006633 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006634 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006635 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006636 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006637 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006638 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006639 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006640 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006641 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006642 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006643 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006644 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006645 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006646 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006647 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006648 llvm_unreachable("Clause is not allowed.");
6649 }
6650 return Res;
6651}
6652
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006653OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6654 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006655 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006656 SourceLocation NameModifierLoc,
6657 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006658 SourceLocation EndLoc) {
6659 Expr *ValExpr = Condition;
6660 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6661 !Condition->isInstantiationDependent() &&
6662 !Condition->containsUnexpandedParameterPack()) {
6663 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006664 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006665 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006666 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006667
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006668 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006669 }
6670
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006671 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6672 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006673}
6674
Alexey Bataev3778b602014-07-17 07:32:53 +00006675OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6676 SourceLocation StartLoc,
6677 SourceLocation LParenLoc,
6678 SourceLocation EndLoc) {
6679 Expr *ValExpr = Condition;
6680 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6681 !Condition->isInstantiationDependent() &&
6682 !Condition->containsUnexpandedParameterPack()) {
6683 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6684 Condition->getExprLoc(), Condition);
6685 if (Val.isInvalid())
6686 return nullptr;
6687
6688 ValExpr = Val.get();
6689 }
6690
6691 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6692}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006693ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6694 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006695 if (!Op)
6696 return ExprError();
6697
6698 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6699 public:
6700 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006701 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006702 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6703 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006704 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6705 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006706 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6707 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006708 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6709 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006710 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6711 QualType T,
6712 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006713 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6714 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006715 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6716 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006717 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006718 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006719 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006720 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6721 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006722 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6723 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006724 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6725 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006726 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006727 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006728 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006729 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6730 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006731 llvm_unreachable("conversion functions are permitted");
6732 }
6733 } ConvertDiagnoser;
6734 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6735}
6736
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006737static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006738 OpenMPClauseKind CKind,
6739 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006740 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6741 !ValExpr->isInstantiationDependent()) {
6742 SourceLocation Loc = ValExpr->getExprLoc();
6743 ExprResult Value =
6744 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6745 if (Value.isInvalid())
6746 return false;
6747
6748 ValExpr = Value.get();
6749 // The expression must evaluate to a non-negative integer value.
6750 llvm::APSInt Result;
6751 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006752 Result.isSigned() &&
6753 !((!StrictlyPositive && Result.isNonNegative()) ||
6754 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006755 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006756 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6757 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006758 return false;
6759 }
6760 }
6761 return true;
6762}
6763
Alexey Bataev568a8332014-03-06 06:15:19 +00006764OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6765 SourceLocation StartLoc,
6766 SourceLocation LParenLoc,
6767 SourceLocation EndLoc) {
6768 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006769
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006770 // OpenMP [2.5, Restrictions]
6771 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006772 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6773 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006774 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006775
Alexey Bataeved09d242014-05-28 05:53:51 +00006776 return new (Context)
6777 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006778}
6779
Alexey Bataev62c87d22014-03-21 04:51:18 +00006780ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006781 OpenMPClauseKind CKind,
6782 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006783 if (!E)
6784 return ExprError();
6785 if (E->isValueDependent() || E->isTypeDependent() ||
6786 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006787 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006788 llvm::APSInt Result;
6789 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6790 if (ICE.isInvalid())
6791 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006792 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6793 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006794 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006795 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6796 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006797 return ExprError();
6798 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006799 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6800 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6801 << E->getSourceRange();
6802 return ExprError();
6803 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006804 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6805 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006806 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006807 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006808 return ICE;
6809}
6810
6811OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6812 SourceLocation LParenLoc,
6813 SourceLocation EndLoc) {
6814 // OpenMP [2.8.1, simd construct, Description]
6815 // The parameter of the safelen clause must be a constant
6816 // positive integer expression.
6817 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6818 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006819 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006820 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006821 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006822}
6823
Alexey Bataev66b15b52015-08-21 11:14:16 +00006824OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6825 SourceLocation LParenLoc,
6826 SourceLocation EndLoc) {
6827 // OpenMP [2.8.1, simd construct, Description]
6828 // The parameter of the simdlen clause must be a constant
6829 // positive integer expression.
6830 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6831 if (Simdlen.isInvalid())
6832 return nullptr;
6833 return new (Context)
6834 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6835}
6836
Alexander Musman64d33f12014-06-04 07:53:32 +00006837OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6838 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006839 SourceLocation LParenLoc,
6840 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006841 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006842 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006843 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006844 // The parameter of the collapse clause must be a constant
6845 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006846 ExprResult NumForLoopsResult =
6847 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6848 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006849 return nullptr;
6850 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006851 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006852}
6853
Alexey Bataev10e775f2015-07-30 11:36:16 +00006854OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6855 SourceLocation EndLoc,
6856 SourceLocation LParenLoc,
6857 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006858 // OpenMP [2.7.1, loop construct, Description]
6859 // OpenMP [2.8.1, simd construct, Description]
6860 // OpenMP [2.9.6, distribute construct, Description]
6861 // The parameter of the ordered clause must be a constant
6862 // positive integer expression if any.
6863 if (NumForLoops && LParenLoc.isValid()) {
6864 ExprResult NumForLoopsResult =
6865 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6866 if (NumForLoopsResult.isInvalid())
6867 return nullptr;
6868 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006869 } else
6870 NumForLoops = nullptr;
6871 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006872 return new (Context)
6873 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6874}
6875
Alexey Bataeved09d242014-05-28 05:53:51 +00006876OMPClause *Sema::ActOnOpenMPSimpleClause(
6877 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6878 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006879 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006880 switch (Kind) {
6881 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006882 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006883 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6884 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006885 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006886 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006887 Res = ActOnOpenMPProcBindClause(
6888 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6889 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006890 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006891 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006892 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006893 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006894 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006895 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006896 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006897 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006898 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006899 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006900 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006901 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006902 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006903 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006904 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006905 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006906 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006907 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006908 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006909 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006910 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006911 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006912 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006913 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006914 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006915 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006916 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006917 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006918 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006919 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006920 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006921 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006922 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006923 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006924 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006925 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006926 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006927 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006928 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006929 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006930 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006931 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006932 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006933 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006934 llvm_unreachable("Clause is not allowed.");
6935 }
6936 return Res;
6937}
6938
Alexey Bataev6402bca2015-12-28 07:25:51 +00006939static std::string
6940getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6941 ArrayRef<unsigned> Exclude = llvm::None) {
6942 std::string Values;
6943 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6944 unsigned Skipped = Exclude.size();
6945 auto S = Exclude.begin(), E = Exclude.end();
6946 for (unsigned i = First; i < Last; ++i) {
6947 if (std::find(S, E, i) != E) {
6948 --Skipped;
6949 continue;
6950 }
6951 Values += "'";
6952 Values += getOpenMPSimpleClauseTypeName(K, i);
6953 Values += "'";
6954 if (i == Bound - Skipped)
6955 Values += " or ";
6956 else if (i != Bound + 1 - Skipped)
6957 Values += ", ";
6958 }
6959 return Values;
6960}
6961
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006962OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6963 SourceLocation KindKwLoc,
6964 SourceLocation StartLoc,
6965 SourceLocation LParenLoc,
6966 SourceLocation EndLoc) {
6967 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006968 static_assert(OMPC_DEFAULT_unknown > 0,
6969 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006970 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006971 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6972 /*Last=*/OMPC_DEFAULT_unknown)
6973 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006974 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006975 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006976 switch (Kind) {
6977 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006978 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006979 break;
6980 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006981 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006982 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006983 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006984 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006985 break;
6986 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006987 return new (Context)
6988 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006989}
6990
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006991OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6992 SourceLocation KindKwLoc,
6993 SourceLocation StartLoc,
6994 SourceLocation LParenLoc,
6995 SourceLocation EndLoc) {
6996 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006997 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006998 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6999 /*Last=*/OMPC_PROC_BIND_unknown)
7000 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007001 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007002 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007003 return new (Context)
7004 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007005}
7006
Alexey Bataev56dafe82014-06-20 07:16:17 +00007007OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007008 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007009 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007010 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007011 SourceLocation EndLoc) {
7012 OMPClause *Res = nullptr;
7013 switch (Kind) {
7014 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007015 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7016 assert(Argument.size() == NumberOfElements &&
7017 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007018 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007019 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7020 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7021 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7022 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7023 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007024 break;
7025 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00007026 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7027 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7028 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7029 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007030 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00007031 case OMPC_dist_schedule:
7032 Res = ActOnOpenMPDistScheduleClause(
7033 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7034 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7035 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007036 case OMPC_defaultmap:
7037 enum { Modifier, DefaultmapKind };
7038 Res = ActOnOpenMPDefaultmapClause(
7039 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7040 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7041 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7042 ArgumentLoc[DefaultmapKind], EndLoc);
7043 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007044 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007045 case OMPC_num_threads:
7046 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007047 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007048 case OMPC_collapse:
7049 case OMPC_default:
7050 case OMPC_proc_bind:
7051 case OMPC_private:
7052 case OMPC_firstprivate:
7053 case OMPC_lastprivate:
7054 case OMPC_shared:
7055 case OMPC_reduction:
7056 case OMPC_linear:
7057 case OMPC_aligned:
7058 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007059 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007060 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007061 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007062 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007063 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007064 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007065 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007066 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007067 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007068 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007069 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007070 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007071 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007072 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007073 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007074 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007075 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007076 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007077 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007078 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007079 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007080 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007081 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007082 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007083 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007084 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007085 llvm_unreachable("Clause is not allowed.");
7086 }
7087 return Res;
7088}
7089
Alexey Bataev6402bca2015-12-28 07:25:51 +00007090static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7091 OpenMPScheduleClauseModifier M2,
7092 SourceLocation M1Loc, SourceLocation M2Loc) {
7093 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7094 SmallVector<unsigned, 2> Excluded;
7095 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7096 Excluded.push_back(M2);
7097 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7098 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7099 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7100 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7101 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7102 << getListOfPossibleValues(OMPC_schedule,
7103 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7104 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7105 Excluded)
7106 << getOpenMPClauseName(OMPC_schedule);
7107 return true;
7108 }
7109 return false;
7110}
7111
Alexey Bataev56dafe82014-06-20 07:16:17 +00007112OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007113 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007114 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007115 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7116 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7117 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7118 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7119 return nullptr;
7120 // OpenMP, 2.7.1, Loop Construct, Restrictions
7121 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7122 // but not both.
7123 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7124 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7125 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7126 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7127 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7128 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7129 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7130 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7131 return nullptr;
7132 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007133 if (Kind == OMPC_SCHEDULE_unknown) {
7134 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007135 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7136 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7137 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7138 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7139 Exclude);
7140 } else {
7141 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7142 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007143 }
7144 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7145 << Values << getOpenMPClauseName(OMPC_schedule);
7146 return nullptr;
7147 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007148 // OpenMP, 2.7.1, Loop Construct, Restrictions
7149 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7150 // schedule(guided).
7151 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7152 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7153 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7154 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7155 diag::err_omp_schedule_nonmonotonic_static);
7156 return nullptr;
7157 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007158 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007159 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007160 if (ChunkSize) {
7161 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7162 !ChunkSize->isInstantiationDependent() &&
7163 !ChunkSize->containsUnexpandedParameterPack()) {
7164 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7165 ExprResult Val =
7166 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7167 if (Val.isInvalid())
7168 return nullptr;
7169
7170 ValExpr = Val.get();
7171
7172 // OpenMP [2.7.1, Restrictions]
7173 // chunk_size must be a loop invariant integer expression with a positive
7174 // value.
7175 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007176 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7177 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7178 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007179 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007180 return nullptr;
7181 }
7182 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007183 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7184 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7185 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007186 }
7187 }
7188 }
7189
Alexey Bataev6402bca2015-12-28 07:25:51 +00007190 return new (Context)
7191 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007192 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007193}
7194
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007195OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7196 SourceLocation StartLoc,
7197 SourceLocation EndLoc) {
7198 OMPClause *Res = nullptr;
7199 switch (Kind) {
7200 case OMPC_ordered:
7201 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7202 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007203 case OMPC_nowait:
7204 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7205 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007206 case OMPC_untied:
7207 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7208 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007209 case OMPC_mergeable:
7210 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7211 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007212 case OMPC_read:
7213 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7214 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007215 case OMPC_write:
7216 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7217 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007218 case OMPC_update:
7219 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7220 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007221 case OMPC_capture:
7222 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7223 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007224 case OMPC_seq_cst:
7225 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7226 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007227 case OMPC_threads:
7228 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7229 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007230 case OMPC_simd:
7231 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7232 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007233 case OMPC_nogroup:
7234 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7235 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007236 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007237 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007238 case OMPC_num_threads:
7239 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007240 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007241 case OMPC_collapse:
7242 case OMPC_schedule:
7243 case OMPC_private:
7244 case OMPC_firstprivate:
7245 case OMPC_lastprivate:
7246 case OMPC_shared:
7247 case OMPC_reduction:
7248 case OMPC_linear:
7249 case OMPC_aligned:
7250 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007251 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007252 case OMPC_default:
7253 case OMPC_proc_bind:
7254 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007255 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007256 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007257 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007258 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007259 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007260 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007261 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007262 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007263 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007264 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007265 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007266 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007267 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007268 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007269 llvm_unreachable("Clause is not allowed.");
7270 }
7271 return Res;
7272}
7273
Alexey Bataev236070f2014-06-20 11:19:47 +00007274OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7275 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007276 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007277 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7278}
7279
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007280OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7281 SourceLocation EndLoc) {
7282 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7283}
7284
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007285OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7286 SourceLocation EndLoc) {
7287 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7288}
7289
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007290OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7291 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007292 return new (Context) OMPReadClause(StartLoc, EndLoc);
7293}
7294
Alexey Bataevdea47612014-07-23 07:46:59 +00007295OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7296 SourceLocation EndLoc) {
7297 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7298}
7299
Alexey Bataev67a4f222014-07-23 10:25:33 +00007300OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7301 SourceLocation EndLoc) {
7302 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7303}
7304
Alexey Bataev459dec02014-07-24 06:46:57 +00007305OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7306 SourceLocation EndLoc) {
7307 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7308}
7309
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007310OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7311 SourceLocation EndLoc) {
7312 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7313}
7314
Alexey Bataev346265e2015-09-25 10:37:12 +00007315OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7316 SourceLocation EndLoc) {
7317 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7318}
7319
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007320OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7321 SourceLocation EndLoc) {
7322 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7323}
7324
Alexey Bataevb825de12015-12-07 10:51:44 +00007325OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7326 SourceLocation EndLoc) {
7327 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7328}
7329
Alexey Bataevc5e02582014-06-16 07:08:35 +00007330OMPClause *Sema::ActOnOpenMPVarListClause(
7331 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7332 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7333 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007334 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007335 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7336 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7337 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007338 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007339 switch (Kind) {
7340 case OMPC_private:
7341 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7342 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007343 case OMPC_firstprivate:
7344 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7345 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007346 case OMPC_lastprivate:
7347 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7348 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007349 case OMPC_shared:
7350 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7351 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007352 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007353 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7354 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007355 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007356 case OMPC_linear:
7357 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007358 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007359 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007360 case OMPC_aligned:
7361 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7362 ColonLoc, EndLoc);
7363 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007364 case OMPC_copyin:
7365 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7366 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007367 case OMPC_copyprivate:
7368 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7369 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007370 case OMPC_flush:
7371 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7372 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007373 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007374 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7375 StartLoc, LParenLoc, EndLoc);
7376 break;
7377 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007378 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7379 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7380 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007381 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007382 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007383 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007384 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007385 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007386 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007387 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007388 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007389 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007390 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007391 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007392 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007393 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007394 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007395 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007396 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007397 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007398 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007399 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007400 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007401 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007402 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007403 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007404 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007405 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007406 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007407 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007408 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007409 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007410 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007411 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007412 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007413 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007414 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007415 llvm_unreachable("Clause is not allowed.");
7416 }
7417 return Res;
7418}
7419
Alexey Bataev90c228f2016-02-08 09:29:13 +00007420ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007421 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007422 ExprResult Res = BuildDeclRefExpr(
7423 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7424 if (!Res.isUsable())
7425 return ExprError();
7426 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7427 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7428 if (!Res.isUsable())
7429 return ExprError();
7430 }
7431 if (VK != VK_LValue && Res.get()->isGLValue()) {
7432 Res = DefaultLvalueConversion(Res.get());
7433 if (!Res.isUsable())
7434 return ExprError();
7435 }
7436 return Res;
7437}
7438
Alexey Bataev60da77e2016-02-29 05:54:20 +00007439static std::pair<ValueDecl *, bool>
7440getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7441 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007442 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7443 RefExpr->containsUnexpandedParameterPack())
7444 return std::make_pair(nullptr, true);
7445
Alexey Bataevd985eda2016-02-10 11:29:16 +00007446 // OpenMP [3.1, C/C++]
7447 // A list item is a variable name.
7448 // OpenMP [2.9.3.3, Restrictions, p.1]
7449 // A variable that is part of another variable (as an array or
7450 // structure element) cannot appear in a private clause.
7451 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007452 enum {
7453 NoArrayExpr = -1,
7454 ArraySubscript = 0,
7455 OMPArraySection = 1
7456 } IsArrayExpr = NoArrayExpr;
7457 if (AllowArraySection) {
7458 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7459 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7460 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7461 Base = TempASE->getBase()->IgnoreParenImpCasts();
7462 RefExpr = Base;
7463 IsArrayExpr = ArraySubscript;
7464 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7465 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7466 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7467 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7468 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7469 Base = TempASE->getBase()->IgnoreParenImpCasts();
7470 RefExpr = Base;
7471 IsArrayExpr = OMPArraySection;
7472 }
7473 }
7474 ELoc = RefExpr->getExprLoc();
7475 ERange = RefExpr->getSourceRange();
7476 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007477 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7478 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7479 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7480 (S.getCurrentThisType().isNull() || !ME ||
7481 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7482 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007483 if (IsArrayExpr != NoArrayExpr)
7484 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7485 << ERange;
7486 else {
7487 S.Diag(ELoc,
7488 AllowArraySection
7489 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7490 : diag::err_omp_expected_var_name_member_expr)
7491 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7492 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007493 return std::make_pair(nullptr, false);
7494 }
7495 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7496}
7497
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007498OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7499 SourceLocation StartLoc,
7500 SourceLocation LParenLoc,
7501 SourceLocation EndLoc) {
7502 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007503 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007504 for (auto &RefExpr : VarList) {
7505 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007506 SourceLocation ELoc;
7507 SourceRange ERange;
7508 Expr *SimpleRefExpr = RefExpr;
7509 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007510 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007511 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007512 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007513 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007514 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007515 ValueDecl *D = Res.first;
7516 if (!D)
7517 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007518
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007519 QualType Type = D->getType();
7520 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007521
7522 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7523 // A variable that appears in a private clause must not have an incomplete
7524 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007525 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007526 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007527 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007528
Alexey Bataev758e55e2013-09-06 18:03:48 +00007529 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7530 // in a Construct]
7531 // Variables with the predetermined data-sharing attributes may not be
7532 // listed in data-sharing attributes clauses, except for the cases
7533 // listed below. For these exceptions only, listing a predetermined
7534 // variable in a data-sharing attribute clause is allowed and overrides
7535 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007536 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007537 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007538 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7539 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007540 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007541 continue;
7542 }
7543
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007544 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007545 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007546 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007547 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7548 << getOpenMPClauseName(OMPC_private) << Type
7549 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7550 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007551 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007552 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007553 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007554 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007555 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007556 continue;
7557 }
7558
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007559 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7560 // A list item cannot appear in both a map clause and a data-sharing
7561 // attribute clause on the same construct
7562 if (DSAStack->getCurrentDirective() == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007563 if (DSAStack->checkMappableExprComponentListsForDecl(
7564 VD, /* CurrentRegionOnly = */ true,
7565 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7566 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007567 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7568 << getOpenMPClauseName(OMPC_private)
7569 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7570 ReportOriginalDSA(*this, DSAStack, D, DVar);
7571 continue;
7572 }
7573 }
7574
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007575 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7576 // A variable of class type (or array thereof) that appears in a private
7577 // clause requires an accessible, unambiguous default constructor for the
7578 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007579 // Generate helper private variable and initialize it with the default
7580 // value. The address of the original variable is replaced by the address of
7581 // the new private variable in CodeGen. This new variable is not added to
7582 // IdResolver, so the code in the OpenMP region uses original variable for
7583 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007584 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007585 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7586 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007587 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007588 if (VDPrivate->isInvalidDecl())
7589 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007590 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007591 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007592
Alexey Bataev90c228f2016-02-08 09:29:13 +00007593 DeclRefExpr *Ref = nullptr;
7594 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007595 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007596 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7597 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007598 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007599 }
7600
Alexey Bataeved09d242014-05-28 05:53:51 +00007601 if (Vars.empty())
7602 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007603
Alexey Bataev03b340a2014-10-21 03:16:40 +00007604 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7605 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007606}
7607
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007608namespace {
7609class DiagsUninitializedSeveretyRAII {
7610private:
7611 DiagnosticsEngine &Diags;
7612 SourceLocation SavedLoc;
7613 bool IsIgnored;
7614
7615public:
7616 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7617 bool IsIgnored)
7618 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7619 if (!IsIgnored) {
7620 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7621 /*Map*/ diag::Severity::Ignored, Loc);
7622 }
7623 }
7624 ~DiagsUninitializedSeveretyRAII() {
7625 if (!IsIgnored)
7626 Diags.popMappings(SavedLoc);
7627 }
7628};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007629}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007630
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007631OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7632 SourceLocation StartLoc,
7633 SourceLocation LParenLoc,
7634 SourceLocation EndLoc) {
7635 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007636 SmallVector<Expr *, 8> PrivateCopies;
7637 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007638 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007639 bool IsImplicitClause =
7640 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7641 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7642
Alexey Bataeved09d242014-05-28 05:53:51 +00007643 for (auto &RefExpr : VarList) {
7644 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007645 SourceLocation ELoc;
7646 SourceRange ERange;
7647 Expr *SimpleRefExpr = RefExpr;
7648 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007649 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007650 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007651 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007652 PrivateCopies.push_back(nullptr);
7653 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007654 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007655 ValueDecl *D = Res.first;
7656 if (!D)
7657 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007658
Alexey Bataev60da77e2016-02-29 05:54:20 +00007659 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007660 QualType Type = D->getType();
7661 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007662
7663 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7664 // A variable that appears in a private clause must not have an incomplete
7665 // type or a reference type.
7666 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007667 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007668 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007669 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007670
7671 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7672 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007673 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007674 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007675 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007676
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007677 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007678 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007679 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007680 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007681 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007682 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007683 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7684 // A list item that specifies a given variable may not appear in more
7685 // than one clause on the same directive, except that a variable may be
7686 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007687 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007688 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007689 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007690 << getOpenMPClauseName(DVar.CKind)
7691 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007692 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007693 continue;
7694 }
7695
7696 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7697 // in a Construct]
7698 // Variables with the predetermined data-sharing attributes may not be
7699 // listed in data-sharing attributes clauses, except for the cases
7700 // listed below. For these exceptions only, listing a predetermined
7701 // variable in a data-sharing attribute clause is allowed and overrides
7702 // the variable's predetermined data-sharing attributes.
7703 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7704 // in a Construct, C/C++, p.2]
7705 // Variables with const-qualified type having no mutable member may be
7706 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007707 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007708 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7709 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007710 << getOpenMPClauseName(DVar.CKind)
7711 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007712 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007713 continue;
7714 }
7715
Alexey Bataevf29276e2014-06-18 04:14:57 +00007716 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007717 // OpenMP [2.9.3.4, Restrictions, p.2]
7718 // A list item that is private within a parallel region must not appear
7719 // in a firstprivate clause on a worksharing construct if any of the
7720 // worksharing regions arising from the worksharing construct ever bind
7721 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007722 if (isOpenMPWorksharingDirective(CurrDir) &&
7723 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007724 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007725 if (DVar.CKind != OMPC_shared &&
7726 (isOpenMPParallelDirective(DVar.DKind) ||
7727 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007728 Diag(ELoc, diag::err_omp_required_access)
7729 << getOpenMPClauseName(OMPC_firstprivate)
7730 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007731 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007732 continue;
7733 }
7734 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007735 // OpenMP [2.9.3.4, Restrictions, p.3]
7736 // A list item that appears in a reduction clause of a parallel construct
7737 // must not appear in a firstprivate clause on a worksharing or task
7738 // construct if any of the worksharing or task regions arising from the
7739 // worksharing or task construct ever bind to any of the parallel regions
7740 // arising from the parallel construct.
7741 // OpenMP [2.9.3.4, Restrictions, p.4]
7742 // A list item that appears in a reduction clause in worksharing
7743 // construct must not appear in a firstprivate clause in a task construct
7744 // encountered during execution of any of the worksharing regions arising
7745 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007746 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007747 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007748 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007749 [](OpenMPDirectiveKind K) -> bool {
7750 return isOpenMPParallelDirective(K) ||
7751 isOpenMPWorksharingDirective(K);
7752 },
7753 false);
7754 if (DVar.CKind == OMPC_reduction &&
7755 (isOpenMPParallelDirective(DVar.DKind) ||
7756 isOpenMPWorksharingDirective(DVar.DKind))) {
7757 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7758 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007759 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007760 continue;
7761 }
7762 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007763
7764 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7765 // A list item that is private within a teams region must not appear in a
7766 // firstprivate clause on a distribute construct if any of the distribute
7767 // regions arising from the distribute construct ever bind to any of the
7768 // teams regions arising from the teams construct.
7769 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7770 // A list item that appears in a reduction clause of a teams construct
7771 // must not appear in a firstprivate clause on a distribute construct if
7772 // any of the distribute regions arising from the distribute construct
7773 // ever bind to any of the teams regions arising from the teams construct.
7774 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7775 // A list item may appear in a firstprivate or lastprivate clause but not
7776 // both.
7777 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007778 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007779 [](OpenMPDirectiveKind K) -> bool {
7780 return isOpenMPTeamsDirective(K);
7781 },
7782 false);
7783 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7784 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007785 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007786 continue;
7787 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007788 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007789 [](OpenMPDirectiveKind K) -> bool {
7790 return isOpenMPTeamsDirective(K);
7791 },
7792 false);
7793 if (DVar.CKind == OMPC_reduction &&
7794 isOpenMPTeamsDirective(DVar.DKind)) {
7795 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007796 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007797 continue;
7798 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007799 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007800 if (DVar.CKind == OMPC_lastprivate) {
7801 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007802 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007803 continue;
7804 }
7805 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007806 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7807 // A list item cannot appear in both a map clause and a data-sharing
7808 // attribute clause on the same construct
7809 if (CurrDir == OMPD_target) {
Samuel Antao90927002016-04-26 14:54:23 +00007810 if (DSAStack->checkMappableExprComponentListsForDecl(
7811 VD, /* CurrentRegionOnly = */ true,
7812 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7813 -> bool { return true; })) {
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007814 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7815 << getOpenMPClauseName(OMPC_firstprivate)
7816 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7817 ReportOriginalDSA(*this, DSAStack, D, DVar);
7818 continue;
7819 }
7820 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007821 }
7822
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007823 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007824 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007825 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007826 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7827 << getOpenMPClauseName(OMPC_firstprivate) << Type
7828 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7829 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007830 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007831 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007832 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007833 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007834 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007835 continue;
7836 }
7837
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007838 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007839 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7840 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007841 // Generate helper private variable and initialize it with the value of the
7842 // original variable. The address of the original variable is replaced by
7843 // the address of the new private variable in the CodeGen. This new variable
7844 // is not added to IdResolver, so the code in the OpenMP region uses
7845 // original variable for proper diagnostics and variable capturing.
7846 Expr *VDInitRefExpr = nullptr;
7847 // For arrays generate initializer for single element and replace it by the
7848 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007849 if (Type->isArrayType()) {
7850 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007851 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007852 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007853 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007854 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007855 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007856 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007857 InitializedEntity Entity =
7858 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007859 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7860
7861 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7862 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7863 if (Result.isInvalid())
7864 VDPrivate->setInvalidDecl();
7865 else
7866 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007867 // Remove temp variable declaration.
7868 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007869 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007870 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7871 ".firstprivate.temp");
7872 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7873 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007874 AddInitializerToDecl(VDPrivate,
7875 DefaultLvalueConversion(VDInitRefExpr).get(),
7876 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007877 }
7878 if (VDPrivate->isInvalidDecl()) {
7879 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007880 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007881 diag::note_omp_task_predetermined_firstprivate_here);
7882 }
7883 continue;
7884 }
7885 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007886 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007887 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7888 RefExpr->getExprLoc());
7889 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007890 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007891 if (TopDVar.CKind == OMPC_lastprivate)
7892 Ref = TopDVar.PrivateCopy;
7893 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007894 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007895 if (!IsOpenMPCapturedDecl(D))
7896 ExprCaptures.push_back(Ref->getDecl());
7897 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007898 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007899 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7900 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007901 PrivateCopies.push_back(VDPrivateRefExpr);
7902 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007903 }
7904
Alexey Bataeved09d242014-05-28 05:53:51 +00007905 if (Vars.empty())
7906 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007907
7908 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007909 Vars, PrivateCopies, Inits,
7910 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007911}
7912
Alexander Musman1bb328c2014-06-04 13:06:39 +00007913OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7914 SourceLocation StartLoc,
7915 SourceLocation LParenLoc,
7916 SourceLocation EndLoc) {
7917 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007918 SmallVector<Expr *, 8> SrcExprs;
7919 SmallVector<Expr *, 8> DstExprs;
7920 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007921 SmallVector<Decl *, 4> ExprCaptures;
7922 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007923 for (auto &RefExpr : VarList) {
7924 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007925 SourceLocation ELoc;
7926 SourceRange ERange;
7927 Expr *SimpleRefExpr = RefExpr;
7928 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007929 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007930 // It will be analyzed later.
7931 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007932 SrcExprs.push_back(nullptr);
7933 DstExprs.push_back(nullptr);
7934 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007935 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007936 ValueDecl *D = Res.first;
7937 if (!D)
7938 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007939
Alexey Bataev74caaf22016-02-20 04:09:36 +00007940 QualType Type = D->getType();
7941 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007942
7943 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7944 // A variable that appears in a lastprivate clause must not have an
7945 // incomplete type or a reference type.
7946 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007947 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007948 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007949 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007950
7951 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7952 // in a Construct]
7953 // Variables with the predetermined data-sharing attributes may not be
7954 // listed in data-sharing attributes clauses, except for the cases
7955 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007956 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007957 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7958 DVar.CKind != OMPC_firstprivate &&
7959 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7960 Diag(ELoc, diag::err_omp_wrong_dsa)
7961 << getOpenMPClauseName(DVar.CKind)
7962 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007963 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007964 continue;
7965 }
7966
Alexey Bataevf29276e2014-06-18 04:14:57 +00007967 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7968 // OpenMP [2.14.3.5, Restrictions, p.2]
7969 // A list item that is private within a parallel region, or that appears in
7970 // the reduction clause of a parallel construct, must not appear in a
7971 // lastprivate clause on a worksharing construct if any of the corresponding
7972 // worksharing regions ever binds to any of the corresponding parallel
7973 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007974 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007975 if (isOpenMPWorksharingDirective(CurrDir) &&
7976 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007977 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007978 if (DVar.CKind != OMPC_shared) {
7979 Diag(ELoc, diag::err_omp_required_access)
7980 << getOpenMPClauseName(OMPC_lastprivate)
7981 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007982 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007983 continue;
7984 }
7985 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007986
7987 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7988 // A list item may appear in a firstprivate or lastprivate clause but not
7989 // both.
7990 if (CurrDir == OMPD_distribute) {
7991 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7992 if (DVar.CKind == OMPC_firstprivate) {
7993 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7994 ReportOriginalDSA(*this, DSAStack, D, DVar);
7995 continue;
7996 }
7997 }
7998
Alexander Musman1bb328c2014-06-04 13:06:39 +00007999 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00008000 // A variable of class type (or array thereof) that appears in a
8001 // lastprivate clause requires an accessible, unambiguous default
8002 // constructor for the class type, unless the list item is also specified
8003 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00008004 // A variable of class type (or array thereof) that appears in a
8005 // lastprivate clause requires an accessible, unambiguous copy assignment
8006 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00008007 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008008 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00008009 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008010 D->hasAttrs() ? &D->getAttrs() : nullptr);
8011 auto *PseudoSrcExpr =
8012 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008013 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008014 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00008015 D->hasAttrs() ? &D->getAttrs() : nullptr);
8016 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00008017 // For arrays generate assignment operation for single element and replace
8018 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00008019 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00008020 PseudoDstExpr, PseudoSrcExpr);
8021 if (AssignmentOp.isInvalid())
8022 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00008023 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00008024 /*DiscardedValue=*/true);
8025 if (AssignmentOp.isInvalid())
8026 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00008027
Alexey Bataev74caaf22016-02-20 04:09:36 +00008028 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00008029 if (!VD) {
8030 if (TopDVar.CKind == OMPC_firstprivate)
8031 Ref = TopDVar.PrivateCopy;
8032 else {
Alexey Bataev61205072016-03-02 04:57:40 +00008033 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00008034 if (!IsOpenMPCapturedDecl(D))
8035 ExprCaptures.push_back(Ref->getDecl());
8036 }
8037 if (TopDVar.CKind == OMPC_firstprivate ||
8038 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008039 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00008040 ExprResult RefRes = DefaultLvalueConversion(Ref);
8041 if (!RefRes.isUsable())
8042 continue;
8043 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00008044 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8045 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008046 if (!PostUpdateRes.isUsable())
8047 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008048 ExprPostUpdates.push_back(
8049 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008050 }
8051 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00008052 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00008053 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8054 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008055 SrcExprs.push_back(PseudoSrcExpr);
8056 DstExprs.push_back(PseudoDstExpr);
8057 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008058 }
8059
8060 if (Vars.empty())
8061 return nullptr;
8062
8063 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008064 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008065 buildPreInits(Context, ExprCaptures),
8066 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008067}
8068
Alexey Bataev758e55e2013-09-06 18:03:48 +00008069OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8070 SourceLocation StartLoc,
8071 SourceLocation LParenLoc,
8072 SourceLocation EndLoc) {
8073 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008074 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008075 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008076 SourceLocation ELoc;
8077 SourceRange ERange;
8078 Expr *SimpleRefExpr = RefExpr;
8079 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008080 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008081 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008082 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008083 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008084 ValueDecl *D = Res.first;
8085 if (!D)
8086 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008087
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008088 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008089 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8090 // in a Construct]
8091 // Variables with the predetermined data-sharing attributes may not be
8092 // listed in data-sharing attributes clauses, except for the cases
8093 // listed below. For these exceptions only, listing a predetermined
8094 // variable in a data-sharing attribute clause is allowed and overrides
8095 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008096 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008097 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8098 DVar.RefExpr) {
8099 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8100 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008101 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008102 continue;
8103 }
8104
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008105 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008106 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008107 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008108 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008109 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008110 }
8111
Alexey Bataeved09d242014-05-28 05:53:51 +00008112 if (Vars.empty())
8113 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008114
8115 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8116}
8117
Alexey Bataevc5e02582014-06-16 07:08:35 +00008118namespace {
8119class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8120 DSAStackTy *Stack;
8121
8122public:
8123 bool VisitDeclRefExpr(DeclRefExpr *E) {
8124 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008125 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008126 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8127 return false;
8128 if (DVar.CKind != OMPC_unknown)
8129 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008130 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008131 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008132 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008133 return true;
8134 return false;
8135 }
8136 return false;
8137 }
8138 bool VisitStmt(Stmt *S) {
8139 for (auto Child : S->children()) {
8140 if (Child && Visit(Child))
8141 return true;
8142 }
8143 return false;
8144 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008145 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008146};
Alexey Bataev23b69422014-06-18 07:08:49 +00008147} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008148
Alexey Bataev60da77e2016-02-29 05:54:20 +00008149namespace {
8150// Transform MemberExpression for specified FieldDecl of current class to
8151// DeclRefExpr to specified OMPCapturedExprDecl.
8152class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8153 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8154 ValueDecl *Field;
8155 DeclRefExpr *CapturedExpr;
8156
8157public:
8158 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8159 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8160
8161 ExprResult TransformMemberExpr(MemberExpr *E) {
8162 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8163 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008164 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008165 return CapturedExpr;
8166 }
8167 return BaseTransform::TransformMemberExpr(E);
8168 }
8169 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8170};
8171} // namespace
8172
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008173template <typename T>
8174static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8175 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8176 for (auto &Set : Lookups) {
8177 for (auto *D : Set) {
8178 if (auto Res = Gen(cast<ValueDecl>(D)))
8179 return Res;
8180 }
8181 }
8182 return T();
8183}
8184
8185static ExprResult
8186buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8187 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8188 const DeclarationNameInfo &ReductionId, QualType Ty,
8189 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8190 if (ReductionIdScopeSpec.isInvalid())
8191 return ExprError();
8192 SmallVector<UnresolvedSet<8>, 4> Lookups;
8193 if (S) {
8194 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8195 Lookup.suppressDiagnostics();
8196 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8197 auto *D = Lookup.getRepresentativeDecl();
8198 do {
8199 S = S->getParent();
8200 } while (S && !S->isDeclScope(D));
8201 if (S)
8202 S = S->getParent();
8203 Lookups.push_back(UnresolvedSet<8>());
8204 Lookups.back().append(Lookup.begin(), Lookup.end());
8205 Lookup.clear();
8206 }
8207 } else if (auto *ULE =
8208 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8209 Lookups.push_back(UnresolvedSet<8>());
8210 Decl *PrevD = nullptr;
8211 for(auto *D : ULE->decls()) {
8212 if (D == PrevD)
8213 Lookups.push_back(UnresolvedSet<8>());
8214 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8215 Lookups.back().addDecl(DRD);
8216 PrevD = D;
8217 }
8218 }
8219 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8220 Ty->containsUnexpandedParameterPack() ||
8221 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8222 return !D->isInvalidDecl() &&
8223 (D->getType()->isDependentType() ||
8224 D->getType()->isInstantiationDependentType() ||
8225 D->getType()->containsUnexpandedParameterPack());
8226 })) {
8227 UnresolvedSet<8> ResSet;
8228 for (auto &Set : Lookups) {
8229 ResSet.append(Set.begin(), Set.end());
8230 // The last item marks the end of all declarations at the specified scope.
8231 ResSet.addDecl(Set[Set.size() - 1]);
8232 }
8233 return UnresolvedLookupExpr::Create(
8234 SemaRef.Context, /*NamingClass=*/nullptr,
8235 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8236 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8237 }
8238 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8239 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8240 if (!D->isInvalidDecl() &&
8241 SemaRef.Context.hasSameType(D->getType(), Ty))
8242 return D;
8243 return nullptr;
8244 }))
8245 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8246 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8247 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8248 if (!D->isInvalidDecl() &&
8249 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8250 !Ty.isMoreQualifiedThan(D->getType()))
8251 return D;
8252 return nullptr;
8253 })) {
8254 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8255 /*DetectVirtual=*/false);
8256 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8257 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8258 VD->getType().getUnqualifiedType()))) {
8259 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8260 /*DiagID=*/0) !=
8261 Sema::AR_inaccessible) {
8262 SemaRef.BuildBasePathArray(Paths, BasePath);
8263 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8264 }
8265 }
8266 }
8267 }
8268 if (ReductionIdScopeSpec.isSet()) {
8269 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8270 return ExprError();
8271 }
8272 return ExprEmpty();
8273}
8274
Alexey Bataevc5e02582014-06-16 07:08:35 +00008275OMPClause *Sema::ActOnOpenMPReductionClause(
8276 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8277 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008278 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8279 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008280 auto DN = ReductionId.getName();
8281 auto OOK = DN.getCXXOverloadedOperator();
8282 BinaryOperatorKind BOK = BO_Comma;
8283
8284 // OpenMP [2.14.3.6, reduction clause]
8285 // C
8286 // reduction-identifier is either an identifier or one of the following
8287 // operators: +, -, *, &, |, ^, && and ||
8288 // C++
8289 // reduction-identifier is either an id-expression or one of the following
8290 // operators: +, -, *, &, |, ^, && and ||
8291 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8292 switch (OOK) {
8293 case OO_Plus:
8294 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008295 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008296 break;
8297 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008298 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008299 break;
8300 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008301 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008302 break;
8303 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008304 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008305 break;
8306 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008307 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008308 break;
8309 case OO_AmpAmp:
8310 BOK = BO_LAnd;
8311 break;
8312 case OO_PipePipe:
8313 BOK = BO_LOr;
8314 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008315 case OO_New:
8316 case OO_Delete:
8317 case OO_Array_New:
8318 case OO_Array_Delete:
8319 case OO_Slash:
8320 case OO_Percent:
8321 case OO_Tilde:
8322 case OO_Exclaim:
8323 case OO_Equal:
8324 case OO_Less:
8325 case OO_Greater:
8326 case OO_LessEqual:
8327 case OO_GreaterEqual:
8328 case OO_PlusEqual:
8329 case OO_MinusEqual:
8330 case OO_StarEqual:
8331 case OO_SlashEqual:
8332 case OO_PercentEqual:
8333 case OO_CaretEqual:
8334 case OO_AmpEqual:
8335 case OO_PipeEqual:
8336 case OO_LessLess:
8337 case OO_GreaterGreater:
8338 case OO_LessLessEqual:
8339 case OO_GreaterGreaterEqual:
8340 case OO_EqualEqual:
8341 case OO_ExclaimEqual:
8342 case OO_PlusPlus:
8343 case OO_MinusMinus:
8344 case OO_Comma:
8345 case OO_ArrowStar:
8346 case OO_Arrow:
8347 case OO_Call:
8348 case OO_Subscript:
8349 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008350 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008351 case NUM_OVERLOADED_OPERATORS:
8352 llvm_unreachable("Unexpected reduction identifier");
8353 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008354 if (auto II = DN.getAsIdentifierInfo()) {
8355 if (II->isStr("max"))
8356 BOK = BO_GT;
8357 else if (II->isStr("min"))
8358 BOK = BO_LT;
8359 }
8360 break;
8361 }
8362 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008363 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008364 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008365 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008366
8367 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008368 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008369 SmallVector<Expr *, 8> LHSs;
8370 SmallVector<Expr *, 8> RHSs;
8371 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008372 SmallVector<Decl *, 4> ExprCaptures;
8373 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008374 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8375 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008376 for (auto RefExpr : VarList) {
8377 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008378 // OpenMP [2.1, C/C++]
8379 // A list item is a variable or array section, subject to the restrictions
8380 // specified in Section 2.4 on page 42 and in each of the sections
8381 // describing clauses and directives for which a list appears.
8382 // OpenMP [2.14.3.3, Restrictions, p.1]
8383 // A variable that is part of another variable (as an array or
8384 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008385 if (!FirstIter && IR != ER)
8386 ++IR;
8387 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008388 SourceLocation ELoc;
8389 SourceRange ERange;
8390 Expr *SimpleRefExpr = RefExpr;
8391 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8392 /*AllowArraySection=*/true);
8393 if (Res.second) {
8394 // It will be analyzed later.
8395 Vars.push_back(RefExpr);
8396 Privates.push_back(nullptr);
8397 LHSs.push_back(nullptr);
8398 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008399 // Try to find 'declare reduction' corresponding construct before using
8400 // builtin/overloaded operators.
8401 QualType Type = Context.DependentTy;
8402 CXXCastPath BasePath;
8403 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8404 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8405 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8406 if (CurContext->isDependentContext() &&
8407 (DeclareReductionRef.isUnset() ||
8408 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8409 ReductionOps.push_back(DeclareReductionRef.get());
8410 else
8411 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008412 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008413 ValueDecl *D = Res.first;
8414 if (!D)
8415 continue;
8416
Alexey Bataeva1764212015-09-30 09:22:36 +00008417 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008418 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8419 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8420 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008421 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008422 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008423 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8424 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8425 Type = ATy->getElementType();
8426 else
8427 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008428 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008429 } else
8430 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8431 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008432
Alexey Bataevc5e02582014-06-16 07:08:35 +00008433 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8434 // A variable that appears in a private clause must not have an incomplete
8435 // type or a reference type.
8436 if (RequireCompleteType(ELoc, Type,
8437 diag::err_omp_reduction_incomplete_type))
8438 continue;
8439 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008440 // A list item that appears in a reduction clause must not be
8441 // const-qualified.
8442 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008443 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008444 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008445 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008446 bool IsDecl = !VD ||
8447 VD->isThisDeclarationADefinition(Context) ==
8448 VarDecl::DeclarationOnly;
8449 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008450 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008451 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008452 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008453 continue;
8454 }
8455 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8456 // If a list-item is a reference type then it must bind to the same object
8457 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008458 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008459 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008460 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008461 DSARefChecker Check(DSAStack);
8462 if (Check.Visit(VDDef->getInit())) {
8463 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8464 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8465 continue;
8466 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008467 }
8468 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008469
Alexey Bataevc5e02582014-06-16 07:08:35 +00008470 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8471 // in a Construct]
8472 // Variables with the predetermined data-sharing attributes may not be
8473 // listed in data-sharing attributes clauses, except for the cases
8474 // listed below. For these exceptions only, listing a predetermined
8475 // variable in a data-sharing attribute clause is allowed and overrides
8476 // the variable's predetermined data-sharing attributes.
8477 // OpenMP [2.14.3.6, Restrictions, p.3]
8478 // Any number of reduction clauses can be specified on the directive,
8479 // but a list item can appear only once in the reduction clauses for that
8480 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008481 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008482 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008483 if (DVar.CKind == OMPC_reduction) {
8484 Diag(ELoc, diag::err_omp_once_referenced)
8485 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008486 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008487 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008488 } else if (DVar.CKind != OMPC_unknown) {
8489 Diag(ELoc, diag::err_omp_wrong_dsa)
8490 << getOpenMPClauseName(DVar.CKind)
8491 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008492 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008493 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008494 }
8495
8496 // OpenMP [2.14.3.6, Restrictions, p.1]
8497 // A list item that appears in a reduction clause of a worksharing
8498 // construct must be shared in the parallel regions to which any of the
8499 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008500 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8501 if (isOpenMPWorksharingDirective(CurrDir) &&
8502 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008503 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008504 if (DVar.CKind != OMPC_shared) {
8505 Diag(ELoc, diag::err_omp_required_access)
8506 << getOpenMPClauseName(OMPC_reduction)
8507 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008508 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008509 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008510 }
8511 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008512
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008513 // Try to find 'declare reduction' corresponding construct before using
8514 // builtin/overloaded operators.
8515 CXXCastPath BasePath;
8516 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8517 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8518 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8519 if (DeclareReductionRef.isInvalid())
8520 continue;
8521 if (CurContext->isDependentContext() &&
8522 (DeclareReductionRef.isUnset() ||
8523 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8524 Vars.push_back(RefExpr);
8525 Privates.push_back(nullptr);
8526 LHSs.push_back(nullptr);
8527 RHSs.push_back(nullptr);
8528 ReductionOps.push_back(DeclareReductionRef.get());
8529 continue;
8530 }
8531 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8532 // Not allowed reduction identifier is found.
8533 Diag(ReductionId.getLocStart(),
8534 diag::err_omp_unknown_reduction_identifier)
8535 << Type << ReductionIdRange;
8536 continue;
8537 }
8538
8539 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8540 // The type of a list item that appears in a reduction clause must be valid
8541 // for the reduction-identifier. For a max or min reduction in C, the type
8542 // of the list item must be an allowed arithmetic data type: char, int,
8543 // float, double, or _Bool, possibly modified with long, short, signed, or
8544 // unsigned. For a max or min reduction in C++, the type of the list item
8545 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8546 // double, or bool, possibly modified with long, short, signed, or unsigned.
8547 if (DeclareReductionRef.isUnset()) {
8548 if ((BOK == BO_GT || BOK == BO_LT) &&
8549 !(Type->isScalarType() ||
8550 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8551 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8552 << getLangOpts().CPlusPlus;
8553 if (!ASE && !OASE) {
8554 bool IsDecl = !VD ||
8555 VD->isThisDeclarationADefinition(Context) ==
8556 VarDecl::DeclarationOnly;
8557 Diag(D->getLocation(),
8558 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8559 << D;
8560 }
8561 continue;
8562 }
8563 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8564 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8565 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8566 if (!ASE && !OASE) {
8567 bool IsDecl = !VD ||
8568 VD->isThisDeclarationADefinition(Context) ==
8569 VarDecl::DeclarationOnly;
8570 Diag(D->getLocation(),
8571 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8572 << D;
8573 }
8574 continue;
8575 }
8576 }
8577
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008578 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008579 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008580 D->hasAttrs() ? &D->getAttrs() : nullptr);
8581 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8582 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008583 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008584 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008585 (!ASE &&
8586 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008587 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008588 // Create pseudo array type for private copy. The size for this array will
8589 // be generated during codegen.
8590 // For array subscripts or single variables Private Ty is the same as Type
8591 // (type of the variable or single array element).
8592 PrivateTy = Context.getVariableArrayType(
8593 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8594 Context.getSizeType(), VK_RValue),
8595 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008596 } else if (!ASE && !OASE &&
8597 Context.getAsArrayType(D->getType().getNonReferenceType()))
8598 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008599 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008600 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8601 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008602 // Add initializer for private variable.
8603 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008604 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8605 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8606 if (DeclareReductionRef.isUsable()) {
8607 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8608 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8609 if (DRD->getInitializer()) {
8610 Init = DRDRef;
8611 RHSVD->setInit(DRDRef);
8612 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008613 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008614 } else {
8615 switch (BOK) {
8616 case BO_Add:
8617 case BO_Xor:
8618 case BO_Or:
8619 case BO_LOr:
8620 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8621 if (Type->isScalarType() || Type->isAnyComplexType())
8622 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8623 break;
8624 case BO_Mul:
8625 case BO_LAnd:
8626 if (Type->isScalarType() || Type->isAnyComplexType()) {
8627 // '*' and '&&' reduction ops - initializer is '1'.
8628 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008629 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008630 break;
8631 case BO_And: {
8632 // '&' reduction op - initializer is '~0'.
8633 QualType OrigType = Type;
8634 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8635 Type = ComplexTy->getElementType();
8636 if (Type->isRealFloatingType()) {
8637 llvm::APFloat InitValue =
8638 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8639 /*isIEEE=*/true);
8640 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8641 Type, ELoc);
8642 } else if (Type->isScalarType()) {
8643 auto Size = Context.getTypeSize(Type);
8644 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8645 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8646 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8647 }
8648 if (Init && OrigType->isAnyComplexType()) {
8649 // Init = 0xFFFF + 0xFFFFi;
8650 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8651 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8652 }
8653 Type = OrigType;
8654 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008655 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008656 case BO_LT:
8657 case BO_GT: {
8658 // 'min' reduction op - initializer is 'Largest representable number in
8659 // the reduction list item type'.
8660 // 'max' reduction op - initializer is 'Least representable number in
8661 // the reduction list item type'.
8662 if (Type->isIntegerType() || Type->isPointerType()) {
8663 bool IsSigned = Type->hasSignedIntegerRepresentation();
8664 auto Size = Context.getTypeSize(Type);
8665 QualType IntTy =
8666 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8667 llvm::APInt InitValue =
8668 (BOK != BO_LT)
8669 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8670 : llvm::APInt::getMinValue(Size)
8671 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8672 : llvm::APInt::getMaxValue(Size);
8673 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8674 if (Type->isPointerType()) {
8675 // Cast to pointer type.
8676 auto CastExpr = BuildCStyleCastExpr(
8677 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8678 SourceLocation(), Init);
8679 if (CastExpr.isInvalid())
8680 continue;
8681 Init = CastExpr.get();
8682 }
8683 } else if (Type->isRealFloatingType()) {
8684 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8685 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8686 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8687 Type, ELoc);
8688 }
8689 break;
8690 }
8691 case BO_PtrMemD:
8692 case BO_PtrMemI:
8693 case BO_MulAssign:
8694 case BO_Div:
8695 case BO_Rem:
8696 case BO_Sub:
8697 case BO_Shl:
8698 case BO_Shr:
8699 case BO_LE:
8700 case BO_GE:
8701 case BO_EQ:
8702 case BO_NE:
8703 case BO_AndAssign:
8704 case BO_XorAssign:
8705 case BO_OrAssign:
8706 case BO_Assign:
8707 case BO_AddAssign:
8708 case BO_SubAssign:
8709 case BO_DivAssign:
8710 case BO_RemAssign:
8711 case BO_ShlAssign:
8712 case BO_ShrAssign:
8713 case BO_Comma:
8714 llvm_unreachable("Unexpected reduction operation");
8715 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008716 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008717 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008718 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8719 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008720 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008721 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008722 if (RHSVD->isInvalidDecl())
8723 continue;
8724 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008725 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8726 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008727 bool IsDecl =
8728 !VD ||
8729 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8730 Diag(D->getLocation(),
8731 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8732 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008733 continue;
8734 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008735 // Store initializer for single element in private copy. Will be used during
8736 // codegen.
8737 PrivateVD->setInit(RHSVD->getInit());
8738 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008739 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008740 ExprResult ReductionOp;
8741 if (DeclareReductionRef.isUsable()) {
8742 QualType RedTy = DeclareReductionRef.get()->getType();
8743 QualType PtrRedTy = Context.getPointerType(RedTy);
8744 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8745 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8746 if (!BasePath.empty()) {
8747 LHS = DefaultLvalueConversion(LHS.get());
8748 RHS = DefaultLvalueConversion(RHS.get());
8749 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8750 CK_UncheckedDerivedToBase, LHS.get(),
8751 &BasePath, LHS.get()->getValueKind());
8752 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8753 CK_UncheckedDerivedToBase, RHS.get(),
8754 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008755 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008756 FunctionProtoType::ExtProtoInfo EPI;
8757 QualType Params[] = {PtrRedTy, PtrRedTy};
8758 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8759 auto *OVE = new (Context) OpaqueValueExpr(
8760 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8761 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8762 Expr *Args[] = {LHS.get(), RHS.get()};
8763 ReductionOp = new (Context)
8764 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8765 } else {
8766 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8767 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8768 if (ReductionOp.isUsable()) {
8769 if (BOK != BO_LT && BOK != BO_GT) {
8770 ReductionOp =
8771 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8772 BO_Assign, LHSDRE, ReductionOp.get());
8773 } else {
8774 auto *ConditionalOp = new (Context) ConditionalOperator(
8775 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8776 RHSDRE, Type, VK_LValue, OK_Ordinary);
8777 ReductionOp =
8778 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8779 BO_Assign, LHSDRE, ConditionalOp);
8780 }
8781 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8782 }
8783 if (ReductionOp.isInvalid())
8784 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008785 }
8786
Alexey Bataev60da77e2016-02-29 05:54:20 +00008787 DeclRefExpr *Ref = nullptr;
8788 Expr *VarsExpr = RefExpr->IgnoreParens();
8789 if (!VD) {
8790 if (ASE || OASE) {
8791 TransformExprToCaptures RebuildToCapture(*this, D);
8792 VarsExpr =
8793 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8794 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008795 } else {
8796 VarsExpr = Ref =
8797 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008798 }
8799 if (!IsOpenMPCapturedDecl(D)) {
8800 ExprCaptures.push_back(Ref->getDecl());
8801 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8802 ExprResult RefRes = DefaultLvalueConversion(Ref);
8803 if (!RefRes.isUsable())
8804 continue;
8805 ExprResult PostUpdateRes =
8806 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8807 SimpleRefExpr, RefRes.get());
8808 if (!PostUpdateRes.isUsable())
8809 continue;
8810 ExprPostUpdates.push_back(
8811 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008812 }
8813 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008814 }
8815 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8816 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008817 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008818 LHSs.push_back(LHSDRE);
8819 RHSs.push_back(RHSDRE);
8820 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008821 }
8822
8823 if (Vars.empty())
8824 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008825
Alexey Bataevc5e02582014-06-16 07:08:35 +00008826 return OMPReductionClause::Create(
8827 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008828 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008829 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8830 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008831}
8832
Alexey Bataevecba70f2016-04-12 11:02:11 +00008833bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8834 SourceLocation LinLoc) {
8835 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8836 LinKind == OMPC_LINEAR_unknown) {
8837 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8838 return true;
8839 }
8840 return false;
8841}
8842
8843bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8844 OpenMPLinearClauseKind LinKind,
8845 QualType Type) {
8846 auto *VD = dyn_cast_or_null<VarDecl>(D);
8847 // A variable must not have an incomplete type or a reference type.
8848 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8849 return true;
8850 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8851 !Type->isReferenceType()) {
8852 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8853 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8854 return true;
8855 }
8856 Type = Type.getNonReferenceType();
8857
8858 // A list item must not be const-qualified.
8859 if (Type.isConstant(Context)) {
8860 Diag(ELoc, diag::err_omp_const_variable)
8861 << getOpenMPClauseName(OMPC_linear);
8862 if (D) {
8863 bool IsDecl =
8864 !VD ||
8865 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8866 Diag(D->getLocation(),
8867 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8868 << D;
8869 }
8870 return true;
8871 }
8872
8873 // A list item must be of integral or pointer type.
8874 Type = Type.getUnqualifiedType().getCanonicalType();
8875 const auto *Ty = Type.getTypePtrOrNull();
8876 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8877 !Ty->isPointerType())) {
8878 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8879 if (D) {
8880 bool IsDecl =
8881 !VD ||
8882 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8883 Diag(D->getLocation(),
8884 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8885 << D;
8886 }
8887 return true;
8888 }
8889 return false;
8890}
8891
Alexey Bataev182227b2015-08-20 10:54:39 +00008892OMPClause *Sema::ActOnOpenMPLinearClause(
8893 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8894 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8895 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008896 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008897 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008898 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008899 SmallVector<Decl *, 4> ExprCaptures;
8900 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008901 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008902 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008903 for (auto &RefExpr : VarList) {
8904 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008905 SourceLocation ELoc;
8906 SourceRange ERange;
8907 Expr *SimpleRefExpr = RefExpr;
8908 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8909 /*AllowArraySection=*/false);
8910 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008911 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008912 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008913 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008914 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008915 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008916 ValueDecl *D = Res.first;
8917 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008918 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008919
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008920 QualType Type = D->getType();
8921 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008922
8923 // OpenMP [2.14.3.7, linear clause]
8924 // A list-item cannot appear in more than one linear clause.
8925 // A list-item that appears in a linear clause cannot appear in any
8926 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008927 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008928 if (DVar.RefExpr) {
8929 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8930 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008931 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008932 continue;
8933 }
8934
Alexey Bataevecba70f2016-04-12 11:02:11 +00008935 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008936 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008937 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008938
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008939 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008940 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8941 D->hasAttrs() ? &D->getAttrs() : nullptr);
8942 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008943 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008944 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008945 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008946 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008947 if (!VD) {
8948 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8949 if (!IsOpenMPCapturedDecl(D)) {
8950 ExprCaptures.push_back(Ref->getDecl());
8951 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8952 ExprResult RefRes = DefaultLvalueConversion(Ref);
8953 if (!RefRes.isUsable())
8954 continue;
8955 ExprResult PostUpdateRes =
8956 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8957 SimpleRefExpr, RefRes.get());
8958 if (!PostUpdateRes.isUsable())
8959 continue;
8960 ExprPostUpdates.push_back(
8961 IgnoredValueConversions(PostUpdateRes.get()).get());
8962 }
8963 }
8964 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008965 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008966 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008967 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008968 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008969 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008970 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8971 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8972
8973 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8974 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008975 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008976 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008977 }
8978
8979 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008980 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008981
8982 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008983 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008984 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8985 !Step->isInstantiationDependent() &&
8986 !Step->containsUnexpandedParameterPack()) {
8987 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008988 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008989 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008990 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008991 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008992
Alexander Musman3276a272015-03-21 10:12:56 +00008993 // Build var to save the step value.
8994 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008995 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008996 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008997 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008998 ExprResult CalcStep =
8999 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009000 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00009001
Alexander Musman8dba6642014-04-22 13:09:42 +00009002 // Warn about zero linear step (it would be probably better specified as
9003 // making corresponding variables 'const').
9004 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00009005 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9006 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00009007 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9008 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00009009 if (!IsConstant && CalcStep.isUsable()) {
9010 // Calculate the step beforehand instead of doing this on each iteration.
9011 // (This is not used if the number of iterations may be kfold-ed).
9012 CalcStepExpr = CalcStep.get();
9013 }
Alexander Musman8dba6642014-04-22 13:09:42 +00009014 }
9015
Alexey Bataev182227b2015-08-20 10:54:39 +00009016 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9017 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009018 StepExpr, CalcStepExpr,
9019 buildPreInits(Context, ExprCaptures),
9020 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00009021}
9022
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009023static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9024 Expr *NumIterations, Sema &SemaRef,
9025 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00009026 // Walk the vars and build update/final expressions for the CodeGen.
9027 SmallVector<Expr *, 8> Updates;
9028 SmallVector<Expr *, 8> Finals;
9029 Expr *Step = Clause.getStep();
9030 Expr *CalcStep = Clause.getCalcStep();
9031 // OpenMP [2.14.3.7, linear clause]
9032 // If linear-step is not specified it is assumed to be 1.
9033 if (Step == nullptr)
9034 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009035 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00009036 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00009037 }
Alexander Musman3276a272015-03-21 10:12:56 +00009038 bool HasErrors = false;
9039 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009040 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009041 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00009042 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009043 SourceLocation ELoc;
9044 SourceRange ERange;
9045 Expr *SimpleRefExpr = RefExpr;
9046 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9047 /*AllowArraySection=*/false);
9048 ValueDecl *D = Res.first;
9049 if (Res.second || !D) {
9050 Updates.push_back(nullptr);
9051 Finals.push_back(nullptr);
9052 HasErrors = true;
9053 continue;
9054 }
9055 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9056 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9057 ->getMemberDecl();
9058 }
9059 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009060 Expr *InitExpr = *CurInit;
9061
9062 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009063 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009064 Expr *CapturedRef;
9065 if (LinKind == OMPC_LINEAR_uval)
9066 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9067 else
9068 CapturedRef =
9069 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9070 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9071 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009072
9073 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009074 ExprResult Update;
9075 if (!Info.first) {
9076 Update =
9077 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9078 InitExpr, IV, Step, /* Subtract */ false);
9079 } else
9080 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009081 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9082 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009083
9084 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009085 ExprResult Final;
9086 if (!Info.first) {
9087 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9088 InitExpr, NumIterations, Step,
9089 /* Subtract */ false);
9090 } else
9091 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009092 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9093 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009094
Alexander Musman3276a272015-03-21 10:12:56 +00009095 if (!Update.isUsable() || !Final.isUsable()) {
9096 Updates.push_back(nullptr);
9097 Finals.push_back(nullptr);
9098 HasErrors = true;
9099 } else {
9100 Updates.push_back(Update.get());
9101 Finals.push_back(Final.get());
9102 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009103 ++CurInit;
9104 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009105 }
9106 Clause.setUpdates(Updates);
9107 Clause.setFinals(Finals);
9108 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009109}
9110
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009111OMPClause *Sema::ActOnOpenMPAlignedClause(
9112 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9113 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9114
9115 SmallVector<Expr *, 8> Vars;
9116 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009117 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9118 SourceLocation ELoc;
9119 SourceRange ERange;
9120 Expr *SimpleRefExpr = RefExpr;
9121 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9122 /*AllowArraySection=*/false);
9123 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009124 // It will be analyzed later.
9125 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009126 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009127 ValueDecl *D = Res.first;
9128 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009129 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009130
Alexey Bataev1efd1662016-03-29 10:59:56 +00009131 QualType QType = D->getType();
9132 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009133
9134 // OpenMP [2.8.1, simd construct, Restrictions]
9135 // The type of list items appearing in the aligned clause must be
9136 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009137 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009138 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009139 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009140 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009141 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009142 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009143 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009145 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009147 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009148 continue;
9149 }
9150
9151 // OpenMP [2.8.1, simd construct, Restrictions]
9152 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009153 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009154 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009155 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9156 << getOpenMPClauseName(OMPC_aligned);
9157 continue;
9158 }
9159
Alexey Bataev1efd1662016-03-29 10:59:56 +00009160 DeclRefExpr *Ref = nullptr;
9161 if (!VD && IsOpenMPCapturedDecl(D))
9162 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9163 Vars.push_back(DefaultFunctionArrayConversion(
9164 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9165 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009166 }
9167
9168 // OpenMP [2.8.1, simd construct, Description]
9169 // The parameter of the aligned clause, alignment, must be a constant
9170 // positive integer expression.
9171 // If no optional parameter is specified, implementation-defined default
9172 // alignments for SIMD instructions on the target platforms are assumed.
9173 if (Alignment != nullptr) {
9174 ExprResult AlignResult =
9175 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9176 if (AlignResult.isInvalid())
9177 return nullptr;
9178 Alignment = AlignResult.get();
9179 }
9180 if (Vars.empty())
9181 return nullptr;
9182
9183 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9184 EndLoc, Vars, Alignment);
9185}
9186
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009187OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9188 SourceLocation StartLoc,
9189 SourceLocation LParenLoc,
9190 SourceLocation EndLoc) {
9191 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009192 SmallVector<Expr *, 8> SrcExprs;
9193 SmallVector<Expr *, 8> DstExprs;
9194 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009195 for (auto &RefExpr : VarList) {
9196 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9197 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009198 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009199 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009200 SrcExprs.push_back(nullptr);
9201 DstExprs.push_back(nullptr);
9202 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009203 continue;
9204 }
9205
Alexey Bataeved09d242014-05-28 05:53:51 +00009206 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009207 // OpenMP [2.1, C/C++]
9208 // A list item is a variable name.
9209 // OpenMP [2.14.4.1, Restrictions, p.1]
9210 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009211 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009212 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009213 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9214 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009215 continue;
9216 }
9217
9218 Decl *D = DE->getDecl();
9219 VarDecl *VD = cast<VarDecl>(D);
9220
9221 QualType Type = VD->getType();
9222 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9223 // It will be analyzed later.
9224 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009225 SrcExprs.push_back(nullptr);
9226 DstExprs.push_back(nullptr);
9227 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009228 continue;
9229 }
9230
9231 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9232 // A list item that appears in a copyin clause must be threadprivate.
9233 if (!DSAStack->isThreadPrivate(VD)) {
9234 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009235 << getOpenMPClauseName(OMPC_copyin)
9236 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009237 continue;
9238 }
9239
9240 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9241 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009242 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009243 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009244 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009245 auto *SrcVD =
9246 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9247 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009248 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009249 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9250 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009251 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9252 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009253 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009254 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009255 // For arrays generate assignment operation for single element and replace
9256 // it by the original array element in CodeGen.
9257 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9258 PseudoDstExpr, PseudoSrcExpr);
9259 if (AssignmentOp.isInvalid())
9260 continue;
9261 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9262 /*DiscardedValue=*/true);
9263 if (AssignmentOp.isInvalid())
9264 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009265
9266 DSAStack->addDSA(VD, DE, OMPC_copyin);
9267 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009268 SrcExprs.push_back(PseudoSrcExpr);
9269 DstExprs.push_back(PseudoDstExpr);
9270 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009271 }
9272
Alexey Bataeved09d242014-05-28 05:53:51 +00009273 if (Vars.empty())
9274 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009275
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009276 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9277 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009278}
9279
Alexey Bataevbae9a792014-06-27 10:37:06 +00009280OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9281 SourceLocation StartLoc,
9282 SourceLocation LParenLoc,
9283 SourceLocation EndLoc) {
9284 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009285 SmallVector<Expr *, 8> SrcExprs;
9286 SmallVector<Expr *, 8> DstExprs;
9287 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009288 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009289 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9290 SourceLocation ELoc;
9291 SourceRange ERange;
9292 Expr *SimpleRefExpr = RefExpr;
9293 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9294 /*AllowArraySection=*/false);
9295 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009296 // It will be analyzed later.
9297 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009298 SrcExprs.push_back(nullptr);
9299 DstExprs.push_back(nullptr);
9300 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009301 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009302 ValueDecl *D = Res.first;
9303 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009304 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009305
Alexey Bataeve122da12016-03-17 10:50:17 +00009306 QualType Type = D->getType();
9307 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009308
9309 // OpenMP [2.14.4.2, Restrictions, p.2]
9310 // A list item that appears in a copyprivate clause may not appear in a
9311 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009312 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9313 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009314 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9315 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009316 Diag(ELoc, diag::err_omp_wrong_dsa)
9317 << getOpenMPClauseName(DVar.CKind)
9318 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009319 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009320 continue;
9321 }
9322
9323 // OpenMP [2.11.4.2, Restrictions, p.1]
9324 // All list items that appear in a copyprivate clause must be either
9325 // threadprivate or private in the enclosing context.
9326 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009327 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009328 if (DVar.CKind == OMPC_shared) {
9329 Diag(ELoc, diag::err_omp_required_access)
9330 << getOpenMPClauseName(OMPC_copyprivate)
9331 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009332 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009333 continue;
9334 }
9335 }
9336 }
9337
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009338 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009339 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009340 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009341 << getOpenMPClauseName(OMPC_copyprivate) << Type
9342 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009343 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009344 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009345 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009346 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009347 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009348 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009349 continue;
9350 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009351
Alexey Bataevbae9a792014-06-27 10:37:06 +00009352 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9353 // A variable of class type (or array thereof) that appears in a
9354 // copyin clause requires an accessible, unambiguous copy assignment
9355 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009356 Type = Context.getBaseElementType(Type.getNonReferenceType())
9357 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009358 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009359 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9360 D->hasAttrs() ? &D->getAttrs() : nullptr);
9361 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009362 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009363 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9364 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009365 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009366 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9367 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009368 PseudoDstExpr, PseudoSrcExpr);
9369 if (AssignmentOp.isInvalid())
9370 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009371 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009372 /*DiscardedValue=*/true);
9373 if (AssignmentOp.isInvalid())
9374 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009375
9376 // No need to mark vars as copyprivate, they are already threadprivate or
9377 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009378 assert(VD || IsOpenMPCapturedDecl(D));
9379 Vars.push_back(
9380 VD ? RefExpr->IgnoreParens()
9381 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009382 SrcExprs.push_back(PseudoSrcExpr);
9383 DstExprs.push_back(PseudoDstExpr);
9384 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009385 }
9386
9387 if (Vars.empty())
9388 return nullptr;
9389
Alexey Bataeva63048e2015-03-23 06:18:07 +00009390 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9391 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009392}
9393
Alexey Bataev6125da92014-07-21 11:26:11 +00009394OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9395 SourceLocation StartLoc,
9396 SourceLocation LParenLoc,
9397 SourceLocation EndLoc) {
9398 if (VarList.empty())
9399 return nullptr;
9400
9401 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9402}
Alexey Bataevdea47612014-07-23 07:46:59 +00009403
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009404OMPClause *
9405Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9406 SourceLocation DepLoc, SourceLocation ColonLoc,
9407 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9408 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009409 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009410 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009411 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009412 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009413 return nullptr;
9414 }
9415 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009416 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9417 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009418 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009419 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009420 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9421 /*Last=*/OMPC_DEPEND_unknown, Except)
9422 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009423 return nullptr;
9424 }
9425 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009426 llvm::APSInt DepCounter(/*BitWidth=*/32);
9427 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9428 if (DepKind == OMPC_DEPEND_sink) {
9429 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9430 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9431 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009432 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009433 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009434 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9435 DSAStack->getParentOrderedRegionParam()) {
9436 for (auto &RefExpr : VarList) {
9437 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9438 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9439 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9440 // It will be analyzed later.
9441 Vars.push_back(RefExpr);
9442 continue;
9443 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009444
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009445 SourceLocation ELoc = RefExpr->getExprLoc();
9446 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9447 if (DepKind == OMPC_DEPEND_sink) {
9448 if (DepCounter >= TotalDepCount) {
9449 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9450 continue;
9451 }
9452 ++DepCounter;
9453 // OpenMP [2.13.9, Summary]
9454 // depend(dependence-type : vec), where dependence-type is:
9455 // 'sink' and where vec is the iteration vector, which has the form:
9456 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9457 // where n is the value specified by the ordered clause in the loop
9458 // directive, xi denotes the loop iteration variable of the i-th nested
9459 // loop associated with the loop directive, and di is a constant
9460 // non-negative integer.
9461 SimpleExpr = SimpleExpr->IgnoreImplicit();
9462 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9463 if (!DE) {
9464 OverloadedOperatorKind OOK = OO_None;
9465 SourceLocation OOLoc;
9466 Expr *LHS, *RHS;
9467 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9468 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9469 OOLoc = BO->getOperatorLoc();
9470 LHS = BO->getLHS()->IgnoreParenImpCasts();
9471 RHS = BO->getRHS()->IgnoreParenImpCasts();
9472 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9473 OOK = OCE->getOperator();
9474 OOLoc = OCE->getOperatorLoc();
9475 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9476 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9477 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9478 OOK = MCE->getMethodDecl()
9479 ->getNameInfo()
9480 .getName()
9481 .getCXXOverloadedOperator();
9482 OOLoc = MCE->getCallee()->getExprLoc();
9483 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9484 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9485 } else {
9486 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9487 continue;
9488 }
9489 DE = dyn_cast<DeclRefExpr>(LHS);
9490 if (!DE) {
9491 Diag(LHS->getExprLoc(),
9492 diag::err_omp_depend_sink_expected_loop_iteration)
9493 << DSAStack->getParentLoopControlVariable(
9494 DepCounter.getZExtValue());
9495 continue;
9496 }
9497 if (OOK != OO_Plus && OOK != OO_Minus) {
9498 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9499 continue;
9500 }
9501 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9502 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9503 if (Res.isInvalid())
9504 continue;
9505 }
9506 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9507 if (!CurContext->isDependentContext() &&
9508 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009509 (!VD ||
9510 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009511 Diag(DE->getExprLoc(),
9512 diag::err_omp_depend_sink_expected_loop_iteration)
9513 << DSAStack->getParentLoopControlVariable(
9514 DepCounter.getZExtValue());
9515 continue;
9516 }
9517 } else {
9518 // OpenMP [2.11.1.1, Restrictions, p.3]
9519 // A variable that is part of another variable (such as a field of a
9520 // structure) but is not an array element or an array section cannot
9521 // appear in a depend clause.
9522 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9523 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9524 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9525 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9526 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009527 (ASE &&
9528 !ASE->getBase()
9529 ->getType()
9530 .getNonReferenceType()
9531 ->isPointerType() &&
9532 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009533 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9534 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009535 continue;
9536 }
9537 }
9538
9539 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9540 }
9541
9542 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9543 TotalDepCount > VarList.size() &&
9544 DSAStack->getParentOrderedRegionParam()) {
9545 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9546 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9547 }
9548 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9549 Vars.empty())
9550 return nullptr;
9551 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009552
9553 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9554 DepLoc, ColonLoc, Vars);
9555}
Michael Wonge710d542015-08-07 16:16:36 +00009556
9557OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9558 SourceLocation LParenLoc,
9559 SourceLocation EndLoc) {
9560 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009561
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009562 // OpenMP [2.9.1, Restrictions]
9563 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009564 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9565 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009566 return nullptr;
9567
Michael Wonge710d542015-08-07 16:16:36 +00009568 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9569}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009570
9571static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9572 DSAStackTy *Stack, CXXRecordDecl *RD) {
9573 if (!RD || RD->isInvalidDecl())
9574 return true;
9575
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009576 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9577 if (auto *CTD = CTSD->getSpecializedTemplate())
9578 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009579 auto QTy = SemaRef.Context.getRecordType(RD);
9580 if (RD->isDynamicClass()) {
9581 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9582 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9583 return false;
9584 }
9585 auto *DC = RD;
9586 bool IsCorrect = true;
9587 for (auto *I : DC->decls()) {
9588 if (I) {
9589 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9590 if (MD->isStatic()) {
9591 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9592 SemaRef.Diag(MD->getLocation(),
9593 diag::note_omp_static_member_in_target);
9594 IsCorrect = false;
9595 }
9596 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9597 if (VD->isStaticDataMember()) {
9598 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9599 SemaRef.Diag(VD->getLocation(),
9600 diag::note_omp_static_member_in_target);
9601 IsCorrect = false;
9602 }
9603 }
9604 }
9605 }
9606
9607 for (auto &I : RD->bases()) {
9608 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9609 I.getType()->getAsCXXRecordDecl()))
9610 IsCorrect = false;
9611 }
9612 return IsCorrect;
9613}
9614
9615static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9616 DSAStackTy *Stack, QualType QTy) {
9617 NamedDecl *ND;
9618 if (QTy->isIncompleteType(&ND)) {
9619 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9620 return false;
9621 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9622 if (!RD->isInvalidDecl() &&
9623 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9624 return false;
9625 }
9626 return true;
9627}
9628
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009629/// \brief Return true if it can be proven that the provided array expression
9630/// (array section or array subscript) does NOT specify the whole size of the
9631/// array whose base type is \a BaseQTy.
9632static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9633 const Expr *E,
9634 QualType BaseQTy) {
9635 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9636
9637 // If this is an array subscript, it refers to the whole size if the size of
9638 // the dimension is constant and equals 1. Also, an array section assumes the
9639 // format of an array subscript if no colon is used.
9640 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9641 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9642 return ATy->getSize().getSExtValue() != 1;
9643 // Size can't be evaluated statically.
9644 return false;
9645 }
9646
9647 assert(OASE && "Expecting array section if not an array subscript.");
9648 auto *LowerBound = OASE->getLowerBound();
9649 auto *Length = OASE->getLength();
9650
9651 // If there is a lower bound that does not evaluates to zero, we are not
9652 // convering the whole dimension.
9653 if (LowerBound) {
9654 llvm::APSInt ConstLowerBound;
9655 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9656 return false; // Can't get the integer value as a constant.
9657 if (ConstLowerBound.getSExtValue())
9658 return true;
9659 }
9660
9661 // If we don't have a length we covering the whole dimension.
9662 if (!Length)
9663 return false;
9664
9665 // If the base is a pointer, we don't have a way to get the size of the
9666 // pointee.
9667 if (BaseQTy->isPointerType())
9668 return false;
9669
9670 // We can only check if the length is the same as the size of the dimension
9671 // if we have a constant array.
9672 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9673 if (!CATy)
9674 return false;
9675
9676 llvm::APSInt ConstLength;
9677 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9678 return false; // Can't get the integer value as a constant.
9679
9680 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9681}
9682
9683// Return true if it can be proven that the provided array expression (array
9684// section or array subscript) does NOT specify a single element of the array
9685// whose base type is \a BaseQTy.
9686static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9687 const Expr *E,
9688 QualType BaseQTy) {
9689 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9690
9691 // An array subscript always refer to a single element. Also, an array section
9692 // assumes the format of an array subscript if no colon is used.
9693 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9694 return false;
9695
9696 assert(OASE && "Expecting array section if not an array subscript.");
9697 auto *Length = OASE->getLength();
9698
9699 // If we don't have a length we have to check if the array has unitary size
9700 // for this dimension. Also, we should always expect a length if the base type
9701 // is pointer.
9702 if (!Length) {
9703 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9704 return ATy->getSize().getSExtValue() != 1;
9705 // We cannot assume anything.
9706 return false;
9707 }
9708
9709 // Check if the length evaluates to 1.
9710 llvm::APSInt ConstLength;
9711 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9712 return false; // Can't get the integer value as a constant.
9713
9714 return ConstLength.getSExtValue() != 1;
9715}
9716
Samuel Antao5de996e2016-01-22 20:21:36 +00009717// Return the expression of the base of the map clause or null if it cannot
9718// be determined and do all the necessary checks to see if the expression is
Samuel Antao90927002016-04-26 14:54:23 +00009719// valid as a standalone map clause expression. In the process, record all the
9720// components of the expression.
9721static Expr *CheckMapClauseExpressionBase(
9722 Sema &SemaRef, Expr *E,
9723 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009724 SourceLocation ELoc = E->getExprLoc();
9725 SourceRange ERange = E->getSourceRange();
9726
9727 // The base of elements of list in a map clause have to be either:
9728 // - a reference to variable or field.
9729 // - a member expression.
9730 // - an array expression.
9731 //
9732 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9733 // reference to 'r'.
9734 //
9735 // If we have:
9736 //
9737 // struct SS {
9738 // Bla S;
9739 // foo() {
9740 // #pragma omp target map (S.Arr[:12]);
9741 // }
9742 // }
9743 //
9744 // We want to retrieve the member expression 'this->S';
9745
9746 Expr *RelevantExpr = nullptr;
9747
Samuel Antao5de996e2016-01-22 20:21:36 +00009748 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9749 // If a list item is an array section, it must specify contiguous storage.
9750 //
9751 // For this restriction it is sufficient that we make sure only references
9752 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009753 // exist except in the rightmost expression (unless they cover the whole
9754 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009755 //
9756 // r.ArrS[3:5].Arr[6:7]
9757 //
9758 // r.ArrS[3:5].x
9759 //
9760 // but these would be valid:
9761 // r.ArrS[3].Arr[6:7]
9762 //
9763 // r.ArrS[3].x
9764
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009765 bool AllowUnitySizeArraySection = true;
9766 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009767
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009768 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009769 E = E->IgnoreParenImpCasts();
9770
9771 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9772 if (!isa<VarDecl>(CurE->getDecl()))
9773 break;
9774
9775 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009776
9777 // If we got a reference to a declaration, we should not expect any array
9778 // section before that.
9779 AllowUnitySizeArraySection = false;
9780 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009781
9782 // Record the component.
9783 CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9784 CurE, CurE->getDecl()));
Samuel Antao5de996e2016-01-22 20:21:36 +00009785 continue;
9786 }
9787
9788 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9789 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9790
9791 if (isa<CXXThisExpr>(BaseE))
9792 // We found a base expression: this->Val.
9793 RelevantExpr = CurE;
9794 else
9795 E = BaseE;
9796
9797 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9798 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9799 << CurE->getSourceRange();
9800 break;
9801 }
9802
9803 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9804
9805 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9806 // A bit-field cannot appear in a map clause.
9807 //
9808 if (FD->isBitField()) {
9809 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9810 << CurE->getSourceRange();
9811 break;
9812 }
9813
9814 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9815 // If the type of a list item is a reference to a type T then the type
9816 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009817 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009818
9819 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9820 // A list item cannot be a variable that is a member of a structure with
9821 // a union type.
9822 //
9823 if (auto *RT = CurType->getAs<RecordType>())
9824 if (RT->isUnionType()) {
9825 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9826 << CurE->getSourceRange();
9827 break;
9828 }
9829
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009830 // If we got a member expression, we should not expect any array section
9831 // before that:
9832 //
9833 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9834 // If a list item is an element of a structure, only the rightmost symbol
9835 // of the variable reference can be an array section.
9836 //
9837 AllowUnitySizeArraySection = false;
9838 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009839
9840 // Record the component.
9841 CurComponents.push_back(
9842 OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
Samuel Antao5de996e2016-01-22 20:21:36 +00009843 continue;
9844 }
9845
9846 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9847 E = CurE->getBase()->IgnoreParenImpCasts();
9848
9849 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9850 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9851 << 0 << CurE->getSourceRange();
9852 break;
9853 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009854
9855 // If we got an array subscript that express the whole dimension we
9856 // can have any array expressions before. If it only expressing part of
9857 // the dimension, we can only have unitary-size array expressions.
9858 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9859 E->getType()))
9860 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +00009861
9862 // Record the component - we don't have any declaration associated.
9863 CurComponents.push_back(
9864 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009865 continue;
9866 }
9867
9868 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009869 E = CurE->getBase()->IgnoreParenImpCasts();
9870
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009871 auto CurType =
9872 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9873
Samuel Antao5de996e2016-01-22 20:21:36 +00009874 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9875 // If the type of a list item is a reference to a type T then the type
9876 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +00009877 if (CurType->isReferenceType())
9878 CurType = CurType->getPointeeType();
9879
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009880 bool IsPointer = CurType->isAnyPointerType();
9881
9882 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009883 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9884 << 0 << CurE->getSourceRange();
9885 break;
9886 }
9887
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009888 bool NotWhole =
9889 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9890 bool NotUnity =
9891 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9892
9893 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9894 // Any array section is currently allowed.
9895 //
9896 // If this array section refers to the whole dimension we can still
9897 // accept other array sections before this one, except if the base is a
9898 // pointer. Otherwise, only unitary sections are accepted.
9899 if (NotWhole || IsPointer)
9900 AllowWholeSizeArraySection = false;
9901 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9902 (AllowWholeSizeArraySection && NotWhole)) {
9903 // A unity or whole array section is not allowed and that is not
9904 // compatible with the properties of the current array section.
9905 SemaRef.Diag(
9906 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9907 << CurE->getSourceRange();
9908 break;
9909 }
Samuel Antao90927002016-04-26 14:54:23 +00009910
9911 // Record the component - we don't have any declaration associated.
9912 CurComponents.push_back(
9913 OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
Samuel Antao5de996e2016-01-22 20:21:36 +00009914 continue;
9915 }
9916
9917 // If nothing else worked, this is not a valid map clause expression.
9918 SemaRef.Diag(ELoc,
9919 diag::err_omp_expected_named_var_member_or_array_expression)
9920 << ERange;
9921 break;
9922 }
9923
9924 return RelevantExpr;
9925}
9926
9927// Return true if expression E associated with value VD has conflicts with other
9928// map information.
Samuel Antao90927002016-04-26 14:54:23 +00009929static bool CheckMapConflicts(
9930 Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9931 bool CurrentRegionOnly,
9932 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009933 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +00009934 SourceLocation ELoc = E->getExprLoc();
9935 SourceRange ERange = E->getSourceRange();
9936
9937 // In order to easily check the conflicts we need to match each component of
9938 // the expression under test with the components of the expressions that are
9939 // already in the stack.
9940
Samuel Antao5de996e2016-01-22 20:21:36 +00009941 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009942 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009943 "Map clause expression with unexpected base!");
9944
9945 // Variables to help detecting enclosing problems in data environment nests.
9946 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +00009947 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +00009948
Samuel Antao90927002016-04-26 14:54:23 +00009949 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9950 VD, CurrentRegionOnly,
9951 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
9952 StackComponents) -> bool {
9953
Samuel Antao5de996e2016-01-22 20:21:36 +00009954 assert(!StackComponents.empty() &&
9955 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +00009956 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +00009957 "Map clause expression with unexpected base!");
9958
Samuel Antao90927002016-04-26 14:54:23 +00009959 // The whole expression in the stack.
9960 auto *RE = StackComponents.front().getAssociatedExpression();
9961
Samuel Antao5de996e2016-01-22 20:21:36 +00009962 // Expressions must start from the same base. Here we detect at which
9963 // point both expressions diverge from each other and see if we can
9964 // detect if the memory referred to both expressions is contiguous and
9965 // do not overlap.
9966 auto CI = CurComponents.rbegin();
9967 auto CE = CurComponents.rend();
9968 auto SI = StackComponents.rbegin();
9969 auto SE = StackComponents.rend();
9970 for (; CI != CE && SI != SE; ++CI, ++SI) {
9971
9972 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9973 // At most one list item can be an array item derived from a given
9974 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +00009975 if (CurrentRegionOnly &&
9976 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
9977 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
9978 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
9979 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
9980 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +00009981 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +00009982 << CI->getAssociatedExpression()->getSourceRange();
9983 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
9984 diag::note_used_here)
9985 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +00009986 return true;
9987 }
9988
9989 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +00009990 if (CI->getAssociatedExpression()->getStmtClass() !=
9991 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +00009992 break;
9993
9994 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +00009995 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +00009996 break;
9997 }
9998
9999 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10000 // List items of map clauses in the same construct must not share
10001 // original storage.
10002 //
10003 // If the expressions are exactly the same or one is a subset of the
10004 // other, it means they are sharing storage.
10005 if (CI == CE && SI == SE) {
10006 if (CurrentRegionOnly) {
10007 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10008 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10009 << RE->getSourceRange();
10010 return true;
10011 } else {
10012 // If we find the same expression in the enclosing data environment,
10013 // that is legal.
10014 IsEnclosedByDataEnvironmentExpr = true;
10015 return false;
10016 }
10017 }
10018
Samuel Antao90927002016-04-26 14:54:23 +000010019 QualType DerivedType =
10020 std::prev(CI)->getAssociatedDeclaration()->getType();
10021 SourceLocation DerivedLoc =
10022 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000010023
10024 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10025 // If the type of a list item is a reference to a type T then the type
10026 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010027 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010028
10029 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10030 // A variable for which the type is pointer and an array section
10031 // derived from that variable must not appear as list items of map
10032 // clauses of the same construct.
10033 //
10034 // Also, cover one of the cases in:
10035 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10036 // If any part of the original storage of a list item has corresponding
10037 // storage in the device data environment, all of the original storage
10038 // must have corresponding storage in the device data environment.
10039 //
10040 if (DerivedType->isAnyPointerType()) {
10041 if (CI == CE || SI == SE) {
10042 SemaRef.Diag(
10043 DerivedLoc,
10044 diag::err_omp_pointer_mapped_along_with_derived_section)
10045 << DerivedLoc;
10046 } else {
10047 assert(CI != CE && SI != SE);
10048 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10049 << DerivedLoc;
10050 }
10051 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10052 << RE->getSourceRange();
10053 return true;
10054 }
10055
10056 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10057 // List items of map clauses in the same construct must not share
10058 // original storage.
10059 //
10060 // An expression is a subset of the other.
10061 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10062 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10063 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10064 << RE->getSourceRange();
10065 return true;
10066 }
10067
10068 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000010069 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000010070 if (!CurrentRegionOnly && SI != SE)
10071 EnclosingExpr = RE;
10072
10073 // The current expression is a subset of the expression in the data
10074 // environment.
10075 IsEnclosedByDataEnvironmentExpr |=
10076 (!CurrentRegionOnly && CI != CE && SI == SE);
10077
10078 return false;
10079 });
10080
10081 if (CurrentRegionOnly)
10082 return FoundError;
10083
10084 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10085 // If any part of the original storage of a list item has corresponding
10086 // storage in the device data environment, all of the original storage must
10087 // have corresponding storage in the device data environment.
10088 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10089 // If a list item is an element of a structure, and a different element of
10090 // the structure has a corresponding list item in the device data environment
10091 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000010092 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000010093 // data environment prior to the task encountering the construct.
10094 //
10095 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10096 SemaRef.Diag(ELoc,
10097 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10098 << ERange;
10099 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10100 << EnclosingExpr->getSourceRange();
10101 return true;
10102 }
10103
10104 return FoundError;
10105}
10106
Samuel Antao23abd722016-01-19 20:40:49 +000010107OMPClause *
10108Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10109 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10110 SourceLocation MapLoc, SourceLocation ColonLoc,
10111 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10112 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010113 SmallVector<Expr *, 4> Vars;
10114
Samuel Antao90927002016-04-26 14:54:23 +000010115 // Keep track of the mappable components and base declarations in this clause.
10116 // Each entry in the list is going to have a list of components associated. We
10117 // record each set of the components so that we can build the clause later on.
10118 // In the end we should have the same amount of declarations and component
10119 // lists.
10120 OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10121 SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10122
10123 ClauseComponents.reserve(VarList.size());
10124 ClauseBaseDeclarations.reserve(VarList.size());
10125
Kelvin Li0bff7af2015-11-23 05:32:03 +000010126 for (auto &RE : VarList) {
10127 assert(RE && "Null expr in omp map");
10128 if (isa<DependentScopeDeclRefExpr>(RE)) {
10129 // It will be analyzed later.
10130 Vars.push_back(RE);
10131 continue;
10132 }
10133 SourceLocation ELoc = RE->getExprLoc();
10134
Kelvin Li0bff7af2015-11-23 05:32:03 +000010135 auto *VE = RE->IgnoreParenLValueCasts();
10136
10137 if (VE->isValueDependent() || VE->isTypeDependent() ||
10138 VE->isInstantiationDependent() ||
10139 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010140 // We can only analyze this information once the missing information is
10141 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010142 Vars.push_back(RE);
10143 continue;
10144 }
10145
10146 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010147
Samuel Antao5de996e2016-01-22 20:21:36 +000010148 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10149 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10150 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010151 continue;
10152 }
10153
Samuel Antao90927002016-04-26 14:54:23 +000010154 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10155 ValueDecl *CurDeclaration = nullptr;
10156
10157 // Obtain the array or member expression bases if required. Also, fill the
10158 // components array with all the components identified in the process.
10159 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
Samuel Antao5de996e2016-01-22 20:21:36 +000010160 if (!BE)
10161 continue;
10162
Samuel Antao90927002016-04-26 14:54:23 +000010163 assert(!CurComponents.empty() &&
10164 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010165
Samuel Antao90927002016-04-26 14:54:23 +000010166 // For the following checks, we rely on the base declaration which is
10167 // expected to be associated with the last component. The declaration is
10168 // expected to be a variable or a field (if 'this' is being mapped).
10169 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10170 assert(CurDeclaration && "Null decl on map clause.");
10171 assert(
10172 CurDeclaration->isCanonicalDecl() &&
10173 "Expecting components to have associated only canonical declarations.");
10174
10175 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10176 auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000010177
10178 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010179 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010180
10181 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10182 // threadprivate variables cannot appear in a map clause.
10183 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010184 auto DVar = DSAStack->getTopDSA(VD, false);
10185 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10186 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10187 continue;
10188 }
10189
Samuel Antao5de996e2016-01-22 20:21:36 +000010190 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10191 // A list item cannot appear in both a map clause and a data-sharing
10192 // attribute clause on the same construct.
10193 //
10194 // TODO: Implement this check - it cannot currently be tested because of
10195 // missing implementation of the other data sharing clauses in target
10196 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010197
Samuel Antao5de996e2016-01-22 20:21:36 +000010198 // Check conflicts with other map clause expressions. We check the conflicts
10199 // with the current construct separately from the enclosing data
10200 // environment, because the restrictions are different.
Samuel Antao90927002016-04-26 14:54:23 +000010201 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10202 /*CurrentRegionOnly=*/true, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010203 break;
Samuel Antao90927002016-04-26 14:54:23 +000010204 if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10205 /*CurrentRegionOnly=*/false, CurComponents))
Samuel Antao5de996e2016-01-22 20:21:36 +000010206 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010207
Samuel Antao5de996e2016-01-22 20:21:36 +000010208 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10209 // If the type of a list item is a reference to a type T then the type will
10210 // be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000010211 QualType Type = CurDeclaration->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000010212
10213 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010214 // A list item must have a mappable type.
10215 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10216 DSAStack, Type))
10217 continue;
10218
Samuel Antaodf67fc42016-01-19 19:15:56 +000010219 // target enter data
10220 // OpenMP [2.10.2, Restrictions, p. 99]
10221 // A map-type must be specified in all map clauses and must be either
10222 // to or alloc.
10223 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10224 if (DKind == OMPD_target_enter_data &&
10225 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10226 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010227 << (IsMapTypeImplicit ? 1 : 0)
10228 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010229 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010230 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010231 }
10232
Samuel Antao72590762016-01-19 20:04:50 +000010233 // target exit_data
10234 // OpenMP [2.10.3, Restrictions, p. 102]
10235 // A map-type must be specified in all map clauses and must be either
10236 // from, release, or delete.
10237 DKind = DSAStack->getCurrentDirective();
10238 if (DKind == OMPD_target_exit_data &&
10239 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10240 MapType == OMPC_MAP_delete)) {
10241 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010242 << (IsMapTypeImplicit ? 1 : 0)
10243 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010244 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010245 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010246 }
10247
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010248 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10249 // A list item cannot appear in both a map clause and a data-sharing
10250 // attribute clause on the same construct
10251 if (DKind == OMPD_target && VD) {
10252 auto DVar = DSAStack->getTopDSA(VD, false);
10253 if (isOpenMPPrivate(DVar.CKind)) {
10254 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10255 << getOpenMPClauseName(DVar.CKind)
10256 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Samuel Antao90927002016-04-26 14:54:23 +000010257 ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010258 continue;
10259 }
10260 }
10261
Samuel Antao90927002016-04-26 14:54:23 +000010262 // Save the current expression.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010263 Vars.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000010264
10265 // Store the components in the stack so that they can be used to check
10266 // against other clauses later on.
10267 DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10268
10269 // Save the components and declaration to create the clause. For purposes of
10270 // the clause creation, any component list that has has base 'this' uses
10271 // null has
10272 ClauseComponents.resize(ClauseComponents.size() + 1);
10273 ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10274 ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10275 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010276 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010277
Samuel Antao5de996e2016-01-22 20:21:36 +000010278 // We need to produce a map clause even if we don't have variables so that
10279 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao90927002016-04-26 14:54:23 +000010280 return OMPMapClause::Create(
10281 Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10282 ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010283}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010284
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010285QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10286 TypeResult ParsedType) {
10287 assert(ParsedType.isUsable());
10288
10289 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10290 if (ReductionType.isNull())
10291 return QualType();
10292
10293 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10294 // A type name in a declare reduction directive cannot be a function type, an
10295 // array type, a reference type, or a type qualified with const, volatile or
10296 // restrict.
10297 if (ReductionType.hasQualifiers()) {
10298 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10299 return QualType();
10300 }
10301
10302 if (ReductionType->isFunctionType()) {
10303 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10304 return QualType();
10305 }
10306 if (ReductionType->isReferenceType()) {
10307 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10308 return QualType();
10309 }
10310 if (ReductionType->isArrayType()) {
10311 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10312 return QualType();
10313 }
10314 return ReductionType;
10315}
10316
10317Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10318 Scope *S, DeclContext *DC, DeclarationName Name,
10319 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10320 AccessSpecifier AS, Decl *PrevDeclInScope) {
10321 SmallVector<Decl *, 8> Decls;
10322 Decls.reserve(ReductionTypes.size());
10323
10324 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10325 ForRedeclaration);
10326 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10327 // A reduction-identifier may not be re-declared in the current scope for the
10328 // same type or for a type that is compatible according to the base language
10329 // rules.
10330 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10331 OMPDeclareReductionDecl *PrevDRD = nullptr;
10332 bool InCompoundScope = true;
10333 if (S != nullptr) {
10334 // Find previous declaration with the same name not referenced in other
10335 // declarations.
10336 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10337 InCompoundScope =
10338 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10339 LookupName(Lookup, S);
10340 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10341 /*AllowInlineNamespace=*/false);
10342 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10343 auto Filter = Lookup.makeFilter();
10344 while (Filter.hasNext()) {
10345 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10346 if (InCompoundScope) {
10347 auto I = UsedAsPrevious.find(PrevDecl);
10348 if (I == UsedAsPrevious.end())
10349 UsedAsPrevious[PrevDecl] = false;
10350 if (auto *D = PrevDecl->getPrevDeclInScope())
10351 UsedAsPrevious[D] = true;
10352 }
10353 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10354 PrevDecl->getLocation();
10355 }
10356 Filter.done();
10357 if (InCompoundScope) {
10358 for (auto &PrevData : UsedAsPrevious) {
10359 if (!PrevData.second) {
10360 PrevDRD = PrevData.first;
10361 break;
10362 }
10363 }
10364 }
10365 } else if (PrevDeclInScope != nullptr) {
10366 auto *PrevDRDInScope = PrevDRD =
10367 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10368 do {
10369 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10370 PrevDRDInScope->getLocation();
10371 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10372 } while (PrevDRDInScope != nullptr);
10373 }
10374 for (auto &TyData : ReductionTypes) {
10375 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10376 bool Invalid = false;
10377 if (I != PreviousRedeclTypes.end()) {
10378 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10379 << TyData.first;
10380 Diag(I->second, diag::note_previous_definition);
10381 Invalid = true;
10382 }
10383 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10384 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10385 Name, TyData.first, PrevDRD);
10386 DC->addDecl(DRD);
10387 DRD->setAccess(AS);
10388 Decls.push_back(DRD);
10389 if (Invalid)
10390 DRD->setInvalidDecl();
10391 else
10392 PrevDRD = DRD;
10393 }
10394
10395 return DeclGroupPtrTy::make(
10396 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10397}
10398
10399void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10400 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10401
10402 // Enter new function scope.
10403 PushFunctionScope();
10404 getCurFunction()->setHasBranchProtectedScope();
10405 getCurFunction()->setHasOMPDeclareReductionCombiner();
10406
10407 if (S != nullptr)
10408 PushDeclContext(S, DRD);
10409 else
10410 CurContext = DRD;
10411
10412 PushExpressionEvaluationContext(PotentiallyEvaluated);
10413
10414 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010415 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10416 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10417 // uses semantics of argument handles by value, but it should be passed by
10418 // reference. C lang does not support references, so pass all parameters as
10419 // pointers.
10420 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010421 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010422 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010423 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10424 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10425 // uses semantics of argument handles by value, but it should be passed by
10426 // reference. C lang does not support references, so pass all parameters as
10427 // pointers.
10428 // Create 'T omp_out;' variable.
10429 auto *OmpOutParm =
10430 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10431 if (S != nullptr) {
10432 PushOnScopeChains(OmpInParm, S);
10433 PushOnScopeChains(OmpOutParm, S);
10434 } else {
10435 DRD->addDecl(OmpInParm);
10436 DRD->addDecl(OmpOutParm);
10437 }
10438}
10439
10440void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10441 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10442 DiscardCleanupsInEvaluationContext();
10443 PopExpressionEvaluationContext();
10444
10445 PopDeclContext();
10446 PopFunctionScopeInfo();
10447
10448 if (Combiner != nullptr)
10449 DRD->setCombiner(Combiner);
10450 else
10451 DRD->setInvalidDecl();
10452}
10453
10454void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10455 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10456
10457 // Enter new function scope.
10458 PushFunctionScope();
10459 getCurFunction()->setHasBranchProtectedScope();
10460
10461 if (S != nullptr)
10462 PushDeclContext(S, DRD);
10463 else
10464 CurContext = DRD;
10465
10466 PushExpressionEvaluationContext(PotentiallyEvaluated);
10467
10468 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010469 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10470 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10471 // uses semantics of argument handles by value, but it should be passed by
10472 // reference. C lang does not support references, so pass all parameters as
10473 // pointers.
10474 // Create 'T omp_priv;' variable.
10475 auto *OmpPrivParm =
10476 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010477 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10478 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10479 // uses semantics of argument handles by value, but it should be passed by
10480 // reference. C lang does not support references, so pass all parameters as
10481 // pointers.
10482 // Create 'T omp_orig;' variable.
10483 auto *OmpOrigParm =
10484 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010485 if (S != nullptr) {
10486 PushOnScopeChains(OmpPrivParm, S);
10487 PushOnScopeChains(OmpOrigParm, S);
10488 } else {
10489 DRD->addDecl(OmpPrivParm);
10490 DRD->addDecl(OmpOrigParm);
10491 }
10492}
10493
10494void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10495 Expr *Initializer) {
10496 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10497 DiscardCleanupsInEvaluationContext();
10498 PopExpressionEvaluationContext();
10499
10500 PopDeclContext();
10501 PopFunctionScopeInfo();
10502
10503 if (Initializer != nullptr)
10504 DRD->setInitializer(Initializer);
10505 else
10506 DRD->setInvalidDecl();
10507}
10508
10509Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10510 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10511 for (auto *D : DeclReductions.get()) {
10512 if (IsValid) {
10513 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10514 if (S != nullptr)
10515 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10516 } else
10517 D->setInvalidDecl();
10518 }
10519 return DeclReductions;
10520}
10521
Kelvin Li099bb8c2015-11-24 20:50:12 +000010522OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10523 SourceLocation StartLoc,
10524 SourceLocation LParenLoc,
10525 SourceLocation EndLoc) {
10526 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010527
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010528 // OpenMP [teams Constrcut, Restrictions]
10529 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010530 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10531 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010532 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010533
10534 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10535}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010536
10537OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10538 SourceLocation StartLoc,
10539 SourceLocation LParenLoc,
10540 SourceLocation EndLoc) {
10541 Expr *ValExpr = ThreadLimit;
10542
10543 // OpenMP [teams Constrcut, Restrictions]
10544 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010545 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10546 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010547 return nullptr;
10548
10549 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10550 EndLoc);
10551}
Alexey Bataeva0569352015-12-01 10:17:31 +000010552
10553OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10554 SourceLocation StartLoc,
10555 SourceLocation LParenLoc,
10556 SourceLocation EndLoc) {
10557 Expr *ValExpr = Priority;
10558
10559 // OpenMP [2.9.1, task Constrcut]
10560 // The priority-value is a non-negative numerical scalar expression.
10561 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10562 /*StrictlyPositive=*/false))
10563 return nullptr;
10564
10565 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10566}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010567
10568OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10569 SourceLocation StartLoc,
10570 SourceLocation LParenLoc,
10571 SourceLocation EndLoc) {
10572 Expr *ValExpr = Grainsize;
10573
10574 // OpenMP [2.9.2, taskloop Constrcut]
10575 // The parameter of the grainsize clause must be a positive integer
10576 // expression.
10577 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10578 /*StrictlyPositive=*/true))
10579 return nullptr;
10580
10581 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10582}
Alexey Bataev382967a2015-12-08 12:06:20 +000010583
10584OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10585 SourceLocation StartLoc,
10586 SourceLocation LParenLoc,
10587 SourceLocation EndLoc) {
10588 Expr *ValExpr = NumTasks;
10589
10590 // OpenMP [2.9.2, taskloop Constrcut]
10591 // The parameter of the num_tasks clause must be a positive integer
10592 // expression.
10593 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10594 /*StrictlyPositive=*/true))
10595 return nullptr;
10596
10597 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10598}
10599
Alexey Bataev28c75412015-12-15 08:19:24 +000010600OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10601 SourceLocation LParenLoc,
10602 SourceLocation EndLoc) {
10603 // OpenMP [2.13.2, critical construct, Description]
10604 // ... where hint-expression is an integer constant expression that evaluates
10605 // to a valid lock hint.
10606 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10607 if (HintExpr.isInvalid())
10608 return nullptr;
10609 return new (Context)
10610 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10611}
10612
Carlo Bertollib4adf552016-01-15 18:50:31 +000010613OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10614 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10615 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10616 SourceLocation EndLoc) {
10617 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10618 std::string Values;
10619 Values += "'";
10620 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10621 Values += "'";
10622 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10623 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10624 return nullptr;
10625 }
10626 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010627 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010628 if (ChunkSize) {
10629 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10630 !ChunkSize->isInstantiationDependent() &&
10631 !ChunkSize->containsUnexpandedParameterPack()) {
10632 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10633 ExprResult Val =
10634 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10635 if (Val.isInvalid())
10636 return nullptr;
10637
10638 ValExpr = Val.get();
10639
10640 // OpenMP [2.7.1, Restrictions]
10641 // chunk_size must be a loop invariant integer expression with a positive
10642 // value.
10643 llvm::APSInt Result;
10644 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10645 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10646 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10647 << "dist_schedule" << ChunkSize->getSourceRange();
10648 return nullptr;
10649 }
10650 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010651 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10652 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10653 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010654 }
10655 }
10656 }
10657
10658 return new (Context)
10659 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010660 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010661}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010662
10663OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10664 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10665 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10666 SourceLocation KindLoc, SourceLocation EndLoc) {
10667 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10668 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10669 Kind != OMPC_DEFAULTMAP_scalar) {
10670 std::string Value;
10671 SourceLocation Loc;
10672 Value += "'";
10673 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10674 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10675 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10676 Loc = MLoc;
10677 } else {
10678 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10679 OMPC_DEFAULTMAP_scalar);
10680 Loc = KindLoc;
10681 }
10682 Value += "'";
10683 Diag(Loc, diag::err_omp_unexpected_clause_value)
10684 << Value << getOpenMPClauseName(OMPC_defaultmap);
10685 return nullptr;
10686 }
10687
10688 return new (Context)
10689 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10690}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010691
10692bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10693 DeclContext *CurLexicalContext = getCurLexicalContext();
10694 if (!CurLexicalContext->isFileContext() &&
10695 !CurLexicalContext->isExternCContext() &&
10696 !CurLexicalContext->isExternCXXContext()) {
10697 Diag(Loc, diag::err_omp_region_not_file_context);
10698 return false;
10699 }
10700 if (IsInOpenMPDeclareTargetContext) {
10701 Diag(Loc, diag::err_omp_enclosed_declare_target);
10702 return false;
10703 }
10704
10705 IsInOpenMPDeclareTargetContext = true;
10706 return true;
10707}
10708
10709void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10710 assert(IsInOpenMPDeclareTargetContext &&
10711 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10712
10713 IsInOpenMPDeclareTargetContext = false;
10714}
10715
10716static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10717 Sema &SemaRef, Decl *D) {
10718 if (!D)
10719 return;
10720 Decl *LD = nullptr;
10721 if (isa<TagDecl>(D)) {
10722 LD = cast<TagDecl>(D)->getDefinition();
10723 } else if (isa<VarDecl>(D)) {
10724 LD = cast<VarDecl>(D)->getDefinition();
10725
10726 // If this is an implicit variable that is legal and we do not need to do
10727 // anything.
10728 if (cast<VarDecl>(D)->isImplicit()) {
10729 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10730 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10731 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10732 return;
10733 }
10734
10735 } else if (isa<FunctionDecl>(D)) {
10736 const FunctionDecl *FD = nullptr;
10737 if (cast<FunctionDecl>(D)->hasBody(FD))
10738 LD = const_cast<FunctionDecl *>(FD);
10739
10740 // If the definition is associated with the current declaration in the
10741 // target region (it can be e.g. a lambda) that is legal and we do not need
10742 // to do anything else.
10743 if (LD == D) {
10744 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10745 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10746 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10747 return;
10748 }
10749 }
10750 if (!LD)
10751 LD = D;
10752 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10753 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10754 // Outlined declaration is not declared target.
10755 if (LD->isOutOfLine()) {
10756 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10757 SemaRef.Diag(SL, diag::note_used_here) << SR;
10758 } else {
10759 DeclContext *DC = LD->getDeclContext();
10760 while (DC) {
10761 if (isa<FunctionDecl>(DC) &&
10762 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10763 break;
10764 DC = DC->getParent();
10765 }
10766 if (DC)
10767 return;
10768
10769 // Is not declared in target context.
10770 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10771 SemaRef.Diag(SL, diag::note_used_here) << SR;
10772 }
10773 // Mark decl as declared target to prevent further diagnostic.
10774 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10775 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10776 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10777 }
10778}
10779
10780static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10781 Sema &SemaRef, DSAStackTy *Stack,
10782 ValueDecl *VD) {
10783 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10784 return true;
10785 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10786 return false;
10787 return true;
10788}
10789
10790void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10791 if (!D || D->isInvalidDecl())
10792 return;
10793 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10794 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10795 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10796 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10797 if (DSAStack->isThreadPrivate(VD)) {
10798 Diag(SL, diag::err_omp_threadprivate_in_target);
10799 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10800 return;
10801 }
10802 }
10803 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10804 // Problem if any with var declared with incomplete type will be reported
10805 // as normal, so no need to check it here.
10806 if ((E || !VD->getType()->isIncompleteType()) &&
10807 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10808 // Mark decl as declared target to prevent further diagnostic.
10809 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10810 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10811 if (ASTMutationListener *ML = Context.getASTMutationListener())
10812 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10813 }
10814 return;
10815 }
10816 }
10817 if (!E) {
10818 // Checking declaration inside declare target region.
10819 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10820 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10821 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10822 if (ASTMutationListener *ML = Context.getASTMutationListener())
10823 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10824 }
10825 return;
10826 }
10827 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10828}