blob: 97647c3f99c2d8a23adbe7c5411b2c9359a017cd [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000027#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000028#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000031#include "clang/Sema/Scope.h"
32#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000033#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000034using namespace clang;
35
Alexey Bataev758e55e2013-09-06 18:03:48 +000036//===----------------------------------------------------------------------===//
37// Stack of data-sharing attributes for variables
38//===----------------------------------------------------------------------===//
39
40namespace {
41/// \brief Default data sharing attributes, which can be applied to directive.
42enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000043 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
45 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046};
Alexey Bataev7ff55242014-06-19 09:13:45 +000047
Alexey Bataevf29276e2014-06-18 04:14:57 +000048template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000049 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000050 bool operator()(T Kind) {
51 for (auto KindEl : Arr)
52 if (KindEl == Kind)
53 return true;
54 return false;
55 }
56
57private:
58 ArrayRef<T> Arr;
59};
Alexey Bataev23b69422014-06-18 07:08:49 +000060struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000061 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000062 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000063};
64
65typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
66typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000067
68/// \brief Stack for tracking declarations used in OpenMP directives and
69/// clauses and their data-sharing attributes.
70class DSAStackTy {
71public:
72 struct DSAVarData {
73 OpenMPDirectiveKind DKind;
74 OpenMPClauseKind CKind;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000075 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000076 DeclRefExpr *PrivateCopy;
Alexey Bataevbae9a792014-06-27 10:37:06 +000077 SourceLocation ImplicitDSALoc;
78 DSAVarData()
79 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
Alexey Bataev90c228f2016-02-08 09:29:13 +000080 PrivateCopy(nullptr), ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000081 };
Alexey Bataeved09d242014-05-28 05:53:51 +000082
Alexey Bataev758e55e2013-09-06 18:03:48 +000083private:
Samuel Antao5de996e2016-01-22 20:21:36 +000084 typedef SmallVector<Expr *, 4> MapInfo;
85
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 struct DSAInfo {
87 OpenMPClauseKind Attributes;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000088 Expr *RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +000089 DeclRefExpr *PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 };
Alexey Bataev90c228f2016-02-08 09:29:13 +000091 typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
92 typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +000093 typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
94 typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
Alexey Bataev90c228f2016-02-08 09:29:13 +000095 typedef llvm::DenseMap<ValueDecl *, MapInfo> MappedDeclsTy;
Alexey Bataev28c75412015-12-15 08:19:24 +000096 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000098
99 struct SharingMapTy {
100 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000102 MappedDeclsTy MappedDecls;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000103 LoopControlVariablesMapTy LCVMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 OpenMPDirectiveKind Directive;
107 DeclarationNameInfo DirectiveName;
108 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000109 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000110 /// \brief first argument (Expr *) contains optional argument of the
111 /// 'ordered' clause, the second one is true if the regions has 'ordered'
112 /// clause, false otherwise.
113 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000114 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000115 bool CancelRegion;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000116 unsigned AssociatedLoops;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000117 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000118 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Scope *CurScope, SourceLocation Loc)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000120 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000121 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000122 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000123 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124 SharingMapTy()
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000127 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000128 CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000129 };
130
Axel Naumann323862e2016-02-03 10:45:22 +0000131 typedef SmallVector<SharingMapTy, 4> StackTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132
133 /// \brief Stack of used declaration and their data-sharing attributes.
134 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000135 /// \brief true, if check for DSA must be from parent directive, false, if
136 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000137 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000138 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000139 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000140 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141
142 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
143
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000144 DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000145
146 /// \brief Checks if the variable is a local for OpenMP region.
147 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000148
Alexey Bataev758e55e2013-09-06 18:03:48 +0000149public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000150 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000151 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
152 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000153
Alexey Bataevaac108a2015-06-23 04:51:00 +0000154 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
155 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000156
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000157 bool isForceVarCapturing() const { return ForceCapturing; }
158 void setForceVarCapturing(bool V) { ForceCapturing = V; }
159
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000161 Scope *CurScope, SourceLocation Loc) {
162 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
163 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000164 }
165
166 void pop() {
167 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
168 Stack.pop_back();
169 }
170
Alexey Bataev28c75412015-12-15 08:19:24 +0000171 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
172 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
173 }
174 const std::pair<OMPCriticalDirective *, llvm::APSInt>
175 getCriticalWithHint(const DeclarationNameInfo &Name) const {
176 auto I = Criticals.find(Name.getAsString());
177 if (I != Criticals.end())
178 return I->second;
179 return std::make_pair(nullptr, llvm::APSInt());
180 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000182 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000183 /// for diagnostics.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000184 Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000185
Alexey Bataev9c821032015-04-30 04:23:23 +0000186 /// \brief Register specified variable as loop control variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000187 void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
Alexey Bataev9c821032015-04-30 04:23:23 +0000188 /// \brief Check if the specified variable is a loop control variable for
189 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000190 /// \return The index of the loop control variable in the list of associated
191 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000192 LCDeclInfo isLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000193 /// \brief Check if the specified variable is a loop control variable for
194 /// parent region.
195 /// \return The index of the loop control variable in the list of associated
196 /// for-loops (from outer to inner).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000197 LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000198 /// \brief Get the loop control variable for the I-th loop (or nullptr) in
199 /// parent directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000200 ValueDecl *getParentLoopControlVariable(unsigned I);
Alexey Bataev9c821032015-04-30 04:23:23 +0000201
Alexey Bataev758e55e2013-09-06 18:03:48 +0000202 /// \brief Adds explicit data sharing attribute to the specified declaration.
Alexey Bataev90c228f2016-02-08 09:29:13 +0000203 void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
204 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Returns data sharing attributes from top of the stack for the
207 /// specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000208 DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000210 DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000211 /// \brief Checks if the specified variables has data-sharing attributes which
212 /// match specified \a CPred predicate in any directive which matches \a DPred
213 /// predicate.
214 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000215 DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000216 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000217 /// \brief Checks if the specified variables has data-sharing attributes which
218 /// match specified \a CPred predicate in any innermost directive which
219 /// matches \a DPred predicate.
220 template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000221 DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
222 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000223 /// \brief Checks if the specified variables has explicit data-sharing
224 /// attributes which match specified \a CPred predicate at the specified
225 /// OpenMP region.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000226 bool hasExplicitDSA(ValueDecl *D,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000227 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
228 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000229
230 /// \brief Returns true if the directive at level \Level matches in the
231 /// specified \a DPred predicate.
232 bool hasExplicitDirective(
233 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
234 unsigned Level);
235
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000236 /// \brief Finds a directive which matches specified \a DPred predicate.
237 template <class NamedDirectivesPredicate>
238 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000239
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240 /// \brief Returns currently analyzed directive.
241 OpenMPDirectiveKind getCurrentDirective() const {
242 return Stack.back().Directive;
243 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000244 /// \brief Returns parent directive.
245 OpenMPDirectiveKind getParentDirective() const {
246 if (Stack.size() > 2)
247 return Stack[Stack.size() - 2].Directive;
248 return OMPD_unknown;
249 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000250 /// \brief Return the directive associated with the provided scope.
251 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000252
253 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000254 void setDefaultDSANone(SourceLocation Loc) {
255 Stack.back().DefaultAttr = DSA_none;
256 Stack.back().DefaultAttrLoc = Loc;
257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000259 void setDefaultDSAShared(SourceLocation Loc) {
260 Stack.back().DefaultAttr = DSA_shared;
261 Stack.back().DefaultAttrLoc = Loc;
262 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000263
264 DefaultDataSharingAttributes getDefaultDSA() const {
265 return Stack.back().DefaultAttr;
266 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000267 SourceLocation getDefaultDSALocation() const {
268 return Stack.back().DefaultAttrLoc;
269 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270
Alexey Bataevf29276e2014-06-18 04:14:57 +0000271 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000273 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000274 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000275 }
276
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000277 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000278 void setOrderedRegion(bool IsOrdered, Expr *Param) {
279 Stack.back().OrderedRegion.setInt(IsOrdered);
280 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000281 }
282 /// \brief Returns true, if parent region is ordered (has associated
283 /// 'ordered' clause), false - otherwise.
284 bool isParentOrderedRegion() const {
285 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000286 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000287 return false;
288 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000289 /// \brief Returns optional parameter for the ordered region.
290 Expr *getParentOrderedRegionParam() const {
291 if (Stack.size() > 2)
292 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
293 return nullptr;
294 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000295 /// \brief Marks current region as nowait (it has a 'nowait' clause).
296 void setNowaitRegion(bool IsNowait = true) {
297 Stack.back().NowaitRegion = IsNowait;
298 }
299 /// \brief Returns true, if parent region is nowait (has associated
300 /// 'nowait' clause), false - otherwise.
301 bool isParentNowaitRegion() const {
302 if (Stack.size() > 2)
303 return Stack[Stack.size() - 2].NowaitRegion;
304 return false;
305 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000306 /// \brief Marks parent region as cancel region.
307 void setParentCancelRegion(bool Cancel = true) {
308 if (Stack.size() > 2)
309 Stack[Stack.size() - 2].CancelRegion =
310 Stack[Stack.size() - 2].CancelRegion || Cancel;
311 }
312 /// \brief Return true if current region has inner cancel construct.
313 bool isCancelRegion() const {
314 return Stack.back().CancelRegion;
315 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000316
Alexey Bataev9c821032015-04-30 04:23:23 +0000317 /// \brief Set collapse value for the region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000318 void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000319 /// \brief Return collapse value for region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000320 unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
Alexey Bataev9c821032015-04-30 04:23:23 +0000321
Alexey Bataev13314bf2014-10-09 04:18:56 +0000322 /// \brief Marks current target region as one with closely nested teams
323 /// region.
324 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
325 if (Stack.size() > 2)
326 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
327 }
328 /// \brief Returns true, if current region has closely nested teams region.
329 bool hasInnerTeamsRegion() const {
330 return getInnerTeamsRegionLoc().isValid();
331 }
332 /// \brief Returns location of the nested teams region (if any).
333 SourceLocation getInnerTeamsRegionLoc() const {
334 if (Stack.size() > 1)
335 return Stack.back().InnerTeamsRegionLoc;
336 return SourceLocation();
337 }
338
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000339 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000341 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000342
Samuel Antao5de996e2016-01-22 20:21:36 +0000343 // Do the check specified in MapInfoCheck and return true if any issue is
344 // found.
345 template <class MapInfoCheck>
346 bool checkMapInfoForVar(ValueDecl *VD, bool CurrentRegionOnly,
347 MapInfoCheck Check) {
348 auto SI = Stack.rbegin();
349 auto SE = Stack.rend();
350
351 if (SI == SE)
352 return false;
353
354 if (CurrentRegionOnly) {
355 SE = std::next(SI);
356 } else {
357 ++SI;
358 }
359
360 for (; SI != SE; ++SI) {
361 auto MI = SI->MappedDecls.find(VD);
362 if (MI != SI->MappedDecls.end()) {
363 for (Expr *E : MI->second) {
364 if (Check(E))
365 return true;
366 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000367 }
368 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000369 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000370 }
371
Samuel Antao5de996e2016-01-22 20:21:36 +0000372 void addExprToVarMapInfo(ValueDecl *VD, Expr *E) {
Kelvin Li0bff7af2015-11-23 05:32:03 +0000373 if (Stack.size() > 1) {
Samuel Antao5de996e2016-01-22 20:21:36 +0000374 Stack.back().MappedDecls[VD].push_back(E);
Kelvin Li0bff7af2015-11-23 05:32:03 +0000375 }
376 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000378bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000379 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
380 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000381}
Alexey Bataeved09d242014-05-28 05:53:51 +0000382} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000384static ValueDecl *getCanonicalDecl(ValueDecl *D) {
385 auto *VD = dyn_cast<VarDecl>(D);
386 auto *FD = dyn_cast<FieldDecl>(D);
387 if (VD != nullptr) {
388 VD = VD->getCanonicalDecl();
389 D = VD;
390 } else {
391 assert(FD);
392 FD = FD->getCanonicalDecl();
393 D = FD;
394 }
395 return D;
396}
397
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000398DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000399 ValueDecl *D) {
400 D = getCanonicalDecl(D);
401 auto *VD = dyn_cast<VarDecl>(D);
402 auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000404 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406 // in a region but not in construct]
407 // File-scope or namespace-scope variables referenced in called routines
408 // in the region are shared unless they appear in a threadprivate
409 // directive.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000410 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000411 DVar.CKind = OMPC_shared;
412
413 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
414 // in a region but not in construct]
415 // Variables with static storage duration that are declared in called
416 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000417 if (VD && VD->hasGlobalStorage())
418 DVar.CKind = OMPC_shared;
419
420 // Non-static data members are shared by default.
421 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000422 DVar.CKind = OMPC_shared;
423
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 return DVar;
425 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000426
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
429 // in a Construct, C/C++, predetermined, p.1]
430 // Variables with automatic storage duration that are declared in a scope
431 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000432 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
433 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000434 DVar.CKind = OMPC_private;
435 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000436 }
437
Alexey Bataev758e55e2013-09-06 18:03:48 +0000438 // Explicitly specified attributes and local variables with predetermined
439 // attributes.
440 if (Iter->SharingMap.count(D)) {
441 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000442 DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000444 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 return DVar;
446 }
447
448 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
449 // in a Construct, C/C++, implicitly determined, p.1]
450 // In a parallel or task construct, the data-sharing attributes of these
451 // variables are determined by the default clause, if present.
452 switch (Iter->DefaultAttr) {
453 case DSA_shared:
454 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000455 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456 return DVar;
457 case DSA_none:
458 return DVar;
459 case DSA_unspecified:
460 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
461 // in a Construct, implicitly determined, p.2]
462 // In a parallel construct, if no default clause is present, these
463 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000464 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000465 if (isOpenMPParallelDirective(DVar.DKind) ||
466 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000467 DVar.CKind = OMPC_shared;
468 return DVar;
469 }
470
471 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
472 // in a Construct, implicitly determined, p.4]
473 // In a task construct, if no default clause is present, a variable that in
474 // the enclosing context is determined to be shared by all implicit tasks
475 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000476 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000478 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000479 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000480 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000481 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000482 // In a task construct, if no default clause is present, a variable
483 // whose data-sharing attribute is not determined by the rules above is
484 // firstprivate.
485 DVarTemp = getDSA(I, D);
486 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000487 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000488 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000489 return DVar;
490 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000492 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000494 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000495 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496 return DVar;
497 }
498 }
499 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
500 // in a Construct, implicitly determined, p.3]
501 // For constructs other than task, if no default clause is present, these
502 // variables inherit their data-sharing attributes from the enclosing
503 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000504 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000505}
506
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000507Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000508 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000509 D = getCanonicalDecl(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000510 auto It = Stack.back().AlignedMap.find(D);
511 if (It == Stack.back().AlignedMap.end()) {
512 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
513 Stack.back().AlignedMap[D] = NewDE;
514 return nullptr;
515 } else {
516 assert(It->second && "Unexpected nullptr expr in the aligned map");
517 return It->second;
518 }
519 return nullptr;
520}
521
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000522void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000523 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000524 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000525 Stack.back().LCVMap.insert(
526 std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
Alexey Bataev9c821032015-04-30 04:23:23 +0000527}
528
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000529DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
Alexey Bataev9c821032015-04-30 04:23:23 +0000530 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000531 D = getCanonicalDecl(D);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000532 return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
533 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000534}
535
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000536DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000537 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000538 D = getCanonicalDecl(D);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000539 return Stack[Stack.size() - 2].LCVMap.count(D) > 0
540 ? Stack[Stack.size() - 2].LCVMap[D]
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000541 : LCDeclInfo(0, nullptr);
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000542}
543
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000544ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000545 assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
546 if (Stack[Stack.size() - 2].LCVMap.size() < I)
547 return nullptr;
548 for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000549 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000550 return Pair.first;
551 }
552 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000553}
554
Alexey Bataev90c228f2016-02-08 09:29:13 +0000555void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
556 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000557 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000558 if (A == OMPC_threadprivate) {
559 Stack[0].SharingMap[D].Attributes = A;
560 Stack[0].SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000561 Stack[0].SharingMap[D].PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 } else {
563 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
564 Stack.back().SharingMap[D].Attributes = A;
565 Stack.back().SharingMap[D].RefExpr = E;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000566 Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
567 if (PrivateCopy)
568 addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000569 }
570}
571
Alexey Bataeved09d242014-05-28 05:53:51 +0000572bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000573 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000574 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000575 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000576 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000577 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 ++I;
579 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000580 if (I == E)
581 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000582 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 Scope *CurScope = getCurScope();
584 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000585 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000586 }
587 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000588 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000589 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000590}
591
Alexey Bataev39f915b82015-05-08 10:41:21 +0000592/// \brief Build a variable declaration for OpenMP loop iteration variable.
593static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000594 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000595 DeclContext *DC = SemaRef.CurContext;
596 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
597 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
598 VarDecl *Decl =
599 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000600 if (Attrs) {
601 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
602 I != E; ++I)
603 Decl->addAttr(*I);
604 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000605 Decl->setImplicit();
606 return Decl;
607}
608
609static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
610 SourceLocation Loc,
611 bool RefersToCapture = false) {
612 D->setReferenced();
613 D->markUsed(S.Context);
614 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
615 SourceLocation(), D, RefersToCapture, Loc, Ty,
616 VK_LValue);
617}
618
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000619DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
620 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621 DSAVarData DVar;
622
623 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
624 // in a Construct, C/C++, predetermined, p.1]
625 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000626 auto *VD = dyn_cast<VarDecl>(D);
627 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
628 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
Samuel Antaof8b50122015-07-13 22:54:53 +0000629 SemaRef.getLangOpts().OpenMPUseTLS &&
630 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000631 (VD && VD->getStorageClass() == SC_Register &&
632 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
633 addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
Alexey Bataev39f915b82015-05-08 10:41:21 +0000634 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000635 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000636 }
637 if (Stack[0].SharingMap.count(D)) {
638 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
639 DVar.CKind = OMPC_threadprivate;
640 return DVar;
641 }
642
Dmitry Polukhin0b0da292016-04-06 11:38:59 +0000643 if (Stack.size() == 1) {
644 // Not in OpenMP execution region and top scope was already checked.
645 return DVar;
646 }
647
Alexey Bataev758e55e2013-09-06 18:03:48 +0000648 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000649 // in a Construct, C/C++, predetermined, p.4]
650 // Static data members are shared.
651 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
652 // in a Construct, C/C++, predetermined, p.7]
653 // Variables with static storage duration that are declared in a scope
654 // inside the construct are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000655 if (VD && VD->isStaticDataMember()) {
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000656 DSAVarData DVarTemp =
657 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
658 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000659 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000660
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000661 DVar.CKind = OMPC_shared;
662 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000663 }
664
665 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000666 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
667 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000668 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
669 // in a Construct, C/C++, predetermined, p.6]
670 // Variables with const qualified type having no mutable member are
671 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000672 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000673 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000674 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
675 if (auto *CTD = CTSD->getSpecializedTemplate())
676 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +0000678 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
679 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000680 // Variables with const-qualified type having no mutable member may be
681 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000682 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
683 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000684 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
685 return DVar;
686
Alexey Bataev758e55e2013-09-06 18:03:48 +0000687 DVar.CKind = OMPC_shared;
688 return DVar;
689 }
690
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 // Explicitly specified attributes and local variables with predetermined
692 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000693 auto StartI = std::next(Stack.rbegin());
694 auto EndI = std::prev(Stack.rend());
695 if (FromParent && StartI != EndI) {
696 StartI = std::next(StartI);
697 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000698 auto I = std::prev(StartI);
699 if (I->SharingMap.count(D)) {
700 DVar.RefExpr = I->SharingMap[D].RefExpr;
Alexey Bataev90c228f2016-02-08 09:29:13 +0000701 DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000702 DVar.CKind = I->SharingMap[D].Attributes;
703 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000704 }
705
706 return DVar;
707}
708
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000709DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
710 bool FromParent) {
711 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000712 auto StartI = Stack.rbegin();
713 auto EndI = std::prev(Stack.rend());
714 if (FromParent && StartI != EndI) {
715 StartI = std::next(StartI);
716 }
717 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000718}
719
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000721DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000722 DirectivesPredicate DPred,
723 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000724 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000725 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000726 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000727 if (FromParent && StartI != EndI) {
728 StartI = std::next(StartI);
729 }
730 for (auto I = StartI, EE = EndI; I != EE; ++I) {
731 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000733 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000734 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000735 return DVar;
736 }
737 return DSAVarData();
738}
739
Alexey Bataevf29276e2014-06-18 04:14:57 +0000740template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000741DSAStackTy::DSAVarData
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000742DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000743 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000744 D = getCanonicalDecl(D);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000745 auto StartI = std::next(Stack.rbegin());
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000746 auto EndI = Stack.rend();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000747 if (FromParent && StartI != EndI) {
748 StartI = std::next(StartI);
749 }
750 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000751 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000752 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000753 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000754 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000755 return DVar;
756 return DSAVarData();
757 }
758 return DSAVarData();
759}
760
Alexey Bataevaac108a2015-06-23 04:51:00 +0000761bool DSAStackTy::hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000762 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
Alexey Bataevaac108a2015-06-23 04:51:00 +0000763 unsigned Level) {
764 if (CPred(ClauseKindMode))
765 return true;
766 if (isClauseParsingMode())
767 ++Level;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000768 D = getCanonicalDecl(D);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000769 auto StartI = Stack.rbegin();
770 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000771 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000772 return false;
773 std::advance(StartI, Level);
774 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
775 CPred(StartI->SharingMap[D].Attributes);
776}
777
Samuel Antao4be30e92015-10-02 17:14:03 +0000778bool DSAStackTy::hasExplicitDirective(
779 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
780 unsigned Level) {
781 if (isClauseParsingMode())
782 ++Level;
783 auto StartI = Stack.rbegin();
784 auto EndI = std::prev(Stack.rend());
785 if (std::distance(StartI, EndI) <= (int)Level)
786 return false;
787 std::advance(StartI, Level);
788 return DPred(StartI->Directive);
789}
790
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000791template <class NamedDirectivesPredicate>
792bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
793 auto StartI = std::next(Stack.rbegin());
794 auto EndI = std::prev(Stack.rend());
795 if (FromParent && StartI != EndI) {
796 StartI = std::next(StartI);
797 }
798 for (auto I = StartI, EE = EndI; I != EE; ++I) {
799 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
800 return true;
801 }
802 return false;
803}
804
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000805OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
806 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
807 if (I->CurScope == S)
808 return I->Directive;
809 return OMPD_unknown;
810}
811
Alexey Bataev758e55e2013-09-06 18:03:48 +0000812void Sema::InitDataSharingAttributesStack() {
813 VarDataSharingAttributesStack = new DSAStackTy(*this);
814}
815
816#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
817
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000818bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000819 const CapturedRegionScopeInfo *RSI) {
820 assert(LangOpts.OpenMP && "OpenMP is not allowed");
821
822 auto &Ctx = getASTContext();
823 bool IsByRef = true;
824
825 // Find the directive that is associated with the provided scope.
826 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000827 auto Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000828
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000829 if (isOpenMPTargetExecutionDirective(DKind)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000830 // This table summarizes how a given variable should be passed to the device
831 // given its type and the clauses where it appears. This table is based on
832 // the description in OpenMP 4.5 [2.10.4, target Construct] and
833 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
834 //
835 // =========================================================================
836 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
837 // | |(tofrom:scalar)| | pvt | | | |
838 // =========================================================================
839 // | scl | | | | - | | bycopy|
840 // | scl | | - | x | - | - | bycopy|
841 // | scl | | x | - | - | - | null |
842 // | scl | x | | | - | | byref |
843 // | scl | x | - | x | - | - | bycopy|
844 // | scl | x | x | - | - | - | null |
845 // | scl | | - | - | - | x | byref |
846 // | scl | x | - | - | - | x | byref |
847 //
848 // | agg | n.a. | | | - | | byref |
849 // | agg | n.a. | - | x | - | - | byref |
850 // | agg | n.a. | x | - | - | - | null |
851 // | agg | n.a. | - | - | - | x | byref |
852 // | agg | n.a. | - | - | - | x[] | byref |
853 //
854 // | ptr | n.a. | | | - | | bycopy|
855 // | ptr | n.a. | - | x | - | - | bycopy|
856 // | ptr | n.a. | x | - | - | - | null |
857 // | ptr | n.a. | - | - | - | x | byref |
858 // | ptr | n.a. | - | - | - | x[] | bycopy|
859 // | ptr | n.a. | - | - | x | | bycopy|
860 // | ptr | n.a. | - | - | x | x | bycopy|
861 // | ptr | n.a. | - | - | x | x[] | bycopy|
862 // =========================================================================
863 // Legend:
864 // scl - scalar
865 // ptr - pointer
866 // agg - aggregate
867 // x - applies
868 // - - invalid in this combination
869 // [] - mapped with an array section
870 // byref - should be mapped by reference
871 // byval - should be mapped by value
872 // null - initialize a local variable to null on the device
873 //
874 // Observations:
875 // - All scalar declarations that show up in a map clause have to be passed
876 // by reference, because they may have been mapped in the enclosing data
877 // environment.
878 // - If the scalar value does not fit the size of uintptr, it has to be
879 // passed by reference, regardless the result in the table above.
880 // - For pointers mapped by value that have either an implicit map or an
881 // array section, the runtime library may pass the NULL value to the
882 // device instead of the value passed to it by the compiler.
883
884 // FIXME: Right now, only implicit maps are implemented. Properly mapping
885 // values requires having the map, private, and firstprivate clauses SEMA
886 // and parsing in place, which we don't yet.
887
888 if (Ty->isReferenceType())
889 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
890 IsByRef = !Ty->isScalarType();
891 }
892
893 // When passing data by value, we need to make sure it fits the uintptr size
894 // and alignment, because the runtime library only deals with uintptr types.
895 // If it does not fit the uintptr size, we need to pass the data by reference
896 // instead.
897 if (!IsByRef &&
898 (Ctx.getTypeSizeInChars(Ty) >
899 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000900 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000901 IsByRef = true;
902
903 return IsByRef;
904}
905
Alexey Bataev90c228f2016-02-08 09:29:13 +0000906VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +0000907 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000908 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +0000909
910 // If we are attempting to capture a global variable in a directive with
911 // 'target' we return true so that this global is also mapped to the device.
912 //
913 // FIXME: If the declaration is enclosed in a 'declare target' directive,
914 // then it should not be captured. Therefore, an extra check has to be
915 // inserted here once support for 'declare target' is added.
916 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000917 auto *VD = dyn_cast<VarDecl>(D);
918 if (VD && !VD->hasLocalStorage()) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000919 if (DSAStack->getCurrentDirective() == OMPD_target &&
Alexey Bataev90c228f2016-02-08 09:29:13 +0000920 !DSAStack->isClauseParsingMode())
921 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000922 if (DSAStack->getCurScope() &&
923 DSAStack->hasDirective(
924 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
925 SourceLocation Loc) -> bool {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000926 return isOpenMPTargetExecutionDirective(K);
Samuel Antao4be30e92015-10-02 17:14:03 +0000927 },
Alexey Bataev90c228f2016-02-08 09:29:13 +0000928 false))
929 return VD;
Samuel Antao4be30e92015-10-02 17:14:03 +0000930 }
931
Alexey Bataev48977c32015-08-04 08:10:48 +0000932 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
933 (!DSAStack->isClauseParsingMode() ||
934 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000935 auto &&Info = DSAStack->isLoopControlVariable(D);
936 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000937 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000938 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000939 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000940 return VD ? VD : Info.second;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000941 auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000942 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +0000943 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000944 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000945 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +0000946 if (DVarPrivate.CKind != OMPC_unknown)
947 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000948 }
Alexey Bataev90c228f2016-02-08 09:29:13 +0000949 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +0000950}
951
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000952bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
Alexey Bataevaac108a2015-06-23 04:51:00 +0000953 assert(LangOpts.OpenMP && "OpenMP is not allowed");
954 return DSAStack->hasExplicitDSA(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000955 D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000956}
957
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000958bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
Samuel Antao4be30e92015-10-02 17:14:03 +0000959 assert(LangOpts.OpenMP && "OpenMP is not allowed");
960 // Return true if the current level is no longer enclosed in a target region.
961
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000962 auto *VD = dyn_cast<VarDecl>(D);
963 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +0000964 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
965 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000966}
967
Alexey Bataeved09d242014-05-28 05:53:51 +0000968void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969
970void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
971 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000972 Scope *CurScope, SourceLocation Loc) {
973 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000974 PushExpressionEvaluationContext(PotentiallyEvaluated);
975}
976
Alexey Bataevaac108a2015-06-23 04:51:00 +0000977void Sema::StartOpenMPClause(OpenMPClauseKind K) {
978 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000979}
980
Alexey Bataevaac108a2015-06-23 04:51:00 +0000981void Sema::EndOpenMPClause() {
982 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000983}
984
Alexey Bataev758e55e2013-09-06 18:03:48 +0000985void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000986 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
987 // A variable of class type (or array thereof) that appears in a lastprivate
988 // clause requires an accessible, unambiguous default constructor for the
989 // class type, unless the list item is also specified in a firstprivate
990 // clause.
991 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000992 for (auto *C : D->clauses()) {
993 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
994 SmallVector<Expr *, 8> PrivateCopies;
995 for (auto *DE : Clause->varlists()) {
996 if (DE->isValueDependent() || DE->isTypeDependent()) {
997 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000998 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000999 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001000 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataev005248a2016-02-25 05:25:57 +00001001 VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1002 QualType Type = VD->getType().getNonReferenceType();
1003 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001004 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001005 // Generate helper private variable and initialize it with the
1006 // default value. The address of the original variable is replaced
1007 // by the address of the new private variable in CodeGen. This new
1008 // variable is not added to IdResolver, so the code in the OpenMP
1009 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001010 auto *VDPrivate = buildVarDecl(
1011 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev005248a2016-02-25 05:25:57 +00001012 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00001013 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1014 if (VDPrivate->isInvalidDecl())
1015 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001016 PrivateCopies.push_back(buildDeclRefExpr(
1017 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001018 } else {
1019 // The variable is also a firstprivate, so initialization sequence
1020 // for private copy is generated already.
1021 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001022 }
1023 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001024 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001025 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001026 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001027 }
1028 }
1029 }
1030
Alexey Bataev758e55e2013-09-06 18:03:48 +00001031 DSAStack->pop();
1032 DiscardCleanupsInEvaluationContext();
1033 PopExpressionEvaluationContext();
1034}
1035
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001036static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1037 Expr *NumIterations, Sema &SemaRef,
1038 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001039
Alexey Bataeva769e072013-03-22 06:34:35 +00001040namespace {
1041
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001042class VarDeclFilterCCC : public CorrectionCandidateCallback {
1043private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001044 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001045
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001046public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001047 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001048 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 NamedDecl *ND = Candidate.getCorrectionDecl();
1050 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1051 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001052 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1053 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001054 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001055 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001056 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001057};
Alexey Bataeved09d242014-05-28 05:53:51 +00001058} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001059
1060ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1061 CXXScopeSpec &ScopeSpec,
1062 const DeclarationNameInfo &Id) {
1063 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1064 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1065
1066 if (Lookup.isAmbiguous())
1067 return ExprError();
1068
1069 VarDecl *VD;
1070 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001071 if (TypoCorrection Corrected = CorrectTypo(
1072 Id, LookupOrdinaryName, CurScope, nullptr,
1073 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001074 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001075 PDiag(Lookup.empty()
1076 ? diag::err_undeclared_var_use_suggest
1077 : diag::err_omp_expected_var_arg_suggest)
1078 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001079 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001080 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001081 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1082 : diag::err_omp_expected_var_arg)
1083 << Id.getName();
1084 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001085 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 } else {
1087 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001088 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001089 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1090 return ExprError();
1091 }
1092 }
1093 Lookup.suppressDiagnostics();
1094
1095 // OpenMP [2.9.2, Syntax, C/C++]
1096 // Variables must be file-scope, namespace-scope, or static block-scope.
1097 if (!VD->hasGlobalStorage()) {
1098 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001099 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1100 bool IsDecl =
1101 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1104 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001105 return ExprError();
1106 }
1107
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001108 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1109 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1111 // A threadprivate directive for file-scope variables must appear outside
1112 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001113 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1114 !getCurLexicalContext()->isTranslationUnit()) {
1115 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001116 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1117 bool IsDecl =
1118 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1119 Diag(VD->getLocation(),
1120 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1121 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001122 return ExprError();
1123 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001124 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1125 // A threadprivate directive for static class member variables must appear
1126 // in the class definition, in the same scope in which the member
1127 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001128 if (CanonicalVD->isStaticDataMember() &&
1129 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1130 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001131 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1132 bool IsDecl =
1133 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1134 Diag(VD->getLocation(),
1135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1136 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001137 return ExprError();
1138 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001139 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1140 // A threadprivate directive for namespace-scope variables must appear
1141 // outside any definition or declaration other than the namespace
1142 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001143 if (CanonicalVD->getDeclContext()->isNamespace() &&
1144 (!getCurLexicalContext()->isFileContext() ||
1145 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1146 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001147 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1148 bool IsDecl =
1149 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1150 Diag(VD->getLocation(),
1151 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1152 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001153 return ExprError();
1154 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001155 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1156 // A threadprivate directive for static block-scope variables must appear
1157 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001158 if (CanonicalVD->isStaticLocal() && CurScope &&
1159 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001160 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001161 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1162 bool IsDecl =
1163 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1164 Diag(VD->getLocation(),
1165 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1166 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 return ExprError();
1168 }
1169
1170 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1171 // A threadprivate directive must lexically precede all references to any
1172 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001173 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001174 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001176 return ExprError();
1177 }
1178
1179 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001180 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1181 SourceLocation(), VD,
1182 /*RefersToEnclosingVariableOrCapture=*/false,
1183 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001184}
1185
Alexey Bataeved09d242014-05-28 05:53:51 +00001186Sema::DeclGroupPtrTy
1187Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1188 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001189 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001190 CurContext->addDecl(D);
1191 return DeclGroupPtrTy::make(DeclGroupRef(D));
1192 }
David Blaikie0403cb12016-01-15 23:43:25 +00001193 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001194}
1195
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001196namespace {
1197class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1198 Sema &SemaRef;
1199
1200public:
1201 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1202 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1203 if (VD->hasLocalStorage()) {
1204 SemaRef.Diag(E->getLocStart(),
1205 diag::err_omp_local_var_in_threadprivate_init)
1206 << E->getSourceRange();
1207 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1208 << VD << VD->getSourceRange();
1209 return true;
1210 }
1211 }
1212 return false;
1213 }
1214 bool VisitStmt(const Stmt *S) {
1215 for (auto Child : S->children()) {
1216 if (Child && Visit(Child))
1217 return true;
1218 }
1219 return false;
1220 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001221 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001222};
1223} // namespace
1224
Alexey Bataeved09d242014-05-28 05:53:51 +00001225OMPThreadPrivateDecl *
1226Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001227 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001228 for (auto &RefExpr : VarList) {
1229 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001230 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1231 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001232
Alexey Bataev376b4a42016-02-09 09:41:09 +00001233 // Mark variable as used.
1234 VD->setReferenced();
1235 VD->markUsed(Context);
1236
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001237 QualType QType = VD->getType();
1238 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1239 // It will be analyzed later.
1240 Vars.push_back(DE);
1241 continue;
1242 }
1243
Alexey Bataeva769e072013-03-22 06:34:35 +00001244 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1245 // A threadprivate variable must not have an incomplete type.
1246 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001247 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001248 continue;
1249 }
1250
1251 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1252 // A threadprivate variable must not have a reference type.
1253 if (VD->getType()->isReferenceType()) {
1254 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001255 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1256 bool IsDecl =
1257 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1258 Diag(VD->getLocation(),
1259 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1260 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001261 continue;
1262 }
1263
Samuel Antaof8b50122015-07-13 22:54:53 +00001264 // Check if this is a TLS variable. If TLS is not being supported, produce
1265 // the corresponding diagnostic.
1266 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1267 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1268 getLangOpts().OpenMPUseTLS &&
1269 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001270 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1271 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001272 Diag(ILoc, diag::err_omp_var_thread_local)
1273 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001274 bool IsDecl =
1275 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1276 Diag(VD->getLocation(),
1277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1278 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001279 continue;
1280 }
1281
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001282 // Check if initial value of threadprivate variable reference variable with
1283 // local storage (it is not supported by runtime).
1284 if (auto Init = VD->getAnyInitializer()) {
1285 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001286 if (Checker.Visit(Init))
1287 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001288 }
1289
Alexey Bataeved09d242014-05-28 05:53:51 +00001290 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001291 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001292 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1293 Context, SourceRange(Loc, Loc)));
1294 if (auto *ML = Context.getASTMutationListener())
1295 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001296 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001297 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001298 if (!Vars.empty()) {
1299 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1300 Vars);
1301 D->setAccess(AS_public);
1302 }
1303 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001304}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001305
Alexey Bataev7ff55242014-06-19 09:13:45 +00001306static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001307 const ValueDecl *D, DSAStackTy::DSAVarData DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001308 bool IsLoopIterVar = false) {
1309 if (DVar.RefExpr) {
1310 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1311 << getOpenMPClauseName(DVar.CKind);
1312 return;
1313 }
1314 enum {
1315 PDSA_StaticMemberShared,
1316 PDSA_StaticLocalVarShared,
1317 PDSA_LoopIterVarPrivate,
1318 PDSA_LoopIterVarLinear,
1319 PDSA_LoopIterVarLastprivate,
1320 PDSA_ConstVarShared,
1321 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001323 PDSA_LocalVarPrivate,
1324 PDSA_Implicit
1325 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001327 auto ReportLoc = D->getLocation();
1328 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001329 if (IsLoopIterVar) {
1330 if (DVar.CKind == OMPC_private)
1331 Reason = PDSA_LoopIterVarPrivate;
1332 else if (DVar.CKind == OMPC_lastprivate)
1333 Reason = PDSA_LoopIterVarLastprivate;
1334 else
1335 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001336 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1337 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 Reason = PDSA_TaskVarFirstprivate;
1339 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001340 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001341 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001343 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001344 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001346 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001347 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001348 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001349 ReportHint = true;
1350 Reason = PDSA_LocalVarPrivate;
1351 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001352 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001353 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001354 << Reason << ReportHint
1355 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1356 } else if (DVar.ImplicitDSALoc.isValid()) {
1357 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1358 << getOpenMPClauseName(DVar.CKind);
1359 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001360}
1361
Alexey Bataev758e55e2013-09-06 18:03:48 +00001362namespace {
1363class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1364 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001365 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001366 bool ErrorFound;
1367 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001368 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001369 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001370
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371public:
1372 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001373 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001374 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001375 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1376 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001377
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001378 auto DVar = Stack->getTopDSA(VD, false);
1379 // Check if the variable has explicit DSA set and stop analysis if it so.
1380 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001381
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001382 auto ELoc = E->getExprLoc();
1383 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001384 // The default(none) clause requires that each variable that is referenced
1385 // in the construct, and does not have a predetermined data-sharing
1386 // attribute, must have its data-sharing attribute explicitly determined
1387 // by being listed in a data-sharing attribute clause.
1388 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001389 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001390 VarsWithInheritedDSA.count(VD) == 0) {
1391 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001392 return;
1393 }
1394
1395 // OpenMP [2.9.3.6, Restrictions, p.2]
1396 // A list item that appears in a reduction clause of the innermost
1397 // enclosing worksharing or parallel construct may not be accessed in an
1398 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001399 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001400 [](OpenMPDirectiveKind K) -> bool {
1401 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001402 isOpenMPWorksharingDirective(K) ||
1403 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001404 },
1405 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001406 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00001407 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001408 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1409 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001410 return;
1411 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001412
1413 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001414 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001415 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1416 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001417 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001418 }
1419 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001420 void VisitMemberExpr(MemberExpr *E) {
1421 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1422 if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1423 auto DVar = Stack->getTopDSA(FD, false);
1424 // Check if the variable has explicit DSA set and stop analysis if it
1425 // so.
1426 if (DVar.RefExpr)
1427 return;
1428
1429 auto ELoc = E->getExprLoc();
1430 auto DKind = Stack->getCurrentDirective();
1431 // OpenMP [2.9.3.6, Restrictions, p.2]
1432 // A list item that appears in a reduction clause of the innermost
1433 // enclosing worksharing or parallel construct may not be accessed in
Alexey Bataevd985eda2016-02-10 11:29:16 +00001434 // an explicit task.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001435 DVar =
1436 Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1437 [](OpenMPDirectiveKind K) -> bool {
1438 return isOpenMPParallelDirective(K) ||
1439 isOpenMPWorksharingDirective(K) ||
1440 isOpenMPTeamsDirective(K);
1441 },
1442 false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001443 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001444 ErrorFound = true;
1445 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1446 ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1447 return;
1448 }
1449
1450 // Define implicit data-sharing attributes for task.
1451 DVar = Stack->getImplicitDSA(FD, false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00001452 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1453 !Stack->isLoopControlVariable(FD).first)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001454 ImplicitFirstprivate.push_back(E);
1455 }
1456 }
1457 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001458 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 for (auto *C : S->clauses()) {
1460 // Skip analysis of arguments of implicitly defined firstprivate clause
1461 // for task directives.
1462 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1463 for (auto *CC : C->children()) {
1464 if (CC)
1465 Visit(CC);
1466 }
1467 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001468 }
1469 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 for (auto *C : S->children()) {
1471 if (C && !isa<OMPExecutableDirective>(C))
1472 Visit(C);
1473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001475
1476 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001477 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001478 llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001479 return VarsWithInheritedDSA;
1480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev7ff55242014-06-19 09:13:45 +00001482 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1483 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001484};
Alexey Bataeved09d242014-05-28 05:53:51 +00001485} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001486
Alexey Bataevbae9a792014-06-27 10:37:06 +00001487void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001488 switch (DKind) {
1489 case OMPD_parallel: {
1490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001491 QualType KmpInt32PtrTy =
1492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 std::make_pair(".global_tid.", KmpInt32PtrTy),
1495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001500 break;
1501 }
1502 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001503 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001504 std::make_pair(StringRef(), QualType()) // __context with shared vars
1505 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001506 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1507 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001508 break;
1509 }
1510 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001511 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001512 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001513 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001516 break;
1517 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001518 case OMPD_for_simd: {
1519 Sema::CapturedParamNameType Params[] = {
1520 std::make_pair(StringRef(), QualType()) // __context with shared vars
1521 };
1522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1523 Params);
1524 break;
1525 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001526 case OMPD_sections: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001532 break;
1533 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001534 case OMPD_section: {
1535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(StringRef(), QualType()) // __context with shared vars
1537 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001538 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1539 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001540 break;
1541 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001542 case OMPD_single: {
1543 Sema::CapturedParamNameType Params[] = {
1544 std::make_pair(StringRef(), QualType()) // __context with shared vars
1545 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1547 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001548 break;
1549 }
Alexander Musman80c22892014-07-17 08:54:58 +00001550 case OMPD_master: {
1551 Sema::CapturedParamNameType Params[] = {
1552 std::make_pair(StringRef(), QualType()) // __context with shared vars
1553 };
1554 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1555 Params);
1556 break;
1557 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001558 case OMPD_critical: {
1559 Sema::CapturedParamNameType Params[] = {
1560 std::make_pair(StringRef(), QualType()) // __context with shared vars
1561 };
1562 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1563 Params);
1564 break;
1565 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001566 case OMPD_parallel_for: {
1567 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001568 QualType KmpInt32PtrTy =
1569 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001570 Sema::CapturedParamNameType Params[] = {
1571 std::make_pair(".global_tid.", KmpInt32PtrTy),
1572 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001579 case OMPD_parallel_for_simd: {
1580 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001581 QualType KmpInt32PtrTy =
1582 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 Sema::CapturedParamNameType Params[] = {
1584 std::make_pair(".global_tid.", KmpInt32PtrTy),
1585 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1586 std::make_pair(StringRef(), QualType()) // __context with shared vars
1587 };
1588 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1589 Params);
1590 break;
1591 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001592 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001593 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001594 QualType KmpInt32PtrTy =
1595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001596 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001597 std::make_pair(".global_tid.", KmpInt32PtrTy),
1598 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001599 std::make_pair(StringRef(), QualType()) // __context with shared vars
1600 };
1601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1602 Params);
1603 break;
1604 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001605 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001606 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001607 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1608 FunctionProtoType::ExtProtoInfo EPI;
1609 EPI.Variadic = true;
1610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001612 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataev48591dd2016-04-20 04:01:36 +00001613 std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1614 std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1615 std::make_pair(".copy_fn.",
1616 Context.getPointerType(CopyFnType).withConst()),
1617 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001618 std::make_pair(StringRef(), QualType()) // __context with shared vars
1619 };
1620 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1621 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001622 // Mark this captured region as inlined, because we don't use outlined
1623 // function directly.
1624 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1625 AlwaysInlineAttr::CreateImplicit(
1626 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001627 break;
1628 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001629 case OMPD_ordered: {
1630 Sema::CapturedParamNameType Params[] = {
1631 std::make_pair(StringRef(), QualType()) // __context with shared vars
1632 };
1633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1634 Params);
1635 break;
1636 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001637 case OMPD_atomic: {
1638 Sema::CapturedParamNameType Params[] = {
1639 std::make_pair(StringRef(), QualType()) // __context with shared vars
1640 };
1641 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1642 Params);
1643 break;
1644 }
Michael Wong65f367f2015-07-21 13:44:28 +00001645 case OMPD_target_data:
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001646 case OMPD_target:
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001647 case OMPD_target_parallel:
1648 case OMPD_target_parallel_for: {
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001649 Sema::CapturedParamNameType Params[] = {
1650 std::make_pair(StringRef(), QualType()) // __context with shared vars
1651 };
1652 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1653 Params);
1654 break;
1655 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001656 case OMPD_teams: {
1657 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001658 QualType KmpInt32PtrTy =
1659 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001660 Sema::CapturedParamNameType Params[] = {
1661 std::make_pair(".global_tid.", KmpInt32PtrTy),
1662 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1663 std::make_pair(StringRef(), QualType()) // __context with shared vars
1664 };
1665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1666 Params);
1667 break;
1668 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001669 case OMPD_taskgroup: {
1670 Sema::CapturedParamNameType Params[] = {
1671 std::make_pair(StringRef(), QualType()) // __context with shared vars
1672 };
1673 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1674 Params);
1675 break;
1676 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001677 case OMPD_taskloop: {
1678 Sema::CapturedParamNameType Params[] = {
1679 std::make_pair(StringRef(), QualType()) // __context with shared vars
1680 };
1681 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1682 Params);
1683 break;
1684 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001685 case OMPD_taskloop_simd: {
1686 Sema::CapturedParamNameType Params[] = {
1687 std::make_pair(StringRef(), QualType()) // __context with shared vars
1688 };
1689 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1690 Params);
1691 break;
1692 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001693 case OMPD_distribute: {
1694 Sema::CapturedParamNameType Params[] = {
1695 std::make_pair(StringRef(), QualType()) // __context with shared vars
1696 };
1697 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1698 Params);
1699 break;
1700 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001701 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001702 case OMPD_taskyield:
1703 case OMPD_barrier:
1704 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001705 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001706 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001707 case OMPD_flush:
Samuel Antaodf67fc42016-01-19 19:15:56 +00001708 case OMPD_target_enter_data:
Samuel Antao72590762016-01-19 20:04:50 +00001709 case OMPD_target_exit_data:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001710 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00001711 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001712 case OMPD_declare_target:
1713 case OMPD_end_declare_target:
Alexey Bataev9959db52014-05-06 10:08:46 +00001714 llvm_unreachable("OpenMP Directive is not allowed");
1715 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001716 llvm_unreachable("Unknown OpenMP directive");
1717 }
1718}
1719
Alexey Bataev3392d762016-02-16 11:18:12 +00001720static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00001721 Expr *CaptureExpr, bool WithInit,
1722 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001723 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00001724 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00001725 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00001726 QualType Ty = Init->getType();
1727 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1728 if (S.getLangOpts().CPlusPlus)
1729 Ty = C.getLValueReferenceType(Ty);
1730 else {
1731 Ty = C.getPointerType(Ty);
1732 ExprResult Res =
1733 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1734 if (!Res.isUsable())
1735 return nullptr;
1736 Init = Res.get();
1737 }
Alexey Bataev61205072016-03-02 04:57:40 +00001738 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00001739 }
1740 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001741 if (!WithInit)
1742 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
Alexey Bataev4244be22016-02-11 05:35:55 +00001743 S.CurContext->addHiddenDecl(CED);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00001744 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1745 /*TypeMayContainAuto=*/true);
Alexey Bataev3392d762016-02-16 11:18:12 +00001746 return CED;
1747}
1748
Alexey Bataev61205072016-03-02 04:57:40 +00001749static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1750 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00001751 OMPCapturedExprDecl *CD;
1752 if (auto *VD = S.IsOpenMPCapturedDecl(D))
1753 CD = cast<OMPCapturedExprDecl>(VD);
1754 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00001755 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1756 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001757 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00001758 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00001759}
1760
Alexey Bataev5a3af132016-03-29 08:58:54 +00001761static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1762 if (!Ref) {
1763 auto *CD =
1764 buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1765 CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1766 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1767 CaptureExpr->getExprLoc());
1768 }
1769 ExprResult Res = Ref;
1770 if (!S.getLangOpts().CPlusPlus &&
1771 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1772 Ref->getType()->isPointerType())
1773 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1774 if (!Res.isUsable())
1775 return ExprError();
1776 return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00001777}
1778
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001779StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1780 ArrayRef<OMPClause *> Clauses) {
1781 if (!S.isUsable()) {
1782 ActOnCapturedRegionError();
1783 return StmtError();
1784 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001785
1786 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00001787 OMPScheduleClause *SC = nullptr;
Alexey Bataev993d2802015-12-28 06:23:08 +00001788 SmallVector<OMPLinearClause *, 4> LCs;
Alexey Bataev040d5402015-05-12 08:35:28 +00001789 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001790 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001791 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001792 Clause->getClauseKind() == OMPC_copyprivate ||
1793 (getLangOpts().OpenMPUseTLS &&
1794 getASTContext().getTargetInfo().isTLSSupported() &&
1795 Clause->getClauseKind() == OMPC_copyin)) {
1796 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001797 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001798 for (auto *VarRef : Clause->children()) {
1799 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001800 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001801 }
1802 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001803 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00001804 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001805 // Mark all variables in private list clauses as used in inner region.
1806 // Required for proper codegen of combined directives.
1807 // TODO: add processing for other clauses.
Alexey Bataev3392d762016-02-16 11:18:12 +00001808 if (auto *C = OMPClauseWithPreInit::get(Clause)) {
Alexey Bataev005248a2016-02-25 05:25:57 +00001809 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1810 for (auto *D : DS->decls())
Alexey Bataev3392d762016-02-16 11:18:12 +00001811 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1812 }
Alexey Bataev4244be22016-02-11 05:35:55 +00001813 }
Alexey Bataev005248a2016-02-25 05:25:57 +00001814 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1815 if (auto *E = C->getPostUpdateExpr())
1816 MarkDeclarationsReferencedInExpr(E);
1817 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001818 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001819 if (Clause->getClauseKind() == OMPC_schedule)
1820 SC = cast<OMPScheduleClause>(Clause);
1821 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00001822 OC = cast<OMPOrderedClause>(Clause);
1823 else if (Clause->getClauseKind() == OMPC_linear)
1824 LCs.push_back(cast<OMPLinearClause>(Clause));
1825 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001826 bool ErrorFound = false;
1827 // OpenMP, 2.7.1 Loop Construct, Restrictions
1828 // The nonmonotonic modifier cannot be specified if an ordered clause is
1829 // specified.
1830 if (SC &&
1831 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1832 SC->getSecondScheduleModifier() ==
1833 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1834 OC) {
1835 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1836 ? SC->getFirstScheduleModifierLoc()
1837 : SC->getSecondScheduleModifierLoc(),
1838 diag::err_omp_schedule_nonmonotonic_ordered)
1839 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1840 ErrorFound = true;
1841 }
Alexey Bataev993d2802015-12-28 06:23:08 +00001842 if (!LCs.empty() && OC && OC->getNumForLoops()) {
1843 for (auto *C : LCs) {
1844 Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1845 << SourceRange(OC->getLocStart(), OC->getLocEnd());
1846 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001847 ErrorFound = true;
1848 }
Alexey Bataev113438c2015-12-30 12:06:23 +00001849 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1850 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1851 OC->getNumForLoops()) {
1852 Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1853 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1854 ErrorFound = true;
1855 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00001856 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00001857 ActOnCapturedRegionError();
1858 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001859 }
1860 return ActOnCapturedRegionEnd(S.get());
1861}
1862
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001863static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1864 OpenMPDirectiveKind CurrentRegion,
1865 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001866 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001867 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001868 // Allowed nesting of constructs
1869 // +------------------+-----------------+------------------------------------+
1870 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1871 // +------------------+-----------------+------------------------------------+
1872 // | parallel | parallel | * |
1873 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001874 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001875 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001876 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001877 // | parallel | simd | * |
1878 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001879 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001880 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001881 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001882 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001883 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001884 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001885 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001886 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001887 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001888 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001889 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001890 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001891 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001892 // | parallel | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001893 // | parallel | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001894 // | parallel | target parallel | * |
1895 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001896 // | parallel | target enter | * |
1897 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001898 // | parallel | target exit | * |
1899 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001900 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001901 // | parallel | cancellation | |
1902 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001903 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001904 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001905 // | parallel | taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001906 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001907 // +------------------+-----------------+------------------------------------+
1908 // | for | parallel | * |
1909 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001910 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001911 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001912 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001913 // | for | simd | * |
1914 // | for | sections | + |
1915 // | for | section | + |
1916 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001917 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001918 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001919 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001920 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001921 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001922 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001923 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001924 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001925 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001926 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001927 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001928 // | for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001929 // | for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001930 // | for | target parallel | * |
1931 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001932 // | for | target enter | * |
1933 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001934 // | for | target exit | * |
1935 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001936 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001937 // | for | cancellation | |
1938 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001939 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001940 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001941 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001942 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001943 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001944 // | master | parallel | * |
1945 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001946 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001947 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001948 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001949 // | master | simd | * |
1950 // | master | sections | + |
1951 // | master | section | + |
1952 // | master | single | + |
1953 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001954 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001955 // | master |parallel sections| * |
1956 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001957 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001958 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001959 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001960 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001961 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001962 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001963 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001964 // | master | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00001965 // | master | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00001966 // | master | target parallel | * |
1967 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00001968 // | master | target enter | * |
1969 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00001970 // | master | target exit | * |
1971 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001972 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001973 // | master | cancellation | |
1974 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001975 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001976 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001977 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001978 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001979 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001980 // | critical | parallel | * |
1981 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001982 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001983 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001984 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001985 // | critical | simd | * |
1986 // | critical | sections | + |
1987 // | critical | section | + |
1988 // | critical | single | + |
1989 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001990 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001991 // | critical |parallel sections| * |
1992 // | critical | task | * |
1993 // | critical | taskyield | * |
1994 // | critical | barrier | + |
1995 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001996 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001997 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001998 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001999 // | critical | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002000 // | critical | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002001 // | critical | target parallel | * |
2002 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002003 // | critical | target enter | * |
2004 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002005 // | critical | target exit | * |
2006 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002007 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002008 // | critical | cancellation | |
2009 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002010 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002011 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002012 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002013 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002014 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002015 // | simd | parallel | |
2016 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002017 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00002018 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002019 // | simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002020 // | simd | simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002021 // | simd | sections | |
2022 // | simd | section | |
2023 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002024 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002025 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002026 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002027 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00002028 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002029 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002030 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002031 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00002032 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002033 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002034 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002035 // | simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002036 // | simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002037 // | simd | target parallel | |
2038 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002039 // | simd | target enter | |
2040 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002041 // | simd | target exit | |
2042 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002043 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002044 // | simd | cancellation | |
2045 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002046 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002047 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002048 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002049 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002050 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00002051 // | for simd | parallel | |
2052 // | for simd | for | |
2053 // | for simd | for simd | |
2054 // | for simd | master | |
2055 // | for simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002056 // | for simd | simd | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002057 // | for simd | sections | |
2058 // | for simd | section | |
2059 // | for simd | single | |
2060 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002061 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002062 // | for simd |parallel sections| |
2063 // | for simd | task | |
2064 // | for simd | taskyield | |
2065 // | for simd | barrier | |
2066 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002067 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002068 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002069 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002070 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002071 // | for simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002072 // | for simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002073 // | for simd | target parallel | |
2074 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002075 // | for simd | target enter | |
2076 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002077 // | for simd | target exit | |
2078 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002079 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002080 // | for simd | cancellation | |
2081 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002082 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002083 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002084 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002085 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002086 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00002087 // | parallel for simd| parallel | |
2088 // | parallel for simd| for | |
2089 // | parallel for simd| for simd | |
2090 // | parallel for simd| master | |
2091 // | parallel for simd| critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002092 // | parallel for simd| simd | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002093 // | parallel for simd| sections | |
2094 // | parallel for simd| section | |
2095 // | parallel for simd| single | |
2096 // | parallel for simd| parallel for | |
2097 // | parallel for simd|parallel for simd| |
2098 // | parallel for simd|parallel sections| |
2099 // | parallel for simd| task | |
2100 // | parallel for simd| taskyield | |
2101 // | parallel for simd| barrier | |
2102 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002103 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002104 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002105 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002106 // | parallel for simd| atomic | |
2107 // | parallel for simd| target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002108 // | parallel for simd| target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002109 // | parallel for simd| target parallel | |
2110 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002111 // | parallel for simd| target enter | |
2112 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002113 // | parallel for simd| target exit | |
2114 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002115 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002116 // | parallel for simd| cancellation | |
2117 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002118 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002119 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002120 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002121 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002122 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002123 // | sections | parallel | * |
2124 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002125 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002126 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002127 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002128 // | sections | simd | * |
2129 // | sections | sections | + |
2130 // | sections | section | * |
2131 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002132 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002133 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002134 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002135 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002136 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002137 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002138 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002139 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002140 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002141 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002142 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002143 // | sections | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002144 // | sections | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002145 // | sections | target parallel | * |
2146 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002147 // | sections | target enter | * |
2148 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002149 // | sections | target exit | * |
2150 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002151 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002152 // | sections | cancellation | |
2153 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002154 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002155 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002156 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002157 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002158 // +------------------+-----------------+------------------------------------+
2159 // | section | parallel | * |
2160 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002161 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002162 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002163 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002164 // | section | simd | * |
2165 // | section | sections | + |
2166 // | section | section | + |
2167 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002168 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002169 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002170 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002171 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002172 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002173 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002174 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002175 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002176 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002177 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002178 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002179 // | section | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002180 // | section | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002181 // | section | target parallel | * |
2182 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002183 // | section | target enter | * |
2184 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002185 // | section | target exit | * |
2186 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002187 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002188 // | section | cancellation | |
2189 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002190 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002191 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002192 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002193 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002194 // +------------------+-----------------+------------------------------------+
2195 // | single | parallel | * |
2196 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002197 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002198 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002199 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002200 // | single | simd | * |
2201 // | single | sections | + |
2202 // | single | section | + |
2203 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002204 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002205 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002206 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002207 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002208 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002209 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002210 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002211 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002212 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002213 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002214 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 // | single | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002216 // | single | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002217 // | single | target parallel | * |
2218 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002219 // | single | target enter | * |
2220 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002221 // | single | target exit | * |
2222 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002223 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002224 // | single | cancellation | |
2225 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002226 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002227 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002228 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002229 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002230 // +------------------+-----------------+------------------------------------+
2231 // | parallel for | parallel | * |
2232 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002233 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002234 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002235 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00002236 // | parallel for | simd | * |
2237 // | parallel for | sections | + |
2238 // | parallel for | section | + |
2239 // | parallel for | single | + |
2240 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002241 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002242 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002243 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002244 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002245 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002246 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002247 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002248 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002249 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00002250 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002251 // | parallel for | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002252 // | parallel for | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002253 // | parallel for | target parallel | * |
2254 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002255 // | parallel for | target enter | * |
2256 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002257 // | parallel for | target exit | * |
2258 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002259 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002260 // | parallel for | cancellation | |
2261 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002262 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002263 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002264 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002265 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002266 // +------------------+-----------------+------------------------------------+
2267 // | parallel sections| parallel | * |
2268 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002269 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002270 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002271 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002272 // | parallel sections| simd | * |
2273 // | parallel sections| sections | + |
2274 // | parallel sections| section | * |
2275 // | parallel sections| single | + |
2276 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002277 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002278 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002279 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002280 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002281 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002282 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002283 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002284 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002285 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002286 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002287 // | parallel sections| target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002288 // | parallel sections| target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002289 // | parallel sections| target parallel | * |
2290 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002291 // | parallel sections| target enter | * |
2292 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002293 // | parallel sections| target exit | * |
2294 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002295 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002296 // | parallel sections| cancellation | |
2297 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00002298 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002299 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002300 // | parallel sections| taskloop simd | * |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002301 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002302 // +------------------+-----------------+------------------------------------+
2303 // | task | parallel | * |
2304 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002305 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002306 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002307 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002308 // | task | simd | * |
2309 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00002310 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002311 // | task | single | + |
2312 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002313 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002314 // | task |parallel sections| * |
2315 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00002316 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002317 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00002318 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002319 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002320 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002321 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002322 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002323 // | task | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002324 // | task | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002325 // | task | target parallel | * |
2326 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002327 // | task | target enter | * |
2328 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002329 // | task | target exit | * |
2330 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002331 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002332 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002333 // | | point | ! |
2334 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002335 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002336 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002337 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002338 // +------------------+-----------------+------------------------------------+
2339 // | ordered | parallel | * |
2340 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002341 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002342 // | ordered | master | * |
2343 // | ordered | critical | * |
2344 // | ordered | simd | * |
2345 // | ordered | sections | + |
2346 // | ordered | section | + |
2347 // | ordered | single | + |
2348 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002349 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002350 // | ordered |parallel sections| * |
2351 // | ordered | task | * |
2352 // | ordered | taskyield | * |
2353 // | ordered | barrier | + |
2354 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002355 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002356 // | ordered | flush | * |
2357 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002358 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002359 // | ordered | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002360 // | ordered | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002361 // | ordered | target parallel | * |
2362 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002363 // | ordered | target enter | * |
2364 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002365 // | ordered | target exit | * |
2366 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002367 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002368 // | ordered | cancellation | |
2369 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002370 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002371 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002372 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002373 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002374 // +------------------+-----------------+------------------------------------+
2375 // | atomic | parallel | |
2376 // | atomic | for | |
2377 // | atomic | for simd | |
2378 // | atomic | master | |
2379 // | atomic | critical | |
2380 // | atomic | simd | |
2381 // | atomic | sections | |
2382 // | atomic | section | |
2383 // | atomic | single | |
2384 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002385 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002386 // | atomic |parallel sections| |
2387 // | atomic | task | |
2388 // | atomic | taskyield | |
2389 // | atomic | barrier | |
2390 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002391 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002392 // | atomic | flush | |
2393 // | atomic | ordered | |
2394 // | atomic | atomic | |
2395 // | atomic | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002396 // | atomic | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002397 // | atomic | target parallel | |
2398 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002399 // | atomic | target enter | |
2400 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002401 // | atomic | target exit | |
2402 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002403 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002404 // | atomic | cancellation | |
2405 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002406 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002407 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002408 // | atomic | taskloop simd | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002409 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002410 // +------------------+-----------------+------------------------------------+
2411 // | target | parallel | * |
2412 // | target | for | * |
2413 // | target | for simd | * |
2414 // | target | master | * |
2415 // | target | critical | * |
2416 // | target | simd | * |
2417 // | target | sections | * |
2418 // | target | section | * |
2419 // | target | single | * |
2420 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002421 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002422 // | target |parallel sections| * |
2423 // | target | task | * |
2424 // | target | taskyield | * |
2425 // | target | barrier | * |
2426 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002427 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002428 // | target | flush | * |
2429 // | target | ordered | * |
2430 // | target | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002431 // | target | target | |
2432 // | target | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002433 // | target | target parallel | |
2434 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002435 // | target | target enter | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002436 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002437 // | target | target exit | |
Samuel Antao72590762016-01-19 20:04:50 +00002438 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002439 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002440 // | target | cancellation | |
2441 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002442 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002443 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002444 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002445 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002446 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002447 // | target parallel | parallel | * |
2448 // | target parallel | for | * |
2449 // | target parallel | for simd | * |
2450 // | target parallel | master | * |
2451 // | target parallel | critical | * |
2452 // | target parallel | simd | * |
2453 // | target parallel | sections | * |
2454 // | target parallel | section | * |
2455 // | target parallel | single | * |
2456 // | target parallel | parallel for | * |
2457 // | target parallel |parallel for simd| * |
2458 // | target parallel |parallel sections| * |
2459 // | target parallel | task | * |
2460 // | target parallel | taskyield | * |
2461 // | target parallel | barrier | * |
2462 // | target parallel | taskwait | * |
2463 // | target parallel | taskgroup | * |
2464 // | target parallel | flush | * |
2465 // | target parallel | ordered | * |
2466 // | target parallel | atomic | * |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002467 // | target parallel | target | |
2468 // | target parallel | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002469 // | target parallel | target parallel | |
2470 // | | for | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002471 // | target parallel | target enter | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002472 // | | data | |
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002473 // | target parallel | target exit | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002474 // | | data | |
2475 // | target parallel | teams | |
2476 // | target parallel | cancellation | |
2477 // | | point | ! |
2478 // | target parallel | cancel | ! |
2479 // | target parallel | taskloop | * |
2480 // | target parallel | taskloop simd | * |
2481 // | target parallel | distribute | |
2482 // +------------------+-----------------+------------------------------------+
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002483 // | target parallel | parallel | * |
2484 // | for | | |
2485 // | target parallel | for | * |
2486 // | for | | |
2487 // | target parallel | for simd | * |
2488 // | for | | |
2489 // | target parallel | master | * |
2490 // | for | | |
2491 // | target parallel | critical | * |
2492 // | for | | |
2493 // | target parallel | simd | * |
2494 // | for | | |
2495 // | target parallel | sections | * |
2496 // | for | | |
2497 // | target parallel | section | * |
2498 // | for | | |
2499 // | target parallel | single | * |
2500 // | for | | |
2501 // | target parallel | parallel for | * |
2502 // | for | | |
2503 // | target parallel |parallel for simd| * |
2504 // | for | | |
2505 // | target parallel |parallel sections| * |
2506 // | for | | |
2507 // | target parallel | task | * |
2508 // | for | | |
2509 // | target parallel | taskyield | * |
2510 // | for | | |
2511 // | target parallel | barrier | * |
2512 // | for | | |
2513 // | target parallel | taskwait | * |
2514 // | for | | |
2515 // | target parallel | taskgroup | * |
2516 // | for | | |
2517 // | target parallel | flush | * |
2518 // | for | | |
2519 // | target parallel | ordered | * |
2520 // | for | | |
2521 // | target parallel | atomic | * |
2522 // | for | | |
2523 // | target parallel | target | |
2524 // | for | | |
2525 // | target parallel | target parallel | |
2526 // | for | | |
2527 // | target parallel | target parallel | |
2528 // | for | for | |
2529 // | target parallel | target enter | |
2530 // | for | data | |
2531 // | target parallel | target exit | |
2532 // | for | data | |
2533 // | target parallel | teams | |
2534 // | for | | |
2535 // | target parallel | cancellation | |
2536 // | for | point | ! |
2537 // | target parallel | cancel | ! |
2538 // | for | | |
2539 // | target parallel | taskloop | * |
2540 // | for | | |
2541 // | target parallel | taskloop simd | * |
2542 // | for | | |
2543 // | target parallel | distribute | |
2544 // | for | | |
2545 // +------------------+-----------------+------------------------------------+
Alexey Bataev13314bf2014-10-09 04:18:56 +00002546 // | teams | parallel | * |
2547 // | teams | for | + |
2548 // | teams | for simd | + |
2549 // | teams | master | + |
2550 // | teams | critical | + |
2551 // | teams | simd | + |
2552 // | teams | sections | + |
2553 // | teams | section | + |
2554 // | teams | single | + |
2555 // | teams | parallel for | * |
2556 // | teams |parallel for simd| * |
2557 // | teams |parallel sections| * |
2558 // | teams | task | + |
2559 // | teams | taskyield | + |
2560 // | teams | barrier | + |
2561 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002562 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002563 // | teams | flush | + |
2564 // | teams | ordered | + |
2565 // | teams | atomic | + |
2566 // | teams | target | + |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002567 // | teams | target parallel | + |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002568 // | teams | target parallel | + |
2569 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002570 // | teams | target enter | + |
2571 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002572 // | teams | target exit | + |
2573 // | | data | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002574 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002575 // | teams | cancellation | |
2576 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002577 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002578 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002579 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002580 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002581 // +------------------+-----------------+------------------------------------+
2582 // | taskloop | parallel | * |
2583 // | taskloop | for | + |
2584 // | taskloop | for simd | + |
2585 // | taskloop | master | + |
2586 // | taskloop | critical | * |
2587 // | taskloop | simd | * |
2588 // | taskloop | sections | + |
2589 // | taskloop | section | + |
2590 // | taskloop | single | + |
2591 // | taskloop | parallel for | * |
2592 // | taskloop |parallel for simd| * |
2593 // | taskloop |parallel sections| * |
2594 // | taskloop | task | * |
2595 // | taskloop | taskyield | * |
2596 // | taskloop | barrier | + |
2597 // | taskloop | taskwait | * |
2598 // | taskloop | taskgroup | * |
2599 // | taskloop | flush | * |
2600 // | taskloop | ordered | + |
2601 // | taskloop | atomic | * |
2602 // | taskloop | target | * |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002603 // | taskloop | target parallel | * |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002604 // | taskloop | target parallel | * |
2605 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002606 // | taskloop | target enter | * |
2607 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002608 // | taskloop | target exit | * |
2609 // | | data | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002610 // | taskloop | teams | + |
2611 // | taskloop | cancellation | |
2612 // | | point | |
2613 // | taskloop | cancel | |
2614 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002615 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002616 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002617 // | taskloop simd | parallel | |
2618 // | taskloop simd | for | |
2619 // | taskloop simd | for simd | |
2620 // | taskloop simd | master | |
2621 // | taskloop simd | critical | |
Alexey Bataev1f092212016-02-02 04:59:52 +00002622 // | taskloop simd | simd | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002623 // | taskloop simd | sections | |
2624 // | taskloop simd | section | |
2625 // | taskloop simd | single | |
2626 // | taskloop simd | parallel for | |
2627 // | taskloop simd |parallel for simd| |
2628 // | taskloop simd |parallel sections| |
2629 // | taskloop simd | task | |
2630 // | taskloop simd | taskyield | |
2631 // | taskloop simd | barrier | |
2632 // | taskloop simd | taskwait | |
2633 // | taskloop simd | taskgroup | |
2634 // | taskloop simd | flush | |
2635 // | taskloop simd | ordered | + (with simd clause) |
2636 // | taskloop simd | atomic | |
2637 // | taskloop simd | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002638 // | taskloop simd | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002639 // | taskloop simd | target parallel | |
2640 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002641 // | taskloop simd | target enter | |
2642 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002643 // | taskloop simd | target exit | |
2644 // | | data | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002645 // | taskloop simd | teams | |
2646 // | taskloop simd | cancellation | |
2647 // | | point | |
2648 // | taskloop simd | cancel | |
2649 // | taskloop simd | taskloop | |
2650 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002651 // | taskloop simd | distribute | |
2652 // +------------------+-----------------+------------------------------------+
2653 // | distribute | parallel | * |
2654 // | distribute | for | * |
2655 // | distribute | for simd | * |
2656 // | distribute | master | * |
2657 // | distribute | critical | * |
2658 // | distribute | simd | * |
2659 // | distribute | sections | * |
2660 // | distribute | section | * |
2661 // | distribute | single | * |
2662 // | distribute | parallel for | * |
2663 // | distribute |parallel for simd| * |
2664 // | distribute |parallel sections| * |
2665 // | distribute | task | * |
2666 // | distribute | taskyield | * |
2667 // | distribute | barrier | * |
2668 // | distribute | taskwait | * |
2669 // | distribute | taskgroup | * |
2670 // | distribute | flush | * |
2671 // | distribute | ordered | + |
2672 // | distribute | atomic | * |
2673 // | distribute | target | |
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002674 // | distribute | target parallel | |
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002675 // | distribute | target parallel | |
2676 // | | for | |
Samuel Antaodf67fc42016-01-19 19:15:56 +00002677 // | distribute | target enter | |
2678 // | | data | |
Samuel Antao72590762016-01-19 20:04:50 +00002679 // | distribute | target exit | |
2680 // | | data | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002681 // | distribute | teams | |
2682 // | distribute | cancellation | + |
2683 // | | point | |
2684 // | distribute | cancel | + |
2685 // | distribute | taskloop | * |
2686 // | distribute | taskloop simd | * |
2687 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002688 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002689 if (Stack->getCurScope()) {
2690 auto ParentRegion = Stack->getParentDirective();
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002691 auto OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002692 bool NestingProhibited = false;
2693 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002694 enum {
2695 NoRecommend,
2696 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002697 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002698 ShouldBeInTargetRegion,
2699 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002700 } Recommend = NoRecommend;
Alexey Bataev1f092212016-02-02 04:59:52 +00002701 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2702 CurrentRegion != OMPD_simd) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002703 // OpenMP [2.16, Nesting of Regions]
2704 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002705 // OpenMP [2.8.1,simd Construct, Restrictions]
2706 // An ordered construct with the simd clause is the only OpenMP construct
2707 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002708 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2709 return true;
2710 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002711 if (ParentRegion == OMPD_atomic) {
2712 // OpenMP [2.16, Nesting of Regions]
2713 // OpenMP constructs may not be nested inside an atomic region.
2714 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2715 return true;
2716 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002717 if (CurrentRegion == OMPD_section) {
2718 // OpenMP [2.7.2, sections Construct, Restrictions]
2719 // Orphaned section directives are prohibited. That is, the section
2720 // directives must appear within the sections construct and must not be
2721 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002722 if (ParentRegion != OMPD_sections &&
2723 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002724 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2725 << (ParentRegion != OMPD_unknown)
2726 << getOpenMPDirectiveName(ParentRegion);
2727 return true;
2728 }
2729 return false;
2730 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002731 // Allow some constructs to be orphaned (they could be used in functions,
2732 // called from OpenMP regions with the required preconditions).
2733 if (ParentRegion == OMPD_unknown)
2734 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002735 if (CurrentRegion == OMPD_cancellation_point ||
2736 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002737 // OpenMP [2.16, Nesting of Regions]
2738 // A cancellation point construct for which construct-type-clause is
2739 // taskgroup must be nested inside a task construct. A cancellation
2740 // point construct for which construct-type-clause is not taskgroup must
2741 // be closely nested inside an OpenMP construct that matches the type
2742 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002743 // A cancel construct for which construct-type-clause is taskgroup must be
2744 // nested inside a task construct. A cancel construct for which
2745 // construct-type-clause is not taskgroup must be closely nested inside an
2746 // OpenMP construct that matches the type specified in
2747 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002748 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002749 !((CancelRegion == OMPD_parallel &&
2750 (ParentRegion == OMPD_parallel ||
2751 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002752 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002753 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2754 ParentRegion == OMPD_target_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002755 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2756 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002757 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2758 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002759 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002760 // OpenMP [2.16, Nesting of Regions]
2761 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002762 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002763 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002764 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002765 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2766 // OpenMP [2.16, Nesting of Regions]
2767 // A critical region may not be nested (closely or otherwise) inside a
2768 // critical region with the same name. Note that this restriction is not
2769 // sufficient to prevent deadlock.
2770 SourceLocation PreviousCriticalLoc;
2771 bool DeadLock =
2772 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2773 OpenMPDirectiveKind K,
2774 const DeclarationNameInfo &DNI,
2775 SourceLocation Loc)
2776 ->bool {
2777 if (K == OMPD_critical &&
2778 DNI.getName() == CurrentName.getName()) {
2779 PreviousCriticalLoc = Loc;
2780 return true;
2781 } else
2782 return false;
2783 },
2784 false /* skip top directive */);
2785 if (DeadLock) {
2786 SemaRef.Diag(StartLoc,
2787 diag::err_omp_prohibited_region_critical_same_name)
2788 << CurrentName.getName();
2789 if (PreviousCriticalLoc.isValid())
2790 SemaRef.Diag(PreviousCriticalLoc,
2791 diag::note_omp_previous_critical_region);
2792 return true;
2793 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002794 } else if (CurrentRegion == OMPD_barrier) {
2795 // OpenMP [2.16, Nesting of Regions]
2796 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002797 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002798 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2799 isOpenMPTaskingDirective(ParentRegion) ||
2800 ParentRegion == OMPD_master ||
2801 ParentRegion == OMPD_critical ||
2802 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002803 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002804 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002805 // OpenMP [2.16, Nesting of Regions]
2806 // A worksharing region may not be closely nested inside a worksharing,
2807 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002808 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2809 isOpenMPTaskingDirective(ParentRegion) ||
2810 ParentRegion == OMPD_master ||
2811 ParentRegion == OMPD_critical ||
2812 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002813 Recommend = ShouldBeInParallelRegion;
2814 } else if (CurrentRegion == OMPD_ordered) {
2815 // OpenMP [2.16, Nesting of Regions]
2816 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002817 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002818 // An ordered region must be closely nested inside a loop region (or
2819 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002820 // OpenMP [2.8.1,simd Construct, Restrictions]
2821 // An ordered construct with the simd clause is the only OpenMP construct
2822 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002823 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002824 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002825 !(isOpenMPSimdDirective(ParentRegion) ||
2826 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002827 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002828 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2829 // OpenMP [2.16, Nesting of Regions]
2830 // If specified, a teams construct must be contained within a target
2831 // construct.
2832 NestingProhibited = ParentRegion != OMPD_target;
2833 Recommend = ShouldBeInTargetRegion;
2834 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2835 }
2836 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2837 // OpenMP [2.16, Nesting of Regions]
2838 // distribute, parallel, parallel sections, parallel workshare, and the
2839 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2840 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002841 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2842 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002843 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002844 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002845 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2846 // OpenMP 4.5 [2.17 Nesting of Regions]
2847 // The region associated with the distribute construct must be strictly
2848 // nested inside a teams region
2849 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2850 Recommend = ShouldBeInTeamsRegion;
2851 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002852 if (!NestingProhibited &&
2853 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2854 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2855 // OpenMP 4.5 [2.17 Nesting of Regions]
2856 // If a target, target update, target data, target enter data, or
2857 // target exit data construct is encountered during execution of a
2858 // target region, the behavior is unspecified.
2859 NestingProhibited = Stack->hasDirective(
2860 [&OffendingRegion](OpenMPDirectiveKind K,
2861 const DeclarationNameInfo &DNI,
2862 SourceLocation Loc) -> bool {
2863 if (isOpenMPTargetExecutionDirective(K)) {
2864 OffendingRegion = K;
2865 return true;
2866 } else
2867 return false;
2868 },
2869 false /* don't skip top directive */);
2870 CloseNesting = false;
2871 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002872 if (NestingProhibited) {
2873 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00002874 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2875 << Recommend << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002876 return true;
2877 }
2878 }
2879 return false;
2880}
2881
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002882static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2883 ArrayRef<OMPClause *> Clauses,
2884 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2885 bool ErrorFound = false;
2886 unsigned NamedModifiersNumber = 0;
2887 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2888 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002889 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002890 for (const auto *C : Clauses) {
2891 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2892 // At most one if clause without a directive-name-modifier can appear on
2893 // the directive.
2894 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2895 if (FoundNameModifiers[CurNM]) {
2896 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2897 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2898 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2899 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002900 } else if (CurNM != OMPD_unknown) {
2901 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002902 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002903 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002904 FoundNameModifiers[CurNM] = IC;
2905 if (CurNM == OMPD_unknown)
2906 continue;
2907 // Check if the specified name modifier is allowed for the current
2908 // directive.
2909 // At most one if clause with the particular directive-name-modifier can
2910 // appear on the directive.
2911 bool MatchFound = false;
2912 for (auto NM : AllowedNameModifiers) {
2913 if (CurNM == NM) {
2914 MatchFound = true;
2915 break;
2916 }
2917 }
2918 if (!MatchFound) {
2919 S.Diag(IC->getNameModifierLoc(),
2920 diag::err_omp_wrong_if_directive_name_modifier)
2921 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2922 ErrorFound = true;
2923 }
2924 }
2925 }
2926 // If any if clause on the directive includes a directive-name-modifier then
2927 // all if clauses on the directive must include a directive-name-modifier.
2928 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2929 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2930 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2931 diag::err_omp_no_more_if_clause);
2932 } else {
2933 std::string Values;
2934 std::string Sep(", ");
2935 unsigned AllowedCnt = 0;
2936 unsigned TotalAllowedNum =
2937 AllowedNameModifiers.size() - NamedModifiersNumber;
2938 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2939 ++Cnt) {
2940 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2941 if (!FoundNameModifiers[NM]) {
2942 Values += "'";
2943 Values += getOpenMPDirectiveName(NM);
2944 Values += "'";
2945 if (AllowedCnt + 2 == TotalAllowedNum)
2946 Values += " or ";
2947 else if (AllowedCnt + 1 != TotalAllowedNum)
2948 Values += Sep;
2949 ++AllowedCnt;
2950 }
2951 }
2952 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2953 diag::err_omp_unnamed_if_clause)
2954 << (TotalAllowedNum > 1) << Values;
2955 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002956 for (auto Loc : NameModifierLoc) {
2957 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2958 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002959 ErrorFound = true;
2960 }
2961 return ErrorFound;
2962}
2963
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002964StmtResult Sema::ActOnOpenMPExecutableDirective(
2965 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2966 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2967 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002968 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002969 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2970 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002971 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002972
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002973 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002974 llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002975 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002976 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002977 if (AStmt) {
2978 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2979
2980 // Check default data sharing attributes for referenced variables.
2981 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2982 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2983 if (DSAChecker.isErrorFound())
2984 return StmtError();
2985 // Generate list of implicitly defined firstprivate variables.
2986 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002987
2988 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2989 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2990 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2991 SourceLocation(), SourceLocation())) {
2992 ClausesWithImplicit.push_back(Implicit);
2993 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2994 DSAChecker.getImplicitFirstprivate().size();
2995 } else
2996 ErrorFound = true;
2997 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002998 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002999
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003000 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003001 switch (Kind) {
3002 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003003 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3004 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003005 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003006 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003007 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003008 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3009 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003010 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003011 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003012 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3013 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003014 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003015 case OMPD_for_simd:
3016 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3017 EndLoc, VarsWithInheritedDSA);
3018 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003019 case OMPD_sections:
3020 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3021 EndLoc);
3022 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003023 case OMPD_section:
3024 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003025 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003026 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3027 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003028 case OMPD_single:
3029 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3030 EndLoc);
3031 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003032 case OMPD_master:
3033 assert(ClausesWithImplicit.empty() &&
3034 "No clauses are allowed for 'omp master' directive");
3035 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3036 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003037 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003038 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3039 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003040 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003041 case OMPD_parallel_for:
3042 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3043 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003044 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003045 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003046 case OMPD_parallel_for_simd:
3047 Res = ActOnOpenMPParallelForSimdDirective(
3048 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003049 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003050 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003051 case OMPD_parallel_sections:
3052 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3053 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003054 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003055 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003056 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003057 Res =
3058 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003059 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003060 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003061 case OMPD_taskyield:
3062 assert(ClausesWithImplicit.empty() &&
3063 "No clauses are allowed for 'omp taskyield' directive");
3064 assert(AStmt == nullptr &&
3065 "No associated statement allowed for 'omp taskyield' directive");
3066 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3067 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003068 case OMPD_barrier:
3069 assert(ClausesWithImplicit.empty() &&
3070 "No clauses are allowed for 'omp barrier' directive");
3071 assert(AStmt == nullptr &&
3072 "No associated statement allowed for 'omp barrier' directive");
3073 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3074 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003075 case OMPD_taskwait:
3076 assert(ClausesWithImplicit.empty() &&
3077 "No clauses are allowed for 'omp taskwait' directive");
3078 assert(AStmt == nullptr &&
3079 "No associated statement allowed for 'omp taskwait' directive");
3080 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3081 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003082 case OMPD_taskgroup:
3083 assert(ClausesWithImplicit.empty() &&
3084 "No clauses are allowed for 'omp taskgroup' directive");
3085 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3086 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003087 case OMPD_flush:
3088 assert(AStmt == nullptr &&
3089 "No associated statement allowed for 'omp flush' directive");
3090 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3091 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003092 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003093 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3094 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003095 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003096 case OMPD_atomic:
3097 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3098 EndLoc);
3099 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003100 case OMPD_teams:
3101 Res =
3102 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3103 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003104 case OMPD_target:
3105 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3106 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003107 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003108 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003109 case OMPD_target_parallel:
3110 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3111 StartLoc, EndLoc);
3112 AllowedNameModifiers.push_back(OMPD_target);
3113 AllowedNameModifiers.push_back(OMPD_parallel);
3114 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003115 case OMPD_target_parallel_for:
3116 Res = ActOnOpenMPTargetParallelForDirective(
3117 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3118 AllowedNameModifiers.push_back(OMPD_target);
3119 AllowedNameModifiers.push_back(OMPD_parallel);
3120 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003121 case OMPD_cancellation_point:
3122 assert(ClausesWithImplicit.empty() &&
3123 "No clauses are allowed for 'omp cancellation point' directive");
3124 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3125 "cancellation point' directive");
3126 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3127 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003128 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003129 assert(AStmt == nullptr &&
3130 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003131 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3132 CancelRegion);
3133 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003134 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003135 case OMPD_target_data:
3136 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3137 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003138 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003139 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003140 case OMPD_target_enter_data:
3141 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3142 EndLoc);
3143 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3144 break;
Samuel Antao72590762016-01-19 20:04:50 +00003145 case OMPD_target_exit_data:
3146 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3147 EndLoc);
3148 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3149 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003150 case OMPD_taskloop:
3151 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3152 EndLoc, VarsWithInheritedDSA);
3153 AllowedNameModifiers.push_back(OMPD_taskloop);
3154 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003155 case OMPD_taskloop_simd:
3156 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3157 EndLoc, VarsWithInheritedDSA);
3158 AllowedNameModifiers.push_back(OMPD_taskloop);
3159 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003160 case OMPD_distribute:
3161 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3162 EndLoc, VarsWithInheritedDSA);
3163 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003164 case OMPD_declare_target:
3165 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003166 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003167 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003168 case OMPD_declare_simd:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003169 llvm_unreachable("OpenMP Directive is not allowed");
3170 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003171 llvm_unreachable("Unknown OpenMP directive");
3172 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003173
Alexey Bataev4acb8592014-07-07 13:01:15 +00003174 for (auto P : VarsWithInheritedDSA) {
3175 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3176 << P.first << P.second->getSourceRange();
3177 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003178 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3179
3180 if (!AllowedNameModifiers.empty())
3181 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3182 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003183
Alexey Bataeved09d242014-05-28 05:53:51 +00003184 if (ErrorFound)
3185 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003186 return Res;
3187}
3188
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003189Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3190 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003191 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003192 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3193 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003194 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003195 assert(Linears.size() == LinModifiers.size());
3196 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003197 if (!DG || DG.get().isNull())
3198 return DeclGroupPtrTy();
3199
3200 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003201 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003202 return DG;
3203 }
3204 auto *ADecl = DG.get().getSingleDecl();
3205 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3206 ADecl = FTD->getTemplatedDecl();
3207
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003208 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3209 if (!FD) {
3210 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003211 return DeclGroupPtrTy();
3212 }
3213
Alexey Bataev2af33e32016-04-07 12:45:37 +00003214 // OpenMP [2.8.2, declare simd construct, Description]
3215 // The parameter of the simdlen clause must be a constant positive integer
3216 // expression.
3217 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003218 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003219 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003220 // OpenMP [2.8.2, declare simd construct, Description]
3221 // The special this pointer can be used as if was one of the arguments to the
3222 // function in any of the linear, aligned, or uniform clauses.
3223 // The uniform clause declares one or more arguments to have an invariant
3224 // value for all concurrent invocations of the function in the execution of a
3225 // single SIMD loop.
Alexey Bataevecba70f2016-04-12 11:02:11 +00003226 llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3227 Expr *UniformedLinearThis = nullptr;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003228 for (auto *E : Uniforms) {
3229 E = E->IgnoreParenImpCasts();
3230 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3231 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3232 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3233 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003234 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3235 UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003236 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003237 }
3238 if (isa<CXXThisExpr>(E)) {
3239 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003240 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003241 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003242 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3243 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003244 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003245 // OpenMP [2.8.2, declare simd construct, Description]
3246 // The aligned clause declares that the object to which each list item points
3247 // is aligned to the number of bytes expressed in the optional parameter of
3248 // the aligned clause.
3249 // The special this pointer can be used as if was one of the arguments to the
3250 // function in any of the linear, aligned, or uniform clauses.
3251 // The type of list items appearing in the aligned clause must be array,
3252 // pointer, reference to array, or reference to pointer.
3253 llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3254 Expr *AlignedThis = nullptr;
3255 for (auto *E : Aligneds) {
3256 E = E->IgnoreParenImpCasts();
3257 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3258 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3259 auto *CanonPVD = PVD->getCanonicalDecl();
3260 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3261 FD->getParamDecl(PVD->getFunctionScopeIndex())
3262 ->getCanonicalDecl() == CanonPVD) {
3263 // OpenMP [2.8.1, simd construct, Restrictions]
3264 // A list-item cannot appear in more than one aligned clause.
3265 if (AlignedArgs.count(CanonPVD) > 0) {
3266 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3267 << 1 << E->getSourceRange();
3268 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3269 diag::note_omp_explicit_dsa)
3270 << getOpenMPClauseName(OMPC_aligned);
3271 continue;
3272 }
3273 AlignedArgs[CanonPVD] = E;
3274 QualType QTy = PVD->getType()
3275 .getNonReferenceType()
3276 .getUnqualifiedType()
3277 .getCanonicalType();
3278 const Type *Ty = QTy.getTypePtrOrNull();
3279 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3280 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3281 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3282 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3283 }
3284 continue;
3285 }
3286 }
3287 if (isa<CXXThisExpr>(E)) {
3288 if (AlignedThis) {
3289 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3290 << 2 << E->getSourceRange();
3291 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3292 << getOpenMPClauseName(OMPC_aligned);
3293 }
3294 AlignedThis = E;
3295 continue;
3296 }
3297 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3298 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3299 }
3300 // The optional parameter of the aligned clause, alignment, must be a constant
3301 // positive integer expression. If no optional parameter is specified,
3302 // implementation-defined default alignments for SIMD instructions on the
3303 // target platforms are assumed.
3304 SmallVector<Expr *, 4> NewAligns;
3305 for (auto *E : Alignments) {
3306 ExprResult Align;
3307 if (E)
3308 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3309 NewAligns.push_back(Align.get());
3310 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003311 // OpenMP [2.8.2, declare simd construct, Description]
3312 // The linear clause declares one or more list items to be private to a SIMD
3313 // lane and to have a linear relationship with respect to the iteration space
3314 // of a loop.
3315 // The special this pointer can be used as if was one of the arguments to the
3316 // function in any of the linear, aligned, or uniform clauses.
3317 // When a linear-step expression is specified in a linear clause it must be
3318 // either a constant integer expression or an integer-typed parameter that is
3319 // specified in a uniform clause on the directive.
3320 llvm::DenseMap<Decl *, Expr *> LinearArgs;
3321 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3322 auto MI = LinModifiers.begin();
3323 for (auto *E : Linears) {
3324 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3325 ++MI;
3326 E = E->IgnoreParenImpCasts();
3327 if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3328 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3329 auto *CanonPVD = PVD->getCanonicalDecl();
3330 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3331 FD->getParamDecl(PVD->getFunctionScopeIndex())
3332 ->getCanonicalDecl() == CanonPVD) {
3333 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3334 // A list-item cannot appear in more than one linear clause.
3335 if (LinearArgs.count(CanonPVD) > 0) {
3336 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3337 << getOpenMPClauseName(OMPC_linear)
3338 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3339 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3340 diag::note_omp_explicit_dsa)
3341 << getOpenMPClauseName(OMPC_linear);
3342 continue;
3343 }
3344 // Each argument can appear in at most one uniform or linear clause.
3345 if (UniformedArgs.count(CanonPVD) > 0) {
3346 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3347 << getOpenMPClauseName(OMPC_linear)
3348 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3349 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3350 diag::note_omp_explicit_dsa)
3351 << getOpenMPClauseName(OMPC_uniform);
3352 continue;
3353 }
3354 LinearArgs[CanonPVD] = E;
3355 if (E->isValueDependent() || E->isTypeDependent() ||
3356 E->isInstantiationDependent() ||
3357 E->containsUnexpandedParameterPack())
3358 continue;
3359 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3360 PVD->getOriginalType());
3361 continue;
3362 }
3363 }
3364 if (isa<CXXThisExpr>(E)) {
3365 if (UniformedLinearThis) {
3366 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3367 << getOpenMPClauseName(OMPC_linear)
3368 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3369 << E->getSourceRange();
3370 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3371 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3372 : OMPC_linear);
3373 continue;
3374 }
3375 UniformedLinearThis = E;
3376 if (E->isValueDependent() || E->isTypeDependent() ||
3377 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3378 continue;
3379 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3380 E->getType());
3381 continue;
3382 }
3383 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3384 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3385 }
3386 Expr *Step = nullptr;
3387 Expr *NewStep = nullptr;
3388 SmallVector<Expr *, 4> NewSteps;
3389 for (auto *E : Steps) {
3390 // Skip the same step expression, it was checked already.
3391 if (Step == E || !E) {
3392 NewSteps.push_back(E ? NewStep : nullptr);
3393 continue;
3394 }
3395 Step = E;
3396 if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3397 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3398 auto *CanonPVD = PVD->getCanonicalDecl();
3399 if (UniformedArgs.count(CanonPVD) == 0) {
3400 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3401 << Step->getSourceRange();
3402 } else if (E->isValueDependent() || E->isTypeDependent() ||
3403 E->isInstantiationDependent() ||
3404 E->containsUnexpandedParameterPack() ||
3405 CanonPVD->getType()->hasIntegerRepresentation())
3406 NewSteps.push_back(Step);
3407 else {
3408 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3409 << Step->getSourceRange();
3410 }
3411 continue;
3412 }
3413 NewStep = Step;
3414 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3415 !Step->isInstantiationDependent() &&
3416 !Step->containsUnexpandedParameterPack()) {
3417 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3418 .get();
3419 if (NewStep)
3420 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3421 }
3422 NewSteps.push_back(NewStep);
3423 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003424 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3425 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003426 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003427 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3428 const_cast<Expr **>(Linears.data()), Linears.size(),
3429 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3430 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003431 ADecl->addAttr(NewAttr);
3432 return ConvertDeclToDeclGroup(ADecl);
3433}
3434
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003435StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3436 Stmt *AStmt,
3437 SourceLocation StartLoc,
3438 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003439 if (!AStmt)
3440 return StmtError();
3441
Alexey Bataev9959db52014-05-06 10:08:46 +00003442 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3443 // 1.2.2 OpenMP Language Terminology
3444 // Structured block - An executable statement with a single entry at the
3445 // top and a single exit at the bottom.
3446 // The point of exit cannot be a branch out of the structured block.
3447 // longjmp() and throw() must not violate the entry/exit criteria.
3448 CS->getCapturedDecl()->setNothrow();
3449
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003450 getCurFunction()->setHasBranchProtectedScope();
3451
Alexey Bataev25e5b442015-09-15 12:52:43 +00003452 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3453 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003454}
3455
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003456namespace {
3457/// \brief Helper class for checking canonical form of the OpenMP loops and
3458/// extracting iteration space of each loop in the loop nest, that will be used
3459/// for IR generation.
3460class OpenMPIterationSpaceChecker {
3461 /// \brief Reference to Sema.
3462 Sema &SemaRef;
3463 /// \brief A location for diagnostics (when there is no some better location).
3464 SourceLocation DefaultLoc;
3465 /// \brief A location for diagnostics (when increment is not compatible).
3466 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467 /// \brief A source location for referring to loop init later.
3468 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003469 /// \brief A source location for referring to condition later.
3470 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003471 /// \brief A source location for referring to increment later.
3472 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003473 /// \brief Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003474 ValueDecl *LCDecl = nullptr;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003475 /// \brief Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003476 Expr *LCRef = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003477 /// \brief Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003478 Expr *LB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003479 /// \brief Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003480 Expr *UB = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003481 /// \brief Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003482 Expr *Step = nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003483 /// \brief This flag is true when condition is one of:
3484 /// Var < UB
3485 /// Var <= UB
3486 /// UB > Var
3487 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003488 bool TestIsLessOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003489 /// \brief This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003490 bool TestIsStrictOp = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003491 /// \brief This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003492 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003493
3494public:
3495 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003496 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003497 /// \brief Check init-expr for canonical loop form and save loop counter
3498 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00003499 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003500 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3501 /// for less/greater and for strict/non-strict comparison.
3502 bool CheckCond(Expr *S);
3503 /// \brief Check incr-expr for canonical loop form and return true if it
3504 /// does not conform, otherwise save loop step (#Step).
3505 bool CheckInc(Expr *S);
3506 /// \brief Return the loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003507 ValueDecl *GetLoopDecl() const { return LCDecl; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003508 /// \brief Return the reference expression to loop counter variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003509 Expr *GetLoopDeclRefExpr() const { return LCRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003510 /// \brief Source range of the loop init.
3511 SourceRange GetInitSrcRange() const { return InitSrcRange; }
3512 /// \brief Source range of the loop condition.
3513 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3514 /// \brief Source range of the loop increment.
3515 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3516 /// \brief True if the step should be subtracted.
3517 bool ShouldSubtractStep() const { return SubtractStep; }
3518 /// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003519 Expr *
3520 BuildNumIterations(Scope *S, const bool LimitedType,
3521 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003522 /// \brief Build the precondition expression for the loops.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003523 Expr *BuildPreCond(Scope *S, Expr *Cond,
3524 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 /// \brief Build reference expression to the counter be used for codegen.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00003526 DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3527 DSAStackTy &DSA) const;
Alexey Bataeva8899172015-08-06 12:30:57 +00003528 /// \brief Build reference expression to the private counter be used for
3529 /// codegen.
3530 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003531 /// \brief Build initization of the counter be used for codegen.
3532 Expr *BuildCounterInit() const;
3533 /// \brief Build step of the counter be used for codegen.
3534 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003535 /// \brief Return true if any expression is dependent.
3536 bool Dependent() const;
3537
3538private:
3539 /// \brief Check the right-hand side of an assignment in the increment
3540 /// expression.
3541 bool CheckIncRHS(Expr *RHS);
3542 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003543 bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003544 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00003545 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003546 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003547 /// \brief Helper to set loop increment.
3548 bool SetStep(Expr *NewStep, bool Subtract);
3549};
3550
3551bool OpenMPIterationSpaceChecker::Dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003552 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003553 assert(!LB && !UB && !Step);
3554 return false;
3555 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003556 return LCDecl->getType()->isDependentType() ||
3557 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3558 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003559}
3560
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003561static Expr *getExprAsWritten(Expr *E) {
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003562 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3563 E = ExprTemp->getSubExpr();
3564
3565 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3566 E = MTE->GetTemporaryExpr();
3567
3568 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3569 E = Binder->getSubExpr();
3570
3571 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3572 E = ICE->getSubExprAsWritten();
3573 return E->IgnoreParens();
3574}
3575
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003576bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3577 Expr *NewLCRefExpr,
3578 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003579 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003580 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003581 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003582 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003583 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003584 LCDecl = getCanonicalDecl(NewLCDecl);
3585 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003586 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3587 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003588 if ((Ctor->isCopyOrMoveConstructor() ||
3589 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3590 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003591 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003592 LB = NewLB;
3593 return false;
3594}
3595
3596bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003597 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003598 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003599 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3600 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003601 if (!NewUB)
3602 return true;
3603 UB = NewUB;
3604 TestIsLessOp = LessOp;
3605 TestIsStrictOp = StrictOp;
3606 ConditionSrcRange = SR;
3607 ConditionLoc = SL;
3608 return false;
3609}
3610
3611bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3612 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003613 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003614 if (!NewStep)
3615 return true;
3616 if (!NewStep->isValueDependent()) {
3617 // Check that the step is integer expression.
3618 SourceLocation StepLoc = NewStep->getLocStart();
3619 ExprResult Val =
3620 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3621 if (Val.isInvalid())
3622 return true;
3623 NewStep = Val.get();
3624
3625 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3626 // If test-expr is of form var relational-op b and relational-op is < or
3627 // <= then incr-expr must cause var to increase on each iteration of the
3628 // loop. If test-expr is of form var relational-op b and relational-op is
3629 // > or >= then incr-expr must cause var to decrease on each iteration of
3630 // the loop.
3631 // If test-expr is of form b relational-op var and relational-op is < or
3632 // <= then incr-expr must cause var to decrease on each iteration of the
3633 // loop. If test-expr is of form b relational-op var and relational-op is
3634 // > or >= then incr-expr must cause var to increase on each iteration of
3635 // the loop.
3636 llvm::APSInt Result;
3637 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3638 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3639 bool IsConstNeg =
3640 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003641 bool IsConstPos =
3642 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003643 bool IsConstZero = IsConstant && !Result.getBoolValue();
3644 if (UB && (IsConstZero ||
3645 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003646 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003647 SemaRef.Diag(NewStep->getExprLoc(),
3648 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003649 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003650 SemaRef.Diag(ConditionLoc,
3651 diag::note_omp_loop_cond_requres_compatible_incr)
3652 << TestIsLessOp << ConditionSrcRange;
3653 return true;
3654 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003655 if (TestIsLessOp == Subtract) {
3656 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3657 NewStep).get();
3658 Subtract = !Subtract;
3659 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003660 }
3661
3662 Step = NewStep;
3663 SubtractStep = Subtract;
3664 return false;
3665}
3666
Alexey Bataev9c821032015-04-30 04:23:23 +00003667bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003668 // Check init-expr for canonical loop form and save loop counter
3669 // variable - #Var and its initialization value - #LB.
3670 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3671 // var = lb
3672 // integer-type var = lb
3673 // random-access-iterator-type var = lb
3674 // pointer-type var = lb
3675 //
3676 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003677 if (EmitDiags) {
3678 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3679 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003680 return true;
3681 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003682 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003683 if (Expr *E = dyn_cast<Expr>(S))
3684 S = E->IgnoreParens();
3685 if (auto BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003686 if (BO->getOpcode() == BO_Assign) {
3687 auto *LHS = BO->getLHS()->IgnoreParens();
3688 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3689 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3690 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3691 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3692 return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3693 }
3694 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3695 if (ME->isArrow() &&
3696 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3697 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3698 }
3699 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003700 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3701 if (DS->isSingleDecl()) {
3702 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00003703 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003704 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00003705 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003706 SemaRef.Diag(S->getLocStart(),
3707 diag::ext_omp_loop_not_canonical_init)
3708 << S->getSourceRange();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003709 return SetLCDeclAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003710 }
3711 }
3712 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003713 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3714 if (CE->getOperator() == OO_Equal) {
3715 auto *LHS = CE->getArg(0);
3716 if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3717 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3718 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3719 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3720 return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3721 }
3722 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3723 if (ME->isArrow() &&
3724 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3725 return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3726 }
3727 }
3728 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003729
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003730 if (Dependent() || SemaRef.CurContext->isDependentContext())
3731 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00003732 if (EmitDiags) {
3733 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3734 << S->getSourceRange();
3735 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003736 return true;
3737}
3738
Alexey Bataev23b69422014-06-18 07:08:49 +00003739/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003740/// variable (which may be the loop variable) if possible.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003741static const ValueDecl *GetInitLCDecl(Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003742 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00003743 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003744 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003745 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3746 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003747 if ((Ctor->isCopyOrMoveConstructor() ||
3748 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3749 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003750 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003751 if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3752 if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3753 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3754 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3755 return getCanonicalDecl(ME->getMemberDecl());
3756 return getCanonicalDecl(VD);
3757 }
3758 }
3759 if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3760 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3761 return getCanonicalDecl(ME->getMemberDecl());
3762 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003763}
3764
3765bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3766 // Check test-expr for canonical form, save upper-bound UB, flags for
3767 // less/greater and for strict/non-strict comparison.
3768 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3769 // var relational-op b
3770 // b relational-op var
3771 //
3772 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003773 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003774 return true;
3775 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003776 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003777 SourceLocation CondLoc = S->getLocStart();
3778 if (auto BO = dyn_cast<BinaryOperator>(S)) {
3779 if (BO->isRelationalOp()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003780 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003781 return SetUB(BO->getRHS(),
3782 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3783 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3784 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003785 if (GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003786 return SetUB(BO->getLHS(),
3787 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3788 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3789 BO->getSourceRange(), BO->getOperatorLoc());
3790 }
3791 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3792 if (CE->getNumArgs() == 2) {
3793 auto Op = CE->getOperator();
3794 switch (Op) {
3795 case OO_Greater:
3796 case OO_GreaterEqual:
3797 case OO_Less:
3798 case OO_LessEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003799 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003800 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3801 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3802 CE->getOperatorLoc());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003803 if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003804 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3805 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3806 CE->getOperatorLoc());
3807 break;
3808 default:
3809 break;
3810 }
3811 }
3812 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003813 if (Dependent() || SemaRef.CurContext->isDependentContext())
3814 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003815 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003816 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817 return true;
3818}
3819
3820bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3821 // RHS of canonical loop form increment can be:
3822 // var + incr
3823 // incr + var
3824 // var - incr
3825 //
3826 RHS = RHS->IgnoreParenImpCasts();
3827 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3828 if (BO->isAdditiveOp()) {
3829 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003830 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003831 return SetStep(BO->getRHS(), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003832 if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003833 return SetStep(BO->getLHS(), false);
3834 }
3835 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3836 bool IsAdd = CE->getOperator() == OO_Plus;
3837 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003838 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003839 return SetStep(CE->getArg(1), !IsAdd);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003840 if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003841 return SetStep(CE->getArg(0), false);
3842 }
3843 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003844 if (Dependent() || SemaRef.CurContext->isDependentContext())
3845 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003846 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003847 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003848 return true;
3849}
3850
3851bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3852 // Check incr-expr for canonical loop form and return true if it
3853 // does not conform.
3854 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3855 // ++var
3856 // var++
3857 // --var
3858 // var--
3859 // var += incr
3860 // var -= incr
3861 // var = var + incr
3862 // var = incr + var
3863 // var = var - incr
3864 //
3865 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003866 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003867 return true;
3868 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003869 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003870 S = S->IgnoreParens();
3871 if (auto UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003872 if (UO->isIncrementDecrementOp() &&
3873 GetInitLCDecl(UO->getSubExpr()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003874 return SetStep(
3875 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3876 (UO->isDecrementOp() ? -1 : 1)).get(),
3877 false);
3878 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3879 switch (BO->getOpcode()) {
3880 case BO_AddAssign:
3881 case BO_SubAssign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003882 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3884 break;
3885 case BO_Assign:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003886 if (GetInitLCDecl(BO->getLHS()) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003887 return CheckIncRHS(BO->getRHS());
3888 break;
3889 default:
3890 break;
3891 }
3892 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3893 switch (CE->getOperator()) {
3894 case OO_PlusPlus:
3895 case OO_MinusMinus:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003896 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897 return SetStep(
3898 SemaRef.ActOnIntegerConstant(
3899 CE->getLocStart(),
3900 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3901 false);
3902 break;
3903 case OO_PlusEqual:
3904 case OO_MinusEqual:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003905 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3907 break;
3908 case OO_Equal:
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 return CheckIncRHS(CE->getArg(1));
3911 break;
3912 default:
3913 break;
3914 }
3915 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003916 if (Dependent() || SemaRef.CurContext->isDependentContext())
3917 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003918 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003919 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003920 return true;
3921}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003922
Alexey Bataev5a3af132016-03-29 08:58:54 +00003923static ExprResult
3924tryBuildCapture(Sema &SemaRef, Expr *Capture,
3925 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3926 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3927 return SemaRef.PerformImplicitConversion(
3928 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3929 /*AllowExplicit=*/true);
3930 auto I = Captures.find(Capture);
3931 if (I != Captures.end())
3932 return buildCapture(SemaRef, Capture, I->second);
3933 DeclRefExpr *Ref = nullptr;
3934 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
3935 Captures[Capture] = Ref;
3936 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003937}
3938
Alexander Musmana5f070a2014-10-01 06:03:56 +00003939/// \brief Build the expression to calculate the number of iterations.
Alexey Bataev5a3af132016-03-29 08:58:54 +00003940Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
3941 Scope *S, const bool LimitedType,
3942 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003943 ExprResult Diff;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003944 auto VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003945 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003946 SemaRef.getLangOpts().CPlusPlus) {
3947 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003948 auto *UBExpr = TestIsLessOp ? UB : LB;
3949 auto *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00003950 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
3951 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003952 if (!Upper || !Lower)
3953 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954
3955 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3956
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003957 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003958 // BuildBinOp already emitted error, this one is to point user to upper
3959 // and lower bound, and to tell what is passed to 'operator-'.
3960 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3961 << Upper->getSourceRange() << Lower->getSourceRange();
3962 return nullptr;
3963 }
3964 }
3965
3966 if (!Diff.isUsable())
3967 return nullptr;
3968
3969 // Upper - Lower [- 1]
3970 if (TestIsStrictOp)
3971 Diff = SemaRef.BuildBinOp(
3972 S, DefaultLoc, BO_Sub, Diff.get(),
3973 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3974 if (!Diff.isUsable())
3975 return nullptr;
3976
3977 // Upper - Lower [- 1] + Step
Alexey Bataev5a3af132016-03-29 08:58:54 +00003978 auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
3979 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003980 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003981 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 if (!Diff.isUsable())
3983 return nullptr;
3984
3985 // Parentheses (for dumping/debugging purposes only).
3986 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3987 if (!Diff.isUsable())
3988 return nullptr;
3989
3990 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003991 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003992 if (!Diff.isUsable())
3993 return nullptr;
3994
Alexander Musman174b3ca2014-10-06 11:16:29 +00003995 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003996 QualType Type = Diff.get()->getType();
3997 auto &C = SemaRef.Context;
3998 bool UseVarType = VarType->hasIntegerRepresentation() &&
3999 C.getTypeSize(Type) > C.getTypeSize(VarType);
4000 if (!Type->isIntegerType() || UseVarType) {
4001 unsigned NewSize =
4002 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4003 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4004 : Type->hasSignedIntegerRepresentation();
4005 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004006 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4007 Diff = SemaRef.PerformImplicitConversion(
4008 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4009 if (!Diff.isUsable())
4010 return nullptr;
4011 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004012 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004013 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004014 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4015 if (NewSize != C.getTypeSize(Type)) {
4016 if (NewSize < C.getTypeSize(Type)) {
4017 assert(NewSize == 64 && "incorrect loop var size");
4018 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4019 << InitSrcRange << ConditionSrcRange;
4020 }
4021 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004022 NewSize, Type->hasSignedIntegerRepresentation() ||
4023 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004024 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4025 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4026 Sema::AA_Converting, true);
4027 if (!Diff.isUsable())
4028 return nullptr;
4029 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004030 }
4031 }
4032
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033 return Diff.get();
4034}
4035
Alexey Bataev5a3af132016-03-29 08:58:54 +00004036Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4037 Scope *S, Expr *Cond,
4038 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004039 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4040 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4041 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004042
Alexey Bataev5a3af132016-03-29 08:58:54 +00004043 auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4044 auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4045 if (!NewLB.isUsable() || !NewUB.isUsable())
4046 return nullptr;
4047
Alexey Bataev62dbb972015-04-22 11:59:37 +00004048 auto CondExpr = SemaRef.BuildBinOp(
4049 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4050 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004051 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004052 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004053 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4054 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004055 CondExpr = SemaRef.PerformImplicitConversion(
4056 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4057 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004058 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004059 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4060 // Otherwise use original loop conditon and evaluate it in runtime.
4061 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4062}
4063
Alexander Musmana5f070a2014-10-01 06:03:56 +00004064/// \brief Build reference expression to the counter be used for codegen.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004065DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004066 llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004067 auto *VD = dyn_cast<VarDecl>(LCDecl);
4068 if (!VD) {
4069 VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4070 auto *Ref = buildDeclRefExpr(
4071 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004072 DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4073 // If the loop control decl is explicitly marked as private, do not mark it
4074 // as captured again.
4075 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4076 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004077 return Ref;
4078 }
4079 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004080 DefaultLoc);
4081}
4082
4083Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004084 if (LCDecl && !LCDecl->isInvalidDecl()) {
4085 auto Type = LCDecl->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00004086 auto *PrivateVar =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004087 buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4088 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004089 if (PrivateVar->isInvalidDecl())
4090 return nullptr;
4091 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4092 }
4093 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094}
4095
4096/// \brief Build initization of the counter be used for codegen.
4097Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4098
4099/// \brief Build step of the counter be used for codegen.
4100Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4101
4102/// \brief Iteration space of a single for loop.
4103struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004104 /// \brief Condition of the loop.
4105 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004106 /// \brief This expression calculates the number of iterations in the loop.
4107 /// It is always possible to calculate it before starting the loop.
4108 Expr *NumIterations;
4109 /// \brief The loop counter variable.
4110 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004111 /// \brief Private loop counter variable.
4112 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004113 /// \brief This is initializer for the initial value of #CounterVar.
4114 Expr *CounterInit;
4115 /// \brief This is step for the #CounterVar used to generate its update:
4116 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4117 Expr *CounterStep;
4118 /// \brief Should step be subtracted?
4119 bool Subtract;
4120 /// \brief Source range of the loop init.
4121 SourceRange InitSrcRange;
4122 /// \brief Source range of the loop condition.
4123 SourceRange CondSrcRange;
4124 /// \brief Source range of the loop increment.
4125 SourceRange IncSrcRange;
4126};
4127
Alexey Bataev23b69422014-06-18 07:08:49 +00004128} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004129
Alexey Bataev9c821032015-04-30 04:23:23 +00004130void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4131 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4132 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004133 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4134 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004135 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4136 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004137 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4138 if (auto *D = ISC.GetLoopDecl()) {
4139 auto *VD = dyn_cast<VarDecl>(D);
4140 if (!VD) {
4141 if (auto *Private = IsOpenMPCapturedDecl(D))
4142 VD = Private;
4143 else {
4144 auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4145 /*WithInit=*/false);
4146 VD = cast<VarDecl>(Ref->getDecl());
4147 }
4148 }
4149 DSAStack->addLoopControlVariable(D, VD);
4150 }
4151 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004152 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004153 }
4154}
4155
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004156/// \brief Called on a for stmt to check and extract its iteration space
4157/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00004158static bool CheckOpenMPIterationSpace(
4159 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4160 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004161 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004162 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004163 LoopIterationSpace &ResultIterSpace,
4164 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004165 // OpenMP [2.6, Canonical Loop Form]
4166 // for (init-expr; test-expr; incr-expr) structured-block
4167 auto For = dyn_cast_or_null<ForStmt>(S);
4168 if (!For) {
4169 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004170 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4171 << getOpenMPDirectiveName(DKind) << NestedLoopCount
4172 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4173 if (NestedLoopCount > 1) {
4174 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4175 SemaRef.Diag(DSA.getConstructLoc(),
4176 diag::note_omp_collapse_ordered_expr)
4177 << 2 << CollapseLoopCountExpr->getSourceRange()
4178 << OrderedLoopCountExpr->getSourceRange();
4179 else if (CollapseLoopCountExpr)
4180 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4181 diag::note_omp_collapse_ordered_expr)
4182 << 0 << CollapseLoopCountExpr->getSourceRange();
4183 else
4184 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4185 diag::note_omp_collapse_ordered_expr)
4186 << 1 << OrderedLoopCountExpr->getSourceRange();
4187 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188 return true;
4189 }
4190 assert(For->getBody());
4191
4192 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4193
4194 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00004195 auto Init = For->getInit();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004196 if (ISC.CheckInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198
4199 bool HasErrors = false;
4200
4201 // Check loop variable's type.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004202 if (auto *LCDecl = ISC.GetLoopDecl()) {
4203 auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004204
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004205 // OpenMP [2.6, Canonical Loop Form]
4206 // Var is one of the following:
4207 // A variable of signed or unsigned integer type.
4208 // For C++, a variable of a random access iterator type.
4209 // For C, a variable of a pointer type.
4210 auto VarType = LCDecl->getType().getNonReferenceType();
4211 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4212 !VarType->isPointerType() &&
4213 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4214 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4215 << SemaRef.getLangOpts().CPlusPlus;
4216 HasErrors = true;
4217 }
4218
4219 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4220 // a Construct
4221 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4222 // parallel for construct is (are) private.
4223 // The loop iteration variable in the associated for-loop of a simd
4224 // construct with just one associated for-loop is linear with a
4225 // constant-linear-step that is the increment of the associated for-loop.
4226 // Exclude loop var from the list of variables with implicitly defined data
4227 // sharing attributes.
4228 VarsWithImplicitDSA.erase(LCDecl);
4229
4230 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4231 // in a Construct, C/C++].
4232 // The loop iteration variable in the associated for-loop of a simd
4233 // construct with just one associated for-loop may be listed in a linear
4234 // clause with a constant-linear-step that is the increment of the
4235 // associated for-loop.
4236 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4237 // parallel for construct may be listed in a private or lastprivate clause.
4238 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4239 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4240 // declared in the loop and it is predetermined as a private.
4241 auto PredeterminedCKind =
4242 isOpenMPSimdDirective(DKind)
4243 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4244 : OMPC_private;
4245 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4246 DVar.CKind != PredeterminedCKind) ||
4247 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4248 isOpenMPDistributeDirective(DKind)) &&
4249 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4250 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4251 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4252 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4253 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4254 << getOpenMPClauseName(PredeterminedCKind);
4255 if (DVar.RefExpr == nullptr)
4256 DVar.CKind = PredeterminedCKind;
4257 ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4258 HasErrors = true;
4259 } else if (LoopDeclRefExpr != nullptr) {
4260 // Make the loop iteration variable private (for worksharing constructs),
4261 // linear (for simd directives with the only one associated loop) or
4262 // lastprivate (for simd directives with several collapsed or ordered
4263 // loops).
4264 if (DVar.CKind == OMPC_unknown)
4265 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4266 /*FromParent=*/false);
4267 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4268 }
4269
4270 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4271
4272 // Check test-expr.
4273 HasErrors |= ISC.CheckCond(For->getCond());
4274
4275 // Check incr-expr.
4276 HasErrors |= ISC.CheckInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004277 }
4278
Alexander Musmana5f070a2014-10-01 06:03:56 +00004279 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004280 return HasErrors;
4281
Alexander Musmana5f070a2014-10-01 06:03:56 +00004282 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004283 ResultIterSpace.PreCond =
4284 ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
Alexander Musman174b3ca2014-10-06 11:16:29 +00004285 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004286 DSA.getCurScope(),
4287 (isOpenMPWorksharingDirective(DKind) ||
4288 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4289 Captures);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004290 ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
Alexey Bataeva8899172015-08-06 12:30:57 +00004291 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004292 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4293 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4294 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4295 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4296 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4297 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4298
Alexey Bataev62dbb972015-04-22 11:59:37 +00004299 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4300 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004301 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004302 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004303 ResultIterSpace.CounterInit == nullptr ||
4304 ResultIterSpace.CounterStep == nullptr);
4305
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004306 return HasErrors;
4307}
4308
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004309/// \brief Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004310static ExprResult
4311BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4312 ExprResult Start,
4313 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004314 // Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004315 auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4316 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004317 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004318 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004319 VarRef.get()->getType())) {
4320 NewStart = SemaRef.PerformImplicitConversion(
4321 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4322 /*AllowExplicit=*/true);
4323 if (!NewStart.isUsable())
4324 return ExprError();
4325 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004326
4327 auto Init =
4328 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4329 return Init;
4330}
4331
Alexander Musmana5f070a2014-10-01 06:03:56 +00004332/// \brief Build 'VarRef = Start + Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004333static ExprResult
4334BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4335 ExprResult VarRef, ExprResult Start, ExprResult Iter,
4336 ExprResult Step, bool Subtract,
4337 llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004338 // Add parentheses (for debugging purposes only).
4339 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4340 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4341 !Step.isUsable())
4342 return ExprError();
4343
Alexey Bataev5a3af132016-03-29 08:58:54 +00004344 ExprResult NewStep = Step;
4345 if (Captures)
4346 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004347 if (NewStep.isInvalid())
4348 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004349 ExprResult Update =
4350 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004351 if (!Update.isUsable())
4352 return ExprError();
4353
Alexey Bataevc0214e02016-02-16 12:13:49 +00004354 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4355 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004356 ExprResult NewStart = Start;
4357 if (Captures)
4358 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004359 if (NewStart.isInvalid())
4360 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004361
Alexey Bataevc0214e02016-02-16 12:13:49 +00004362 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4363 ExprResult SavedUpdate = Update;
4364 ExprResult UpdateVal;
4365 if (VarRef.get()->getType()->isOverloadableType() ||
4366 NewStart.get()->getType()->isOverloadableType() ||
4367 Update.get()->getType()->isOverloadableType()) {
4368 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4369 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4370 Update =
4371 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4372 if (Update.isUsable()) {
4373 UpdateVal =
4374 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4375 VarRef.get(), SavedUpdate.get());
4376 if (UpdateVal.isUsable()) {
4377 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4378 UpdateVal.get());
4379 }
4380 }
4381 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4382 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004383
Alexey Bataevc0214e02016-02-16 12:13:49 +00004384 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4385 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4386 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4387 NewStart.get(), SavedUpdate.get());
4388 if (!Update.isUsable())
4389 return ExprError();
4390
Alexey Bataev11481f52016-02-17 10:29:05 +00004391 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4392 VarRef.get()->getType())) {
4393 Update = SemaRef.PerformImplicitConversion(
4394 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4395 if (!Update.isUsable())
4396 return ExprError();
4397 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004398
4399 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4400 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004401 return Update;
4402}
4403
4404/// \brief Convert integer expression \a E to make it have at least \a Bits
4405/// bits.
4406static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4407 Sema &SemaRef) {
4408 if (E == nullptr)
4409 return ExprError();
4410 auto &C = SemaRef.Context;
4411 QualType OldType = E->getType();
4412 unsigned HasBits = C.getTypeSize(OldType);
4413 if (HasBits >= Bits)
4414 return ExprResult(E);
4415 // OK to convert to signed, because new type has more bits than old.
4416 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4417 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4418 true);
4419}
4420
4421/// \brief Check if the given expression \a E is a constant integer that fits
4422/// into \a Bits bits.
4423static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4424 if (E == nullptr)
4425 return false;
4426 llvm::APSInt Result;
4427 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4428 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4429 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004430}
4431
Alexey Bataev5a3af132016-03-29 08:58:54 +00004432/// Build preinits statement for the given declarations.
4433static Stmt *buildPreInits(ASTContext &Context,
4434 SmallVectorImpl<Decl *> &PreInits) {
4435 if (!PreInits.empty()) {
4436 return new (Context) DeclStmt(
4437 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4438 SourceLocation(), SourceLocation());
4439 }
4440 return nullptr;
4441}
4442
4443/// Build preinits statement for the given declarations.
4444static Stmt *buildPreInits(ASTContext &Context,
4445 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4446 if (!Captures.empty()) {
4447 SmallVector<Decl *, 16> PreInits;
4448 for (auto &Pair : Captures)
4449 PreInits.push_back(Pair.second->getDecl());
4450 return buildPreInits(Context, PreInits);
4451 }
4452 return nullptr;
4453}
4454
4455/// Build postupdate expression for the given list of postupdates expressions.
4456static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4457 Expr *PostUpdate = nullptr;
4458 if (!PostUpdates.empty()) {
4459 for (auto *E : PostUpdates) {
4460 Expr *ConvE = S.BuildCStyleCastExpr(
4461 E->getExprLoc(),
4462 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4463 E->getExprLoc(), E)
4464 .get();
4465 PostUpdate = PostUpdate
4466 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4467 PostUpdate, ConvE)
4468 .get()
4469 : ConvE;
4470 }
4471 }
4472 return PostUpdate;
4473}
4474
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004475/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004476/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4477/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004478static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00004479CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4480 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4481 DSAStackTy &DSA,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004482 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004483 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004484 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004485 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004486 // Found 'collapse' clause - calculate collapse number.
4487 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004488 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004489 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004490 }
4491 if (OrderedLoopCountExpr) {
4492 // Found 'ordered' clause - calculate collapse number.
4493 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004494 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4495 if (Result.getLimitedValue() < NestedLoopCount) {
4496 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4497 diag::err_omp_wrong_ordered_loop_count)
4498 << OrderedLoopCountExpr->getSourceRange();
4499 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4500 diag::note_collapse_loop_count)
4501 << CollapseLoopCountExpr->getSourceRange();
4502 }
4503 NestedLoopCount = Result.getLimitedValue();
4504 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004505 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004506 // This is helper routine for loop directives (e.g., 'for', 'simd',
4507 // 'for simd', etc.).
Alexey Bataev5a3af132016-03-29 08:58:54 +00004508 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004509 SmallVector<LoopIterationSpace, 4> IterSpaces;
4510 IterSpaces.resize(NestedLoopCount);
4511 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004512 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004513 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004514 NestedLoopCount, CollapseLoopCountExpr,
4515 OrderedLoopCountExpr, VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004516 IterSpaces[Cnt], Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004517 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004518 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004519 // OpenMP [2.8.1, simd construct, Restrictions]
4520 // All loops associated with the construct must be perfectly nested; that
4521 // is, there must be no intervening code nor any OpenMP directive between
4522 // any two loops.
4523 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004524 }
4525
Alexander Musmana5f070a2014-10-01 06:03:56 +00004526 Built.clear(/* size */ NestedLoopCount);
4527
4528 if (SemaRef.CurContext->isDependentContext())
4529 return NestedLoopCount;
4530
4531 // An example of what is generated for the following code:
4532 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004533 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004534 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004535 // for (k = 0; k < NK; ++k)
4536 // for (j = J0; j < NJ; j+=2) {
4537 // <loop body>
4538 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004539 //
4540 // We generate the code below.
4541 // Note: the loop body may be outlined in CodeGen.
4542 // Note: some counters may be C++ classes, operator- is used to find number of
4543 // iterations and operator+= to calculate counter value.
4544 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4545 // or i64 is currently supported).
4546 //
4547 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4548 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4549 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4550 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4551 // // similar updates for vars in clauses (e.g. 'linear')
4552 // <loop body (using local i and j)>
4553 // }
4554 // i = NI; // assign final values of counters
4555 // j = NJ;
4556 //
4557
4558 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4559 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004560 // Precondition tests if there is at least one iteration (all conditions are
4561 // true).
4562 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004563 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004564 ExprResult LastIteration32 = WidenIterationCount(
4565 32 /* Bits */, SemaRef.PerformImplicitConversion(
4566 N0->IgnoreImpCasts(), N0->getType(),
4567 Sema::AA_Converting, /*AllowExplicit=*/true)
4568 .get(),
4569 SemaRef);
4570 ExprResult LastIteration64 = WidenIterationCount(
4571 64 /* Bits */, SemaRef.PerformImplicitConversion(
4572 N0->IgnoreImpCasts(), N0->getType(),
4573 Sema::AA_Converting, /*AllowExplicit=*/true)
4574 .get(),
4575 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004576
4577 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4578 return NestedLoopCount;
4579
4580 auto &C = SemaRef.Context;
4581 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4582
4583 Scope *CurScope = DSA.getCurScope();
4584 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004585 if (PreCond.isUsable()) {
4586 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4587 PreCond.get(), IterSpaces[Cnt].PreCond);
4588 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004589 auto N = IterSpaces[Cnt].NumIterations;
4590 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4591 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004592 LastIteration32 = SemaRef.BuildBinOp(
4593 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4594 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4595 Sema::AA_Converting,
4596 /*AllowExplicit=*/true)
4597 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004598 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004599 LastIteration64 = SemaRef.BuildBinOp(
4600 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4601 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4602 Sema::AA_Converting,
4603 /*AllowExplicit=*/true)
4604 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004605 }
4606
4607 // Choose either the 32-bit or 64-bit version.
4608 ExprResult LastIteration = LastIteration64;
4609 if (LastIteration32.isUsable() &&
4610 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4611 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4612 FitsInto(
4613 32 /* Bits */,
4614 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4615 LastIteration64.get(), SemaRef)))
4616 LastIteration = LastIteration32;
4617
4618 if (!LastIteration.isUsable())
4619 return 0;
4620
4621 // Save the number of iterations.
4622 ExprResult NumIterations = LastIteration;
4623 {
4624 LastIteration = SemaRef.BuildBinOp(
4625 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4626 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4627 if (!LastIteration.isUsable())
4628 return 0;
4629 }
4630
4631 // Calculate the last iteration number beforehand instead of doing this on
4632 // each iteration. Do not do this if the number of iterations may be kfold-ed.
4633 llvm::APSInt Result;
4634 bool IsConstant =
4635 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4636 ExprResult CalcLastIteration;
4637 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004638 ExprResult SaveRef =
4639 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004640 LastIteration = SaveRef;
4641
4642 // Prepare SaveRef + 1.
4643 NumIterations = SemaRef.BuildBinOp(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004644 CurScope, SourceLocation(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00004645 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4646 if (!NumIterations.isUsable())
4647 return 0;
4648 }
4649
4650 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4651
Alexander Musmanc6388682014-12-15 07:07:06 +00004652 QualType VType = LastIteration.get()->getType();
4653 // Build variables passed into runtime, nesessary for worksharing directives.
4654 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004655 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4656 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004657 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004658 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4659 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004660 SemaRef.AddInitializerToDecl(
4661 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4662 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4663
4664 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00004665 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4666 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004667 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4668 /*DirectInit*/ false,
4669 /*TypeMayContainAuto*/ false);
4670
4671 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4672 // This will be used to implement clause 'lastprivate'.
4673 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00004674 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4675 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004676 SemaRef.AddInitializerToDecl(
4677 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4678 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4679
4680 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00004681 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
4682 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00004683 SemaRef.AddInitializerToDecl(
4684 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4685 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4686
4687 // Build expression: UB = min(UB, LastIteration)
4688 // It is nesessary for CodeGen of directives with static scheduling.
4689 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4690 UB.get(), LastIteration.get());
4691 ExprResult CondOp = SemaRef.ActOnConditionalOp(
4692 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4693 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4694 CondOp.get());
4695 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4696 }
4697
4698 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004699 ExprResult IV;
4700 ExprResult Init;
4701 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00004702 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
4703 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00004704 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004705 isOpenMPTaskLoopDirective(DKind) ||
4706 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004707 ? LB.get()
4708 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4709 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4710 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004711 }
4712
Alexander Musmanc6388682014-12-15 07:07:06 +00004713 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004714 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00004715 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004716 (isOpenMPWorksharingDirective(DKind) ||
4717 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00004718 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4719 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4720 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004721
4722 // Loop increment (IV = IV + 1)
4723 SourceLocation IncLoc;
4724 ExprResult Inc =
4725 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4726 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4727 if (!Inc.isUsable())
4728 return 0;
4729 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00004730 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4731 if (!Inc.isUsable())
4732 return 0;
4733
4734 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4735 // Used for directives with static scheduling.
4736 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00004737 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4738 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00004739 // LB + ST
4740 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4741 if (!NextLB.isUsable())
4742 return 0;
4743 // LB = LB + ST
4744 NextLB =
4745 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4746 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4747 if (!NextLB.isUsable())
4748 return 0;
4749 // UB + ST
4750 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4751 if (!NextUB.isUsable())
4752 return 0;
4753 // UB = UB + ST
4754 NextUB =
4755 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4756 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4757 if (!NextUB.isUsable())
4758 return 0;
4759 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004760
4761 // Build updates and final values of the loop counters.
4762 bool HasErrors = false;
4763 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004764 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004765 Built.Updates.resize(NestedLoopCount);
4766 Built.Finals.resize(NestedLoopCount);
4767 {
4768 ExprResult Div;
4769 // Go from inner nested loop to outer.
4770 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4771 LoopIterationSpace &IS = IterSpaces[Cnt];
4772 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4773 // Build: Iter = (IV / Div) % IS.NumIters
4774 // where Div is product of previous iterations' IS.NumIters.
4775 ExprResult Iter;
4776 if (Div.isUsable()) {
4777 Iter =
4778 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4779 } else {
4780 Iter = IV;
4781 assert((Cnt == (int)NestedLoopCount - 1) &&
4782 "unusable div expected on first iteration only");
4783 }
4784
4785 if (Cnt != 0 && Iter.isUsable())
4786 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4787 IS.NumIterations);
4788 if (!Iter.isUsable()) {
4789 HasErrors = true;
4790 break;
4791 }
4792
Alexey Bataev39f915b82015-05-08 10:41:21 +00004793 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004794 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4795 auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4796 IS.CounterVar->getExprLoc(),
4797 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004798 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004799 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004800 if (!Init.isUsable()) {
4801 HasErrors = true;
4802 break;
4803 }
Alexey Bataev5a3af132016-03-29 08:58:54 +00004804 ExprResult Update = BuildCounterUpdate(
4805 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4806 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004807 if (!Update.isUsable()) {
4808 HasErrors = true;
4809 break;
4810 }
4811
4812 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4813 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00004814 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004815 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004816 if (!Final.isUsable()) {
4817 HasErrors = true;
4818 break;
4819 }
4820
4821 // Build Div for the next iteration: Div <- Div * IS.NumIters
4822 if (Cnt != 0) {
4823 if (Div.isUnset())
4824 Div = IS.NumIterations;
4825 else
4826 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4827 IS.NumIterations);
4828
4829 // Add parentheses (for debugging purposes only).
4830 if (Div.isUsable())
4831 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4832 if (!Div.isUsable()) {
4833 HasErrors = true;
4834 break;
4835 }
4836 }
4837 if (!Update.isUsable() || !Final.isUsable()) {
4838 HasErrors = true;
4839 break;
4840 }
4841 // Save results
4842 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004843 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004844 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004845 Built.Updates[Cnt] = Update.get();
4846 Built.Finals[Cnt] = Final.get();
4847 }
4848 }
4849
4850 if (HasErrors)
4851 return 0;
4852
4853 // Save results
4854 Built.IterationVarRef = IV.get();
4855 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004856 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004857 Built.CalcLastIteration =
4858 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004859 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00004860 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00004861 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004862 Built.Init = Init.get();
4863 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004864 Built.LB = LB.get();
4865 Built.UB = UB.get();
4866 Built.IL = IL.get();
4867 Built.ST = ST.get();
4868 Built.EUB = EUB.get();
4869 Built.NLB = NextLB.get();
4870 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004871
Alexey Bataevabfc0692014-06-25 06:52:00 +00004872 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004873}
4874
Alexey Bataev10e775f2015-07-30 11:36:16 +00004875static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004876 auto CollapseClauses =
4877 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4878 if (CollapseClauses.begin() != CollapseClauses.end())
4879 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004880 return nullptr;
4881}
4882
Alexey Bataev10e775f2015-07-30 11:36:16 +00004883static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004884 auto OrderedClauses =
4885 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4886 if (OrderedClauses.begin() != OrderedClauses.end())
4887 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004888 return nullptr;
4889}
4890
Alexey Bataev66b15b52015-08-21 11:14:16 +00004891static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4892 const Expr *Safelen) {
4893 llvm::APSInt SimdlenRes, SafelenRes;
4894 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4895 Simdlen->isInstantiationDependent() ||
4896 Simdlen->containsUnexpandedParameterPack())
4897 return false;
4898 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4899 Safelen->isInstantiationDependent() ||
4900 Safelen->containsUnexpandedParameterPack())
4901 return false;
4902 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4903 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4904 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4905 // If both simdlen and safelen clauses are specified, the value of the simdlen
4906 // parameter must be less than or equal to the value of the safelen parameter.
4907 if (SimdlenRes > SafelenRes) {
4908 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4909 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4910 return true;
4911 }
4912 return false;
4913}
4914
Alexey Bataev4acb8592014-07-07 13:01:15 +00004915StmtResult Sema::ActOnOpenMPSimdDirective(
4916 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4917 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004918 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004919 if (!AStmt)
4920 return StmtError();
4921
4922 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004923 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004924 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4925 // define the nested loops number.
4926 unsigned NestedLoopCount = CheckOpenMPLoop(
4927 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4928 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004929 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004930 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004931
Alexander Musmana5f070a2014-10-01 06:03:56 +00004932 assert((CurContext->isDependentContext() || B.builtAll()) &&
4933 "omp simd loop exprs were not built");
4934
Alexander Musman3276a272015-03-21 10:12:56 +00004935 if (!CurContext->isDependentContext()) {
4936 // Finalize the clauses that need pre-built expressions for CodeGen.
4937 for (auto C : Clauses) {
4938 if (auto LC = dyn_cast<OMPLinearClause>(C))
4939 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004940 B.NumIterations, *this, CurScope,
4941 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00004942 return StmtError();
4943 }
4944 }
4945
Alexey Bataev66b15b52015-08-21 11:14:16 +00004946 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4947 // If both simdlen and safelen clauses are specified, the value of the simdlen
4948 // parameter must be less than or equal to the value of the safelen parameter.
4949 OMPSafelenClause *Safelen = nullptr;
4950 OMPSimdlenClause *Simdlen = nullptr;
4951 for (auto *Clause : Clauses) {
4952 if (Clause->getClauseKind() == OMPC_safelen)
4953 Safelen = cast<OMPSafelenClause>(Clause);
4954 else if (Clause->getClauseKind() == OMPC_simdlen)
4955 Simdlen = cast<OMPSimdlenClause>(Clause);
4956 if (Safelen && Simdlen)
4957 break;
4958 }
4959 if (Simdlen && Safelen &&
4960 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4961 Safelen->getSafelen()))
4962 return StmtError();
4963
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004964 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004965 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4966 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004967}
4968
Alexey Bataev4acb8592014-07-07 13:01:15 +00004969StmtResult Sema::ActOnOpenMPForDirective(
4970 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4971 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00004972 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004973 if (!AStmt)
4974 return StmtError();
4975
4976 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004977 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004978 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4979 // define the nested loops number.
4980 unsigned NestedLoopCount = CheckOpenMPLoop(
4981 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4982 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004983 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004984 return StmtError();
4985
Alexander Musmana5f070a2014-10-01 06:03:56 +00004986 assert((CurContext->isDependentContext() || B.builtAll()) &&
4987 "omp for loop exprs were not built");
4988
Alexey Bataev54acd402015-08-04 11:18:19 +00004989 if (!CurContext->isDependentContext()) {
4990 // Finalize the clauses that need pre-built expressions for CodeGen.
4991 for (auto C : Clauses) {
4992 if (auto LC = dyn_cast<OMPLinearClause>(C))
4993 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004994 B.NumIterations, *this, CurScope,
4995 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00004996 return StmtError();
4997 }
4998 }
4999
Alexey Bataevf29276e2014-06-18 04:14:57 +00005000 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005001 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005002 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005003}
5004
Alexander Musmanf82886e2014-09-18 05:12:34 +00005005StmtResult Sema::ActOnOpenMPForSimdDirective(
5006 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5007 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005008 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005009 if (!AStmt)
5010 return StmtError();
5011
5012 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005013 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005014 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5015 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005016 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005017 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5018 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5019 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005020 if (NestedLoopCount == 0)
5021 return StmtError();
5022
Alexander Musmanc6388682014-12-15 07:07:06 +00005023 assert((CurContext->isDependentContext() || B.builtAll()) &&
5024 "omp for simd loop exprs were not built");
5025
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005026 if (!CurContext->isDependentContext()) {
5027 // Finalize the clauses that need pre-built expressions for CodeGen.
5028 for (auto C : Clauses) {
5029 if (auto LC = dyn_cast<OMPLinearClause>(C))
5030 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005031 B.NumIterations, *this, CurScope,
5032 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005033 return StmtError();
5034 }
5035 }
5036
Alexey Bataev66b15b52015-08-21 11:14:16 +00005037 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5038 // If both simdlen and safelen clauses are specified, the value of the simdlen
5039 // parameter must be less than or equal to the value of the safelen parameter.
5040 OMPSafelenClause *Safelen = nullptr;
5041 OMPSimdlenClause *Simdlen = nullptr;
5042 for (auto *Clause : Clauses) {
5043 if (Clause->getClauseKind() == OMPC_safelen)
5044 Safelen = cast<OMPSafelenClause>(Clause);
5045 else if (Clause->getClauseKind() == OMPC_simdlen)
5046 Simdlen = cast<OMPSimdlenClause>(Clause);
5047 if (Safelen && Simdlen)
5048 break;
5049 }
5050 if (Simdlen && Safelen &&
5051 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5052 Safelen->getSafelen()))
5053 return StmtError();
5054
Alexander Musmanf82886e2014-09-18 05:12:34 +00005055 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005056 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5057 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005058}
5059
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005060StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5061 Stmt *AStmt,
5062 SourceLocation StartLoc,
5063 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005064 if (!AStmt)
5065 return StmtError();
5066
5067 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005068 auto BaseStmt = AStmt;
5069 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5070 BaseStmt = CS->getCapturedStmt();
5071 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5072 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005073 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005074 return StmtError();
5075 // All associated statements must be '#pragma omp section' except for
5076 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005077 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005078 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5079 if (SectionStmt)
5080 Diag(SectionStmt->getLocStart(),
5081 diag::err_omp_sections_substmt_not_section);
5082 return StmtError();
5083 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005084 cast<OMPSectionDirective>(SectionStmt)
5085 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005086 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005087 } else {
5088 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5089 return StmtError();
5090 }
5091
5092 getCurFunction()->setHasBranchProtectedScope();
5093
Alexey Bataev25e5b442015-09-15 12:52:43 +00005094 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5095 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005096}
5097
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005098StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5099 SourceLocation StartLoc,
5100 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005101 if (!AStmt)
5102 return StmtError();
5103
5104 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005105
5106 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005107 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005108
Alexey Bataev25e5b442015-09-15 12:52:43 +00005109 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5110 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005111}
5112
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005113StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5114 Stmt *AStmt,
5115 SourceLocation StartLoc,
5116 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005117 if (!AStmt)
5118 return StmtError();
5119
5120 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005121
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005122 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005123
Alexey Bataev3255bf32015-01-19 05:20:46 +00005124 // OpenMP [2.7.3, single Construct, Restrictions]
5125 // The copyprivate clause must not be used with the nowait clause.
5126 OMPClause *Nowait = nullptr;
5127 OMPClause *Copyprivate = nullptr;
5128 for (auto *Clause : Clauses) {
5129 if (Clause->getClauseKind() == OMPC_nowait)
5130 Nowait = Clause;
5131 else if (Clause->getClauseKind() == OMPC_copyprivate)
5132 Copyprivate = Clause;
5133 if (Copyprivate && Nowait) {
5134 Diag(Copyprivate->getLocStart(),
5135 diag::err_omp_single_copyprivate_with_nowait);
5136 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5137 return StmtError();
5138 }
5139 }
5140
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005141 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5142}
5143
Alexander Musman80c22892014-07-17 08:54:58 +00005144StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5145 SourceLocation StartLoc,
5146 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005147 if (!AStmt)
5148 return StmtError();
5149
5150 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005151
5152 getCurFunction()->setHasBranchProtectedScope();
5153
5154 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5155}
5156
Alexey Bataev28c75412015-12-15 08:19:24 +00005157StmtResult Sema::ActOnOpenMPCriticalDirective(
5158 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5159 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005160 if (!AStmt)
5161 return StmtError();
5162
5163 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005164
Alexey Bataev28c75412015-12-15 08:19:24 +00005165 bool ErrorFound = false;
5166 llvm::APSInt Hint;
5167 SourceLocation HintLoc;
5168 bool DependentHint = false;
5169 for (auto *C : Clauses) {
5170 if (C->getClauseKind() == OMPC_hint) {
5171 if (!DirName.getName()) {
5172 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5173 ErrorFound = true;
5174 }
5175 Expr *E = cast<OMPHintClause>(C)->getHint();
5176 if (E->isTypeDependent() || E->isValueDependent() ||
5177 E->isInstantiationDependent())
5178 DependentHint = true;
5179 else {
5180 Hint = E->EvaluateKnownConstInt(Context);
5181 HintLoc = C->getLocStart();
5182 }
5183 }
5184 }
5185 if (ErrorFound)
5186 return StmtError();
5187 auto Pair = DSAStack->getCriticalWithHint(DirName);
5188 if (Pair.first && DirName.getName() && !DependentHint) {
5189 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5190 Diag(StartLoc, diag::err_omp_critical_with_hint);
5191 if (HintLoc.isValid()) {
5192 Diag(HintLoc, diag::note_omp_critical_hint_here)
5193 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5194 } else
5195 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5196 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5197 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5198 << 1
5199 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5200 /*Radix=*/10, /*Signed=*/false);
5201 } else
5202 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5203 }
5204 }
5205
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005206 getCurFunction()->setHasBranchProtectedScope();
5207
Alexey Bataev28c75412015-12-15 08:19:24 +00005208 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5209 Clauses, AStmt);
5210 if (!Pair.first && DirName.getName() && !DependentHint)
5211 DSAStack->addCriticalWithHint(Dir, Hint);
5212 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005213}
5214
Alexey Bataev4acb8592014-07-07 13:01:15 +00005215StmtResult Sema::ActOnOpenMPParallelForDirective(
5216 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5217 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005218 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005219 if (!AStmt)
5220 return StmtError();
5221
Alexey Bataev4acb8592014-07-07 13:01:15 +00005222 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5223 // 1.2.2 OpenMP Language Terminology
5224 // Structured block - An executable statement with a single entry at the
5225 // top and a single exit at the bottom.
5226 // The point of exit cannot be a branch out of the structured block.
5227 // longjmp() and throw() must not violate the entry/exit criteria.
5228 CS->getCapturedDecl()->setNothrow();
5229
Alexander Musmanc6388682014-12-15 07:07:06 +00005230 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005231 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5232 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005233 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005234 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5235 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5236 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005237 if (NestedLoopCount == 0)
5238 return StmtError();
5239
Alexander Musmana5f070a2014-10-01 06:03:56 +00005240 assert((CurContext->isDependentContext() || B.builtAll()) &&
5241 "omp parallel for loop exprs were not built");
5242
Alexey Bataev54acd402015-08-04 11:18:19 +00005243 if (!CurContext->isDependentContext()) {
5244 // Finalize the clauses that need pre-built expressions for CodeGen.
5245 for (auto C : Clauses) {
5246 if (auto LC = dyn_cast<OMPLinearClause>(C))
5247 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005248 B.NumIterations, *this, CurScope,
5249 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005250 return StmtError();
5251 }
5252 }
5253
Alexey Bataev4acb8592014-07-07 13:01:15 +00005254 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005255 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005256 NestedLoopCount, Clauses, AStmt, B,
5257 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005258}
5259
Alexander Musmane4e893b2014-09-23 09:33:00 +00005260StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5261 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5262 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00005263 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005264 if (!AStmt)
5265 return StmtError();
5266
Alexander Musmane4e893b2014-09-23 09:33:00 +00005267 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5268 // 1.2.2 OpenMP Language Terminology
5269 // Structured block - An executable statement with a single entry at the
5270 // top and a single exit at the bottom.
5271 // The point of exit cannot be a branch out of the structured block.
5272 // longjmp() and throw() must not violate the entry/exit criteria.
5273 CS->getCapturedDecl()->setNothrow();
5274
Alexander Musmanc6388682014-12-15 07:07:06 +00005275 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005276 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5277 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005278 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00005279 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5280 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5281 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005282 if (NestedLoopCount == 0)
5283 return StmtError();
5284
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005285 if (!CurContext->isDependentContext()) {
5286 // Finalize the clauses that need pre-built expressions for CodeGen.
5287 for (auto C : Clauses) {
5288 if (auto LC = dyn_cast<OMPLinearClause>(C))
5289 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005290 B.NumIterations, *this, CurScope,
5291 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005292 return StmtError();
5293 }
5294 }
5295
Alexey Bataev66b15b52015-08-21 11:14:16 +00005296 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5297 // If both simdlen and safelen clauses are specified, the value of the simdlen
5298 // parameter must be less than or equal to the value of the safelen parameter.
5299 OMPSafelenClause *Safelen = nullptr;
5300 OMPSimdlenClause *Simdlen = nullptr;
5301 for (auto *Clause : Clauses) {
5302 if (Clause->getClauseKind() == OMPC_safelen)
5303 Safelen = cast<OMPSafelenClause>(Clause);
5304 else if (Clause->getClauseKind() == OMPC_simdlen)
5305 Simdlen = cast<OMPSimdlenClause>(Clause);
5306 if (Safelen && Simdlen)
5307 break;
5308 }
5309 if (Simdlen && Safelen &&
5310 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5311 Safelen->getSafelen()))
5312 return StmtError();
5313
Alexander Musmane4e893b2014-09-23 09:33:00 +00005314 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005315 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005316 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005317}
5318
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005319StmtResult
5320Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5321 Stmt *AStmt, SourceLocation StartLoc,
5322 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005323 if (!AStmt)
5324 return StmtError();
5325
5326 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005327 auto BaseStmt = AStmt;
5328 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5329 BaseStmt = CS->getCapturedStmt();
5330 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5331 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005332 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005333 return StmtError();
5334 // All associated statements must be '#pragma omp section' except for
5335 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005336 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005337 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5338 if (SectionStmt)
5339 Diag(SectionStmt->getLocStart(),
5340 diag::err_omp_parallel_sections_substmt_not_section);
5341 return StmtError();
5342 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005343 cast<OMPSectionDirective>(SectionStmt)
5344 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005345 }
5346 } else {
5347 Diag(AStmt->getLocStart(),
5348 diag::err_omp_parallel_sections_not_compound_stmt);
5349 return StmtError();
5350 }
5351
5352 getCurFunction()->setHasBranchProtectedScope();
5353
Alexey Bataev25e5b442015-09-15 12:52:43 +00005354 return OMPParallelSectionsDirective::Create(
5355 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005356}
5357
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005358StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5359 Stmt *AStmt, SourceLocation StartLoc,
5360 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005361 if (!AStmt)
5362 return StmtError();
5363
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005364 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5365 // 1.2.2 OpenMP Language Terminology
5366 // Structured block - An executable statement with a single entry at the
5367 // top and a single exit at the bottom.
5368 // The point of exit cannot be a branch out of the structured block.
5369 // longjmp() and throw() must not violate the entry/exit criteria.
5370 CS->getCapturedDecl()->setNothrow();
5371
5372 getCurFunction()->setHasBranchProtectedScope();
5373
Alexey Bataev25e5b442015-09-15 12:52:43 +00005374 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5375 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005376}
5377
Alexey Bataev68446b72014-07-18 07:47:19 +00005378StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5379 SourceLocation EndLoc) {
5380 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5381}
5382
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005383StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5384 SourceLocation EndLoc) {
5385 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5386}
5387
Alexey Bataev2df347a2014-07-18 10:17:07 +00005388StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5389 SourceLocation EndLoc) {
5390 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5391}
5392
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005393StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5394 SourceLocation StartLoc,
5395 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005396 if (!AStmt)
5397 return StmtError();
5398
5399 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005400
5401 getCurFunction()->setHasBranchProtectedScope();
5402
5403 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5404}
5405
Alexey Bataev6125da92014-07-21 11:26:11 +00005406StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5407 SourceLocation StartLoc,
5408 SourceLocation EndLoc) {
5409 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5410 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5411}
5412
Alexey Bataev346265e2015-09-25 10:37:12 +00005413StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5414 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005415 SourceLocation StartLoc,
5416 SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005417 OMPClause *DependFound = nullptr;
5418 OMPClause *DependSourceClause = nullptr;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005419 OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005420 bool ErrorFound = false;
Alexey Bataev346265e2015-09-25 10:37:12 +00005421 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005422 OMPSIMDClause *SC = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005423 for (auto *C : Clauses) {
5424 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5425 DependFound = C;
5426 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5427 if (DependSourceClause) {
5428 Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5429 << getOpenMPDirectiveName(OMPD_ordered)
5430 << getOpenMPClauseName(OMPC_depend) << 2;
5431 ErrorFound = true;
5432 } else
5433 DependSourceClause = C;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005434 if (DependSinkClause) {
5435 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5436 << 0;
5437 ErrorFound = true;
5438 }
5439 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5440 if (DependSourceClause) {
5441 Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5442 << 1;
5443 ErrorFound = true;
5444 }
5445 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00005446 }
5447 } else if (C->getClauseKind() == OMPC_threads)
Alexey Bataev346265e2015-09-25 10:37:12 +00005448 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005449 else if (C->getClauseKind() == OMPC_simd)
5450 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00005451 }
Alexey Bataeveb482352015-12-18 05:05:56 +00005452 if (!ErrorFound && !SC &&
5453 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005454 // OpenMP [2.8.1,simd Construct, Restrictions]
5455 // An ordered construct with the simd clause is the only OpenMP construct
5456 // that can appear in the simd region.
5457 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00005458 ErrorFound = true;
5459 } else if (DependFound && (TC || SC)) {
5460 Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5461 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5462 ErrorFound = true;
5463 } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5464 Diag(DependFound->getLocStart(),
5465 diag::err_omp_ordered_directive_without_param);
5466 ErrorFound = true;
5467 } else if (TC || Clauses.empty()) {
5468 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5469 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5470 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5471 << (TC != nullptr);
5472 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5473 ErrorFound = true;
5474 }
5475 }
5476 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005477 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00005478
5479 if (AStmt) {
5480 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5481
5482 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005483 }
Alexey Bataev346265e2015-09-25 10:37:12 +00005484
5485 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005486}
5487
Alexey Bataev1d160b12015-03-13 12:27:31 +00005488namespace {
5489/// \brief Helper class for checking expression in 'omp atomic [update]'
5490/// construct.
5491class OpenMPAtomicUpdateChecker {
5492 /// \brief Error results for atomic update expressions.
5493 enum ExprAnalysisErrorCode {
5494 /// \brief A statement is not an expression statement.
5495 NotAnExpression,
5496 /// \brief Expression is not builtin binary or unary operation.
5497 NotABinaryOrUnaryExpression,
5498 /// \brief Unary operation is not post-/pre- increment/decrement operation.
5499 NotAnUnaryIncDecExpression,
5500 /// \brief An expression is not of scalar type.
5501 NotAScalarType,
5502 /// \brief A binary operation is not an assignment operation.
5503 NotAnAssignmentOp,
5504 /// \brief RHS part of the binary operation is not a binary expression.
5505 NotABinaryExpression,
5506 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5507 /// expression.
5508 NotABinaryOperator,
5509 /// \brief RHS binary operation does not have reference to the updated LHS
5510 /// part.
5511 NotAnUpdateExpression,
5512 /// \brief No errors is found.
5513 NoError
5514 };
5515 /// \brief Reference to Sema.
5516 Sema &SemaRef;
5517 /// \brief A location for note diagnostics (when error is found).
5518 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005519 /// \brief 'x' lvalue part of the source atomic expression.
5520 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005521 /// \brief 'expr' rvalue part of the source atomic expression.
5522 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005523 /// \brief Helper expression of the form
5524 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5525 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5526 Expr *UpdateExpr;
5527 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5528 /// important for non-associative operations.
5529 bool IsXLHSInRHSPart;
5530 BinaryOperatorKind Op;
5531 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005532 /// \brief true if the source expression is a postfix unary operation, false
5533 /// if it is a prefix unary operation.
5534 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005535
5536public:
5537 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00005538 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00005539 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00005540 /// \brief Check specified statement that it is suitable for 'atomic update'
5541 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00005542 /// expression. If DiagId and NoteId == 0, then only check is performed
5543 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00005544 /// \param DiagId Diagnostic which should be emitted if error is found.
5545 /// \param NoteId Diagnostic note for the main error message.
5546 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00005547 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005548 /// \brief Return the 'x' lvalue part of the source atomic expression.
5549 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00005550 /// \brief Return the 'expr' rvalue part of the source atomic expression.
5551 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00005552 /// \brief Return the update expression used in calculation of the updated
5553 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5554 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5555 Expr *getUpdateExpr() const { return UpdateExpr; }
5556 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5557 /// false otherwise.
5558 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5559
Alexey Bataevb78ca832015-04-01 03:33:17 +00005560 /// \brief true if the source expression is a postfix unary operation, false
5561 /// if it is a prefix unary operation.
5562 bool isPostfixUpdate() const { return IsPostfixUpdate; }
5563
Alexey Bataev1d160b12015-03-13 12:27:31 +00005564private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00005565 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5566 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00005567};
5568} // namespace
5569
5570bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5571 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5572 ExprAnalysisErrorCode ErrorFound = NoError;
5573 SourceLocation ErrorLoc, NoteLoc;
5574 SourceRange ErrorRange, NoteRange;
5575 // Allowed constructs are:
5576 // x = x binop expr;
5577 // x = expr binop x;
5578 if (AtomicBinOp->getOpcode() == BO_Assign) {
5579 X = AtomicBinOp->getLHS();
5580 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5581 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5582 if (AtomicInnerBinOp->isMultiplicativeOp() ||
5583 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5584 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005585 Op = AtomicInnerBinOp->getOpcode();
5586 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005587 auto *LHS = AtomicInnerBinOp->getLHS();
5588 auto *RHS = AtomicInnerBinOp->getRHS();
5589 llvm::FoldingSetNodeID XId, LHSId, RHSId;
5590 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5591 /*Canonical=*/true);
5592 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5593 /*Canonical=*/true);
5594 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5595 /*Canonical=*/true);
5596 if (XId == LHSId) {
5597 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005598 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005599 } else if (XId == RHSId) {
5600 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005601 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005602 } else {
5603 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5604 ErrorRange = AtomicInnerBinOp->getSourceRange();
5605 NoteLoc = X->getExprLoc();
5606 NoteRange = X->getSourceRange();
5607 ErrorFound = NotAnUpdateExpression;
5608 }
5609 } else {
5610 ErrorLoc = AtomicInnerBinOp->getExprLoc();
5611 ErrorRange = AtomicInnerBinOp->getSourceRange();
5612 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5613 NoteRange = SourceRange(NoteLoc, NoteLoc);
5614 ErrorFound = NotABinaryOperator;
5615 }
5616 } else {
5617 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5618 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5619 ErrorFound = NotABinaryExpression;
5620 }
5621 } else {
5622 ErrorLoc = AtomicBinOp->getExprLoc();
5623 ErrorRange = AtomicBinOp->getSourceRange();
5624 NoteLoc = AtomicBinOp->getOperatorLoc();
5625 NoteRange = SourceRange(NoteLoc, NoteLoc);
5626 ErrorFound = NotAnAssignmentOp;
5627 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005628 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005629 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5630 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5631 return true;
5632 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005633 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005634 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005635}
5636
5637bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5638 unsigned NoteId) {
5639 ExprAnalysisErrorCode ErrorFound = NoError;
5640 SourceLocation ErrorLoc, NoteLoc;
5641 SourceRange ErrorRange, NoteRange;
5642 // Allowed constructs are:
5643 // x++;
5644 // x--;
5645 // ++x;
5646 // --x;
5647 // x binop= expr;
5648 // x = x binop expr;
5649 // x = expr binop x;
5650 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5651 AtomicBody = AtomicBody->IgnoreParenImpCasts();
5652 if (AtomicBody->getType()->isScalarType() ||
5653 AtomicBody->isInstantiationDependent()) {
5654 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5655 AtomicBody->IgnoreParenImpCasts())) {
5656 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005657 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00005658 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00005659 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005660 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005661 X = AtomicCompAssignOp->getLHS();
5662 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005663 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5664 AtomicBody->IgnoreParenImpCasts())) {
5665 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00005666 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5667 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005668 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00005669 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5670 // Check for Unary Operation
5671 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005672 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005673 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5674 OpLoc = AtomicUnaryOp->getOperatorLoc();
5675 X = AtomicUnaryOp->getSubExpr();
5676 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5677 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005678 } else {
5679 ErrorFound = NotAnUnaryIncDecExpression;
5680 ErrorLoc = AtomicUnaryOp->getExprLoc();
5681 ErrorRange = AtomicUnaryOp->getSourceRange();
5682 NoteLoc = AtomicUnaryOp->getOperatorLoc();
5683 NoteRange = SourceRange(NoteLoc, NoteLoc);
5684 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005685 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005686 ErrorFound = NotABinaryOrUnaryExpression;
5687 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5688 NoteRange = ErrorRange = AtomicBody->getSourceRange();
5689 }
5690 } else {
5691 ErrorFound = NotAScalarType;
5692 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5693 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5694 }
5695 } else {
5696 ErrorFound = NotAnExpression;
5697 NoteLoc = ErrorLoc = S->getLocStart();
5698 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5699 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005700 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005701 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5702 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5703 return true;
5704 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00005705 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00005706 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00005707 // Build an update expression of form 'OpaqueValueExpr(x) binop
5708 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5709 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5710 auto *OVEX = new (SemaRef.getASTContext())
5711 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5712 auto *OVEExpr = new (SemaRef.getASTContext())
5713 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5714 auto Update =
5715 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5716 IsXLHSInRHSPart ? OVEExpr : OVEX);
5717 if (Update.isInvalid())
5718 return true;
5719 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5720 Sema::AA_Casting);
5721 if (Update.isInvalid())
5722 return true;
5723 UpdateExpr = Update.get();
5724 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00005725 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00005726}
5727
Alexey Bataev0162e452014-07-22 10:10:35 +00005728StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5729 Stmt *AStmt,
5730 SourceLocation StartLoc,
5731 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005732 if (!AStmt)
5733 return StmtError();
5734
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005735 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00005736 // 1.2.2 OpenMP Language Terminology
5737 // Structured block - An executable statement with a single entry at the
5738 // top and a single exit at the bottom.
5739 // The point of exit cannot be a branch out of the structured block.
5740 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00005741 OpenMPClauseKind AtomicKind = OMPC_unknown;
5742 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005743 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00005744 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00005745 C->getClauseKind() == OMPC_update ||
5746 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00005747 if (AtomicKind != OMPC_unknown) {
5748 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5749 << SourceRange(C->getLocStart(), C->getLocEnd());
5750 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5751 << getOpenMPClauseName(AtomicKind);
5752 } else {
5753 AtomicKind = C->getClauseKind();
5754 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005755 }
5756 }
5757 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005758
Alexey Bataev459dec02014-07-24 06:46:57 +00005759 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00005760 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5761 Body = EWC->getSubExpr();
5762
Alexey Bataev62cec442014-11-18 10:14:22 +00005763 Expr *X = nullptr;
5764 Expr *V = nullptr;
5765 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00005766 Expr *UE = nullptr;
5767 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005768 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00005769 // OpenMP [2.12.6, atomic Construct]
5770 // In the next expressions:
5771 // * x and v (as applicable) are both l-value expressions with scalar type.
5772 // * During the execution of an atomic region, multiple syntactic
5773 // occurrences of x must designate the same storage location.
5774 // * Neither of v and expr (as applicable) may access the storage location
5775 // designated by x.
5776 // * Neither of x and expr (as applicable) may access the storage location
5777 // designated by v.
5778 // * expr is an expression with scalar type.
5779 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5780 // * binop, binop=, ++, and -- are not overloaded operators.
5781 // * The expression x binop expr must be numerically equivalent to x binop
5782 // (expr). This requirement is satisfied if the operators in expr have
5783 // precedence greater than binop, or by using parentheses around expr or
5784 // subexpressions of expr.
5785 // * The expression expr binop x must be numerically equivalent to (expr)
5786 // binop x. This requirement is satisfied if the operators in expr have
5787 // precedence equal to or greater than binop, or by using parentheses around
5788 // expr or subexpressions of expr.
5789 // * For forms that allow multiple occurrences of x, the number of times
5790 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00005791 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005792 enum {
5793 NotAnExpression,
5794 NotAnAssignmentOp,
5795 NotAScalarType,
5796 NotAnLValue,
5797 NoError
5798 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00005799 SourceLocation ErrorLoc, NoteLoc;
5800 SourceRange ErrorRange, NoteRange;
5801 // If clause is read:
5802 // v = x;
5803 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5804 auto AtomicBinOp =
5805 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5806 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5807 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5808 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5809 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5810 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5811 if (!X->isLValue() || !V->isLValue()) {
5812 auto NotLValueExpr = X->isLValue() ? V : X;
5813 ErrorFound = NotAnLValue;
5814 ErrorLoc = AtomicBinOp->getExprLoc();
5815 ErrorRange = AtomicBinOp->getSourceRange();
5816 NoteLoc = NotLValueExpr->getExprLoc();
5817 NoteRange = NotLValueExpr->getSourceRange();
5818 }
5819 } else if (!X->isInstantiationDependent() ||
5820 !V->isInstantiationDependent()) {
5821 auto NotScalarExpr =
5822 (X->isInstantiationDependent() || X->getType()->isScalarType())
5823 ? V
5824 : X;
5825 ErrorFound = NotAScalarType;
5826 ErrorLoc = AtomicBinOp->getExprLoc();
5827 ErrorRange = AtomicBinOp->getSourceRange();
5828 NoteLoc = NotScalarExpr->getExprLoc();
5829 NoteRange = NotScalarExpr->getSourceRange();
5830 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005831 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00005832 ErrorFound = NotAnAssignmentOp;
5833 ErrorLoc = AtomicBody->getExprLoc();
5834 ErrorRange = AtomicBody->getSourceRange();
5835 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5836 : AtomicBody->getExprLoc();
5837 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5838 : AtomicBody->getSourceRange();
5839 }
5840 } else {
5841 ErrorFound = NotAnExpression;
5842 NoteLoc = ErrorLoc = Body->getLocStart();
5843 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005844 }
Alexey Bataev62cec442014-11-18 10:14:22 +00005845 if (ErrorFound != NoError) {
5846 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5847 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005848 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5849 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00005850 return StmtError();
5851 } else if (CurContext->isDependentContext())
5852 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00005853 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005854 enum {
5855 NotAnExpression,
5856 NotAnAssignmentOp,
5857 NotAScalarType,
5858 NotAnLValue,
5859 NoError
5860 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00005861 SourceLocation ErrorLoc, NoteLoc;
5862 SourceRange ErrorRange, NoteRange;
5863 // If clause is write:
5864 // x = expr;
5865 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5866 auto AtomicBinOp =
5867 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5868 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00005869 X = AtomicBinOp->getLHS();
5870 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00005871 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5872 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5873 if (!X->isLValue()) {
5874 ErrorFound = NotAnLValue;
5875 ErrorLoc = AtomicBinOp->getExprLoc();
5876 ErrorRange = AtomicBinOp->getSourceRange();
5877 NoteLoc = X->getExprLoc();
5878 NoteRange = X->getSourceRange();
5879 }
5880 } else if (!X->isInstantiationDependent() ||
5881 !E->isInstantiationDependent()) {
5882 auto NotScalarExpr =
5883 (X->isInstantiationDependent() || X->getType()->isScalarType())
5884 ? E
5885 : X;
5886 ErrorFound = NotAScalarType;
5887 ErrorLoc = AtomicBinOp->getExprLoc();
5888 ErrorRange = AtomicBinOp->getSourceRange();
5889 NoteLoc = NotScalarExpr->getExprLoc();
5890 NoteRange = NotScalarExpr->getSourceRange();
5891 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005892 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005893 ErrorFound = NotAnAssignmentOp;
5894 ErrorLoc = AtomicBody->getExprLoc();
5895 ErrorRange = AtomicBody->getSourceRange();
5896 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5897 : AtomicBody->getExprLoc();
5898 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5899 : AtomicBody->getSourceRange();
5900 }
5901 } else {
5902 ErrorFound = NotAnExpression;
5903 NoteLoc = ErrorLoc = Body->getLocStart();
5904 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005905 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005906 if (ErrorFound != NoError) {
5907 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5908 << ErrorRange;
5909 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5910 << NoteRange;
5911 return StmtError();
5912 } else if (CurContext->isDependentContext())
5913 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005914 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005915 // If clause is update:
5916 // x++;
5917 // x--;
5918 // ++x;
5919 // --x;
5920 // x binop= expr;
5921 // x = x binop expr;
5922 // x = expr binop x;
5923 OpenMPAtomicUpdateChecker Checker(*this);
5924 if (Checker.checkStatement(
5925 Body, (AtomicKind == OMPC_update)
5926 ? diag::err_omp_atomic_update_not_expression_statement
5927 : diag::err_omp_atomic_not_expression_statement,
5928 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005929 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005930 if (!CurContext->isDependentContext()) {
5931 E = Checker.getExpr();
5932 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005933 UE = Checker.getUpdateExpr();
5934 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005935 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005936 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005937 enum {
5938 NotAnAssignmentOp,
5939 NotACompoundStatement,
5940 NotTwoSubstatements,
5941 NotASpecificExpression,
5942 NoError
5943 } ErrorFound = NoError;
5944 SourceLocation ErrorLoc, NoteLoc;
5945 SourceRange ErrorRange, NoteRange;
5946 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5947 // If clause is a capture:
5948 // v = x++;
5949 // v = x--;
5950 // v = ++x;
5951 // v = --x;
5952 // v = x binop= expr;
5953 // v = x = x binop expr;
5954 // v = x = expr binop x;
5955 auto *AtomicBinOp =
5956 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5957 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5958 V = AtomicBinOp->getLHS();
5959 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5960 OpenMPAtomicUpdateChecker Checker(*this);
5961 if (Checker.checkStatement(
5962 Body, diag::err_omp_atomic_capture_not_expression_statement,
5963 diag::note_omp_atomic_update))
5964 return StmtError();
5965 E = Checker.getExpr();
5966 X = Checker.getX();
5967 UE = Checker.getUpdateExpr();
5968 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5969 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005970 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005971 ErrorLoc = AtomicBody->getExprLoc();
5972 ErrorRange = AtomicBody->getSourceRange();
5973 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5974 : AtomicBody->getExprLoc();
5975 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5976 : AtomicBody->getSourceRange();
5977 ErrorFound = NotAnAssignmentOp;
5978 }
5979 if (ErrorFound != NoError) {
5980 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5981 << ErrorRange;
5982 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5983 return StmtError();
5984 } else if (CurContext->isDependentContext()) {
5985 UE = V = E = X = nullptr;
5986 }
5987 } else {
5988 // If clause is a capture:
5989 // { v = x; x = expr; }
5990 // { v = x; x++; }
5991 // { v = x; x--; }
5992 // { v = x; ++x; }
5993 // { v = x; --x; }
5994 // { v = x; x binop= expr; }
5995 // { v = x; x = x binop expr; }
5996 // { v = x; x = expr binop x; }
5997 // { x++; v = x; }
5998 // { x--; v = x; }
5999 // { ++x; v = x; }
6000 // { --x; v = x; }
6001 // { x binop= expr; v = x; }
6002 // { x = x binop expr; v = x; }
6003 // { x = expr binop x; v = x; }
6004 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6005 // Check that this is { expr1; expr2; }
6006 if (CS->size() == 2) {
6007 auto *First = CS->body_front();
6008 auto *Second = CS->body_back();
6009 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6010 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6011 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6012 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6013 // Need to find what subexpression is 'v' and what is 'x'.
6014 OpenMPAtomicUpdateChecker Checker(*this);
6015 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6016 BinaryOperator *BinOp = nullptr;
6017 if (IsUpdateExprFound) {
6018 BinOp = dyn_cast<BinaryOperator>(First);
6019 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6020 }
6021 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6022 // { v = x; x++; }
6023 // { v = x; x--; }
6024 // { v = x; ++x; }
6025 // { v = x; --x; }
6026 // { v = x; x binop= expr; }
6027 // { v = x; x = x binop expr; }
6028 // { v = x; x = expr binop x; }
6029 // Check that the first expression has form v = x.
6030 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6031 llvm::FoldingSetNodeID XId, PossibleXId;
6032 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6033 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6034 IsUpdateExprFound = XId == PossibleXId;
6035 if (IsUpdateExprFound) {
6036 V = BinOp->getLHS();
6037 X = Checker.getX();
6038 E = Checker.getExpr();
6039 UE = Checker.getUpdateExpr();
6040 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006041 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006042 }
6043 }
6044 if (!IsUpdateExprFound) {
6045 IsUpdateExprFound = !Checker.checkStatement(First);
6046 BinOp = nullptr;
6047 if (IsUpdateExprFound) {
6048 BinOp = dyn_cast<BinaryOperator>(Second);
6049 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6050 }
6051 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6052 // { x++; v = x; }
6053 // { x--; v = x; }
6054 // { ++x; v = x; }
6055 // { --x; v = x; }
6056 // { x binop= expr; v = x; }
6057 // { x = x binop expr; v = x; }
6058 // { x = expr binop x; v = x; }
6059 // Check that the second expression has form v = x.
6060 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6061 llvm::FoldingSetNodeID XId, PossibleXId;
6062 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6063 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6064 IsUpdateExprFound = XId == PossibleXId;
6065 if (IsUpdateExprFound) {
6066 V = BinOp->getLHS();
6067 X = Checker.getX();
6068 E = Checker.getExpr();
6069 UE = Checker.getUpdateExpr();
6070 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006071 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006072 }
6073 }
6074 }
6075 if (!IsUpdateExprFound) {
6076 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006077 auto *FirstExpr = dyn_cast<Expr>(First);
6078 auto *SecondExpr = dyn_cast<Expr>(Second);
6079 if (!FirstExpr || !SecondExpr ||
6080 !(FirstExpr->isInstantiationDependent() ||
6081 SecondExpr->isInstantiationDependent())) {
6082 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6083 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006084 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006085 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6086 : First->getLocStart();
6087 NoteRange = ErrorRange = FirstBinOp
6088 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006089 : SourceRange(ErrorLoc, ErrorLoc);
6090 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006091 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6092 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6093 ErrorFound = NotAnAssignmentOp;
6094 NoteLoc = ErrorLoc = SecondBinOp
6095 ? SecondBinOp->getOperatorLoc()
6096 : Second->getLocStart();
6097 NoteRange = ErrorRange =
6098 SecondBinOp ? SecondBinOp->getSourceRange()
6099 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006100 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006101 auto *PossibleXRHSInFirst =
6102 FirstBinOp->getRHS()->IgnoreParenImpCasts();
6103 auto *PossibleXLHSInSecond =
6104 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6105 llvm::FoldingSetNodeID X1Id, X2Id;
6106 PossibleXRHSInFirst->Profile(X1Id, Context,
6107 /*Canonical=*/true);
6108 PossibleXLHSInSecond->Profile(X2Id, Context,
6109 /*Canonical=*/true);
6110 IsUpdateExprFound = X1Id == X2Id;
6111 if (IsUpdateExprFound) {
6112 V = FirstBinOp->getLHS();
6113 X = SecondBinOp->getLHS();
6114 E = SecondBinOp->getRHS();
6115 UE = nullptr;
6116 IsXLHSInRHSPart = false;
6117 IsPostfixUpdate = true;
6118 } else {
6119 ErrorFound = NotASpecificExpression;
6120 ErrorLoc = FirstBinOp->getExprLoc();
6121 ErrorRange = FirstBinOp->getSourceRange();
6122 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6123 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6124 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006125 }
6126 }
6127 }
6128 }
6129 } else {
6130 NoteLoc = ErrorLoc = Body->getLocStart();
6131 NoteRange = ErrorRange =
6132 SourceRange(Body->getLocStart(), Body->getLocStart());
6133 ErrorFound = NotTwoSubstatements;
6134 }
6135 } else {
6136 NoteLoc = ErrorLoc = Body->getLocStart();
6137 NoteRange = ErrorRange =
6138 SourceRange(Body->getLocStart(), Body->getLocStart());
6139 ErrorFound = NotACompoundStatement;
6140 }
6141 if (ErrorFound != NoError) {
6142 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6143 << ErrorRange;
6144 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6145 return StmtError();
6146 } else if (CurContext->isDependentContext()) {
6147 UE = V = E = X = nullptr;
6148 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006149 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006150 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006151
6152 getCurFunction()->setHasBranchProtectedScope();
6153
Alexey Bataev62cec442014-11-18 10:14:22 +00006154 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006155 X, V, E, UE, IsXLHSInRHSPart,
6156 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006157}
6158
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006159StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6160 Stmt *AStmt,
6161 SourceLocation StartLoc,
6162 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006163 if (!AStmt)
6164 return StmtError();
6165
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006166 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6167 // 1.2.2 OpenMP Language Terminology
6168 // Structured block - An executable statement with a single entry at the
6169 // top and a single exit at the bottom.
6170 // The point of exit cannot be a branch out of the structured block.
6171 // longjmp() and throw() must not violate the entry/exit criteria.
6172 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006173
Alexey Bataev13314bf2014-10-09 04:18:56 +00006174 // OpenMP [2.16, Nesting of Regions]
6175 // If specified, a teams construct must be contained within a target
6176 // construct. That target construct must contain no statements or directives
6177 // outside of the teams construct.
6178 if (DSAStack->hasInnerTeamsRegion()) {
6179 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6180 bool OMPTeamsFound = true;
6181 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6182 auto I = CS->body_begin();
6183 while (I != CS->body_end()) {
6184 auto OED = dyn_cast<OMPExecutableDirective>(*I);
6185 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6186 OMPTeamsFound = false;
6187 break;
6188 }
6189 ++I;
6190 }
6191 assert(I != CS->body_end() && "Not found statement");
6192 S = *I;
6193 }
6194 if (!OMPTeamsFound) {
6195 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6196 Diag(DSAStack->getInnerTeamsRegionLoc(),
6197 diag::note_omp_nested_teams_construct_here);
6198 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6199 << isa<OMPExecutableDirective>(S);
6200 return StmtError();
6201 }
6202 }
6203
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006204 getCurFunction()->setHasBranchProtectedScope();
6205
6206 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6207}
6208
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006209StmtResult
6210Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6211 Stmt *AStmt, SourceLocation StartLoc,
6212 SourceLocation EndLoc) {
6213 if (!AStmt)
6214 return StmtError();
6215
6216 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6217 // 1.2.2 OpenMP Language Terminology
6218 // Structured block - An executable statement with a single entry at the
6219 // top and a single exit at the bottom.
6220 // The point of exit cannot be a branch out of the structured block.
6221 // longjmp() and throw() must not violate the entry/exit criteria.
6222 CS->getCapturedDecl()->setNothrow();
6223
6224 getCurFunction()->setHasBranchProtectedScope();
6225
6226 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6227 AStmt);
6228}
6229
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006230StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6231 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6232 SourceLocation EndLoc,
6233 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6234 if (!AStmt)
6235 return StmtError();
6236
6237 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6238 // 1.2.2 OpenMP Language Terminology
6239 // Structured block - An executable statement with a single entry at the
6240 // top and a single exit at the bottom.
6241 // The point of exit cannot be a branch out of the structured block.
6242 // longjmp() and throw() must not violate the entry/exit criteria.
6243 CS->getCapturedDecl()->setNothrow();
6244
6245 OMPLoopDirective::HelperExprs B;
6246 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6247 // define the nested loops number.
6248 unsigned NestedLoopCount =
6249 CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6250 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6251 VarsWithImplicitDSA, B);
6252 if (NestedLoopCount == 0)
6253 return StmtError();
6254
6255 assert((CurContext->isDependentContext() || B.builtAll()) &&
6256 "omp target parallel for loop exprs were not built");
6257
6258 if (!CurContext->isDependentContext()) {
6259 // Finalize the clauses that need pre-built expressions for CodeGen.
6260 for (auto C : Clauses) {
6261 if (auto LC = dyn_cast<OMPLinearClause>(C))
6262 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006263 B.NumIterations, *this, CurScope,
6264 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006265 return StmtError();
6266 }
6267 }
6268
6269 getCurFunction()->setHasBranchProtectedScope();
6270 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6271 NestedLoopCount, Clauses, AStmt,
6272 B, DSAStack->isCancelRegion());
6273}
6274
Samuel Antaodf67fc42016-01-19 19:15:56 +00006275/// \brief Check for existence of a map clause in the list of clauses.
6276static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6277 for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6278 I != E; ++I) {
6279 if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6280 return true;
6281 }
6282 }
6283
6284 return false;
6285}
6286
Michael Wong65f367f2015-07-21 13:44:28 +00006287StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6288 Stmt *AStmt,
6289 SourceLocation StartLoc,
6290 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006291 if (!AStmt)
6292 return StmtError();
6293
6294 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6295
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006296 // OpenMP [2.10.1, Restrictions, p. 97]
6297 // At least one map clause must appear on the directive.
6298 if (!HasMapClause(Clauses)) {
6299 Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6300 getOpenMPDirectiveName(OMPD_target_data);
6301 return StmtError();
6302 }
6303
Michael Wong65f367f2015-07-21 13:44:28 +00006304 getCurFunction()->setHasBranchProtectedScope();
6305
6306 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6307 AStmt);
6308}
6309
Samuel Antaodf67fc42016-01-19 19:15:56 +00006310StmtResult
6311Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6312 SourceLocation StartLoc,
6313 SourceLocation EndLoc) {
6314 // OpenMP [2.10.2, Restrictions, p. 99]
6315 // At least one map clause must appear on the directive.
6316 if (!HasMapClause(Clauses)) {
6317 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6318 << getOpenMPDirectiveName(OMPD_target_enter_data);
6319 return StmtError();
6320 }
6321
6322 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6323 Clauses);
6324}
6325
Samuel Antao72590762016-01-19 20:04:50 +00006326StmtResult
6327Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6328 SourceLocation StartLoc,
6329 SourceLocation EndLoc) {
6330 // OpenMP [2.10.3, Restrictions, p. 102]
6331 // At least one map clause must appear on the directive.
6332 if (!HasMapClause(Clauses)) {
6333 Diag(StartLoc, diag::err_omp_no_map_for_directive)
6334 << getOpenMPDirectiveName(OMPD_target_exit_data);
6335 return StmtError();
6336 }
6337
6338 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6339}
6340
Alexey Bataev13314bf2014-10-09 04:18:56 +00006341StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6342 Stmt *AStmt, SourceLocation StartLoc,
6343 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006344 if (!AStmt)
6345 return StmtError();
6346
Alexey Bataev13314bf2014-10-09 04:18:56 +00006347 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6348 // 1.2.2 OpenMP Language Terminology
6349 // Structured block - An executable statement with a single entry at the
6350 // top and a single exit at the bottom.
6351 // The point of exit cannot be a branch out of the structured block.
6352 // longjmp() and throw() must not violate the entry/exit criteria.
6353 CS->getCapturedDecl()->setNothrow();
6354
6355 getCurFunction()->setHasBranchProtectedScope();
6356
6357 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6358}
6359
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006360StmtResult
6361Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6362 SourceLocation EndLoc,
6363 OpenMPDirectiveKind CancelRegion) {
6364 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6365 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6366 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6367 << getOpenMPDirectiveName(CancelRegion);
6368 return StmtError();
6369 }
6370 if (DSAStack->isParentNowaitRegion()) {
6371 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6372 return StmtError();
6373 }
6374 if (DSAStack->isParentOrderedRegion()) {
6375 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6376 return StmtError();
6377 }
6378 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6379 CancelRegion);
6380}
6381
Alexey Bataev87933c72015-09-18 08:07:34 +00006382StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6383 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00006384 SourceLocation EndLoc,
6385 OpenMPDirectiveKind CancelRegion) {
6386 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6387 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6388 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6389 << getOpenMPDirectiveName(CancelRegion);
6390 return StmtError();
6391 }
6392 if (DSAStack->isParentNowaitRegion()) {
6393 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6394 return StmtError();
6395 }
6396 if (DSAStack->isParentOrderedRegion()) {
6397 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6398 return StmtError();
6399 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006400 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00006401 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6402 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00006403}
6404
Alexey Bataev382967a2015-12-08 12:06:20 +00006405static bool checkGrainsizeNumTasksClauses(Sema &S,
6406 ArrayRef<OMPClause *> Clauses) {
6407 OMPClause *PrevClause = nullptr;
6408 bool ErrorFound = false;
6409 for (auto *C : Clauses) {
6410 if (C->getClauseKind() == OMPC_grainsize ||
6411 C->getClauseKind() == OMPC_num_tasks) {
6412 if (!PrevClause)
6413 PrevClause = C;
6414 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6415 S.Diag(C->getLocStart(),
6416 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6417 << getOpenMPClauseName(C->getClauseKind())
6418 << getOpenMPClauseName(PrevClause->getClauseKind());
6419 S.Diag(PrevClause->getLocStart(),
6420 diag::note_omp_previous_grainsize_num_tasks)
6421 << getOpenMPClauseName(PrevClause->getClauseKind());
6422 ErrorFound = true;
6423 }
6424 }
6425 }
6426 return ErrorFound;
6427}
6428
Alexey Bataev49f6e782015-12-01 04:18:41 +00006429StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6430 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6431 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006432 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00006433 if (!AStmt)
6434 return StmtError();
6435
6436 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6437 OMPLoopDirective::HelperExprs B;
6438 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6439 // define the nested loops number.
6440 unsigned NestedLoopCount =
6441 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006442 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00006443 VarsWithImplicitDSA, B);
6444 if (NestedLoopCount == 0)
6445 return StmtError();
6446
6447 assert((CurContext->isDependentContext() || B.builtAll()) &&
6448 "omp for loop exprs were not built");
6449
Alexey Bataev382967a2015-12-08 12:06:20 +00006450 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6451 // The grainsize clause and num_tasks clause are mutually exclusive and may
6452 // not appear on the same taskloop directive.
6453 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6454 return StmtError();
6455
Alexey Bataev49f6e782015-12-01 04:18:41 +00006456 getCurFunction()->setHasBranchProtectedScope();
6457 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6458 NestedLoopCount, Clauses, AStmt, B);
6459}
6460
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006461StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6463 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006464 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006465 if (!AStmt)
6466 return StmtError();
6467
6468 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6469 OMPLoopDirective::HelperExprs B;
6470 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6471 // define the nested loops number.
6472 unsigned NestedLoopCount =
6473 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6474 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6475 VarsWithImplicitDSA, B);
6476 if (NestedLoopCount == 0)
6477 return StmtError();
6478
6479 assert((CurContext->isDependentContext() || B.builtAll()) &&
6480 "omp for loop exprs were not built");
6481
Alexey Bataev5a3af132016-03-29 08:58:54 +00006482 if (!CurContext->isDependentContext()) {
6483 // Finalize the clauses that need pre-built expressions for CodeGen.
6484 for (auto C : Clauses) {
6485 if (auto LC = dyn_cast<OMPLinearClause>(C))
6486 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006487 B.NumIterations, *this, CurScope,
6488 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00006489 return StmtError();
6490 }
6491 }
6492
Alexey Bataev382967a2015-12-08 12:06:20 +00006493 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6494 // The grainsize clause and num_tasks clause are mutually exclusive and may
6495 // not appear on the same taskloop directive.
6496 if (checkGrainsizeNumTasksClauses(*this, Clauses))
6497 return StmtError();
6498
Alexey Bataev0a6ed842015-12-03 09:40:15 +00006499 getCurFunction()->setHasBranchProtectedScope();
6500 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6501 NestedLoopCount, Clauses, AStmt, B);
6502}
6503
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006504StmtResult Sema::ActOnOpenMPDistributeDirective(
6505 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6506 SourceLocation EndLoc,
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00006507 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006508 if (!AStmt)
6509 return StmtError();
6510
6511 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6512 OMPLoopDirective::HelperExprs B;
6513 // In presence of clause 'collapse' with number of loops, it will
6514 // define the nested loops number.
6515 unsigned NestedLoopCount =
6516 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6517 nullptr /*ordered not a clause on distribute*/, AStmt,
6518 *this, *DSAStack, VarsWithImplicitDSA, B);
6519 if (NestedLoopCount == 0)
6520 return StmtError();
6521
6522 assert((CurContext->isDependentContext() || B.builtAll()) &&
6523 "omp for loop exprs were not built");
6524
6525 getCurFunction()->setHasBranchProtectedScope();
6526 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6527 NestedLoopCount, Clauses, AStmt, B);
6528}
6529
Alexey Bataeved09d242014-05-28 05:53:51 +00006530OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006531 SourceLocation StartLoc,
6532 SourceLocation LParenLoc,
6533 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006534 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006535 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00006536 case OMPC_final:
6537 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6538 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00006539 case OMPC_num_threads:
6540 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6541 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006542 case OMPC_safelen:
6543 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6544 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00006545 case OMPC_simdlen:
6546 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6547 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00006548 case OMPC_collapse:
6549 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6550 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006551 case OMPC_ordered:
6552 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6553 break;
Michael Wonge710d542015-08-07 16:16:36 +00006554 case OMPC_device:
6555 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6556 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00006557 case OMPC_num_teams:
6558 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6559 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006560 case OMPC_thread_limit:
6561 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6562 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00006563 case OMPC_priority:
6564 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6565 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006566 case OMPC_grainsize:
6567 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6568 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00006569 case OMPC_num_tasks:
6570 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6571 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00006572 case OMPC_hint:
6573 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6574 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006575 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006576 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006577 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006578 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006579 case OMPC_private:
6580 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006581 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006582 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006583 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006584 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006585 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006586 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006587 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00006588 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006589 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006590 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006591 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006592 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006593 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006594 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006595 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006596 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006597 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006598 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00006599 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006600 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006601 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00006602 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006603 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006604 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006605 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006606 case OMPC_uniform:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006607 llvm_unreachable("Clause is not allowed.");
6608 }
6609 return Res;
6610}
6611
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006612OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6613 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006614 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006615 SourceLocation NameModifierLoc,
6616 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006617 SourceLocation EndLoc) {
6618 Expr *ValExpr = Condition;
6619 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6620 !Condition->isInstantiationDependent() &&
6621 !Condition->containsUnexpandedParameterPack()) {
6622 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00006623 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006624 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006625 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006626
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006627 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006628 }
6629
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006630 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6631 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006632}
6633
Alexey Bataev3778b602014-07-17 07:32:53 +00006634OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6635 SourceLocation StartLoc,
6636 SourceLocation LParenLoc,
6637 SourceLocation EndLoc) {
6638 Expr *ValExpr = Condition;
6639 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6640 !Condition->isInstantiationDependent() &&
6641 !Condition->containsUnexpandedParameterPack()) {
6642 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6643 Condition->getExprLoc(), Condition);
6644 if (Val.isInvalid())
6645 return nullptr;
6646
6647 ValExpr = Val.get();
6648 }
6649
6650 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6651}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006652ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6653 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00006654 if (!Op)
6655 return ExprError();
6656
6657 class IntConvertDiagnoser : public ICEConvertDiagnoser {
6658 public:
6659 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00006660 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00006661 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6662 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006663 return S.Diag(Loc, diag::err_omp_not_integral) << T;
6664 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006665 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6666 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006667 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6668 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006669 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6670 QualType T,
6671 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006672 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6673 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006674 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6675 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006676 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006677 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006678 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006679 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6680 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006681 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6682 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006683 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6684 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006685 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00006686 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00006687 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006688 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6689 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00006690 llvm_unreachable("conversion functions are permitted");
6691 }
6692 } ConvertDiagnoser;
6693 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6694}
6695
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006696static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00006697 OpenMPClauseKind CKind,
6698 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006699 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6700 !ValExpr->isInstantiationDependent()) {
6701 SourceLocation Loc = ValExpr->getExprLoc();
6702 ExprResult Value =
6703 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6704 if (Value.isInvalid())
6705 return false;
6706
6707 ValExpr = Value.get();
6708 // The expression must evaluate to a non-negative integer value.
6709 llvm::APSInt Result;
6710 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00006711 Result.isSigned() &&
6712 !((!StrictlyPositive && Result.isNonNegative()) ||
6713 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006714 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006715 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6716 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006717 return false;
6718 }
6719 }
6720 return true;
6721}
6722
Alexey Bataev568a8332014-03-06 06:15:19 +00006723OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6724 SourceLocation StartLoc,
6725 SourceLocation LParenLoc,
6726 SourceLocation EndLoc) {
6727 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00006728
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006729 // OpenMP [2.5, Restrictions]
6730 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00006731 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6732 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006733 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00006734
Alexey Bataeved09d242014-05-28 05:53:51 +00006735 return new (Context)
6736 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00006737}
6738
Alexey Bataev62c87d22014-03-21 04:51:18 +00006739ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006740 OpenMPClauseKind CKind,
6741 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006742 if (!E)
6743 return ExprError();
6744 if (E->isValueDependent() || E->isTypeDependent() ||
6745 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00006746 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006747 llvm::APSInt Result;
6748 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6749 if (ICE.isInvalid())
6750 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006751 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6752 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00006753 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006754 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6755 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00006756 return ExprError();
6757 }
Alexander Musman09184fe2014-09-30 05:29:28 +00006758 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6759 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6760 << E->getSourceRange();
6761 return ExprError();
6762 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006763 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6764 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00006765 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006766 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00006767 return ICE;
6768}
6769
6770OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6771 SourceLocation LParenLoc,
6772 SourceLocation EndLoc) {
6773 // OpenMP [2.8.1, simd construct, Description]
6774 // The parameter of the safelen clause must be a constant
6775 // positive integer expression.
6776 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6777 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006778 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00006779 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006780 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00006781}
6782
Alexey Bataev66b15b52015-08-21 11:14:16 +00006783OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6784 SourceLocation LParenLoc,
6785 SourceLocation EndLoc) {
6786 // OpenMP [2.8.1, simd construct, Description]
6787 // The parameter of the simdlen clause must be a constant
6788 // positive integer expression.
6789 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6790 if (Simdlen.isInvalid())
6791 return nullptr;
6792 return new (Context)
6793 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6794}
6795
Alexander Musman64d33f12014-06-04 07:53:32 +00006796OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6797 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00006798 SourceLocation LParenLoc,
6799 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00006800 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006801 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00006802 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00006803 // The parameter of the collapse clause must be a constant
6804 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00006805 ExprResult NumForLoopsResult =
6806 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6807 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00006808 return nullptr;
6809 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00006810 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00006811}
6812
Alexey Bataev10e775f2015-07-30 11:36:16 +00006813OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6814 SourceLocation EndLoc,
6815 SourceLocation LParenLoc,
6816 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00006817 // OpenMP [2.7.1, loop construct, Description]
6818 // OpenMP [2.8.1, simd construct, Description]
6819 // OpenMP [2.9.6, distribute construct, Description]
6820 // The parameter of the ordered clause must be a constant
6821 // positive integer expression if any.
6822 if (NumForLoops && LParenLoc.isValid()) {
6823 ExprResult NumForLoopsResult =
6824 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6825 if (NumForLoopsResult.isInvalid())
6826 return nullptr;
6827 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00006828 } else
6829 NumForLoops = nullptr;
6830 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00006831 return new (Context)
6832 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6833}
6834
Alexey Bataeved09d242014-05-28 05:53:51 +00006835OMPClause *Sema::ActOnOpenMPSimpleClause(
6836 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6837 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006838 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006839 switch (Kind) {
6840 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006841 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00006842 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6843 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006844 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006845 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00006846 Res = ActOnOpenMPProcBindClause(
6847 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6848 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006849 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006850 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006851 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006852 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006853 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006854 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006855 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006856 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006857 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006858 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00006859 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00006860 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006861 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00006862 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006863 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006864 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006865 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006866 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006867 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006868 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006869 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006870 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006871 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006872 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006873 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006874 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006875 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006876 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006877 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006878 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006879 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006880 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006881 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006882 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006883 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006884 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006885 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006886 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006887 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006888 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00006889 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006890 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006891 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00006892 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006893 llvm_unreachable("Clause is not allowed.");
6894 }
6895 return Res;
6896}
6897
Alexey Bataev6402bca2015-12-28 07:25:51 +00006898static std::string
6899getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6900 ArrayRef<unsigned> Exclude = llvm::None) {
6901 std::string Values;
6902 unsigned Bound = Last >= 2 ? Last - 2 : 0;
6903 unsigned Skipped = Exclude.size();
6904 auto S = Exclude.begin(), E = Exclude.end();
6905 for (unsigned i = First; i < Last; ++i) {
6906 if (std::find(S, E, i) != E) {
6907 --Skipped;
6908 continue;
6909 }
6910 Values += "'";
6911 Values += getOpenMPSimpleClauseTypeName(K, i);
6912 Values += "'";
6913 if (i == Bound - Skipped)
6914 Values += " or ";
6915 else if (i != Bound + 1 - Skipped)
6916 Values += ", ";
6917 }
6918 return Values;
6919}
6920
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006921OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6922 SourceLocation KindKwLoc,
6923 SourceLocation StartLoc,
6924 SourceLocation LParenLoc,
6925 SourceLocation EndLoc) {
6926 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00006927 static_assert(OMPC_DEFAULT_unknown > 0,
6928 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006929 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006930 << getListOfPossibleValues(OMPC_default, /*First=*/0,
6931 /*Last=*/OMPC_DEFAULT_unknown)
6932 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006933 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006934 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00006935 switch (Kind) {
6936 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006937 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006938 break;
6939 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006940 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006941 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006942 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006943 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00006944 break;
6945 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006946 return new (Context)
6947 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006948}
6949
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006950OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
6951 SourceLocation KindKwLoc,
6952 SourceLocation StartLoc,
6953 SourceLocation LParenLoc,
6954 SourceLocation EndLoc) {
6955 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006956 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00006957 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
6958 /*Last=*/OMPC_PROC_BIND_unknown)
6959 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006960 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006961 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006962 return new (Context)
6963 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006964}
6965
Alexey Bataev56dafe82014-06-20 07:16:17 +00006966OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006967 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006968 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00006969 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00006970 SourceLocation EndLoc) {
6971 OMPClause *Res = nullptr;
6972 switch (Kind) {
6973 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006974 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
6975 assert(Argument.size() == NumberOfElements &&
6976 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006977 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00006978 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
6979 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
6980 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
6981 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
6982 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006983 break;
6984 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00006985 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
6986 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
6987 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
6988 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006989 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00006990 case OMPC_dist_schedule:
6991 Res = ActOnOpenMPDistScheduleClause(
6992 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
6993 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
6994 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00006995 case OMPC_defaultmap:
6996 enum { Modifier, DefaultmapKind };
6997 Res = ActOnOpenMPDefaultmapClause(
6998 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
6999 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7000 StartLoc, LParenLoc, ArgumentLoc[Modifier],
7001 ArgumentLoc[DefaultmapKind], EndLoc);
7002 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00007003 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007004 case OMPC_num_threads:
7005 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007006 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007007 case OMPC_collapse:
7008 case OMPC_default:
7009 case OMPC_proc_bind:
7010 case OMPC_private:
7011 case OMPC_firstprivate:
7012 case OMPC_lastprivate:
7013 case OMPC_shared:
7014 case OMPC_reduction:
7015 case OMPC_linear:
7016 case OMPC_aligned:
7017 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007018 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007019 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007020 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007021 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007022 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007023 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007024 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007025 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007026 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007027 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007028 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007029 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007030 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007031 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007032 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007033 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007034 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007035 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007036 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007037 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007038 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007039 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007040 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007041 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007042 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007043 case OMPC_uniform:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007044 llvm_unreachable("Clause is not allowed.");
7045 }
7046 return Res;
7047}
7048
Alexey Bataev6402bca2015-12-28 07:25:51 +00007049static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7050 OpenMPScheduleClauseModifier M2,
7051 SourceLocation M1Loc, SourceLocation M2Loc) {
7052 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7053 SmallVector<unsigned, 2> Excluded;
7054 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7055 Excluded.push_back(M2);
7056 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7057 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7058 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7059 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7060 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7061 << getListOfPossibleValues(OMPC_schedule,
7062 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7063 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7064 Excluded)
7065 << getOpenMPClauseName(OMPC_schedule);
7066 return true;
7067 }
7068 return false;
7069}
7070
Alexey Bataev56dafe82014-06-20 07:16:17 +00007071OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00007072 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00007073 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00007074 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7075 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7076 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7077 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7078 return nullptr;
7079 // OpenMP, 2.7.1, Loop Construct, Restrictions
7080 // Either the monotonic modifier or the nonmonotonic modifier can be specified
7081 // but not both.
7082 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7083 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7084 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7085 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7086 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7087 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7088 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7089 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7090 return nullptr;
7091 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007092 if (Kind == OMPC_SCHEDULE_unknown) {
7093 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00007094 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7095 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7096 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7097 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7098 Exclude);
7099 } else {
7100 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7101 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007102 }
7103 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7104 << Values << getOpenMPClauseName(OMPC_schedule);
7105 return nullptr;
7106 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00007107 // OpenMP, 2.7.1, Loop Construct, Restrictions
7108 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7109 // schedule(guided).
7110 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7111 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7112 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7113 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7114 diag::err_omp_schedule_nonmonotonic_static);
7115 return nullptr;
7116 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00007117 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00007118 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00007119 if (ChunkSize) {
7120 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7121 !ChunkSize->isInstantiationDependent() &&
7122 !ChunkSize->containsUnexpandedParameterPack()) {
7123 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7124 ExprResult Val =
7125 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7126 if (Val.isInvalid())
7127 return nullptr;
7128
7129 ValExpr = Val.get();
7130
7131 // OpenMP [2.7.1, Restrictions]
7132 // chunk_size must be a loop invariant integer expression with a positive
7133 // value.
7134 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00007135 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7136 if (Result.isSigned() && !Result.isStrictlyPositive()) {
7137 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00007138 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00007139 return nullptr;
7140 }
7141 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00007142 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7143 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7144 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007145 }
7146 }
7147 }
7148
Alexey Bataev6402bca2015-12-28 07:25:51 +00007149 return new (Context)
7150 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00007151 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00007152}
7153
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007154OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7155 SourceLocation StartLoc,
7156 SourceLocation EndLoc) {
7157 OMPClause *Res = nullptr;
7158 switch (Kind) {
7159 case OMPC_ordered:
7160 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7161 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00007162 case OMPC_nowait:
7163 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7164 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007165 case OMPC_untied:
7166 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7167 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007168 case OMPC_mergeable:
7169 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7170 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007171 case OMPC_read:
7172 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7173 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00007174 case OMPC_write:
7175 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7176 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00007177 case OMPC_update:
7178 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7179 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00007180 case OMPC_capture:
7181 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7182 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007183 case OMPC_seq_cst:
7184 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7185 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00007186 case OMPC_threads:
7187 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7188 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007189 case OMPC_simd:
7190 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7191 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00007192 case OMPC_nogroup:
7193 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7194 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007195 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007196 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007197 case OMPC_num_threads:
7198 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007199 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007200 case OMPC_collapse:
7201 case OMPC_schedule:
7202 case OMPC_private:
7203 case OMPC_firstprivate:
7204 case OMPC_lastprivate:
7205 case OMPC_shared:
7206 case OMPC_reduction:
7207 case OMPC_linear:
7208 case OMPC_aligned:
7209 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00007210 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007211 case OMPC_default:
7212 case OMPC_proc_bind:
7213 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00007214 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007215 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00007216 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007217 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007218 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007219 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007220 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007221 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00007222 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007223 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007224 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007225 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007226 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007227 case OMPC_uniform:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007228 llvm_unreachable("Clause is not allowed.");
7229 }
7230 return Res;
7231}
7232
Alexey Bataev236070f2014-06-20 11:19:47 +00007233OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7234 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007235 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00007236 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7237}
7238
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007239OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7240 SourceLocation EndLoc) {
7241 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7242}
7243
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007244OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7245 SourceLocation EndLoc) {
7246 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7247}
7248
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007249OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7250 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007251 return new (Context) OMPReadClause(StartLoc, EndLoc);
7252}
7253
Alexey Bataevdea47612014-07-23 07:46:59 +00007254OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7255 SourceLocation EndLoc) {
7256 return new (Context) OMPWriteClause(StartLoc, EndLoc);
7257}
7258
Alexey Bataev67a4f222014-07-23 10:25:33 +00007259OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7260 SourceLocation EndLoc) {
7261 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7262}
7263
Alexey Bataev459dec02014-07-24 06:46:57 +00007264OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7265 SourceLocation EndLoc) {
7266 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7267}
7268
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007269OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7270 SourceLocation EndLoc) {
7271 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7272}
7273
Alexey Bataev346265e2015-09-25 10:37:12 +00007274OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7275 SourceLocation EndLoc) {
7276 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7277}
7278
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007279OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7280 SourceLocation EndLoc) {
7281 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7282}
7283
Alexey Bataevb825de12015-12-07 10:51:44 +00007284OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7285 SourceLocation EndLoc) {
7286 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7287}
7288
Alexey Bataevc5e02582014-06-16 07:08:35 +00007289OMPClause *Sema::ActOnOpenMPVarListClause(
7290 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7291 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7292 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007293 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00007294 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7295 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7296 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007297 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007298 switch (Kind) {
7299 case OMPC_private:
7300 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7301 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007302 case OMPC_firstprivate:
7303 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7304 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007305 case OMPC_lastprivate:
7306 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7307 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00007308 case OMPC_shared:
7309 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7310 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007311 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00007312 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7313 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007314 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00007315 case OMPC_linear:
7316 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00007317 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00007318 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007319 case OMPC_aligned:
7320 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7321 ColonLoc, EndLoc);
7322 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007323 case OMPC_copyin:
7324 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7325 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007326 case OMPC_copyprivate:
7327 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7328 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00007329 case OMPC_flush:
7330 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7331 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007332 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00007333 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7334 StartLoc, LParenLoc, EndLoc);
7335 break;
7336 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00007337 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7338 DepLinMapLoc, ColonLoc, VarList, StartLoc,
7339 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007340 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007341 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00007342 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00007343 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00007344 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00007345 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00007346 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007347 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00007348 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00007349 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00007350 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00007351 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00007352 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00007353 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007354 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00007355 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00007356 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00007357 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00007358 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00007359 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00007360 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00007361 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00007362 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00007363 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007364 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00007365 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007366 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00007367 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00007368 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00007369 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00007370 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00007371 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007372 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00007373 case OMPC_uniform:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007374 llvm_unreachable("Clause is not allowed.");
7375 }
7376 return Res;
7377}
7378
Alexey Bataev90c228f2016-02-08 09:29:13 +00007379ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00007380 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00007381 ExprResult Res = BuildDeclRefExpr(
7382 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7383 if (!Res.isUsable())
7384 return ExprError();
7385 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7386 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7387 if (!Res.isUsable())
7388 return ExprError();
7389 }
7390 if (VK != VK_LValue && Res.get()->isGLValue()) {
7391 Res = DefaultLvalueConversion(Res.get());
7392 if (!Res.isUsable())
7393 return ExprError();
7394 }
7395 return Res;
7396}
7397
Alexey Bataev60da77e2016-02-29 05:54:20 +00007398static std::pair<ValueDecl *, bool>
7399getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7400 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007401 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7402 RefExpr->containsUnexpandedParameterPack())
7403 return std::make_pair(nullptr, true);
7404
Alexey Bataevd985eda2016-02-10 11:29:16 +00007405 // OpenMP [3.1, C/C++]
7406 // A list item is a variable name.
7407 // OpenMP [2.9.3.3, Restrictions, p.1]
7408 // A variable that is part of another variable (as an array or
7409 // structure element) cannot appear in a private clause.
7410 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007411 enum {
7412 NoArrayExpr = -1,
7413 ArraySubscript = 0,
7414 OMPArraySection = 1
7415 } IsArrayExpr = NoArrayExpr;
7416 if (AllowArraySection) {
7417 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7418 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7419 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7420 Base = TempASE->getBase()->IgnoreParenImpCasts();
7421 RefExpr = Base;
7422 IsArrayExpr = ArraySubscript;
7423 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7424 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7425 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7426 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7427 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7428 Base = TempASE->getBase()->IgnoreParenImpCasts();
7429 RefExpr = Base;
7430 IsArrayExpr = OMPArraySection;
7431 }
7432 }
7433 ELoc = RefExpr->getExprLoc();
7434 ERange = RefExpr->getSourceRange();
7435 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007436 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7437 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7438 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7439 (S.getCurrentThisType().isNull() || !ME ||
7440 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7441 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00007442 if (IsArrayExpr != NoArrayExpr)
7443 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7444 << ERange;
7445 else {
7446 S.Diag(ELoc,
7447 AllowArraySection
7448 ? diag::err_omp_expected_var_name_member_expr_or_array_item
7449 : diag::err_omp_expected_var_name_member_expr)
7450 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7451 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007452 return std::make_pair(nullptr, false);
7453 }
7454 return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7455}
7456
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007457OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7458 SourceLocation StartLoc,
7459 SourceLocation LParenLoc,
7460 SourceLocation EndLoc) {
7461 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00007462 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00007463 for (auto &RefExpr : VarList) {
7464 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007465 SourceLocation ELoc;
7466 SourceRange ERange;
7467 Expr *SimpleRefExpr = RefExpr;
7468 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007469 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007470 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007471 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007472 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007473 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007474 ValueDecl *D = Res.first;
7475 if (!D)
7476 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007477
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007478 QualType Type = D->getType();
7479 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007480
7481 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7482 // A variable that appears in a private clause must not have an incomplete
7483 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007484 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007485 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007486 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007487
Alexey Bataev758e55e2013-09-06 18:03:48 +00007488 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7489 // in a Construct]
7490 // Variables with the predetermined data-sharing attributes may not be
7491 // listed in data-sharing attributes clauses, except for the cases
7492 // listed below. For these exceptions only, listing a predetermined
7493 // variable in a data-sharing attribute clause is allowed and overrides
7494 // the variable's predetermined data-sharing attributes.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007495 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007496 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007497 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7498 << getOpenMPClauseName(OMPC_private);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007499 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00007500 continue;
7501 }
7502
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007503 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007504 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007505 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007506 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7507 << getOpenMPClauseName(OMPC_private) << Type
7508 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7509 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007510 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007511 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007512 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007514 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007515 continue;
7516 }
7517
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007518 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7519 // A list item cannot appear in both a map clause and a data-sharing
7520 // attribute clause on the same construct
7521 if (DSAStack->getCurrentDirective() == OMPD_target) {
7522 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7523 [&](Expr *RE) -> bool {return true;})) {
7524 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7525 << getOpenMPClauseName(OMPC_private)
7526 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7527 ReportOriginalDSA(*this, DSAStack, D, DVar);
7528 continue;
7529 }
7530 }
7531
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007532 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7533 // A variable of class type (or array thereof) that appears in a private
7534 // clause requires an accessible, unambiguous default constructor for the
7535 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00007536 // Generate helper private variable and initialize it with the default
7537 // value. The address of the original variable is replaced by the address of
7538 // the new private variable in CodeGen. This new variable is not added to
7539 // IdResolver, so the code in the OpenMP region uses original variable for
7540 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007541 Type = Type.getUnqualifiedType();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007542 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7543 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007544 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007545 if (VDPrivate->isInvalidDecl())
7546 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007547 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00007548 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007549
Alexey Bataev90c228f2016-02-08 09:29:13 +00007550 DeclRefExpr *Ref = nullptr;
7551 if (!VD)
Alexey Bataev61205072016-03-02 04:57:40 +00007552 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00007553 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7554 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00007555 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007556 }
7557
Alexey Bataeved09d242014-05-28 05:53:51 +00007558 if (Vars.empty())
7559 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007560
Alexey Bataev03b340a2014-10-21 03:16:40 +00007561 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7562 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00007563}
7564
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007565namespace {
7566class DiagsUninitializedSeveretyRAII {
7567private:
7568 DiagnosticsEngine &Diags;
7569 SourceLocation SavedLoc;
7570 bool IsIgnored;
7571
7572public:
7573 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7574 bool IsIgnored)
7575 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7576 if (!IsIgnored) {
7577 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7578 /*Map*/ diag::Severity::Ignored, Loc);
7579 }
7580 }
7581 ~DiagsUninitializedSeveretyRAII() {
7582 if (!IsIgnored)
7583 Diags.popMappings(SavedLoc);
7584 }
7585};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00007586}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007587
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007588OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7589 SourceLocation StartLoc,
7590 SourceLocation LParenLoc,
7591 SourceLocation EndLoc) {
7592 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007593 SmallVector<Expr *, 8> PrivateCopies;
7594 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00007595 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007596 bool IsImplicitClause =
7597 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7598 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7599
Alexey Bataeved09d242014-05-28 05:53:51 +00007600 for (auto &RefExpr : VarList) {
7601 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007602 SourceLocation ELoc;
7603 SourceRange ERange;
7604 Expr *SimpleRefExpr = RefExpr;
7605 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007606 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007607 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007608 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007609 PrivateCopies.push_back(nullptr);
7610 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007611 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007612 ValueDecl *D = Res.first;
7613 if (!D)
7614 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007615
Alexey Bataev60da77e2016-02-29 05:54:20 +00007616 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007617 QualType Type = D->getType();
7618 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007619
7620 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7621 // A variable that appears in a private clause must not have an incomplete
7622 // type or a reference type.
7623 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00007624 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007625 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007626 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007627
7628 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7629 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00007630 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007631 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007632 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007633
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007634 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00007635 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007636 if (!IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007637 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007638 TopDVar = DVar;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007639 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007640 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7641 // A list item that specifies a given variable may not appear in more
7642 // than one clause on the same directive, except that a variable may be
7643 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007644 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00007645 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007646 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007647 << getOpenMPClauseName(DVar.CKind)
7648 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007649 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007650 continue;
7651 }
7652
7653 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7654 // in a Construct]
7655 // Variables with the predetermined data-sharing attributes may not be
7656 // listed in data-sharing attributes clauses, except for the cases
7657 // listed below. For these exceptions only, listing a predetermined
7658 // variable in a data-sharing attribute clause is allowed and overrides
7659 // the variable's predetermined data-sharing attributes.
7660 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7661 // in a Construct, C/C++, p.2]
7662 // Variables with const-qualified type having no mutable member may be
7663 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00007664 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007665 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7666 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00007667 << getOpenMPClauseName(DVar.CKind)
7668 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007669 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007670 continue;
7671 }
7672
Alexey Bataevf29276e2014-06-18 04:14:57 +00007673 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007674 // OpenMP [2.9.3.4, Restrictions, p.2]
7675 // A list item that is private within a parallel region must not appear
7676 // in a firstprivate clause on a worksharing construct if any of the
7677 // worksharing regions arising from the worksharing construct ever bind
7678 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00007679 if (isOpenMPWorksharingDirective(CurrDir) &&
7680 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007681 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007682 if (DVar.CKind != OMPC_shared &&
7683 (isOpenMPParallelDirective(DVar.DKind) ||
7684 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00007685 Diag(ELoc, diag::err_omp_required_access)
7686 << getOpenMPClauseName(OMPC_firstprivate)
7687 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007688 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007689 continue;
7690 }
7691 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007692 // OpenMP [2.9.3.4, Restrictions, p.3]
7693 // A list item that appears in a reduction clause of a parallel construct
7694 // must not appear in a firstprivate clause on a worksharing or task
7695 // construct if any of the worksharing or task regions arising from the
7696 // worksharing or task construct ever bind to any of the parallel regions
7697 // arising from the parallel construct.
7698 // OpenMP [2.9.3.4, Restrictions, p.4]
7699 // A list item that appears in a reduction clause in worksharing
7700 // construct must not appear in a firstprivate clause in a task construct
7701 // encountered during execution of any of the worksharing regions arising
7702 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00007703 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007704 DVar =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007705 DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007706 [](OpenMPDirectiveKind K) -> bool {
7707 return isOpenMPParallelDirective(K) ||
7708 isOpenMPWorksharingDirective(K);
7709 },
7710 false);
7711 if (DVar.CKind == OMPC_reduction &&
7712 (isOpenMPParallelDirective(DVar.DKind) ||
7713 isOpenMPWorksharingDirective(DVar.DKind))) {
7714 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7715 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007716 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007717 continue;
7718 }
7719 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007720
7721 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7722 // A list item that is private within a teams region must not appear in a
7723 // firstprivate clause on a distribute construct if any of the distribute
7724 // regions arising from the distribute construct ever bind to any of the
7725 // teams regions arising from the teams construct.
7726 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7727 // A list item that appears in a reduction clause of a teams construct
7728 // must not appear in a firstprivate clause on a distribute construct if
7729 // any of the distribute regions arising from the distribute construct
7730 // ever bind to any of the teams regions arising from the teams construct.
7731 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7732 // A list item may appear in a firstprivate or lastprivate clause but not
7733 // both.
7734 if (CurrDir == OMPD_distribute) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007735 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007736 [](OpenMPDirectiveKind K) -> bool {
7737 return isOpenMPTeamsDirective(K);
7738 },
7739 false);
7740 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7741 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007742 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007743 continue;
7744 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007745 DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007746 [](OpenMPDirectiveKind K) -> bool {
7747 return isOpenMPTeamsDirective(K);
7748 },
7749 false);
7750 if (DVar.CKind == OMPC_reduction &&
7751 isOpenMPTeamsDirective(DVar.DKind)) {
7752 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007753 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007754 continue;
7755 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007756 DVar = DSAStack->getTopDSA(D, false);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007757 if (DVar.CKind == OMPC_lastprivate) {
7758 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
Alexey Bataevd985eda2016-02-10 11:29:16 +00007759 ReportOriginalDSA(*this, DSAStack, D, DVar);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007760 continue;
7761 }
7762 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +00007763 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7764 // A list item cannot appear in both a map clause and a data-sharing
7765 // attribute clause on the same construct
7766 if (CurrDir == OMPD_target) {
7767 if(DSAStack->checkMapInfoForVar(VD, /* CurrentRegionOnly = */ true,
7768 [&](Expr *RE) -> bool {return true;})) {
7769 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7770 << getOpenMPClauseName(OMPC_firstprivate)
7771 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7772 ReportOriginalDSA(*this, DSAStack, D, DVar);
7773 continue;
7774 }
7775 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007776 }
7777
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007778 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007779 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00007780 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007781 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7782 << getOpenMPClauseName(OMPC_firstprivate) << Type
7783 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7784 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007785 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007786 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00007787 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007788 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00007789 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007790 continue;
7791 }
7792
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007793 Type = Type.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007794 auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7795 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007796 // Generate helper private variable and initialize it with the value of the
7797 // original variable. The address of the original variable is replaced by
7798 // the address of the new private variable in the CodeGen. This new variable
7799 // is not added to IdResolver, so the code in the OpenMP region uses
7800 // original variable for proper diagnostics and variable capturing.
7801 Expr *VDInitRefExpr = nullptr;
7802 // For arrays generate initializer for single element and replace it by the
7803 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007804 if (Type->isArrayType()) {
7805 auto VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00007806 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007807 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007808 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007809 ElemType = ElemType.getUnqualifiedType();
Alexey Bataevd985eda2016-02-10 11:29:16 +00007810 auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007811 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00007812 InitializedEntity Entity =
7813 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007814 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7815
7816 InitializationSequence InitSeq(*this, Entity, Kind, Init);
7817 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7818 if (Result.isInvalid())
7819 VDPrivate->setInvalidDecl();
7820 else
7821 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007822 // Remove temp variable declaration.
7823 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007824 } else {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007825 auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7826 ".firstprivate.temp");
7827 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7828 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00007829 AddInitializerToDecl(VDPrivate,
7830 DefaultLvalueConversion(VDInitRefExpr).get(),
7831 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007832 }
7833 if (VDPrivate->isInvalidDecl()) {
7834 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00007835 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007836 diag::note_omp_task_predetermined_firstprivate_here);
7837 }
7838 continue;
7839 }
7840 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007841 auto VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00007842 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7843 RefExpr->getExprLoc());
7844 DeclRefExpr *Ref = nullptr;
Alexey Bataev417089f2016-02-17 13:19:37 +00007845 if (!VD) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007846 if (TopDVar.CKind == OMPC_lastprivate)
7847 Ref = TopDVar.PrivateCopy;
7848 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007849 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataev005248a2016-02-25 05:25:57 +00007850 if (!IsOpenMPCapturedDecl(D))
7851 ExprCaptures.push_back(Ref->getDecl());
7852 }
Alexey Bataev417089f2016-02-17 13:19:37 +00007853 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00007854 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7855 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00007856 PrivateCopies.push_back(VDPrivateRefExpr);
7857 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007858 }
7859
Alexey Bataeved09d242014-05-28 05:53:51 +00007860 if (Vars.empty())
7861 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007862
7863 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00007864 Vars, PrivateCopies, Inits,
7865 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00007866}
7867
Alexander Musman1bb328c2014-06-04 13:06:39 +00007868OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7869 SourceLocation StartLoc,
7870 SourceLocation LParenLoc,
7871 SourceLocation EndLoc) {
7872 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00007873 SmallVector<Expr *, 8> SrcExprs;
7874 SmallVector<Expr *, 8> DstExprs;
7875 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00007876 SmallVector<Decl *, 4> ExprCaptures;
7877 SmallVector<Expr *, 4> ExprPostUpdates;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007878 for (auto &RefExpr : VarList) {
7879 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00007880 SourceLocation ELoc;
7881 SourceRange ERange;
7882 Expr *SimpleRefExpr = RefExpr;
7883 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007884 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00007885 // It will be analyzed later.
7886 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00007887 SrcExprs.push_back(nullptr);
7888 DstExprs.push_back(nullptr);
7889 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007890 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007891 ValueDecl *D = Res.first;
7892 if (!D)
7893 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007894
Alexey Bataev74caaf22016-02-20 04:09:36 +00007895 QualType Type = D->getType();
7896 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007897
7898 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7899 // A variable that appears in a lastprivate clause must not have an
7900 // incomplete type or a reference type.
7901 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00007902 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00007903 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007904 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00007905
7906 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7907 // in a Construct]
7908 // Variables with the predetermined data-sharing attributes may not be
7909 // listed in data-sharing attributes clauses, except for the cases
7910 // listed below.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007911 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007912 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7913 DVar.CKind != OMPC_firstprivate &&
7914 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7915 Diag(ELoc, diag::err_omp_wrong_dsa)
7916 << getOpenMPClauseName(DVar.CKind)
7917 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007918 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00007919 continue;
7920 }
7921
Alexey Bataevf29276e2014-06-18 04:14:57 +00007922 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7923 // OpenMP [2.14.3.5, Restrictions, p.2]
7924 // A list item that is private within a parallel region, or that appears in
7925 // the reduction clause of a parallel construct, must not appear in a
7926 // lastprivate clause on a worksharing construct if any of the corresponding
7927 // worksharing regions ever binds to any of the corresponding parallel
7928 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00007929 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00007930 if (isOpenMPWorksharingDirective(CurrDir) &&
7931 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +00007932 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007933 if (DVar.CKind != OMPC_shared) {
7934 Diag(ELoc, diag::err_omp_required_access)
7935 << getOpenMPClauseName(OMPC_lastprivate)
7936 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev74caaf22016-02-20 04:09:36 +00007937 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00007938 continue;
7939 }
7940 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00007941
7942 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7943 // A list item may appear in a firstprivate or lastprivate clause but not
7944 // both.
7945 if (CurrDir == OMPD_distribute) {
7946 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7947 if (DVar.CKind == OMPC_firstprivate) {
7948 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7949 ReportOriginalDSA(*this, DSAStack, D, DVar);
7950 continue;
7951 }
7952 }
7953
Alexander Musman1bb328c2014-06-04 13:06:39 +00007954 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00007955 // A variable of class type (or array thereof) that appears in a
7956 // lastprivate clause requires an accessible, unambiguous default
7957 // constructor for the class type, unless the list item is also specified
7958 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00007959 // A variable of class type (or array thereof) that appears in a
7960 // lastprivate clause requires an accessible, unambiguous copy assignment
7961 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00007962 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00007963 auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007964 Type.getUnqualifiedType(), ".lastprivate.src",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007965 D->hasAttrs() ? &D->getAttrs() : nullptr);
7966 auto *PseudoSrcExpr =
7967 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007968 auto *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007969 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +00007970 D->hasAttrs() ? &D->getAttrs() : nullptr);
7971 auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +00007972 // For arrays generate assignment operation for single element and replace
7973 // it by the original array element in CodeGen.
Alexey Bataev74caaf22016-02-20 04:09:36 +00007974 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
Alexey Bataev38e89532015-04-16 04:54:05 +00007975 PseudoDstExpr, PseudoSrcExpr);
7976 if (AssignmentOp.isInvalid())
7977 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +00007978 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00007979 /*DiscardedValue=*/true);
7980 if (AssignmentOp.isInvalid())
7981 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00007982
Alexey Bataev74caaf22016-02-20 04:09:36 +00007983 DeclRefExpr *Ref = nullptr;
Alexey Bataev005248a2016-02-25 05:25:57 +00007984 if (!VD) {
7985 if (TopDVar.CKind == OMPC_firstprivate)
7986 Ref = TopDVar.PrivateCopy;
7987 else {
Alexey Bataev61205072016-03-02 04:57:40 +00007988 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00007989 if (!IsOpenMPCapturedDecl(D))
7990 ExprCaptures.push_back(Ref->getDecl());
7991 }
7992 if (TopDVar.CKind == OMPC_firstprivate ||
7993 (!IsOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +00007994 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +00007995 ExprResult RefRes = DefaultLvalueConversion(Ref);
7996 if (!RefRes.isUsable())
7997 continue;
7998 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +00007999 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8000 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008001 if (!PostUpdateRes.isUsable())
8002 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008003 ExprPostUpdates.push_back(
8004 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +00008005 }
8006 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00008007 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataev74caaf22016-02-20 04:09:36 +00008008 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8009 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +00008010 SrcExprs.push_back(PseudoSrcExpr);
8011 DstExprs.push_back(PseudoDstExpr);
8012 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00008013 }
8014
8015 if (Vars.empty())
8016 return nullptr;
8017
8018 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +00008019 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008020 buildPreInits(Context, ExprCaptures),
8021 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +00008022}
8023
Alexey Bataev758e55e2013-09-06 18:03:48 +00008024OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8025 SourceLocation StartLoc,
8026 SourceLocation LParenLoc,
8027 SourceLocation EndLoc) {
8028 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00008029 for (auto &RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008030 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00008031 SourceLocation ELoc;
8032 SourceRange ERange;
8033 Expr *SimpleRefExpr = RefExpr;
8034 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008035 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00008036 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008037 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008038 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008039 ValueDecl *D = Res.first;
8040 if (!D)
8041 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008042
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008043 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008044 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8045 // in a Construct]
8046 // Variables with the predetermined data-sharing attributes may not be
8047 // listed in data-sharing attributes clauses, except for the cases
8048 // listed below. For these exceptions only, listing a predetermined
8049 // variable in a data-sharing attribute clause is allowed and overrides
8050 // the variable's predetermined data-sharing attributes.
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008051 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00008052 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8053 DVar.RefExpr) {
8054 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8055 << getOpenMPClauseName(OMPC_shared);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008056 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008057 continue;
8058 }
8059
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008060 DeclRefExpr *Ref = nullptr;
Alexey Bataev1efd1662016-03-29 10:59:56 +00008061 if (!VD && IsOpenMPCapturedDecl(D))
Alexey Bataev61205072016-03-02 04:57:40 +00008062 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +00008063 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataev1efd1662016-03-29 10:59:56 +00008064 Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008065 }
8066
Alexey Bataeved09d242014-05-28 05:53:51 +00008067 if (Vars.empty())
8068 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00008069
8070 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8071}
8072
Alexey Bataevc5e02582014-06-16 07:08:35 +00008073namespace {
8074class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8075 DSAStackTy *Stack;
8076
8077public:
8078 bool VisitDeclRefExpr(DeclRefExpr *E) {
8079 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008080 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008081 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8082 return false;
8083 if (DVar.CKind != OMPC_unknown)
8084 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008085 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00008086 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00008087 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008088 return true;
8089 return false;
8090 }
8091 return false;
8092 }
8093 bool VisitStmt(Stmt *S) {
8094 for (auto Child : S->children()) {
8095 if (Child && Visit(Child))
8096 return true;
8097 }
8098 return false;
8099 }
Alexey Bataev23b69422014-06-18 07:08:49 +00008100 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00008101};
Alexey Bataev23b69422014-06-18 07:08:49 +00008102} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00008103
Alexey Bataev60da77e2016-02-29 05:54:20 +00008104namespace {
8105// Transform MemberExpression for specified FieldDecl of current class to
8106// DeclRefExpr to specified OMPCapturedExprDecl.
8107class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8108 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8109 ValueDecl *Field;
8110 DeclRefExpr *CapturedExpr;
8111
8112public:
8113 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8114 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8115
8116 ExprResult TransformMemberExpr(MemberExpr *E) {
8117 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8118 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +00008119 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008120 return CapturedExpr;
8121 }
8122 return BaseTransform::TransformMemberExpr(E);
8123 }
8124 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8125};
8126} // namespace
8127
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008128template <typename T>
8129static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8130 const llvm::function_ref<T(ValueDecl *)> &Gen) {
8131 for (auto &Set : Lookups) {
8132 for (auto *D : Set) {
8133 if (auto Res = Gen(cast<ValueDecl>(D)))
8134 return Res;
8135 }
8136 }
8137 return T();
8138}
8139
8140static ExprResult
8141buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8142 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8143 const DeclarationNameInfo &ReductionId, QualType Ty,
8144 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8145 if (ReductionIdScopeSpec.isInvalid())
8146 return ExprError();
8147 SmallVector<UnresolvedSet<8>, 4> Lookups;
8148 if (S) {
8149 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8150 Lookup.suppressDiagnostics();
8151 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8152 auto *D = Lookup.getRepresentativeDecl();
8153 do {
8154 S = S->getParent();
8155 } while (S && !S->isDeclScope(D));
8156 if (S)
8157 S = S->getParent();
8158 Lookups.push_back(UnresolvedSet<8>());
8159 Lookups.back().append(Lookup.begin(), Lookup.end());
8160 Lookup.clear();
8161 }
8162 } else if (auto *ULE =
8163 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8164 Lookups.push_back(UnresolvedSet<8>());
8165 Decl *PrevD = nullptr;
8166 for(auto *D : ULE->decls()) {
8167 if (D == PrevD)
8168 Lookups.push_back(UnresolvedSet<8>());
8169 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8170 Lookups.back().addDecl(DRD);
8171 PrevD = D;
8172 }
8173 }
8174 if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8175 Ty->containsUnexpandedParameterPack() ||
8176 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8177 return !D->isInvalidDecl() &&
8178 (D->getType()->isDependentType() ||
8179 D->getType()->isInstantiationDependentType() ||
8180 D->getType()->containsUnexpandedParameterPack());
8181 })) {
8182 UnresolvedSet<8> ResSet;
8183 for (auto &Set : Lookups) {
8184 ResSet.append(Set.begin(), Set.end());
8185 // The last item marks the end of all declarations at the specified scope.
8186 ResSet.addDecl(Set[Set.size() - 1]);
8187 }
8188 return UnresolvedLookupExpr::Create(
8189 SemaRef.Context, /*NamingClass=*/nullptr,
8190 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8191 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8192 }
8193 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8194 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8195 if (!D->isInvalidDecl() &&
8196 SemaRef.Context.hasSameType(D->getType(), Ty))
8197 return D;
8198 return nullptr;
8199 }))
8200 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8201 if (auto *VD = filterLookupForUDR<ValueDecl *>(
8202 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8203 if (!D->isInvalidDecl() &&
8204 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8205 !Ty.isMoreQualifiedThan(D->getType()))
8206 return D;
8207 return nullptr;
8208 })) {
8209 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8210 /*DetectVirtual=*/false);
8211 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8212 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8213 VD->getType().getUnqualifiedType()))) {
8214 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8215 /*DiagID=*/0) !=
8216 Sema::AR_inaccessible) {
8217 SemaRef.BuildBasePathArray(Paths, BasePath);
8218 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8219 }
8220 }
8221 }
8222 }
8223 if (ReductionIdScopeSpec.isSet()) {
8224 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8225 return ExprError();
8226 }
8227 return ExprEmpty();
8228}
8229
Alexey Bataevc5e02582014-06-16 07:08:35 +00008230OMPClause *Sema::ActOnOpenMPReductionClause(
8231 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8232 SourceLocation ColonLoc, SourceLocation EndLoc,
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008233 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8234 ArrayRef<Expr *> UnresolvedReductions) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00008235 auto DN = ReductionId.getName();
8236 auto OOK = DN.getCXXOverloadedOperator();
8237 BinaryOperatorKind BOK = BO_Comma;
8238
8239 // OpenMP [2.14.3.6, reduction clause]
8240 // C
8241 // reduction-identifier is either an identifier or one of the following
8242 // operators: +, -, *, &, |, ^, && and ||
8243 // C++
8244 // reduction-identifier is either an id-expression or one of the following
8245 // operators: +, -, *, &, |, ^, && and ||
8246 // FIXME: Only 'min' and 'max' identifiers are supported for now.
8247 switch (OOK) {
8248 case OO_Plus:
8249 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008250 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008251 break;
8252 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008253 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008254 break;
8255 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008256 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008257 break;
8258 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008259 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008260 break;
8261 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008262 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008263 break;
8264 case OO_AmpAmp:
8265 BOK = BO_LAnd;
8266 break;
8267 case OO_PipePipe:
8268 BOK = BO_LOr;
8269 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008270 case OO_New:
8271 case OO_Delete:
8272 case OO_Array_New:
8273 case OO_Array_Delete:
8274 case OO_Slash:
8275 case OO_Percent:
8276 case OO_Tilde:
8277 case OO_Exclaim:
8278 case OO_Equal:
8279 case OO_Less:
8280 case OO_Greater:
8281 case OO_LessEqual:
8282 case OO_GreaterEqual:
8283 case OO_PlusEqual:
8284 case OO_MinusEqual:
8285 case OO_StarEqual:
8286 case OO_SlashEqual:
8287 case OO_PercentEqual:
8288 case OO_CaretEqual:
8289 case OO_AmpEqual:
8290 case OO_PipeEqual:
8291 case OO_LessLess:
8292 case OO_GreaterGreater:
8293 case OO_LessLessEqual:
8294 case OO_GreaterGreaterEqual:
8295 case OO_EqualEqual:
8296 case OO_ExclaimEqual:
8297 case OO_PlusPlus:
8298 case OO_MinusMinus:
8299 case OO_Comma:
8300 case OO_ArrowStar:
8301 case OO_Arrow:
8302 case OO_Call:
8303 case OO_Subscript:
8304 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00008305 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008306 case NUM_OVERLOADED_OPERATORS:
8307 llvm_unreachable("Unexpected reduction identifier");
8308 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008309 if (auto II = DN.getAsIdentifierInfo()) {
8310 if (II->isStr("max"))
8311 BOK = BO_GT;
8312 else if (II->isStr("min"))
8313 BOK = BO_LT;
8314 }
8315 break;
8316 }
8317 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008318 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +00008319 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008320 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008321
8322 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008323 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008324 SmallVector<Expr *, 8> LHSs;
8325 SmallVector<Expr *, 8> RHSs;
8326 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev61205072016-03-02 04:57:40 +00008327 SmallVector<Decl *, 4> ExprCaptures;
8328 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008329 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8330 bool FirstIter = true;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008331 for (auto RefExpr : VarList) {
8332 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +00008333 // OpenMP [2.1, C/C++]
8334 // A list item is a variable or array section, subject to the restrictions
8335 // specified in Section 2.4 on page 42 and in each of the sections
8336 // describing clauses and directives for which a list appears.
8337 // OpenMP [2.14.3.3, Restrictions, p.1]
8338 // A variable that is part of another variable (as an array or
8339 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008340 if (!FirstIter && IR != ER)
8341 ++IR;
8342 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008343 SourceLocation ELoc;
8344 SourceRange ERange;
8345 Expr *SimpleRefExpr = RefExpr;
8346 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8347 /*AllowArraySection=*/true);
8348 if (Res.second) {
8349 // It will be analyzed later.
8350 Vars.push_back(RefExpr);
8351 Privates.push_back(nullptr);
8352 LHSs.push_back(nullptr);
8353 RHSs.push_back(nullptr);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008354 // Try to find 'declare reduction' corresponding construct before using
8355 // builtin/overloaded operators.
8356 QualType Type = Context.DependentTy;
8357 CXXCastPath BasePath;
8358 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8359 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8360 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8361 if (CurContext->isDependentContext() &&
8362 (DeclareReductionRef.isUnset() ||
8363 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8364 ReductionOps.push_back(DeclareReductionRef.get());
8365 else
8366 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00008367 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008368 ValueDecl *D = Res.first;
8369 if (!D)
8370 continue;
8371
Alexey Bataeva1764212015-09-30 09:22:36 +00008372 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008373 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8374 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8375 if (ASE)
Alexey Bataev31300ed2016-02-04 11:27:03 +00008376 Type = ASE->getType().getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008377 else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008378 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8379 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8380 Type = ATy->getElementType();
8381 else
8382 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008383 Type = Type.getNonReferenceType();
Alexey Bataev60da77e2016-02-29 05:54:20 +00008384 } else
8385 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8386 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +00008387
Alexey Bataevc5e02582014-06-16 07:08:35 +00008388 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8389 // A variable that appears in a private clause must not have an incomplete
8390 // type or a reference type.
8391 if (RequireCompleteType(ELoc, Type,
8392 diag::err_omp_reduction_incomplete_type))
8393 continue;
8394 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +00008395 // A list item that appears in a reduction clause must not be
8396 // const-qualified.
8397 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008398 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00008399 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008400 if (!ASE && !OASE) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008401 bool IsDecl = !VD ||
8402 VD->isThisDeclarationADefinition(Context) ==
8403 VarDecl::DeclarationOnly;
8404 Diag(D->getLocation(),
Alexey Bataeva1764212015-09-30 09:22:36 +00008405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +00008406 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +00008407 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008408 continue;
8409 }
8410 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8411 // If a list-item is a reference type then it must bind to the same object
8412 // for all threads of the team.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008413 if (!ASE && !OASE && VD) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008414 VarDecl *VDDef = VD->getDefinition();
Alexey Bataev31300ed2016-02-04 11:27:03 +00008415 if (VD->getType()->isReferenceType() && VDDef) {
Alexey Bataeva1764212015-09-30 09:22:36 +00008416 DSARefChecker Check(DSAStack);
8417 if (Check.Visit(VDDef->getInit())) {
8418 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8419 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8420 continue;
8421 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00008422 }
8423 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008424
Alexey Bataevc5e02582014-06-16 07:08:35 +00008425 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8426 // in a Construct]
8427 // Variables with the predetermined data-sharing attributes may not be
8428 // listed in data-sharing attributes clauses, except for the cases
8429 // listed below. For these exceptions only, listing a predetermined
8430 // variable in a data-sharing attribute clause is allowed and overrides
8431 // the variable's predetermined data-sharing attributes.
8432 // OpenMP [2.14.3.6, Restrictions, p.3]
8433 // Any number of reduction clauses can be specified on the directive,
8434 // but a list item can appear only once in the reduction clauses for that
8435 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00008436 DSAStackTy::DSAVarData DVar;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008437 DVar = DSAStack->getTopDSA(D, false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008438 if (DVar.CKind == OMPC_reduction) {
8439 Diag(ELoc, diag::err_omp_once_referenced)
8440 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008441 if (DVar.RefExpr)
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008442 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008443 } else if (DVar.CKind != OMPC_unknown) {
8444 Diag(ELoc, diag::err_omp_wrong_dsa)
8445 << getOpenMPClauseName(DVar.CKind)
8446 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008447 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008448 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008449 }
8450
8451 // OpenMP [2.14.3.6, Restrictions, p.1]
8452 // A list item that appears in a reduction clause of a worksharing
8453 // construct must be shared in the parallel regions to which any of the
8454 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008455 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8456 if (isOpenMPWorksharingDirective(CurrDir) &&
8457 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00008458 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008459 if (DVar.CKind != OMPC_shared) {
8460 Diag(ELoc, diag::err_omp_required_access)
8461 << getOpenMPClauseName(OMPC_reduction)
8462 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev60da77e2016-02-29 05:54:20 +00008463 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008464 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00008465 }
8466 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008467
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008468 // Try to find 'declare reduction' corresponding construct before using
8469 // builtin/overloaded operators.
8470 CXXCastPath BasePath;
8471 ExprResult DeclareReductionRef = buildDeclareReductionRef(
8472 *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8473 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8474 if (DeclareReductionRef.isInvalid())
8475 continue;
8476 if (CurContext->isDependentContext() &&
8477 (DeclareReductionRef.isUnset() ||
8478 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8479 Vars.push_back(RefExpr);
8480 Privates.push_back(nullptr);
8481 LHSs.push_back(nullptr);
8482 RHSs.push_back(nullptr);
8483 ReductionOps.push_back(DeclareReductionRef.get());
8484 continue;
8485 }
8486 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8487 // Not allowed reduction identifier is found.
8488 Diag(ReductionId.getLocStart(),
8489 diag::err_omp_unknown_reduction_identifier)
8490 << Type << ReductionIdRange;
8491 continue;
8492 }
8493
8494 // OpenMP [2.14.3.6, reduction clause, Restrictions]
8495 // The type of a list item that appears in a reduction clause must be valid
8496 // for the reduction-identifier. For a max or min reduction in C, the type
8497 // of the list item must be an allowed arithmetic data type: char, int,
8498 // float, double, or _Bool, possibly modified with long, short, signed, or
8499 // unsigned. For a max or min reduction in C++, the type of the list item
8500 // must be an allowed arithmetic data type: char, wchar_t, int, float,
8501 // double, or bool, possibly modified with long, short, signed, or unsigned.
8502 if (DeclareReductionRef.isUnset()) {
8503 if ((BOK == BO_GT || BOK == BO_LT) &&
8504 !(Type->isScalarType() ||
8505 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8506 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8507 << getLangOpts().CPlusPlus;
8508 if (!ASE && !OASE) {
8509 bool IsDecl = !VD ||
8510 VD->isThisDeclarationADefinition(Context) ==
8511 VarDecl::DeclarationOnly;
8512 Diag(D->getLocation(),
8513 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8514 << D;
8515 }
8516 continue;
8517 }
8518 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8519 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8520 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8521 if (!ASE && !OASE) {
8522 bool IsDecl = !VD ||
8523 VD->isThisDeclarationADefinition(Context) ==
8524 VarDecl::DeclarationOnly;
8525 Diag(D->getLocation(),
8526 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8527 << D;
8528 }
8529 continue;
8530 }
8531 }
8532
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008533 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008534 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
Alexey Bataev60da77e2016-02-29 05:54:20 +00008535 D->hasAttrs() ? &D->getAttrs() : nullptr);
8536 auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8537 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008538 auto PrivateTy = Type;
Alexey Bataev1189bd02016-01-26 12:20:39 +00008539 if (OASE ||
Alexey Bataev60da77e2016-02-29 05:54:20 +00008540 (!ASE &&
8541 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Alexey Bataev1189bd02016-01-26 12:20:39 +00008542 // For arays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008543 // Create pseudo array type for private copy. The size for this array will
8544 // be generated during codegen.
8545 // For array subscripts or single variables Private Ty is the same as Type
8546 // (type of the variable or single array element).
8547 PrivateTy = Context.getVariableArrayType(
8548 Type, new (Context) OpaqueValueExpr(SourceLocation(),
8549 Context.getSizeType(), VK_RValue),
8550 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +00008551 } else if (!ASE && !OASE &&
8552 Context.getAsArrayType(D->getType().getNonReferenceType()))
8553 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008554 // Private copy.
Alexey Bataev60da77e2016-02-29 05:54:20 +00008555 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8556 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008557 // Add initializer for private variable.
8558 Expr *Init = nullptr;
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008559 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8560 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8561 if (DeclareReductionRef.isUsable()) {
8562 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8563 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8564 if (DRD->getInitializer()) {
8565 Init = DRDRef;
8566 RHSVD->setInit(DRDRef);
8567 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008568 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008569 } else {
8570 switch (BOK) {
8571 case BO_Add:
8572 case BO_Xor:
8573 case BO_Or:
8574 case BO_LOr:
8575 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8576 if (Type->isScalarType() || Type->isAnyComplexType())
8577 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8578 break;
8579 case BO_Mul:
8580 case BO_LAnd:
8581 if (Type->isScalarType() || Type->isAnyComplexType()) {
8582 // '*' and '&&' reduction ops - initializer is '1'.
8583 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00008584 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008585 break;
8586 case BO_And: {
8587 // '&' reduction op - initializer is '~0'.
8588 QualType OrigType = Type;
8589 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8590 Type = ComplexTy->getElementType();
8591 if (Type->isRealFloatingType()) {
8592 llvm::APFloat InitValue =
8593 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8594 /*isIEEE=*/true);
8595 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8596 Type, ELoc);
8597 } else if (Type->isScalarType()) {
8598 auto Size = Context.getTypeSize(Type);
8599 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8600 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8601 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8602 }
8603 if (Init && OrigType->isAnyComplexType()) {
8604 // Init = 0xFFFF + 0xFFFFi;
8605 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8606 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8607 }
8608 Type = OrigType;
8609 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008610 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008611 case BO_LT:
8612 case BO_GT: {
8613 // 'min' reduction op - initializer is 'Largest representable number in
8614 // the reduction list item type'.
8615 // 'max' reduction op - initializer is 'Least representable number in
8616 // the reduction list item type'.
8617 if (Type->isIntegerType() || Type->isPointerType()) {
8618 bool IsSigned = Type->hasSignedIntegerRepresentation();
8619 auto Size = Context.getTypeSize(Type);
8620 QualType IntTy =
8621 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8622 llvm::APInt InitValue =
8623 (BOK != BO_LT)
8624 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8625 : llvm::APInt::getMinValue(Size)
8626 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8627 : llvm::APInt::getMaxValue(Size);
8628 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8629 if (Type->isPointerType()) {
8630 // Cast to pointer type.
8631 auto CastExpr = BuildCStyleCastExpr(
8632 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8633 SourceLocation(), Init);
8634 if (CastExpr.isInvalid())
8635 continue;
8636 Init = CastExpr.get();
8637 }
8638 } else if (Type->isRealFloatingType()) {
8639 llvm::APFloat InitValue = llvm::APFloat::getLargest(
8640 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8641 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8642 Type, ELoc);
8643 }
8644 break;
8645 }
8646 case BO_PtrMemD:
8647 case BO_PtrMemI:
8648 case BO_MulAssign:
8649 case BO_Div:
8650 case BO_Rem:
8651 case BO_Sub:
8652 case BO_Shl:
8653 case BO_Shr:
8654 case BO_LE:
8655 case BO_GE:
8656 case BO_EQ:
8657 case BO_NE:
8658 case BO_AndAssign:
8659 case BO_XorAssign:
8660 case BO_OrAssign:
8661 case BO_Assign:
8662 case BO_AddAssign:
8663 case BO_SubAssign:
8664 case BO_DivAssign:
8665 case BO_RemAssign:
8666 case BO_ShlAssign:
8667 case BO_ShrAssign:
8668 case BO_Comma:
8669 llvm_unreachable("Unexpected reduction operation");
8670 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008671 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008672 if (Init && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008673 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8674 /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008675 } else if (!Init)
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008676 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008677 if (RHSVD->isInvalidDecl())
8678 continue;
8679 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008680 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8681 << ReductionIdRange;
Alexey Bataev60da77e2016-02-29 05:54:20 +00008682 bool IsDecl =
8683 !VD ||
8684 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8685 Diag(D->getLocation(),
8686 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8687 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008688 continue;
8689 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008690 // Store initializer for single element in private copy. Will be used during
8691 // codegen.
8692 PrivateVD->setInit(RHSVD->getInit());
8693 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008694 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008695 ExprResult ReductionOp;
8696 if (DeclareReductionRef.isUsable()) {
8697 QualType RedTy = DeclareReductionRef.get()->getType();
8698 QualType PtrRedTy = Context.getPointerType(RedTy);
8699 ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8700 ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8701 if (!BasePath.empty()) {
8702 LHS = DefaultLvalueConversion(LHS.get());
8703 RHS = DefaultLvalueConversion(RHS.get());
8704 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8705 CK_UncheckedDerivedToBase, LHS.get(),
8706 &BasePath, LHS.get()->getValueKind());
8707 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8708 CK_UncheckedDerivedToBase, RHS.get(),
8709 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008710 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +00008711 FunctionProtoType::ExtProtoInfo EPI;
8712 QualType Params[] = {PtrRedTy, PtrRedTy};
8713 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8714 auto *OVE = new (Context) OpaqueValueExpr(
8715 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8716 DefaultLvalueConversion(DeclareReductionRef.get()).get());
8717 Expr *Args[] = {LHS.get(), RHS.get()};
8718 ReductionOp = new (Context)
8719 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8720 } else {
8721 ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8722 ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8723 if (ReductionOp.isUsable()) {
8724 if (BOK != BO_LT && BOK != BO_GT) {
8725 ReductionOp =
8726 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8727 BO_Assign, LHSDRE, ReductionOp.get());
8728 } else {
8729 auto *ConditionalOp = new (Context) ConditionalOperator(
8730 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8731 RHSDRE, Type, VK_LValue, OK_Ordinary);
8732 ReductionOp =
8733 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8734 BO_Assign, LHSDRE, ConditionalOp);
8735 }
8736 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8737 }
8738 if (ReductionOp.isInvalid())
8739 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00008740 }
8741
Alexey Bataev60da77e2016-02-29 05:54:20 +00008742 DeclRefExpr *Ref = nullptr;
8743 Expr *VarsExpr = RefExpr->IgnoreParens();
8744 if (!VD) {
8745 if (ASE || OASE) {
8746 TransformExprToCaptures RebuildToCapture(*this, D);
8747 VarsExpr =
8748 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8749 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +00008750 } else {
8751 VarsExpr = Ref =
8752 buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +00008753 }
8754 if (!IsOpenMPCapturedDecl(D)) {
8755 ExprCaptures.push_back(Ref->getDecl());
8756 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8757 ExprResult RefRes = DefaultLvalueConversion(Ref);
8758 if (!RefRes.isUsable())
8759 continue;
8760 ExprResult PostUpdateRes =
8761 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8762 SimpleRefExpr, RefRes.get());
8763 if (!PostUpdateRes.isUsable())
8764 continue;
8765 ExprPostUpdates.push_back(
8766 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +00008767 }
8768 }
Alexey Bataev60da77e2016-02-29 05:54:20 +00008769 }
8770 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8771 Vars.push_back(VarsExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008772 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00008773 LHSs.push_back(LHSDRE);
8774 RHSs.push_back(RHSDRE);
8775 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00008776 }
8777
8778 if (Vars.empty())
8779 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +00008780
Alexey Bataevc5e02582014-06-16 07:08:35 +00008781 return OMPReductionClause::Create(
8782 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00008783 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008784 LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8785 buildPostUpdate(*this, ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +00008786}
8787
Alexey Bataevecba70f2016-04-12 11:02:11 +00008788bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8789 SourceLocation LinLoc) {
8790 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8791 LinKind == OMPC_LINEAR_unknown) {
8792 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8793 return true;
8794 }
8795 return false;
8796}
8797
8798bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8799 OpenMPLinearClauseKind LinKind,
8800 QualType Type) {
8801 auto *VD = dyn_cast_or_null<VarDecl>(D);
8802 // A variable must not have an incomplete type or a reference type.
8803 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8804 return true;
8805 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8806 !Type->isReferenceType()) {
8807 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8808 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8809 return true;
8810 }
8811 Type = Type.getNonReferenceType();
8812
8813 // A list item must not be const-qualified.
8814 if (Type.isConstant(Context)) {
8815 Diag(ELoc, diag::err_omp_const_variable)
8816 << getOpenMPClauseName(OMPC_linear);
8817 if (D) {
8818 bool IsDecl =
8819 !VD ||
8820 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8821 Diag(D->getLocation(),
8822 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8823 << D;
8824 }
8825 return true;
8826 }
8827
8828 // A list item must be of integral or pointer type.
8829 Type = Type.getUnqualifiedType().getCanonicalType();
8830 const auto *Ty = Type.getTypePtrOrNull();
8831 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8832 !Ty->isPointerType())) {
8833 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8834 if (D) {
8835 bool IsDecl =
8836 !VD ||
8837 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8838 Diag(D->getLocation(),
8839 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8840 << D;
8841 }
8842 return true;
8843 }
8844 return false;
8845}
8846
Alexey Bataev182227b2015-08-20 10:54:39 +00008847OMPClause *Sema::ActOnOpenMPLinearClause(
8848 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8849 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8850 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008851 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008852 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00008853 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008854 SmallVector<Decl *, 4> ExprCaptures;
8855 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008856 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +00008857 LinKind = OMPC_LINEAR_val;
Alexey Bataeved09d242014-05-28 05:53:51 +00008858 for (auto &RefExpr : VarList) {
8859 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008860 SourceLocation ELoc;
8861 SourceRange ERange;
8862 Expr *SimpleRefExpr = RefExpr;
8863 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8864 /*AllowArraySection=*/false);
8865 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +00008866 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00008867 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008868 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00008869 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00008870 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008871 ValueDecl *D = Res.first;
8872 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +00008873 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +00008874
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008875 QualType Type = D->getType();
8876 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +00008877
8878 // OpenMP [2.14.3.7, linear clause]
8879 // A list-item cannot appear in more than one linear clause.
8880 // A list-item that appears in a linear clause cannot appear in any
8881 // other data-sharing attribute clause.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008882 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00008883 if (DVar.RefExpr) {
8884 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8885 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008886 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00008887 continue;
8888 }
8889
Alexey Bataevecba70f2016-04-12 11:02:11 +00008890 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +00008891 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00008892 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +00008893
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008894 // Build private copy of original var.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008895 auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8896 D->hasAttrs() ? &D->getAttrs() : nullptr);
8897 auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008898 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008899 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008900 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008901 DeclRefExpr *Ref = nullptr;
Alexey Bataev78849fb2016-03-09 09:49:00 +00008902 if (!VD) {
8903 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8904 if (!IsOpenMPCapturedDecl(D)) {
8905 ExprCaptures.push_back(Ref->getDecl());
8906 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8907 ExprResult RefRes = DefaultLvalueConversion(Ref);
8908 if (!RefRes.isUsable())
8909 continue;
8910 ExprResult PostUpdateRes =
8911 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8912 SimpleRefExpr, RefRes.get());
8913 if (!PostUpdateRes.isUsable())
8914 continue;
8915 ExprPostUpdates.push_back(
8916 IgnoredValueConversions(PostUpdateRes.get()).get());
8917 }
8918 }
8919 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008920 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008921 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008922 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008923 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008924 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexey Bataev2bbf7212016-03-03 03:52:24 +00008925 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8926 auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
8927
8928 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
8929 Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008930 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00008931 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00008932 }
8933
8934 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008935 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008936
8937 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00008938 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00008939 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
8940 !Step->isInstantiationDependent() &&
8941 !Step->containsUnexpandedParameterPack()) {
8942 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008943 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00008944 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008945 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008946 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00008947
Alexander Musman3276a272015-03-21 10:12:56 +00008948 // Build var to save the step value.
8949 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008950 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00008951 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00008952 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00008953 ExprResult CalcStep =
8954 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008955 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00008956
Alexander Musman8dba6642014-04-22 13:09:42 +00008957 // Warn about zero linear step (it would be probably better specified as
8958 // making corresponding variables 'const').
8959 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00008960 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
8961 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00008962 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
8963 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00008964 if (!IsConstant && CalcStep.isUsable()) {
8965 // Calculate the step beforehand instead of doing this on each iteration.
8966 // (This is not used if the number of iterations may be kfold-ed).
8967 CalcStepExpr = CalcStep.get();
8968 }
Alexander Musman8dba6642014-04-22 13:09:42 +00008969 }
8970
Alexey Bataev182227b2015-08-20 10:54:39 +00008971 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
8972 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +00008973 StepExpr, CalcStepExpr,
8974 buildPreInits(Context, ExprCaptures),
8975 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +00008976}
8977
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008978static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
8979 Expr *NumIterations, Sema &SemaRef,
8980 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +00008981 // Walk the vars and build update/final expressions for the CodeGen.
8982 SmallVector<Expr *, 8> Updates;
8983 SmallVector<Expr *, 8> Finals;
8984 Expr *Step = Clause.getStep();
8985 Expr *CalcStep = Clause.getCalcStep();
8986 // OpenMP [2.14.3.7, linear clause]
8987 // If linear-step is not specified it is assumed to be 1.
8988 if (Step == nullptr)
8989 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008990 else if (CalcStep) {
Alexander Musman3276a272015-03-21 10:12:56 +00008991 Step = cast<BinaryOperator>(CalcStep)->getLHS();
Alexey Bataev5a3af132016-03-29 08:58:54 +00008992 }
Alexander Musman3276a272015-03-21 10:12:56 +00008993 bool HasErrors = false;
8994 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00008995 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00008996 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00008997 for (auto &RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +00008998 SourceLocation ELoc;
8999 SourceRange ERange;
9000 Expr *SimpleRefExpr = RefExpr;
9001 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9002 /*AllowArraySection=*/false);
9003 ValueDecl *D = Res.first;
9004 if (Res.second || !D) {
9005 Updates.push_back(nullptr);
9006 Finals.push_back(nullptr);
9007 HasErrors = true;
9008 continue;
9009 }
9010 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9011 D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9012 ->getMemberDecl();
9013 }
9014 auto &&Info = Stack->isLoopControlVariable(D);
Alexander Musman3276a272015-03-21 10:12:56 +00009015 Expr *InitExpr = *CurInit;
9016
9017 // Build privatized reference to the current linear var.
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009018 auto DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00009019 Expr *CapturedRef;
9020 if (LinKind == OMPC_LINEAR_uval)
9021 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9022 else
9023 CapturedRef =
9024 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9025 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9026 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009027
9028 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009029 ExprResult Update;
9030 if (!Info.first) {
9031 Update =
9032 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9033 InitExpr, IV, Step, /* Subtract */ false);
9034 } else
9035 Update = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009036 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9037 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00009038
9039 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009040 ExprResult Final;
9041 if (!Info.first) {
9042 Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9043 InitExpr, NumIterations, Step,
9044 /* Subtract */ false);
9045 } else
9046 Final = *CurPrivate;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009047 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9048 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00009049
Alexander Musman3276a272015-03-21 10:12:56 +00009050 if (!Update.isUsable() || !Final.isUsable()) {
9051 Updates.push_back(nullptr);
9052 Finals.push_back(nullptr);
9053 HasErrors = true;
9054 } else {
9055 Updates.push_back(Update.get());
9056 Finals.push_back(Final.get());
9057 }
Richard Trieucc3949d2016-02-18 22:34:54 +00009058 ++CurInit;
9059 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00009060 }
9061 Clause.setUpdates(Updates);
9062 Clause.setFinals(Finals);
9063 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00009064}
9065
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009066OMPClause *Sema::ActOnOpenMPAlignedClause(
9067 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9068 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9069
9070 SmallVector<Expr *, 8> Vars;
9071 for (auto &RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +00009072 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9073 SourceLocation ELoc;
9074 SourceRange ERange;
9075 Expr *SimpleRefExpr = RefExpr;
9076 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9077 /*AllowArraySection=*/false);
9078 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009079 // It will be analyzed later.
9080 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009081 }
Alexey Bataev1efd1662016-03-29 10:59:56 +00009082 ValueDecl *D = Res.first;
9083 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009084 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009085
Alexey Bataev1efd1662016-03-29 10:59:56 +00009086 QualType QType = D->getType();
9087 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009088
9089 // OpenMP [2.8.1, simd construct, Restrictions]
9090 // The type of list items appearing in the aligned clause must be
9091 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009092 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009093 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +00009094 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009095 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009096 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009097 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +00009098 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009099 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +00009100 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009101 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +00009102 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009103 continue;
9104 }
9105
9106 // OpenMP [2.8.1, simd construct, Restrictions]
9107 // A list-item cannot appear in more than one aligned clause.
Alexey Bataev1efd1662016-03-29 10:59:56 +00009108 if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00009109 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009110 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9111 << getOpenMPClauseName(OMPC_aligned);
9112 continue;
9113 }
9114
Alexey Bataev1efd1662016-03-29 10:59:56 +00009115 DeclRefExpr *Ref = nullptr;
9116 if (!VD && IsOpenMPCapturedDecl(D))
9117 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9118 Vars.push_back(DefaultFunctionArrayConversion(
9119 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9120 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009121 }
9122
9123 // OpenMP [2.8.1, simd construct, Description]
9124 // The parameter of the aligned clause, alignment, must be a constant
9125 // positive integer expression.
9126 // If no optional parameter is specified, implementation-defined default
9127 // alignments for SIMD instructions on the target platforms are assumed.
9128 if (Alignment != nullptr) {
9129 ExprResult AlignResult =
9130 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9131 if (AlignResult.isInvalid())
9132 return nullptr;
9133 Alignment = AlignResult.get();
9134 }
9135 if (Vars.empty())
9136 return nullptr;
9137
9138 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9139 EndLoc, Vars, Alignment);
9140}
9141
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009142OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9143 SourceLocation StartLoc,
9144 SourceLocation LParenLoc,
9145 SourceLocation EndLoc) {
9146 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009147 SmallVector<Expr *, 8> SrcExprs;
9148 SmallVector<Expr *, 8> DstExprs;
9149 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00009150 for (auto &RefExpr : VarList) {
9151 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9152 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009153 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009154 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009155 SrcExprs.push_back(nullptr);
9156 DstExprs.push_back(nullptr);
9157 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009158 continue;
9159 }
9160
Alexey Bataeved09d242014-05-28 05:53:51 +00009161 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009162 // OpenMP [2.1, C/C++]
9163 // A list item is a variable name.
9164 // OpenMP [2.14.4.1, Restrictions, p.1]
9165 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00009166 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009167 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009168 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9169 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009170 continue;
9171 }
9172
9173 Decl *D = DE->getDecl();
9174 VarDecl *VD = cast<VarDecl>(D);
9175
9176 QualType Type = VD->getType();
9177 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9178 // It will be analyzed later.
9179 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009180 SrcExprs.push_back(nullptr);
9181 DstExprs.push_back(nullptr);
9182 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009183 continue;
9184 }
9185
9186 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9187 // A list item that appears in a copyin clause must be threadprivate.
9188 if (!DSAStack->isThreadPrivate(VD)) {
9189 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00009190 << getOpenMPClauseName(OMPC_copyin)
9191 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009192 continue;
9193 }
9194
9195 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9196 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00009197 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009198 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009199 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009200 auto *SrcVD =
9201 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9202 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00009203 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009204 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9205 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00009206 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9207 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009208 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009209 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009210 // For arrays generate assignment operation for single element and replace
9211 // it by the original array element in CodeGen.
9212 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9213 PseudoDstExpr, PseudoSrcExpr);
9214 if (AssignmentOp.isInvalid())
9215 continue;
9216 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9217 /*DiscardedValue=*/true);
9218 if (AssignmentOp.isInvalid())
9219 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009220
9221 DSAStack->addDSA(VD, DE, OMPC_copyin);
9222 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009223 SrcExprs.push_back(PseudoSrcExpr);
9224 DstExprs.push_back(PseudoDstExpr);
9225 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009226 }
9227
Alexey Bataeved09d242014-05-28 05:53:51 +00009228 if (Vars.empty())
9229 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009230
Alexey Bataevf56f98c2015-04-16 05:39:01 +00009231 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9232 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009233}
9234
Alexey Bataevbae9a792014-06-27 10:37:06 +00009235OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9236 SourceLocation StartLoc,
9237 SourceLocation LParenLoc,
9238 SourceLocation EndLoc) {
9239 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00009240 SmallVector<Expr *, 8> SrcExprs;
9241 SmallVector<Expr *, 8> DstExprs;
9242 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009243 for (auto &RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009244 assert(RefExpr && "NULL expr in OpenMP linear clause.");
9245 SourceLocation ELoc;
9246 SourceRange ERange;
9247 Expr *SimpleRefExpr = RefExpr;
9248 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9249 /*AllowArraySection=*/false);
9250 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009251 // It will be analyzed later.
9252 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009253 SrcExprs.push_back(nullptr);
9254 DstExprs.push_back(nullptr);
9255 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009256 }
Alexey Bataeve122da12016-03-17 10:50:17 +00009257 ValueDecl *D = Res.first;
9258 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +00009259 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009260
Alexey Bataeve122da12016-03-17 10:50:17 +00009261 QualType Type = D->getType();
9262 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009263
9264 // OpenMP [2.14.4.2, Restrictions, p.2]
9265 // A list item that appears in a copyprivate clause may not appear in a
9266 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +00009267 if (!VD || !DSAStack->isThreadPrivate(VD)) {
9268 auto DVar = DSAStack->getTopDSA(D, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00009269 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9270 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00009271 Diag(ELoc, diag::err_omp_wrong_dsa)
9272 << getOpenMPClauseName(DVar.CKind)
9273 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve122da12016-03-17 10:50:17 +00009274 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009275 continue;
9276 }
9277
9278 // OpenMP [2.11.4.2, Restrictions, p.1]
9279 // All list items that appear in a copyprivate clause must be either
9280 // threadprivate or private in the enclosing context.
9281 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +00009282 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009283 if (DVar.CKind == OMPC_shared) {
9284 Diag(ELoc, diag::err_omp_required_access)
9285 << getOpenMPClauseName(OMPC_copyprivate)
9286 << "threadprivate or private in the enclosing context";
Alexey Bataeve122da12016-03-17 10:50:17 +00009287 ReportOriginalDSA(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009288 continue;
9289 }
9290 }
9291 }
9292
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009293 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009294 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009295 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009296 << getOpenMPClauseName(OMPC_copyprivate) << Type
9297 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009298 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +00009299 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009300 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +00009301 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009302 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +00009303 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +00009304 continue;
9305 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009306
Alexey Bataevbae9a792014-06-27 10:37:06 +00009307 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9308 // A variable of class type (or array thereof) that appears in a
9309 // copyin clause requires an accessible, unambiguous copy assignment
9310 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009311 Type = Context.getBaseElementType(Type.getNonReferenceType())
9312 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00009313 auto *SrcVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009314 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9315 D->hasAttrs() ? &D->getAttrs() : nullptr);
9316 auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009317 auto *DstVD =
Alexey Bataeve122da12016-03-17 10:50:17 +00009318 buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9319 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00009320 auto *PseudoDstExpr =
Alexey Bataeve122da12016-03-17 10:50:17 +00009321 buildDeclRefExpr(*this, DstVD, Type, ELoc);
9322 auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009323 PseudoDstExpr, PseudoSrcExpr);
9324 if (AssignmentOp.isInvalid())
9325 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +00009326 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +00009327 /*DiscardedValue=*/true);
9328 if (AssignmentOp.isInvalid())
9329 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009330
9331 // No need to mark vars as copyprivate, they are already threadprivate or
9332 // implicitly private.
Alexey Bataeve122da12016-03-17 10:50:17 +00009333 assert(VD || IsOpenMPCapturedDecl(D));
9334 Vars.push_back(
9335 VD ? RefExpr->IgnoreParens()
9336 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +00009337 SrcExprs.push_back(PseudoSrcExpr);
9338 DstExprs.push_back(PseudoDstExpr);
9339 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00009340 }
9341
9342 if (Vars.empty())
9343 return nullptr;
9344
Alexey Bataeva63048e2015-03-23 06:18:07 +00009345 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9346 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00009347}
9348
Alexey Bataev6125da92014-07-21 11:26:11 +00009349OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9350 SourceLocation StartLoc,
9351 SourceLocation LParenLoc,
9352 SourceLocation EndLoc) {
9353 if (VarList.empty())
9354 return nullptr;
9355
9356 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9357}
Alexey Bataevdea47612014-07-23 07:46:59 +00009358
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009359OMPClause *
9360Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9361 SourceLocation DepLoc, SourceLocation ColonLoc,
9362 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9363 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009364 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009365 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +00009366 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009367 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +00009368 return nullptr;
9369 }
9370 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009371 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9372 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009373 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009374 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009375 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9376 /*Last=*/OMPC_DEPEND_unknown, Except)
9377 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009378 return nullptr;
9379 }
9380 SmallVector<Expr *, 8> Vars;
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009381 llvm::APSInt DepCounter(/*BitWidth=*/32);
9382 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9383 if (DepKind == OMPC_DEPEND_sink) {
9384 if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9385 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9386 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009387 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009388 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009389 if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9390 DSAStack->getParentOrderedRegionParam()) {
9391 for (auto &RefExpr : VarList) {
9392 assert(RefExpr && "NULL expr in OpenMP shared clause.");
9393 if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9394 (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9395 // It will be analyzed later.
9396 Vars.push_back(RefExpr);
9397 continue;
9398 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009399
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009400 SourceLocation ELoc = RefExpr->getExprLoc();
9401 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9402 if (DepKind == OMPC_DEPEND_sink) {
9403 if (DepCounter >= TotalDepCount) {
9404 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9405 continue;
9406 }
9407 ++DepCounter;
9408 // OpenMP [2.13.9, Summary]
9409 // depend(dependence-type : vec), where dependence-type is:
9410 // 'sink' and where vec is the iteration vector, which has the form:
9411 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9412 // where n is the value specified by the ordered clause in the loop
9413 // directive, xi denotes the loop iteration variable of the i-th nested
9414 // loop associated with the loop directive, and di is a constant
9415 // non-negative integer.
9416 SimpleExpr = SimpleExpr->IgnoreImplicit();
9417 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9418 if (!DE) {
9419 OverloadedOperatorKind OOK = OO_None;
9420 SourceLocation OOLoc;
9421 Expr *LHS, *RHS;
9422 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9423 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9424 OOLoc = BO->getOperatorLoc();
9425 LHS = BO->getLHS()->IgnoreParenImpCasts();
9426 RHS = BO->getRHS()->IgnoreParenImpCasts();
9427 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9428 OOK = OCE->getOperator();
9429 OOLoc = OCE->getOperatorLoc();
9430 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9431 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9432 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9433 OOK = MCE->getMethodDecl()
9434 ->getNameInfo()
9435 .getName()
9436 .getCXXOverloadedOperator();
9437 OOLoc = MCE->getCallee()->getExprLoc();
9438 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9439 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9440 } else {
9441 Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9442 continue;
9443 }
9444 DE = dyn_cast<DeclRefExpr>(LHS);
9445 if (!DE) {
9446 Diag(LHS->getExprLoc(),
9447 diag::err_omp_depend_sink_expected_loop_iteration)
9448 << DSAStack->getParentLoopControlVariable(
9449 DepCounter.getZExtValue());
9450 continue;
9451 }
9452 if (OOK != OO_Plus && OOK != OO_Minus) {
9453 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9454 continue;
9455 }
9456 ExprResult Res = VerifyPositiveIntegerConstantInClause(
9457 RHS, OMPC_depend, /*StrictlyPositive=*/false);
9458 if (Res.isInvalid())
9459 continue;
9460 }
9461 auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9462 if (!CurContext->isDependentContext() &&
9463 DSAStack->getParentOrderedRegionParam() &&
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00009464 (!VD ||
9465 DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009466 Diag(DE->getExprLoc(),
9467 diag::err_omp_depend_sink_expected_loop_iteration)
9468 << DSAStack->getParentLoopControlVariable(
9469 DepCounter.getZExtValue());
9470 continue;
9471 }
9472 } else {
9473 // OpenMP [2.11.1.1, Restrictions, p.3]
9474 // A variable that is part of another variable (such as a field of a
9475 // structure) but is not an array element or an array section cannot
9476 // appear in a depend clause.
9477 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9478 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9479 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9480 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9481 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev31300ed2016-02-04 11:27:03 +00009482 (ASE &&
9483 !ASE->getBase()
9484 ->getType()
9485 .getNonReferenceType()
9486 ->isPointerType() &&
9487 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009488 Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9489 << 0 << RefExpr->getSourceRange();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009490 continue;
9491 }
9492 }
9493
9494 Vars.push_back(RefExpr->IgnoreParenImpCasts());
9495 }
9496
9497 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9498 TotalDepCount > VarList.size() &&
9499 DSAStack->getParentOrderedRegionParam()) {
9500 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9501 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9502 }
9503 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9504 Vars.empty())
9505 return nullptr;
9506 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009507
9508 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9509 DepLoc, ColonLoc, Vars);
9510}
Michael Wonge710d542015-08-07 16:16:36 +00009511
9512OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9513 SourceLocation LParenLoc,
9514 SourceLocation EndLoc) {
9515 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00009516
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009517 // OpenMP [2.9.1, Restrictions]
9518 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00009519 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9520 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009521 return nullptr;
9522
Michael Wonge710d542015-08-07 16:16:36 +00009523 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9524}
Kelvin Li0bff7af2015-11-23 05:32:03 +00009525
9526static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9527 DSAStackTy *Stack, CXXRecordDecl *RD) {
9528 if (!RD || RD->isInvalidDecl())
9529 return true;
9530
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00009531 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9532 if (auto *CTD = CTSD->getSpecializedTemplate())
9533 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00009534 auto QTy = SemaRef.Context.getRecordType(RD);
9535 if (RD->isDynamicClass()) {
9536 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9537 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9538 return false;
9539 }
9540 auto *DC = RD;
9541 bool IsCorrect = true;
9542 for (auto *I : DC->decls()) {
9543 if (I) {
9544 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9545 if (MD->isStatic()) {
9546 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9547 SemaRef.Diag(MD->getLocation(),
9548 diag::note_omp_static_member_in_target);
9549 IsCorrect = false;
9550 }
9551 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9552 if (VD->isStaticDataMember()) {
9553 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9554 SemaRef.Diag(VD->getLocation(),
9555 diag::note_omp_static_member_in_target);
9556 IsCorrect = false;
9557 }
9558 }
9559 }
9560 }
9561
9562 for (auto &I : RD->bases()) {
9563 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9564 I.getType()->getAsCXXRecordDecl()))
9565 IsCorrect = false;
9566 }
9567 return IsCorrect;
9568}
9569
9570static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9571 DSAStackTy *Stack, QualType QTy) {
9572 NamedDecl *ND;
9573 if (QTy->isIncompleteType(&ND)) {
9574 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9575 return false;
9576 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9577 if (!RD->isInvalidDecl() &&
9578 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9579 return false;
9580 }
9581 return true;
9582}
9583
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009584/// \brief Return true if it can be proven that the provided array expression
9585/// (array section or array subscript) does NOT specify the whole size of the
9586/// array whose base type is \a BaseQTy.
9587static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9588 const Expr *E,
9589 QualType BaseQTy) {
9590 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9591
9592 // If this is an array subscript, it refers to the whole size if the size of
9593 // the dimension is constant and equals 1. Also, an array section assumes the
9594 // format of an array subscript if no colon is used.
9595 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9596 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9597 return ATy->getSize().getSExtValue() != 1;
9598 // Size can't be evaluated statically.
9599 return false;
9600 }
9601
9602 assert(OASE && "Expecting array section if not an array subscript.");
9603 auto *LowerBound = OASE->getLowerBound();
9604 auto *Length = OASE->getLength();
9605
9606 // If there is a lower bound that does not evaluates to zero, we are not
9607 // convering the whole dimension.
9608 if (LowerBound) {
9609 llvm::APSInt ConstLowerBound;
9610 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9611 return false; // Can't get the integer value as a constant.
9612 if (ConstLowerBound.getSExtValue())
9613 return true;
9614 }
9615
9616 // If we don't have a length we covering the whole dimension.
9617 if (!Length)
9618 return false;
9619
9620 // If the base is a pointer, we don't have a way to get the size of the
9621 // pointee.
9622 if (BaseQTy->isPointerType())
9623 return false;
9624
9625 // We can only check if the length is the same as the size of the dimension
9626 // if we have a constant array.
9627 auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9628 if (!CATy)
9629 return false;
9630
9631 llvm::APSInt ConstLength;
9632 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9633 return false; // Can't get the integer value as a constant.
9634
9635 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9636}
9637
9638// Return true if it can be proven that the provided array expression (array
9639// section or array subscript) does NOT specify a single element of the array
9640// whose base type is \a BaseQTy.
9641static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9642 const Expr *E,
9643 QualType BaseQTy) {
9644 auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9645
9646 // An array subscript always refer to a single element. Also, an array section
9647 // assumes the format of an array subscript if no colon is used.
9648 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9649 return false;
9650
9651 assert(OASE && "Expecting array section if not an array subscript.");
9652 auto *Length = OASE->getLength();
9653
9654 // If we don't have a length we have to check if the array has unitary size
9655 // for this dimension. Also, we should always expect a length if the base type
9656 // is pointer.
9657 if (!Length) {
9658 if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9659 return ATy->getSize().getSExtValue() != 1;
9660 // We cannot assume anything.
9661 return false;
9662 }
9663
9664 // Check if the length evaluates to 1.
9665 llvm::APSInt ConstLength;
9666 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9667 return false; // Can't get the integer value as a constant.
9668
9669 return ConstLength.getSExtValue() != 1;
9670}
9671
Samuel Antao5de996e2016-01-22 20:21:36 +00009672// Return the expression of the base of the map clause or null if it cannot
9673// be determined and do all the necessary checks to see if the expression is
9674// valid as a standalone map clause expression.
9675static Expr *CheckMapClauseExpressionBase(Sema &SemaRef, Expr *E) {
9676 SourceLocation ELoc = E->getExprLoc();
9677 SourceRange ERange = E->getSourceRange();
9678
9679 // The base of elements of list in a map clause have to be either:
9680 // - a reference to variable or field.
9681 // - a member expression.
9682 // - an array expression.
9683 //
9684 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9685 // reference to 'r'.
9686 //
9687 // If we have:
9688 //
9689 // struct SS {
9690 // Bla S;
9691 // foo() {
9692 // #pragma omp target map (S.Arr[:12]);
9693 // }
9694 // }
9695 //
9696 // We want to retrieve the member expression 'this->S';
9697
9698 Expr *RelevantExpr = nullptr;
9699
Samuel Antao5de996e2016-01-22 20:21:36 +00009700 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9701 // If a list item is an array section, it must specify contiguous storage.
9702 //
9703 // For this restriction it is sufficient that we make sure only references
9704 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009705 // exist except in the rightmost expression (unless they cover the whole
9706 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +00009707 //
9708 // r.ArrS[3:5].Arr[6:7]
9709 //
9710 // r.ArrS[3:5].x
9711 //
9712 // but these would be valid:
9713 // r.ArrS[3].Arr[6:7]
9714 //
9715 // r.ArrS[3].x
9716
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009717 bool AllowUnitySizeArraySection = true;
9718 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +00009719
Dmitry Polukhin644a9252016-03-11 07:58:34 +00009720 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009721 E = E->IgnoreParenImpCasts();
9722
9723 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9724 if (!isa<VarDecl>(CurE->getDecl()))
9725 break;
9726
9727 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009728
9729 // If we got a reference to a declaration, we should not expect any array
9730 // section before that.
9731 AllowUnitySizeArraySection = false;
9732 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009733 continue;
9734 }
9735
9736 if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9737 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9738
9739 if (isa<CXXThisExpr>(BaseE))
9740 // We found a base expression: this->Val.
9741 RelevantExpr = CurE;
9742 else
9743 E = BaseE;
9744
9745 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9746 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9747 << CurE->getSourceRange();
9748 break;
9749 }
9750
9751 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9752
9753 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9754 // A bit-field cannot appear in a map clause.
9755 //
9756 if (FD->isBitField()) {
9757 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9758 << CurE->getSourceRange();
9759 break;
9760 }
9761
9762 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9763 // If the type of a list item is a reference to a type T then the type
9764 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009765 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +00009766
9767 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9768 // A list item cannot be a variable that is a member of a structure with
9769 // a union type.
9770 //
9771 if (auto *RT = CurType->getAs<RecordType>())
9772 if (RT->isUnionType()) {
9773 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9774 << CurE->getSourceRange();
9775 break;
9776 }
9777
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009778 // If we got a member expression, we should not expect any array section
9779 // before that:
9780 //
9781 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9782 // If a list item is an element of a structure, only the rightmost symbol
9783 // of the variable reference can be an array section.
9784 //
9785 AllowUnitySizeArraySection = false;
9786 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009787 continue;
9788 }
9789
9790 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9791 E = CurE->getBase()->IgnoreParenImpCasts();
9792
9793 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9794 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9795 << 0 << CurE->getSourceRange();
9796 break;
9797 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009798
9799 // If we got an array subscript that express the whole dimension we
9800 // can have any array expressions before. If it only expressing part of
9801 // the dimension, we can only have unitary-size array expressions.
9802 if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9803 E->getType()))
9804 AllowWholeSizeArraySection = false;
Samuel Antao5de996e2016-01-22 20:21:36 +00009805 continue;
9806 }
9807
9808 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009809 E = CurE->getBase()->IgnoreParenImpCasts();
9810
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009811 auto CurType =
9812 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9813
Samuel Antao5de996e2016-01-22 20:21:36 +00009814 // 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 Antao5de996e2016-01-22 20:21:36 +00009817 if (CurType->isReferenceType())
9818 CurType = CurType->getPointeeType();
9819
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009820 bool IsPointer = CurType->isAnyPointerType();
9821
9822 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +00009823 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9824 << 0 << CurE->getSourceRange();
9825 break;
9826 }
9827
Samuel Antaoa9f35cb2016-03-09 15:46:05 +00009828 bool NotWhole =
9829 CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9830 bool NotUnity =
9831 CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9832
9833 if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9834 // Any array section is currently allowed.
9835 //
9836 // If this array section refers to the whole dimension we can still
9837 // accept other array sections before this one, except if the base is a
9838 // pointer. Otherwise, only unitary sections are accepted.
9839 if (NotWhole || IsPointer)
9840 AllowWholeSizeArraySection = false;
9841 } else if ((AllowUnitySizeArraySection && NotUnity) ||
9842 (AllowWholeSizeArraySection && NotWhole)) {
9843 // A unity or whole array section is not allowed and that is not
9844 // compatible with the properties of the current array section.
9845 SemaRef.Diag(
9846 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9847 << CurE->getSourceRange();
9848 break;
9849 }
Samuel Antao5de996e2016-01-22 20:21:36 +00009850 continue;
9851 }
9852
9853 // If nothing else worked, this is not a valid map clause expression.
9854 SemaRef.Diag(ELoc,
9855 diag::err_omp_expected_named_var_member_or_array_expression)
9856 << ERange;
9857 break;
9858 }
9859
9860 return RelevantExpr;
9861}
9862
9863// Return true if expression E associated with value VD has conflicts with other
9864// map information.
9865static bool CheckMapConflicts(Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD,
9866 Expr *E, bool CurrentRegionOnly) {
9867 assert(VD && E);
9868
9869 // Types used to organize the components of a valid map clause.
9870 typedef std::pair<Expr *, ValueDecl *> MapExpressionComponent;
9871 typedef SmallVector<MapExpressionComponent, 4> MapExpressionComponents;
9872
9873 // Helper to extract the components in the map clause expression E and store
9874 // them into MEC. This assumes that E is a valid map clause expression, i.e.
9875 // it has already passed the single clause checks.
9876 auto ExtractMapExpressionComponents = [](Expr *TE,
9877 MapExpressionComponents &MEC) {
9878 while (true) {
9879 TE = TE->IgnoreParenImpCasts();
9880
9881 if (auto *CurE = dyn_cast<DeclRefExpr>(TE)) {
9882 MEC.push_back(
9883 MapExpressionComponent(CurE, cast<VarDecl>(CurE->getDecl())));
9884 break;
9885 }
9886
9887 if (auto *CurE = dyn_cast<MemberExpr>(TE)) {
9888 auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9889
9890 MEC.push_back(MapExpressionComponent(
9891 CurE, cast<FieldDecl>(CurE->getMemberDecl())));
9892 if (isa<CXXThisExpr>(BaseE))
9893 break;
9894
9895 TE = BaseE;
9896 continue;
9897 }
9898
9899 if (auto *CurE = dyn_cast<ArraySubscriptExpr>(TE)) {
9900 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9901 TE = CurE->getBase()->IgnoreParenImpCasts();
9902 continue;
9903 }
9904
9905 if (auto *CurE = dyn_cast<OMPArraySectionExpr>(TE)) {
9906 MEC.push_back(MapExpressionComponent(CurE, nullptr));
9907 TE = CurE->getBase()->IgnoreParenImpCasts();
9908 continue;
9909 }
9910
9911 llvm_unreachable(
9912 "Expecting only valid map clause expressions at this point!");
9913 }
9914 };
9915
9916 SourceLocation ELoc = E->getExprLoc();
9917 SourceRange ERange = E->getSourceRange();
9918
9919 // In order to easily check the conflicts we need to match each component of
9920 // the expression under test with the components of the expressions that are
9921 // already in the stack.
9922
9923 MapExpressionComponents CurComponents;
9924 ExtractMapExpressionComponents(E, CurComponents);
9925
9926 assert(!CurComponents.empty() && "Map clause expression with no components!");
9927 assert(CurComponents.back().second == VD &&
9928 "Map clause expression with unexpected base!");
9929
9930 // Variables to help detecting enclosing problems in data environment nests.
9931 bool IsEnclosedByDataEnvironmentExpr = false;
9932 Expr *EnclosingExpr = nullptr;
9933
9934 bool FoundError =
9935 DSAS->checkMapInfoForVar(VD, CurrentRegionOnly, [&](Expr *RE) -> bool {
9936 MapExpressionComponents StackComponents;
9937 ExtractMapExpressionComponents(RE, StackComponents);
9938 assert(!StackComponents.empty() &&
9939 "Map clause expression with no components!");
9940 assert(StackComponents.back().second == VD &&
9941 "Map clause expression with unexpected base!");
9942
9943 // Expressions must start from the same base. Here we detect at which
9944 // point both expressions diverge from each other and see if we can
9945 // detect if the memory referred to both expressions is contiguous and
9946 // do not overlap.
9947 auto CI = CurComponents.rbegin();
9948 auto CE = CurComponents.rend();
9949 auto SI = StackComponents.rbegin();
9950 auto SE = StackComponents.rend();
9951 for (; CI != CE && SI != SE; ++CI, ++SI) {
9952
9953 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
9954 // At most one list item can be an array item derived from a given
9955 // variable in map clauses of the same construct.
9956 if (CurrentRegionOnly && (isa<ArraySubscriptExpr>(CI->first) ||
9957 isa<OMPArraySectionExpr>(CI->first)) &&
9958 (isa<ArraySubscriptExpr>(SI->first) ||
9959 isa<OMPArraySectionExpr>(SI->first))) {
9960 SemaRef.Diag(CI->first->getExprLoc(),
9961 diag::err_omp_multiple_array_items_in_map_clause)
9962 << CI->first->getSourceRange();
9963 ;
9964 SemaRef.Diag(SI->first->getExprLoc(), diag::note_used_here)
9965 << SI->first->getSourceRange();
9966 return true;
9967 }
9968
9969 // Do both expressions have the same kind?
9970 if (CI->first->getStmtClass() != SI->first->getStmtClass())
9971 break;
9972
9973 // Are we dealing with different variables/fields?
9974 if (CI->second != SI->second)
9975 break;
9976 }
9977
9978 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
9979 // List items of map clauses in the same construct must not share
9980 // original storage.
9981 //
9982 // If the expressions are exactly the same or one is a subset of the
9983 // other, it means they are sharing storage.
9984 if (CI == CE && SI == SE) {
9985 if (CurrentRegionOnly) {
9986 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
9987 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
9988 << RE->getSourceRange();
9989 return true;
9990 } else {
9991 // If we find the same expression in the enclosing data environment,
9992 // that is legal.
9993 IsEnclosedByDataEnvironmentExpr = true;
9994 return false;
9995 }
9996 }
9997
9998 QualType DerivedType = std::prev(CI)->first->getType();
9999 SourceLocation DerivedLoc = std::prev(CI)->first->getExprLoc();
10000
10001 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10002 // If the type of a list item is a reference to a type T then the type
10003 // will be considered to be T for all purposes of this clause.
10004 if (DerivedType->isReferenceType())
10005 DerivedType = DerivedType->getPointeeType();
10006
10007 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10008 // A variable for which the type is pointer and an array section
10009 // derived from that variable must not appear as list items of map
10010 // clauses of the same construct.
10011 //
10012 // Also, cover one of the cases in:
10013 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10014 // If any part of the original storage of a list item has corresponding
10015 // storage in the device data environment, all of the original storage
10016 // must have corresponding storage in the device data environment.
10017 //
10018 if (DerivedType->isAnyPointerType()) {
10019 if (CI == CE || SI == SE) {
10020 SemaRef.Diag(
10021 DerivedLoc,
10022 diag::err_omp_pointer_mapped_along_with_derived_section)
10023 << DerivedLoc;
10024 } else {
10025 assert(CI != CE && SI != SE);
10026 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10027 << DerivedLoc;
10028 }
10029 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10030 << RE->getSourceRange();
10031 return true;
10032 }
10033
10034 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10035 // List items of map clauses in the same construct must not share
10036 // original storage.
10037 //
10038 // An expression is a subset of the other.
10039 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10040 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10041 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10042 << RE->getSourceRange();
10043 return true;
10044 }
10045
10046 // The current expression uses the same base as other expression in the
10047 // data environment but does not contain it completelly.
10048 if (!CurrentRegionOnly && SI != SE)
10049 EnclosingExpr = RE;
10050
10051 // The current expression is a subset of the expression in the data
10052 // environment.
10053 IsEnclosedByDataEnvironmentExpr |=
10054 (!CurrentRegionOnly && CI != CE && SI == SE);
10055
10056 return false;
10057 });
10058
10059 if (CurrentRegionOnly)
10060 return FoundError;
10061
10062 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10063 // If any part of the original storage of a list item has corresponding
10064 // storage in the device data environment, all of the original storage must
10065 // have corresponding storage in the device data environment.
10066 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10067 // If a list item is an element of a structure, and a different element of
10068 // the structure has a corresponding list item in the device data environment
10069 // prior to a task encountering the construct associated with the map clause,
10070 // then the list item must also have a correspnding list item in the device
10071 // data environment prior to the task encountering the construct.
10072 //
10073 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10074 SemaRef.Diag(ELoc,
10075 diag::err_omp_original_storage_is_shared_and_does_not_contain)
10076 << ERange;
10077 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10078 << EnclosingExpr->getSourceRange();
10079 return true;
10080 }
10081
10082 return FoundError;
10083}
10084
Samuel Antao23abd722016-01-19 20:40:49 +000010085OMPClause *
10086Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10087 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10088 SourceLocation MapLoc, SourceLocation ColonLoc,
10089 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10090 SourceLocation LParenLoc, SourceLocation EndLoc) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010091 SmallVector<Expr *, 4> Vars;
10092
10093 for (auto &RE : VarList) {
10094 assert(RE && "Null expr in omp map");
10095 if (isa<DependentScopeDeclRefExpr>(RE)) {
10096 // It will be analyzed later.
10097 Vars.push_back(RE);
10098 continue;
10099 }
10100 SourceLocation ELoc = RE->getExprLoc();
10101
Kelvin Li0bff7af2015-11-23 05:32:03 +000010102 auto *VE = RE->IgnoreParenLValueCasts();
10103
10104 if (VE->isValueDependent() || VE->isTypeDependent() ||
10105 VE->isInstantiationDependent() ||
10106 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000010107 // We can only analyze this information once the missing information is
10108 // resolved.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010109 Vars.push_back(RE);
10110 continue;
10111 }
10112
10113 auto *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010114
Samuel Antao5de996e2016-01-22 20:21:36 +000010115 if (!RE->IgnoreParenImpCasts()->isLValue()) {
10116 Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10117 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010118 continue;
10119 }
10120
Samuel Antao5de996e2016-01-22 20:21:36 +000010121 // Obtain the array or member expression bases if required.
10122 auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr);
10123 if (!BE)
10124 continue;
10125
10126 // If the base is a reference to a variable, we rely on that variable for
10127 // the following checks. If it is a 'this' expression we rely on the field.
10128 ValueDecl *D = nullptr;
10129 if (auto *DRE = dyn_cast<DeclRefExpr>(BE)) {
10130 D = DRE->getDecl();
10131 } else {
10132 auto *ME = cast<MemberExpr>(BE);
10133 assert(isa<CXXThisExpr>(ME->getBase()) && "Unexpected expression!");
10134 D = ME->getMemberDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +000010135 }
10136 assert(D && "Null decl on map clause.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000010137
Samuel Antao5de996e2016-01-22 20:21:36 +000010138 auto *VD = dyn_cast<VarDecl>(D);
10139 auto *FD = dyn_cast<FieldDecl>(D);
10140
10141 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000010142 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000010143
10144 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10145 // threadprivate variables cannot appear in a map clause.
10146 if (VD && DSAStack->isThreadPrivate(VD)) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000010147 auto DVar = DSAStack->getTopDSA(VD, false);
10148 Diag(ELoc, diag::err_omp_threadprivate_in_map);
10149 ReportOriginalDSA(*this, DSAStack, VD, DVar);
10150 continue;
10151 }
10152
Samuel Antao5de996e2016-01-22 20:21:36 +000010153 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10154 // A list item cannot appear in both a map clause and a data-sharing
10155 // attribute clause on the same construct.
10156 //
10157 // TODO: Implement this check - it cannot currently be tested because of
10158 // missing implementation of the other data sharing clauses in target
10159 // directives.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010160
Samuel Antao5de996e2016-01-22 20:21:36 +000010161 // Check conflicts with other map clause expressions. We check the conflicts
10162 // with the current construct separately from the enclosing data
10163 // environment, because the restrictions are different.
10164 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
10165 /*CurrentRegionOnly=*/true))
10166 break;
10167 if (CheckMapConflicts(*this, DSAStack, D, SimpleExpr,
10168 /*CurrentRegionOnly=*/false))
10169 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000010170
Samuel Antao5de996e2016-01-22 20:21:36 +000010171 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10172 // If the type of a list item is a reference to a type T then the type will
10173 // be considered to be T for all purposes of this clause.
10174 QualType Type = D->getType();
10175 if (Type->isReferenceType())
10176 Type = Type->getPointeeType();
10177
10178 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000010179 // A list item must have a mappable type.
10180 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10181 DSAStack, Type))
10182 continue;
10183
Samuel Antaodf67fc42016-01-19 19:15:56 +000010184 // target enter data
10185 // OpenMP [2.10.2, Restrictions, p. 99]
10186 // A map-type must be specified in all map clauses and must be either
10187 // to or alloc.
10188 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10189 if (DKind == OMPD_target_enter_data &&
10190 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10191 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010192 << (IsMapTypeImplicit ? 1 : 0)
10193 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antaodf67fc42016-01-19 19:15:56 +000010194 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010195 continue;
Samuel Antaodf67fc42016-01-19 19:15:56 +000010196 }
10197
Samuel Antao72590762016-01-19 20:04:50 +000010198 // target exit_data
10199 // OpenMP [2.10.3, Restrictions, p. 102]
10200 // A map-type must be specified in all map clauses and must be either
10201 // from, release, or delete.
10202 DKind = DSAStack->getCurrentDirective();
10203 if (DKind == OMPD_target_exit_data &&
10204 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10205 MapType == OMPC_MAP_delete)) {
10206 Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
Samuel Antao23abd722016-01-19 20:40:49 +000010207 << (IsMapTypeImplicit ? 1 : 0)
10208 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
Samuel Antao72590762016-01-19 20:04:50 +000010209 << getOpenMPDirectiveName(DKind);
Samuel Antao5de996e2016-01-22 20:21:36 +000010210 continue;
Samuel Antao72590762016-01-19 20:04:50 +000010211 }
10212
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010213 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10214 // A list item cannot appear in both a map clause and a data-sharing
10215 // attribute clause on the same construct
10216 if (DKind == OMPD_target && VD) {
10217 auto DVar = DSAStack->getTopDSA(VD, false);
10218 if (isOpenMPPrivate(DVar.CKind)) {
10219 Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10220 << getOpenMPClauseName(DVar.CKind)
10221 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10222 ReportOriginalDSA(*this, DSAStack, D, DVar);
10223 continue;
10224 }
10225 }
10226
Kelvin Li0bff7af2015-11-23 05:32:03 +000010227 Vars.push_back(RE);
Samuel Antao5de996e2016-01-22 20:21:36 +000010228 DSAStack->addExprToVarMapInfo(D, RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010229 }
Kelvin Li0bff7af2015-11-23 05:32:03 +000010230
Samuel Antao5de996e2016-01-22 20:21:36 +000010231 // We need to produce a map clause even if we don't have variables so that
10232 // other diagnostics related with non-existing map clauses are accurate.
Kelvin Li0bff7af2015-11-23 05:32:03 +000010233 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
Samuel Antao23abd722016-01-19 20:40:49 +000010234 MapTypeModifier, MapType, IsMapTypeImplicit,
10235 MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000010236}
Kelvin Li099bb8c2015-11-24 20:50:12 +000010237
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010238QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10239 TypeResult ParsedType) {
10240 assert(ParsedType.isUsable());
10241
10242 QualType ReductionType = GetTypeFromParser(ParsedType.get());
10243 if (ReductionType.isNull())
10244 return QualType();
10245
10246 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10247 // A type name in a declare reduction directive cannot be a function type, an
10248 // array type, a reference type, or a type qualified with const, volatile or
10249 // restrict.
10250 if (ReductionType.hasQualifiers()) {
10251 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10252 return QualType();
10253 }
10254
10255 if (ReductionType->isFunctionType()) {
10256 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10257 return QualType();
10258 }
10259 if (ReductionType->isReferenceType()) {
10260 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10261 return QualType();
10262 }
10263 if (ReductionType->isArrayType()) {
10264 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10265 return QualType();
10266 }
10267 return ReductionType;
10268}
10269
10270Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10271 Scope *S, DeclContext *DC, DeclarationName Name,
10272 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10273 AccessSpecifier AS, Decl *PrevDeclInScope) {
10274 SmallVector<Decl *, 8> Decls;
10275 Decls.reserve(ReductionTypes.size());
10276
10277 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10278 ForRedeclaration);
10279 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10280 // A reduction-identifier may not be re-declared in the current scope for the
10281 // same type or for a type that is compatible according to the base language
10282 // rules.
10283 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10284 OMPDeclareReductionDecl *PrevDRD = nullptr;
10285 bool InCompoundScope = true;
10286 if (S != nullptr) {
10287 // Find previous declaration with the same name not referenced in other
10288 // declarations.
10289 FunctionScopeInfo *ParentFn = getEnclosingFunction();
10290 InCompoundScope =
10291 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10292 LookupName(Lookup, S);
10293 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10294 /*AllowInlineNamespace=*/false);
10295 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10296 auto Filter = Lookup.makeFilter();
10297 while (Filter.hasNext()) {
10298 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10299 if (InCompoundScope) {
10300 auto I = UsedAsPrevious.find(PrevDecl);
10301 if (I == UsedAsPrevious.end())
10302 UsedAsPrevious[PrevDecl] = false;
10303 if (auto *D = PrevDecl->getPrevDeclInScope())
10304 UsedAsPrevious[D] = true;
10305 }
10306 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10307 PrevDecl->getLocation();
10308 }
10309 Filter.done();
10310 if (InCompoundScope) {
10311 for (auto &PrevData : UsedAsPrevious) {
10312 if (!PrevData.second) {
10313 PrevDRD = PrevData.first;
10314 break;
10315 }
10316 }
10317 }
10318 } else if (PrevDeclInScope != nullptr) {
10319 auto *PrevDRDInScope = PrevDRD =
10320 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10321 do {
10322 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10323 PrevDRDInScope->getLocation();
10324 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10325 } while (PrevDRDInScope != nullptr);
10326 }
10327 for (auto &TyData : ReductionTypes) {
10328 auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10329 bool Invalid = false;
10330 if (I != PreviousRedeclTypes.end()) {
10331 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10332 << TyData.first;
10333 Diag(I->second, diag::note_previous_definition);
10334 Invalid = true;
10335 }
10336 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10337 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10338 Name, TyData.first, PrevDRD);
10339 DC->addDecl(DRD);
10340 DRD->setAccess(AS);
10341 Decls.push_back(DRD);
10342 if (Invalid)
10343 DRD->setInvalidDecl();
10344 else
10345 PrevDRD = DRD;
10346 }
10347
10348 return DeclGroupPtrTy::make(
10349 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10350}
10351
10352void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10353 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10354
10355 // Enter new function scope.
10356 PushFunctionScope();
10357 getCurFunction()->setHasBranchProtectedScope();
10358 getCurFunction()->setHasOMPDeclareReductionCombiner();
10359
10360 if (S != nullptr)
10361 PushDeclContext(S, DRD);
10362 else
10363 CurContext = DRD;
10364
10365 PushExpressionEvaluationContext(PotentiallyEvaluated);
10366
10367 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010368 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10369 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10370 // uses semantics of argument handles by value, but it should be passed by
10371 // reference. C lang does not support references, so pass all parameters as
10372 // pointers.
10373 // Create 'T omp_in;' variable.
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010374 auto *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010375 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010376 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10377 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10378 // uses semantics of argument handles by value, but it should be passed by
10379 // reference. C lang does not support references, so pass all parameters as
10380 // pointers.
10381 // Create 'T omp_out;' variable.
10382 auto *OmpOutParm =
10383 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10384 if (S != nullptr) {
10385 PushOnScopeChains(OmpInParm, S);
10386 PushOnScopeChains(OmpOutParm, S);
10387 } else {
10388 DRD->addDecl(OmpInParm);
10389 DRD->addDecl(OmpOutParm);
10390 }
10391}
10392
10393void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10394 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10395 DiscardCleanupsInEvaluationContext();
10396 PopExpressionEvaluationContext();
10397
10398 PopDeclContext();
10399 PopFunctionScopeInfo();
10400
10401 if (Combiner != nullptr)
10402 DRD->setCombiner(Combiner);
10403 else
10404 DRD->setInvalidDecl();
10405}
10406
10407void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10408 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10409
10410 // Enter new function scope.
10411 PushFunctionScope();
10412 getCurFunction()->setHasBranchProtectedScope();
10413
10414 if (S != nullptr)
10415 PushDeclContext(S, DRD);
10416 else
10417 CurContext = DRD;
10418
10419 PushExpressionEvaluationContext(PotentiallyEvaluated);
10420
10421 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010422 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10423 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10424 // uses semantics of argument handles by value, but it should be passed by
10425 // reference. C lang does not support references, so pass all parameters as
10426 // pointers.
10427 // Create 'T omp_priv;' variable.
10428 auto *OmpPrivParm =
10429 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010430 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10431 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10432 // uses semantics of argument handles by value, but it should be passed by
10433 // reference. C lang does not support references, so pass all parameters as
10434 // pointers.
10435 // Create 'T omp_orig;' variable.
10436 auto *OmpOrigParm =
10437 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000010438 if (S != nullptr) {
10439 PushOnScopeChains(OmpPrivParm, S);
10440 PushOnScopeChains(OmpOrigParm, S);
10441 } else {
10442 DRD->addDecl(OmpPrivParm);
10443 DRD->addDecl(OmpOrigParm);
10444 }
10445}
10446
10447void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10448 Expr *Initializer) {
10449 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10450 DiscardCleanupsInEvaluationContext();
10451 PopExpressionEvaluationContext();
10452
10453 PopDeclContext();
10454 PopFunctionScopeInfo();
10455
10456 if (Initializer != nullptr)
10457 DRD->setInitializer(Initializer);
10458 else
10459 DRD->setInvalidDecl();
10460}
10461
10462Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10463 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10464 for (auto *D : DeclReductions.get()) {
10465 if (IsValid) {
10466 auto *DRD = cast<OMPDeclareReductionDecl>(D);
10467 if (S != nullptr)
10468 PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10469 } else
10470 D->setInvalidDecl();
10471 }
10472 return DeclReductions;
10473}
10474
Kelvin Li099bb8c2015-11-24 20:50:12 +000010475OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10476 SourceLocation StartLoc,
10477 SourceLocation LParenLoc,
10478 SourceLocation EndLoc) {
10479 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010480
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010481 // OpenMP [teams Constrcut, Restrictions]
10482 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010483 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10484 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010485 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000010486
10487 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10488}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010489
10490OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10491 SourceLocation StartLoc,
10492 SourceLocation LParenLoc,
10493 SourceLocation EndLoc) {
10494 Expr *ValExpr = ThreadLimit;
10495
10496 // OpenMP [teams Constrcut, Restrictions]
10497 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +000010498 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10499 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010500 return nullptr;
10501
10502 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10503 EndLoc);
10504}
Alexey Bataeva0569352015-12-01 10:17:31 +000010505
10506OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10507 SourceLocation StartLoc,
10508 SourceLocation LParenLoc,
10509 SourceLocation EndLoc) {
10510 Expr *ValExpr = Priority;
10511
10512 // OpenMP [2.9.1, task Constrcut]
10513 // The priority-value is a non-negative numerical scalar expression.
10514 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10515 /*StrictlyPositive=*/false))
10516 return nullptr;
10517
10518 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10519}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010520
10521OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10522 SourceLocation StartLoc,
10523 SourceLocation LParenLoc,
10524 SourceLocation EndLoc) {
10525 Expr *ValExpr = Grainsize;
10526
10527 // OpenMP [2.9.2, taskloop Constrcut]
10528 // The parameter of the grainsize clause must be a positive integer
10529 // expression.
10530 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10531 /*StrictlyPositive=*/true))
10532 return nullptr;
10533
10534 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10535}
Alexey Bataev382967a2015-12-08 12:06:20 +000010536
10537OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10538 SourceLocation StartLoc,
10539 SourceLocation LParenLoc,
10540 SourceLocation EndLoc) {
10541 Expr *ValExpr = NumTasks;
10542
10543 // OpenMP [2.9.2, taskloop Constrcut]
10544 // The parameter of the num_tasks clause must be a positive integer
10545 // expression.
10546 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10547 /*StrictlyPositive=*/true))
10548 return nullptr;
10549
10550 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10551}
10552
Alexey Bataev28c75412015-12-15 08:19:24 +000010553OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10554 SourceLocation LParenLoc,
10555 SourceLocation EndLoc) {
10556 // OpenMP [2.13.2, critical construct, Description]
10557 // ... where hint-expression is an integer constant expression that evaluates
10558 // to a valid lock hint.
10559 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10560 if (HintExpr.isInvalid())
10561 return nullptr;
10562 return new (Context)
10563 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10564}
10565
Carlo Bertollib4adf552016-01-15 18:50:31 +000010566OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10567 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10568 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10569 SourceLocation EndLoc) {
10570 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10571 std::string Values;
10572 Values += "'";
10573 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10574 Values += "'";
10575 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10576 << Values << getOpenMPClauseName(OMPC_dist_schedule);
10577 return nullptr;
10578 }
10579 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000010580 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000010581 if (ChunkSize) {
10582 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10583 !ChunkSize->isInstantiationDependent() &&
10584 !ChunkSize->containsUnexpandedParameterPack()) {
10585 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10586 ExprResult Val =
10587 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10588 if (Val.isInvalid())
10589 return nullptr;
10590
10591 ValExpr = Val.get();
10592
10593 // OpenMP [2.7.1, Restrictions]
10594 // chunk_size must be a loop invariant integer expression with a positive
10595 // value.
10596 llvm::APSInt Result;
10597 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10598 if (Result.isSigned() && !Result.isStrictlyPositive()) {
10599 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10600 << "dist_schedule" << ChunkSize->getSourceRange();
10601 return nullptr;
10602 }
10603 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
Alexey Bataev5a3af132016-03-29 08:58:54 +000010604 llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10605 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10606 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010607 }
10608 }
10609 }
10610
10611 return new (Context)
10612 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000010613 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000010614}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010615
10616OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10617 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10618 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10619 SourceLocation KindLoc, SourceLocation EndLoc) {
10620 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10621 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10622 Kind != OMPC_DEFAULTMAP_scalar) {
10623 std::string Value;
10624 SourceLocation Loc;
10625 Value += "'";
10626 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10627 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10628 OMPC_DEFAULTMAP_MODIFIER_tofrom);
10629 Loc = MLoc;
10630 } else {
10631 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10632 OMPC_DEFAULTMAP_scalar);
10633 Loc = KindLoc;
10634 }
10635 Value += "'";
10636 Diag(Loc, diag::err_omp_unexpected_clause_value)
10637 << Value << getOpenMPClauseName(OMPC_defaultmap);
10638 return nullptr;
10639 }
10640
10641 return new (Context)
10642 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10643}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000010644
10645bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10646 DeclContext *CurLexicalContext = getCurLexicalContext();
10647 if (!CurLexicalContext->isFileContext() &&
10648 !CurLexicalContext->isExternCContext() &&
10649 !CurLexicalContext->isExternCXXContext()) {
10650 Diag(Loc, diag::err_omp_region_not_file_context);
10651 return false;
10652 }
10653 if (IsInOpenMPDeclareTargetContext) {
10654 Diag(Loc, diag::err_omp_enclosed_declare_target);
10655 return false;
10656 }
10657
10658 IsInOpenMPDeclareTargetContext = true;
10659 return true;
10660}
10661
10662void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10663 assert(IsInOpenMPDeclareTargetContext &&
10664 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10665
10666 IsInOpenMPDeclareTargetContext = false;
10667}
10668
10669static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10670 Sema &SemaRef, Decl *D) {
10671 if (!D)
10672 return;
10673 Decl *LD = nullptr;
10674 if (isa<TagDecl>(D)) {
10675 LD = cast<TagDecl>(D)->getDefinition();
10676 } else if (isa<VarDecl>(D)) {
10677 LD = cast<VarDecl>(D)->getDefinition();
10678
10679 // If this is an implicit variable that is legal and we do not need to do
10680 // anything.
10681 if (cast<VarDecl>(D)->isImplicit()) {
10682 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10683 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10684 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10685 return;
10686 }
10687
10688 } else if (isa<FunctionDecl>(D)) {
10689 const FunctionDecl *FD = nullptr;
10690 if (cast<FunctionDecl>(D)->hasBody(FD))
10691 LD = const_cast<FunctionDecl *>(FD);
10692
10693 // If the definition is associated with the current declaration in the
10694 // target region (it can be e.g. a lambda) that is legal and we do not need
10695 // to do anything else.
10696 if (LD == D) {
10697 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10698 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10699 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10700 return;
10701 }
10702 }
10703 if (!LD)
10704 LD = D;
10705 if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10706 (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10707 // Outlined declaration is not declared target.
10708 if (LD->isOutOfLine()) {
10709 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10710 SemaRef.Diag(SL, diag::note_used_here) << SR;
10711 } else {
10712 DeclContext *DC = LD->getDeclContext();
10713 while (DC) {
10714 if (isa<FunctionDecl>(DC) &&
10715 cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10716 break;
10717 DC = DC->getParent();
10718 }
10719 if (DC)
10720 return;
10721
10722 // Is not declared in target context.
10723 SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10724 SemaRef.Diag(SL, diag::note_used_here) << SR;
10725 }
10726 // Mark decl as declared target to prevent further diagnostic.
10727 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10728 if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10729 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10730 }
10731}
10732
10733static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10734 Sema &SemaRef, DSAStackTy *Stack,
10735 ValueDecl *VD) {
10736 if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10737 return true;
10738 if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10739 return false;
10740 return true;
10741}
10742
10743void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10744 if (!D || D->isInvalidDecl())
10745 return;
10746 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10747 SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10748 // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10749 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10750 if (DSAStack->isThreadPrivate(VD)) {
10751 Diag(SL, diag::err_omp_threadprivate_in_target);
10752 ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10753 return;
10754 }
10755 }
10756 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10757 // Problem if any with var declared with incomplete type will be reported
10758 // as normal, so no need to check it here.
10759 if ((E || !VD->getType()->isIncompleteType()) &&
10760 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10761 // Mark decl as declared target to prevent further diagnostic.
10762 if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10763 VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10764 if (ASTMutationListener *ML = Context.getASTMutationListener())
10765 ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10766 }
10767 return;
10768 }
10769 }
10770 if (!E) {
10771 // Checking declaration inside declare target region.
10772 if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10773 (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10774 D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10775 if (ASTMutationListener *ML = Context.getASTMutationListener())
10776 ML->DeclarationMarkedOpenMPDeclareTarget(D);
10777 }
10778 return;
10779 }
10780 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10781}